Move InMemoryDatabase setup into a module
Storing the injector in the InMemoryDatabase instance was a hack. Instead, move towards a more comprehensive Guice stack for tests by moving this setup into a module. Keep the same manual create()/drop() behavior of InMemoryDatabase, though eventually we may want to do something with LifecycleManagers and move towards a similar idiom to that used in the acceptance tests. Change-Id: I917f6dab4751541fa63bfe3318818fbe4bda20f2
This commit is contained in:
@@ -14,40 +14,22 @@
|
||||
|
||||
package com.google.gerrit.testutil;
|
||||
|
||||
import static com.google.inject.Scopes.SINGLETON;
|
||||
|
||||
import com.google.gerrit.reviewdb.client.CurrentSchemaVersion;
|
||||
import com.google.gerrit.reviewdb.client.SystemConfig;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.GerritPersonIdent;
|
||||
import com.google.gerrit.server.GerritPersonIdentProvider;
|
||||
import com.google.gerrit.server.config.AllProjectsName;
|
||||
import com.google.gerrit.server.config.AllProjectsNameProvider;
|
||||
import com.google.gerrit.server.config.AnonymousCowardName;
|
||||
import com.google.gerrit.server.config.AnonymousCowardNameProvider;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.config.SitePath;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.schema.Current;
|
||||
import com.google.gerrit.server.schema.DataSourceType;
|
||||
import com.google.gerrit.server.schema.SchemaCreator;
|
||||
import com.google.gerrit.server.schema.SchemaVersion;
|
||||
import com.google.gwtorm.jdbc.Database;
|
||||
import com.google.gwtorm.jdbc.SimpleDataSource;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
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.Key;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
@@ -64,6 +46,11 @@ import javax.sql.DataSource;
|
||||
* the JVM running the unit tests doesn't run out of heap space.
|
||||
*/
|
||||
public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
public static InMemoryDatabase newDatabase() {
|
||||
return Guice.createInjector(new InMemoryModule())
|
||||
.getInstance(InMemoryDatabase.class);
|
||||
}
|
||||
|
||||
private static int dbCnt;
|
||||
|
||||
private static synchronized DataSource newDataSource() throws SQLException {
|
||||
@@ -81,15 +68,21 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
}
|
||||
}
|
||||
|
||||
private final SchemaVersion schemaVersion;
|
||||
private final SchemaCreator schemaCreator;
|
||||
|
||||
private Connection openHandle;
|
||||
private Database<ReviewDb> database;
|
||||
private boolean created;
|
||||
private SchemaVersion schemaVersion;
|
||||
private Injector injector;
|
||||
|
||||
public InMemoryDatabase() throws OrmException {
|
||||
@Inject
|
||||
InMemoryDatabase(SchemaVersion schemaVersion,
|
||||
SchemaCreator schemaCreator) throws OrmException {
|
||||
this.schemaVersion = schemaVersion;
|
||||
this.schemaCreator = schemaCreator;
|
||||
|
||||
try {
|
||||
final DataSource dataSource = newDataSource();
|
||||
DataSource dataSource = newDataSource();
|
||||
|
||||
// Open one connection. This will peg the database into memory
|
||||
// until someone calls drop on us, allowing subsequent connections
|
||||
@@ -101,54 +94,11 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
//
|
||||
database = new Database<ReviewDb>(dataSource, ReviewDb.class);
|
||||
|
||||
injector = Guice.createInjector(new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
install(new SchemaVersion.Module());
|
||||
|
||||
bind(File.class) //
|
||||
.annotatedWith(SitePath.class) //
|
||||
.toInstance(new File("."));
|
||||
|
||||
Config cfg = new Config();
|
||||
cfg.setString("gerrit", null, "basePath", "git");
|
||||
cfg.setString("gerrit", null, "allProjects", "Test-Projects");
|
||||
cfg.setString("user", null, "name", "Gerrit Code Review");
|
||||
cfg.setString("user", null, "email", "gerrit@localhost");
|
||||
|
||||
bind(Config.class) //
|
||||
.annotatedWith(GerritServerConfig.class) //
|
||||
.toInstance(cfg);
|
||||
|
||||
bind(PersonIdent.class) //
|
||||
.annotatedWith(GerritPersonIdent.class) //
|
||||
.toProvider(GerritPersonIdentProvider.class);
|
||||
|
||||
bind(AllProjectsName.class) //
|
||||
.toProvider(AllProjectsNameProvider.class);
|
||||
|
||||
bind(GitRepositoryManager.class) //
|
||||
.to(InMemoryRepositoryManager.class).in(SINGLETON);
|
||||
|
||||
bind(String.class) //
|
||||
.annotatedWith(AnonymousCowardName.class) //
|
||||
.toProvider(AnonymousCowardNameProvider.class);
|
||||
|
||||
bind(DataSourceType.class) //
|
||||
.to(InMemoryH2Type.class);
|
||||
}
|
||||
});
|
||||
schemaVersion = injector.getInstance(
|
||||
Key.get(SchemaVersion.class, Current.class));
|
||||
} catch (SQLException e) {
|
||||
throw new OrmException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> T getInstance(Class<T> clazz) {
|
||||
return injector.getInstance(clazz);
|
||||
}
|
||||
|
||||
public Database<ReviewDb> getDatabase() {
|
||||
return database;
|
||||
}
|
||||
@@ -165,7 +115,7 @@ public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
|
||||
final ReviewDb c = open();
|
||||
try {
|
||||
try {
|
||||
getInstance(SchemaCreator.class).create(c);
|
||||
schemaCreator.create(c);
|
||||
} catch (IOException e) {
|
||||
throw new OrmException("Cannot create in-memory database", e);
|
||||
} catch (ConfigInvalidException e) {
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
// Copyright (C) 2013 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.package com.google.gerrit.server.git;
|
||||
|
||||
package com.google.gerrit.testutil;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkState;
|
||||
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;
|
||||
import com.google.gerrit.server.GerritPersonIdentProvider;
|
||||
import com.google.gerrit.server.RemotePeer;
|
||||
import com.google.gerrit.server.cache.h2.DefaultCacheFactory;
|
||||
import com.google.gerrit.server.config.AllProjectsName;
|
||||
import com.google.gerrit.server.config.AllProjectsNameProvider;
|
||||
import com.google.gerrit.server.config.AnonymousCowardName;
|
||||
import com.google.gerrit.server.config.AnonymousCowardNameProvider;
|
||||
import com.google.gerrit.server.config.CanonicalWebUrlModule;
|
||||
import com.google.gerrit.server.config.CanonicalWebUrlProvider;
|
||||
import com.google.gerrit.server.config.FactoryModule;
|
||||
import com.google.gerrit.server.config.GerritGlobalModule;
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.gerrit.server.config.SitePath;
|
||||
import com.google.gerrit.server.config.TrackingFooters;
|
||||
import com.google.gerrit.server.config.TrackingFootersProvider;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.git.PerThreadRequestScope;
|
||||
import com.google.gerrit.server.index.ChangeSchemas;
|
||||
import com.google.gerrit.server.index.IndexModule;
|
||||
import com.google.gerrit.server.index.IndexModule.IndexType;
|
||||
import com.google.gerrit.server.index.NoIndexModule;
|
||||
import com.google.gerrit.server.mail.SignedTokenEmailTokenVerifier;
|
||||
import com.google.gerrit.server.mail.SmtpEmailSender;
|
||||
import com.google.gerrit.server.schema.Current;
|
||||
import com.google.gerrit.server.schema.DataSourceType;
|
||||
import com.google.gerrit.server.schema.SchemaCreator;
|
||||
import com.google.gerrit.server.schema.SchemaVersion;
|
||||
import com.google.gerrit.server.ssh.NoSshKeyCache;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
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.Provider;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.ProvisionException;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.TypeLiteral;
|
||||
import com.google.inject.servlet.RequestScoped;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class InMemoryModule extends FactoryModule {
|
||||
public static Config newDefaultConfig() {
|
||||
Config cfg = new Config();
|
||||
cfg.setEnum("auth", null, "type", AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT);
|
||||
cfg.setString("gerrit", null, "basePath", "git");
|
||||
cfg.setString("gerrit", null, "allProjects", "Test-Projects");
|
||||
cfg.setString("user", null, "name", "Gerrit Code Review");
|
||||
cfg.setString("user", null, "email", "gerrit@localhost");
|
||||
cfg.setBoolean("sendemail", null, "enable", false);
|
||||
cfg.setString("cache", null, "directory", null);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
private final Config cfg;
|
||||
|
||||
public InMemoryModule() {
|
||||
this(newDefaultConfig());
|
||||
}
|
||||
|
||||
public InMemoryModule(Config cfg) {
|
||||
this.cfg = cfg;
|
||||
}
|
||||
|
||||
public void inject(Object instance) {
|
||||
Guice.createInjector(this).injectMembers(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure() {
|
||||
// For simplicity, don't create child injectors, just use this one to get a
|
||||
// few required modules.
|
||||
Injector cfgInjector = Guice.createInjector(new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(Config.class).annotatedWith(GerritServerConfig.class)
|
||||
.toInstance(cfg);
|
||||
}
|
||||
});
|
||||
install(cfgInjector.getInstance(GerritGlobalModule.class));
|
||||
|
||||
bindScope(RequestScoped.class, PerThreadRequestScope.REQUEST);
|
||||
|
||||
install(new SchemaVersion.Module());
|
||||
|
||||
bind(File.class).annotatedWith(SitePath.class).toInstance(new File("."));
|
||||
bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(cfg);
|
||||
try {
|
||||
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;
|
||||
}
|
||||
bind(PersonIdent.class)
|
||||
.annotatedWith(GerritPersonIdent.class)
|
||||
.toProvider(GerritPersonIdentProvider.class);
|
||||
bind(String.class)
|
||||
.annotatedWith(AnonymousCowardName.class)
|
||||
.toProvider(AnonymousCowardNameProvider.class);
|
||||
bind(AllProjectsName.class)
|
||||
.toProvider(AllProjectsNameProvider.class);
|
||||
bind(GitRepositoryManager.class)
|
||||
.to(InMemoryRepositoryManager.class);
|
||||
bind(InMemoryRepositoryManager.class).in(SINGLETON);
|
||||
bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class)
|
||||
.in(SINGLETON);
|
||||
|
||||
bind(DataSourceType.class)
|
||||
.to(InMemoryH2Type.class);
|
||||
bind(new TypeLiteral<SchemaFactory<ReviewDb>>() {})
|
||||
.to(InMemoryDatabase.class);
|
||||
|
||||
bind(ChangeHooks.class).to(DisabledChangeHooks.class);
|
||||
install(NoSshKeyCache.module());
|
||||
install(new CanonicalWebUrlModule() {
|
||||
@Override
|
||||
protected Class<? extends Provider<String>> provider() {
|
||||
return CanonicalWebUrlProvider.class;
|
||||
}
|
||||
});
|
||||
install(new DefaultCacheFactory.Module());
|
||||
install(new SmtpEmailSender.Module());
|
||||
install(new SignedTokenEmailTokenVerifier.Module());
|
||||
|
||||
IndexType indexType = IndexModule.getIndexType(cfgInjector);
|
||||
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));
|
||||
break;
|
||||
case SQL:
|
||||
install(new NoIndexModule());
|
||||
break;
|
||||
default:
|
||||
throw new ProvisionException(
|
||||
"index type unsupported in tests: " + indexType);
|
||||
}
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
InMemoryDatabase getInMemoryDatabase(@Current SchemaVersion schemaVersion,
|
||||
SchemaCreator schemaCreator) throws OrmException {
|
||||
return new InMemoryDatabase(schemaVersion, schemaCreator);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user