diff --git a/gerrit-pgm/pom.xml b/gerrit-pgm/pom.xml
index 659310de16..ab04ed02ca 100644
--- a/gerrit-pgm/pom.xml
+++ b/gerrit-pgm/pom.xml
@@ -83,5 +83,10 @@ limitations under the License.
org.eclipse.jetty
jetty-servlet
+
+
+ org.eclipse.jgit
+ org.eclipse.jgit.junit
+
diff --git a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_x.java b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_x.java
index 842f28d688..f039f0f765 100644
--- a/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_x.java
+++ b/gerrit-pgm/src/main/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_x.java
@@ -42,7 +42,7 @@ import java.util.Properties;
/** Upgrade from a 2.0.x site to a 2.1 site. */
@Singleton
class UpgradeFrom2_0_x implements InitStep {
- private static final String[] etcFiles = {"gerrit.config", //
+ static final String[] etcFiles = {"gerrit.config", //
"secure.config", //
"replication.config", //
"ssh_host_rsa_key", //
@@ -78,7 +78,7 @@ class UpgradeFrom2_0_x implements InitStep {
this.etc_dir = site.etc_dir;
}
- public boolean isNeedUpgrade() {
+ boolean isNeedUpgrade() {
for (String name : etcFiles) {
if (new File(site_path, name).exists()) {
return true;
diff --git a/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/InitTestCase.java b/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/InitTestCase.java
new file mode 100644
index 0000000000..2964f50fb0
--- /dev/null
+++ b/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/InitTestCase.java
@@ -0,0 +1,26 @@
+// 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.pgm.init;
+
+import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
+
+import java.io.File;
+import java.io.IOException;
+
+public abstract class InitTestCase extends LocalDiskRepositoryTestCase {
+ protected File newSitePath() throws IOException {
+ return new File(createWorkRepository().getWorkDir(), "test_site");
+ }
+}
diff --git a/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_xTest.java b/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_xTest.java
new file mode 100644
index 0000000000..5dc0cd3684
--- /dev/null
+++ b/gerrit-pgm/src/test/java/com/google/gerrit/pgm/init/UpgradeFrom2_0_xTest.java
@@ -0,0 +1,102 @@
+// 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.pgm.init;
+
+import static org.easymock.EasyMock.eq;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.classextension.EasyMock.createStrictMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+
+import com.google.gerrit.pgm.util.ConsoleUI;
+import com.google.gerrit.server.config.SitePaths;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.FileBasedConfig;
+import org.eclipse.jgit.util.IO;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+
+public class UpgradeFrom2_0_xTest extends InitTestCase {
+ public void testUpgrade() throws IOException, ConfigInvalidException {
+ final File p = newSitePath();
+ final SitePaths site = new SitePaths(p);
+ assertTrue(site.isNew);
+ assertTrue(site.site_path.mkdir());
+ assertTrue(site.etc_dir.mkdir());
+
+ for (String n : UpgradeFrom2_0_x.etcFiles) {
+ Writer w = new FileWriter(new File(p, n));
+ try {
+ w.write("# " + n + "\n");
+ } finally {
+ w.close();
+ }
+ }
+
+ FileBasedConfig old = new FileBasedConfig(new File(p, "gerrit.config"));
+ old.setString("ldap", null, "username", "ldap.user");
+ old.setString("ldap", null, "password", "ldap.s3kr3t");
+
+ old.setString("sendemail", null, "smtpUser", "email.user");
+ old.setString("sendemail", null, "smtpPass", "email.s3kr3t");
+ old.save();
+
+ final InitFlags flags = new InitFlags(site);
+ final ConsoleUI ui = createStrictMock(ConsoleUI.class);
+ Section.Factory sections = new Section.Factory() {
+ @Override
+ public Section get(String name) {
+ return new Section(flags, site, ui, name);
+ }
+ };
+
+ expect(ui.yesno(eq(true), eq("Upgrade '%s'"), eq(p.getCanonicalPath())))
+ .andReturn(true);
+ replay(ui);
+
+ UpgradeFrom2_0_x u = new UpgradeFrom2_0_x(site, flags, ui, sections);
+ assertTrue(u.isNeedUpgrade());
+ u.run();
+ assertFalse(u.isNeedUpgrade());
+ verify(ui);
+
+ for (String n : UpgradeFrom2_0_x.etcFiles) {
+ if ("gerrit.config".equals(n)) continue;
+ if ("secure.config".equals(n)) continue;
+ assertEquals("# " + n + "\n",//
+ new String(IO.readFully(new File(site.etc_dir, n)), "UTF-8"));
+ }
+
+ FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config);
+ FileBasedConfig sec = new FileBasedConfig(site.secure_config);
+ cfg.load();
+ sec.load();
+
+ assertEquals("email.user", cfg.getString("sendemail", null, "smtpUser"));
+ assertNull(cfg.getString("sendemail", null, "smtpPass"));
+ assertEquals("email.s3kr3t", sec.getString("sendemail", null, "smtpPass"));
+
+ assertEquals("ldap.user", cfg.getString("ldap", null, "username"));
+ assertNull(cfg.getString("ldap", null, "password"));
+ assertEquals("ldap.s3kr3t", sec.getString("ldap", null, "password"));
+
+ u.run();
+ }
+}