Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import static com.google.inject.Scopes.SINGLETON; 18 : 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.exceptions.StorageException; 21 : import com.google.gerrit.extensions.events.LifecycleListener; 22 : import com.google.gerrit.lifecycle.LifecycleModule; 23 : import com.google.gerrit.metrics.MetricMaker; 24 : import com.google.gerrit.metrics.MetricsReservoirConfig; 25 : import com.google.gerrit.server.config.AllProjectsConfigProvider; 26 : import com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider; 27 : import com.google.gerrit.server.config.FileBasedGlobalPluginConfigProvider; 28 : import com.google.gerrit.server.config.GerritServerConfig; 29 : import com.google.gerrit.server.config.GlobalPluginConfigProvider; 30 : import com.google.gerrit.server.config.MetricsReservoirConfigImpl; 31 : import com.google.gerrit.server.config.SitePath; 32 : import com.google.gerrit.server.config.SitePaths; 33 : import com.google.gerrit.server.config.TrackingFooters; 34 : import com.google.gerrit.server.config.TrackingFootersProvider; 35 : import com.google.gerrit.server.git.GitRepositoryManager; 36 : import com.google.gerrit.server.schema.SchemaCreator; 37 : import com.google.gerrit.server.schema.SchemaModule; 38 : import com.google.gerrit.testing.InMemoryRepositoryManager; 39 : import com.google.inject.Inject; 40 : import com.google.inject.ProvisionException; 41 : import com.google.inject.Scopes; 42 : import java.io.IOException; 43 : import java.nio.file.Files; 44 : import java.nio.file.Path; 45 : import org.eclipse.jgit.errors.ConfigInvalidException; 46 : import org.eclipse.jgit.lib.Config; 47 : 48 : class InMemoryTestingDatabaseModule extends LifecycleModule { 49 : private final Config cfg; 50 : private final Path sitePath; 51 : @Nullable private final InMemoryRepositoryManager repoManager; 52 : 53 : InMemoryTestingDatabaseModule( 54 132 : Config cfg, Path sitePath, @Nullable InMemoryRepositoryManager repoManager) { 55 132 : this.cfg = cfg; 56 132 : this.sitePath = sitePath; 57 132 : this.repoManager = repoManager; 58 132 : makeSiteDirs(sitePath); 59 132 : } 60 : 61 : @Override 62 : protected void configure() { 63 132 : bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(cfg); 64 132 : bind(GlobalPluginConfigProvider.class).to(FileBasedGlobalPluginConfigProvider.class); 65 132 : bind(AllProjectsConfigProvider.class).to(FileBasedAllProjectsConfigProvider.class); 66 132 : bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath); 67 : 68 132 : if (repoManager != null) { 69 3 : bind(GitRepositoryManager.class).toInstance(repoManager); 70 : } else { 71 132 : bind(GitRepositoryManager.class).to(InMemoryRepositoryManager.class); 72 132 : bind(InMemoryRepositoryManager.class).in(SINGLETON); 73 : } 74 : 75 132 : bind(MetricsReservoirConfig.class).to(MetricsReservoirConfigImpl.class).in(Scopes.SINGLETON); 76 132 : bind(MetricMaker.class).to(TestMetricMaker.class); 77 : 78 132 : listener().to(CreateSchema.class); 79 : 80 132 : bind(SitePaths.class); 81 132 : bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON); 82 : 83 132 : install(new SchemaModule()); 84 : 85 132 : install(new SshdModule()); 86 132 : } 87 : 88 : static class CreateSchema implements LifecycleListener { 89 : private final SchemaCreator schemaCreator; 90 : 91 : @Inject 92 132 : CreateSchema(SchemaCreator schemaCreator) { 93 132 : this.schemaCreator = schemaCreator; 94 132 : } 95 : 96 : @Override 97 : public void start() { 98 : try { 99 132 : schemaCreator.ensureCreated(); 100 0 : } catch (IOException | ConfigInvalidException e) { 101 0 : throw new StorageException(e); 102 132 : } 103 132 : } 104 : 105 : @Override 106 132 : public void stop() {} 107 : } 108 : 109 : private static void makeSiteDirs(Path p) { 110 : try { 111 132 : Files.createDirectories(p.resolve("etc")); 112 0 : } catch (IOException e) { 113 0 : throw new ProvisionException(e.getMessage(), e); 114 132 : } 115 132 : } 116 : }