Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[p2-dev] QueryUtil changes and their impact on testability

Now that many of the *Query classes have moved into static methods on
QueryUtil, there is a problem with unit testability.

QueryUtil has a static initializers that use ExpressionUtil.

ExpressionUtil has static initializers that *depend on running in an
OSGi environment* (not unit test):

public final class ExpressionUtil {
	private static final LDAPFilterParser ldapFilterParser = new
LDAPFilterParser(ExpressionFactory.INSTANCE);
	public static final IExpression TRUE_EXPRESSION =
getFactory().constant(Boolean.TRUE);
	public static final IExpression FALSE_EXPRESSION =
getFactory().constant(Boolean.FALSE);

	private ExpressionUtil() {
		//We don't want to ppl to instantiate this class
	}

	/**
	 * Returns the global expression factory
	 * @return The global expression factory.
	 */
	public static IExpressionFactory getFactory() {
		return MetadataActivator.getExpressionFactory();
	}

(As soon as one even references ExpressionUtil, its static
initializers are called which in turn invoke #getFactory which rely on
MetadataActivator to have been activated by OSGi context.)

Therefore, if any class uses QueryUtil in its implementation (which
many must do now since the *Query classes have gone away), those
classes cannot be unit tested.  One could try and get around this by
setting MetadataActivator.instance, but that would only get you so
far.  Metadata#getExpressionFactory would still fail and cannot be
stubbed out so far as I can tell.

Am I missing something?  How are people testing this code that makes
use of QueryUtil?


Back to the top