From a1fe8214703f9cf38a6adf93b0013a41f9199ed3 Mon Sep 17 00:00:00 2001 From: Dave Borowitz Date: Fri, 11 Oct 2013 16:08:22 -0700 Subject: [PATCH] Remove direct Lucene dependency from InMemoryModule Custom secondary index implementations should be able to use this module for writing index tests without linking against Lucene. However, for other potential uses of this module for tests within Gerrit, it makes sense to still use Lucene by default without installing any additional modules. So, as a hack, keep the Lucene dependency but construct it via reflection. The same argument could be made about the H2 cache dependency, but the specific use case we are interested in at the moment is the index implementation, so extract that one first. Change-Id: I7d213a9f233b819ec0bc6632323e65ac9b270200 --- .../gerrit/testutil/InMemoryModule.java | 46 +++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java b/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java index c4d5b93385..d67d5e6411 100644 --- a/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java +++ b/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryModule.java @@ -19,7 +19,6 @@ import static com.google.inject.Scopes.SINGLETON; import com.google.gerrit.common.ChangeHooks; import com.google.gerrit.common.DisabledChangeHooks; -import com.google.gerrit.lucene.LuceneIndexModule; import com.google.gerrit.reviewdb.client.AuthType; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.GerritPersonIdent; @@ -55,6 +54,7 @@ import com.google.gwtorm.server.SchemaFactory; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Injector; +import com.google.inject.Module; import com.google.inject.Provider; import com.google.inject.Provides; import com.google.inject.ProvisionException; @@ -66,6 +66,8 @@ import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.PersonIdent; import java.io.File; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; @@ -121,9 +123,7 @@ public class InMemoryModule extends FactoryModule { bind(SocketAddress.class).annotatedWith(RemotePeer.class) .toInstance(new InetSocketAddress(InetAddress.getLocalHost(), 1234)); } catch (UnknownHostException e) { - ProvisionException pe = new ProvisionException(e.getMessage()); - pe.initCause(e); - throw pe; + throw newProvisionException(e); } bind(PersonIdent.class) .annotatedWith(GerritPersonIdent.class) @@ -165,10 +165,7 @@ public class InMemoryModule extends FactoryModule { if (indexType != null) { switch (indexType) { case LUCENE: - int version = cfg.getInt("index", "lucene", "testVersion", -1); - checkState(ChangeSchemas.ALL.containsKey(version), - "invalid index.lucene.testVersion %s", version); - install(new LuceneIndexModule(version, 0, null)); + install(luceneIndexModule()); break; case SQL: install(new NoIndexModule()); @@ -186,4 +183,37 @@ public class InMemoryModule extends FactoryModule { SchemaCreator schemaCreator) throws OrmException { return new InMemoryDatabase(schemaVersion, schemaCreator); } + + private Module luceneIndexModule() { + try { + int version = cfg.getInt("index", "lucene", "testVersion", -1); + checkState(ChangeSchemas.ALL.containsKey(version), + "invalid index.lucene.testVersion %s", version); + Class clazz = + Class.forName("com.google.gerrit.lucene.LuceneIndexModule"); + Constructor c = + clazz.getConstructor(Integer.class, int.class, String.class); + return (Module) c.newInstance(Integer.valueOf(version), 0, (String) null); + } catch (ClassNotFoundException e) { + throw newProvisionException(e); + } catch (SecurityException e) { + throw newProvisionException(e); + } catch (NoSuchMethodException e) { + throw newProvisionException(e); + } catch (IllegalArgumentException e) { + throw newProvisionException(e); + } catch (InstantiationException e) { + throw newProvisionException(e); + } catch (IllegalAccessException e) { + throw newProvisionException(e); + } catch (InvocationTargetException e) { + throw newProvisionException(e); + } + } + + private static ProvisionException newProvisionException(Throwable cause) { + ProvisionException pe = new ProvisionException(cause.getMessage()); + pe.initCause(cause); + return pe; + } }