summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/test/java/com/google/gerrit/testutil/InMemoryDatabase.java
blob: 303c49776035ac5baa193487865e9ee728f1cb04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (C) 2009 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.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 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;
import java.util.Properties;

import javax.sql.DataSource;

/**
 * An in-memory test instance of {@link ReviewDb} database.
 * <p>
 * Test classes should create one instance of this class for each unique test
 * database they want to use. When the tests needing this instance are complete,
 * ensure that {@link #drop(InMemoryDatabase)} is called to free the resources so
 * the JVM running the unit tests doesn't run out of heap space.
 */
public class InMemoryDatabase implements SchemaFactory<ReviewDb> {
  private static int dbCnt;

  private static synchronized DataSource newDataSource() throws SQLException {
    final Properties p = new Properties();
    p.setProperty("driver", org.h2.Driver.class.getName());
    p.setProperty("url", "jdbc:h2:mem:" + "Test_" + (++dbCnt));
    final DataSource dataSource = new SimpleDataSource(p);
    return dataSource;
  }

  /** Drop the database from memory; does nothing if the instance was null. */
  public static void drop(final InMemoryDatabase db) {
    if (db != null) {
      db.drop();
    }
  }

  private Connection openHandle;
  private Database<ReviewDb> database;
  private boolean created;
  private SchemaVersion schemaVersion;
  private Injector injector;

  public InMemoryDatabase() throws OrmException {
    try {
      final DataSource dataSource = newDataSource();

      // Open one connection. This will peg the database into memory
      // until someone calls drop on us, allowing subsequent connections
      // opened against the same URL to go to the same set of tables.
      //
      openHandle = dataSource.getConnection();

      // Build the access layer around the connection factory.
      //
      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;
  }

  @Override
  public ReviewDb open() throws OrmException {
    return getDatabase().open();
  }

  /** Ensure the database schema has been created and initialized. */
  public InMemoryDatabase create() throws OrmException {
    if (!created) {
      created = true;
      final ReviewDb c = open();
      try {
        try {
          getInstance(SchemaCreator.class).create(c);
        } catch (IOException e) {
          throw new OrmException("Cannot create in-memory database", e);
        } catch (ConfigInvalidException e) {
          throw new OrmException("Cannot create in-memory database", e);
        }
      } finally {
        c.close();
      }
    }
    return this;
  }

  /** Drop this database from memory so it no longer exists. */
  public void drop() {
    if (openHandle != null) {
      try {
        openHandle.close();
      } catch (SQLException e) {
        System.err.println("WARNING: Cannot close database connection");
        e.printStackTrace(System.err);
      }
      openHandle = null;
      database = null;
    }
  }

  public SystemConfig getSystemConfig() throws OrmException {
    final ReviewDb c = open();
    try {
      return c.systemConfig().get(new SystemConfig.Key());
    } finally {
      c.close();
    }
  }

  public CurrentSchemaVersion getSchemaVersion() throws OrmException {
    final ReviewDb c = open();
    try {
      return c.schemaVersion().get(new CurrentSchemaVersion.Key());
    } finally {
      c.close();
    }
  }

  public void assertSchemaVersion() throws OrmException {
    final CurrentSchemaVersion act = getSchemaVersion();
    TestCase.assertEquals(schemaVersion.getVersionNbr(), act.versionNbr);
  }
}