summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/test/java/com/google/gerrit/server/schema/SchemaUpdaterTest.java
blob: 5b86f46e0f58f4b60b3d58203e2625577ee5b078 (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
// 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.server.schema;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.lifecycle.LifecycleManager;
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.AllUsersName;
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.SitePaths;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.testutil.InMemoryDatabase;
import com.google.gerrit.testutil.InMemoryH2Type;
import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gerrit.testutil.TestUpdateUI;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Guice;
import com.google.inject.Key;
import com.google.inject.ProvisionException;
import com.google.inject.TypeLiteral;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SchemaUpdaterTest {
  private LifecycleManager lifecycle;
  private InMemoryDatabase db;

  @Before
  public void setUp() throws Exception {
    lifecycle = new LifecycleManager();
    db = InMemoryDatabase.newDatabase(lifecycle);
    lifecycle.start();
  }

  @After
  public void tearDown() throws Exception {
    if (lifecycle != null) {
      lifecycle.stop();
    }
    InMemoryDatabase.drop(db);
  }

  @Test
  public void update() throws OrmException, FileNotFoundException, IOException {
    db.create();

    final Path site = Paths.get(UUID.randomUUID().toString());
    final SitePaths paths = new SitePaths(site);
    SchemaUpdater u =
        Guice.createInjector(
                new FactoryModule() {
                  @Override
                  protected void configure() {
                    TypeLiteral<SchemaFactory<ReviewDb>> schemaFactory =
                        new TypeLiteral<SchemaFactory<ReviewDb>>() {};
                    bind(schemaFactory).to(NotesMigrationSchemaFactory.class);
                    bind(Key.get(schemaFactory, ReviewDbFactory.class)).toInstance(db);
                    bind(SitePaths.class).toInstance(paths);

                    Config cfg = new Config();
                    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).toInstance(new AllProjectsName("All-Projects"));
                    bind(AllUsersName.class).toInstance(new AllUsersName("All-Users"));

                    bind(GitRepositoryManager.class).toInstance(new InMemoryRepositoryManager());

                    bind(String.class) //
                        .annotatedWith(AnonymousCowardName.class) //
                        .toProvider(AnonymousCowardNameProvider.class);

                    bind(DataSourceType.class).to(InMemoryH2Type.class);

                    bind(SystemGroupBackend.class);
                    install(new NotesMigration.Module());
                  }
                })
            .getInstance(SchemaUpdater.class);

    for (SchemaVersion s = u.getLatestSchemaVersion(); s.getVersionNbr() > 1; s = s.getPrior()) {
      try {
        assertThat(s.getPrior().getVersionNbr())
            .named(
                "schema %s has prior version %s. Not true that",
                s.getVersionNbr(), s.getPrior().getVersionNbr())
            .isEqualTo(s.getVersionNbr() - 1);
      } catch (ProvisionException e) {
        // Ignored
        // The oldest supported schema version doesn't have a prior schema
        // version.
        break;
      }
    }

    u.update(new TestUpdateUI());

    db.assertSchemaVersion();
    final SystemConfig sc = db.getSystemConfig();
    assertThat(sc.sitePath).isEqualTo(paths.site_path.toAbsolutePath().toString());
  }
}