We consider the composite hypothesis testing problem of classifying an unknown probability distribution based on a sequence of random samples drawn according to this distribution. Specifically, if $A$ is a subset of the space of all probability measures $\mathscr{M}_1(\Sigma)$ over some compact Polish space $\Sigma$, we want to decide whether or not the unknown distribution belongs to $A$ or its complement. We propose an algorithm which leads a.s. to a correct decision for any $A$ satisfying certain structural assumptions. A refined decision procedure is also presented which, given a countable collection $A_i \subset \mathscr{M}_1(\Sigma), i = 1,2,\ldots$, each satisfying the structural assumption, will eventually determine a.s. the membership of the distribution in any finite number of the $A_i$. Applications to density estimation are discussed.