Acceptance tests framework: Allow tests to change Gerrit configuration
Tests may want to contribute to the Gerrit configuration. Each method in the test class should be able to add or override one or more parameters of the Gerrit server configuration. public class UseGerritConfigAnnotationTest extends AbstractDaemonTest { @Inject @GerritServerConfig Config serverConfig; @Test @GerritConfig(name="x.y", value="z") public void testOne() { assertEquals("z", serverConfig.getString("x", null, "y")); } @Test @GerritConfigs({ @GerritConfig(name="x.y", value="z"), @GerritConfig(name="a.b", value="c"), }) public void testMultiple() { assertEquals("z", serverConfig.getString("x", null, "y")); assertEquals("c", serverConfig.getString("a", null, "b")); } } Change-Id: I506d9f86280f1b067d2083cb33726009ed72c218
This commit is contained in:

committed by
Sasa Zivkov

parent
0a1bbae9ac
commit
feb5e7e98a
@@ -14,20 +14,51 @@
|
||||
|
||||
package com.google.gerrit.acceptance;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
public abstract class AbstractDaemonTest {
|
||||
protected GerritServer server;
|
||||
|
||||
@Before
|
||||
public final void beforeTest() throws Exception {
|
||||
server = GerritServer.start();
|
||||
@Rule
|
||||
public TestRule testRunner = new TestRule() {
|
||||
@Override
|
||||
public Statement apply(final Statement base, final Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
beforeTest(config(description));
|
||||
base.evaluate();
|
||||
afterTest();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
private static Config config(Description description) {
|
||||
GerritConfigs cfgs = description.getAnnotation(GerritConfigs.class);
|
||||
GerritConfig cfg = description.getAnnotation(GerritConfig.class);
|
||||
if (cfgs != null && cfg != null) {
|
||||
throw new IllegalStateException("Use either @GerritConfigs or @GerritConfig not both");
|
||||
}
|
||||
if (cfgs != null) {
|
||||
return ConfigAnnotationParser.parse(cfgs);
|
||||
} else if (cfg != null) {
|
||||
return ConfigAnnotationParser.parse(cfg);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void beforeTest(Config cfg) throws Exception {
|
||||
server = GerritServer.start(cfg);
|
||||
server.getTestInjector().injectMembers(this);
|
||||
}
|
||||
|
||||
@After
|
||||
public final void afterTest() throws Exception {
|
||||
private void afterTest() throws Exception {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,58 @@
|
||||
// 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.acceptance;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
class ConfigAnnotationParser {
|
||||
|
||||
private static Splitter splitter = Splitter.on(".").trimResults();
|
||||
|
||||
static Config parse(GerritConfigs annotation) {
|
||||
if (annotation == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Config cfg = new Config();
|
||||
for (GerritConfig c : annotation.value()) {
|
||||
parse(cfg, c);
|
||||
}
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static Config parse(GerritConfig annotation) {
|
||||
Config cfg = new Config();
|
||||
parse(cfg, annotation);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static private void parse(Config cfg, GerritConfig c) {
|
||||
ArrayList<String> l = Lists.newArrayList(splitter.split(c.name()));
|
||||
if (l.size() == 2) {
|
||||
cfg.setString(l.get(0), null, l.get(1), c.value());
|
||||
} else if (l.size() == 3) {
|
||||
cfg.setString(l.get(0), l.get(1), l.get(2), c.value());
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"GerritConfig.name must be of the format"
|
||||
+ " section.subsection.name or section.name");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
// 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.acceptance;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({METHOD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface GerritConfig {
|
||||
String name();
|
||||
String value();
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
// 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.acceptance;
|
||||
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target({METHOD})
|
||||
@Retention(RUNTIME)
|
||||
public @interface GerritConfigs {
|
||||
public GerritConfig[] value();
|
||||
}
|
@@ -23,6 +23,7 @@ import com.google.inject.Injector;
|
||||
import com.google.inject.Module;
|
||||
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.RepositoryCache;
|
||||
import org.eclipse.jgit.storage.file.FileBasedConfig;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
@@ -42,11 +43,11 @@ import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class GerritServer {
|
||||
public class GerritServer {
|
||||
|
||||
/** Returns fully started Gerrit server */
|
||||
static GerritServer start() throws Exception {
|
||||
final File site = initSite();
|
||||
static GerritServer start(Config base) throws Exception {
|
||||
final File site = initSite(base);
|
||||
final CyclicBarrier serverStarted = new CyclicBarrier(2);
|
||||
final Daemon daemon = new Daemon(new Runnable() {
|
||||
public void run() {
|
||||
@@ -80,7 +81,7 @@ class GerritServer {
|
||||
return new GerritServer(site, i, daemon, daemonService);
|
||||
}
|
||||
|
||||
private static File initSite() throws Exception {
|
||||
private static File initSite(Config base) throws Exception {
|
||||
File tmp = TempFileUtil.createTempDirectory();
|
||||
Init init = new Init();
|
||||
int rc = init.main(new String[] {
|
||||
@@ -93,10 +94,11 @@ class GerritServer {
|
||||
InetSocketAddress http = newPort();
|
||||
InetSocketAddress sshd = newPort();
|
||||
String url = "http://" + format(http) + "/";
|
||||
FileBasedConfig cfg = new FileBasedConfig(
|
||||
MergeableFileBasedConfig cfg = new MergeableFileBasedConfig(
|
||||
new File(new File(tmp, "etc"), "gerrit.config"),
|
||||
FS.DETECTED);
|
||||
cfg.load();
|
||||
cfg.merge(base);
|
||||
cfg.setString("gerrit", null, "canonicalWebUrl", url);
|
||||
cfg.setString("httpd", null, "listenUrl", url);
|
||||
cfg.setString("sshd", null, "listenAddress", format(sshd));
|
||||
|
@@ -0,0 +1,60 @@
|
||||
// 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.acceptance;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.storage.file.FileBasedConfig;
|
||||
import org.eclipse.jgit.util.FS;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* A file based Config that can merge another Config instance.
|
||||
*/
|
||||
public class MergeableFileBasedConfig extends FileBasedConfig {
|
||||
public MergeableFileBasedConfig(File cfgLocation, FS fs) {
|
||||
super(cfgLocation, fs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge another Config into this Config.
|
||||
*
|
||||
* In case a configuration parameter exists both in this instance and in the
|
||||
* merged instance then the value in this instance will simply replaced by
|
||||
* the value from the merged instance.
|
||||
*
|
||||
* @param s Config to merge into this instance
|
||||
*/
|
||||
public void merge(Config s) {
|
||||
if (s == null) {
|
||||
return;
|
||||
}
|
||||
for (String section : s.getSections()) {
|
||||
for (String subsection : s.getSubsections(section)) {
|
||||
for (String name : s.getNames(section, subsection)) {
|
||||
setStringList(section, subsection, name, Lists.newArrayList(s
|
||||
.getStringList(section, subsection, name)));
|
||||
}
|
||||
}
|
||||
|
||||
for (String name : s.getNames(section)) {
|
||||
setStringList(section, null, name,
|
||||
Lists.newArrayList(s.getStringList(section, null, name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
// 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.acceptance;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import com.google.gerrit.server.config.GerritServerConfig;
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UseGerritConfigAnnotationTest extends AbstractDaemonTest {
|
||||
|
||||
@Inject
|
||||
@GerritServerConfig
|
||||
Config serverConfig;
|
||||
|
||||
@Test
|
||||
@GerritConfig(name="x.y", value="z")
|
||||
public void testOne() {
|
||||
assertEquals("z", serverConfig.getString("x", null, "y"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@GerritConfigs({
|
||||
@GerritConfig(name="x.y", value="z"),
|
||||
@GerritConfig(name="a.b", value="c"),
|
||||
})
|
||||
public void testMultiple() {
|
||||
assertEquals("z", serverConfig.getString("x", null, "y"));
|
||||
assertEquals("c", serverConfig.getString("a", null, "b"));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user