mirror of
https://github.com/google/nomulus.git
synced 2025-05-15 17:07:15 +02:00
Improve RegistryToolEnvironment setup behavior
Here's an alternate approach that I think simplifies the existing code quite a bit. Now instead of doing: RegistryToolEnvironment.loadFromArgs(args); RegistryToolEnvironment.get().setup(); You just do one chained call: RegistryToolEnvironment.parseFromArgs(args).setup(); or call setup() on a known environment constant: RegistryToolEnvironment.ALPHA.setup(); This avoids having loadFromArgs() implicitly set the active env (but *not* do setup) and then having setup() *not* set the active env, both of which were confusing. Now parseFromArgs() is only responsible for parsing from args, and setup() both sets the env as the active one and does the environment variable setup (which also ensures that the RegistryToolEnvironment.instance field doesn't get out of sync with the RegistryEnvironment value). In addition, this CL adds a runCommandInEnvironment() method to CommandTestCase and ensures that the UNITTEST environment is always set before constructing the default command instance. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=117492978
This commit is contained in:
parent
14c794aa45
commit
5174c1c63f
7 changed files with 60 additions and 36 deletions
|
@ -72,8 +72,7 @@ public final class GtechTool {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(args);
|
RegistryToolEnvironment.parseFromArgs(args).setup();
|
||||||
RegistryToolEnvironment.get().setup();
|
|
||||||
new RegistryCli().run("gtech_tool", args, COMMAND_MAP);
|
new RegistryCli().run("gtech_tool", args, COMMAND_MAP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,8 +76,7 @@ public final class RegistryTool {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(args);
|
RegistryToolEnvironment.parseFromArgs(args).setup();
|
||||||
RegistryToolEnvironment.get().setup();
|
|
||||||
new RegistryCli().run("registry_tool", args, COMMAND_MAP);
|
new RegistryCli().run("registry_tool", args, COMMAND_MAP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
package com.google.domain.registry.tools;
|
package com.google.domain.registry.tools;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
@ -60,21 +60,23 @@ enum RegistryToolEnvironment {
|
||||||
*
|
*
|
||||||
* @see #get()
|
* @see #get()
|
||||||
*/
|
*/
|
||||||
static void loadFromArgs(String[] args) {
|
static RegistryToolEnvironment parseFromArgs(String[] args) {
|
||||||
instance = valueOf(getFlagValue(args, FLAGS).toUpperCase());
|
return valueOf(getFlagValue(args, FLAGS).toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current environment.
|
* Returns the current environment.
|
||||||
*
|
*
|
||||||
* <p>This should be called after {@link #loadFromArgs(String[])}.
|
* <p>This should be called after {@link #parseFromArgs(String[])}.
|
||||||
*/
|
*/
|
||||||
static RegistryToolEnvironment get() {
|
static RegistryToolEnvironment get() {
|
||||||
return checkNotNull(instance);
|
checkState(instance != null, "No RegistryToolEnvironment has been set up");
|
||||||
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Setup execution environment. Call this method before any classes are loaded. */
|
/** Setup execution environment. Call this method before any classes are loaded. */
|
||||||
void setup() {
|
void setup() {
|
||||||
|
instance = this;
|
||||||
System.setProperty(RegistryEnvironment.PROPERTY, actualEnvironment.name());
|
System.setProperty(RegistryEnvironment.PROPERTY, actualEnvironment.name());
|
||||||
for (ImmutableMap.Entry<String, String> entry : extraProperties.entrySet()) {
|
for (ImmutableMap.Entry<String, String> entry : extraProperties.entrySet()) {
|
||||||
System.setProperty(entry.getKey(), entry.getValue());
|
System.setProperty(entry.getKey(), entry.getValue());
|
||||||
|
|
|
@ -55,7 +55,7 @@ public abstract class CommandTestCase<C extends Command> {
|
||||||
|
|
||||||
private ByteArrayOutputStream stdout = new ByteArrayOutputStream();
|
private ByteArrayOutputStream stdout = new ByteArrayOutputStream();
|
||||||
|
|
||||||
C command = newCommandInstance();
|
protected C command;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final AppEngineRule appEngine = AppEngineRule.builder()
|
public final AppEngineRule appEngine = AppEngineRule.builder()
|
||||||
|
@ -70,12 +70,15 @@ public abstract class CommandTestCase<C extends Command> {
|
||||||
public TemporaryFolder tmpDir = new TemporaryFolder();
|
public TemporaryFolder tmpDir = new TemporaryFolder();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void initStreams() {
|
public final void beforeCommandTestCase() {
|
||||||
|
// Ensure the UNITTEST environment has been set before constructing a new command instance.
|
||||||
|
RegistryToolEnvironment.UNITTEST.setup();
|
||||||
|
command = newCommandInstance();
|
||||||
System.setOut(new PrintStream(stdout));
|
System.setOut(new PrintStream(stdout));
|
||||||
}
|
}
|
||||||
|
|
||||||
void runCommand(String... args) throws Exception {
|
void runCommandInEnvironment(RegistryToolEnvironment env, String... args) throws Exception {
|
||||||
RegistryToolEnvironment.UNITTEST.setup();
|
env.setup();
|
||||||
try {
|
try {
|
||||||
JCommander jcommander = new JCommander(command);
|
JCommander jcommander = new JCommander(command);
|
||||||
jcommander.addConverterFactory(new ParameterFactory());
|
jcommander.addConverterFactory(new ParameterFactory());
|
||||||
|
@ -85,9 +88,15 @@ public abstract class CommandTestCase<C extends Command> {
|
||||||
// Clear the session cache so that subsequent reads for verification purposes hit datastore.
|
// Clear the session cache so that subsequent reads for verification purposes hit datastore.
|
||||||
// This primarily matters for AutoTimestamp fields, which otherwise won't have updated values.
|
// This primarily matters for AutoTimestamp fields, which otherwise won't have updated values.
|
||||||
ofy().clearSessionCache();
|
ofy().clearSessionCache();
|
||||||
|
// Reset back to UNITTEST environment.
|
||||||
|
RegistryToolEnvironment.UNITTEST.setup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void runCommand(String... args) throws Exception {
|
||||||
|
runCommandInEnvironment(RegistryToolEnvironment.UNITTEST, args);
|
||||||
|
}
|
||||||
|
|
||||||
/** Adds "--force" as the first parameter, then runs the command. */
|
/** Adds "--force" as the first parameter, then runs the command. */
|
||||||
void runCommandForced(String... args) throws Exception {
|
void runCommandForced(String... args) throws Exception {
|
||||||
runCommand(ObjectArrays.concat("--force", args));
|
runCommand(ObjectArrays.concat("--force", args));
|
||||||
|
|
|
@ -51,8 +51,7 @@ public class GtechToolTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
|
RegistryToolEnvironment.UNITTEST.setup();
|
||||||
RegistryToolEnvironment.get().setup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -30,35 +30,50 @@ public class RegistryToolEnvironmentTest {
|
||||||
public final ExpectedException thrown = ExpectedException.none();
|
public final ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_shortNotation_works() throws Exception {
|
public void testGet_withoutSetup_throws() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
|
thrown.expect(IllegalStateException.class);
|
||||||
|
RegistryToolEnvironment.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetup_changesEnvironmentReturnedByGet() throws Exception {
|
||||||
|
RegistryToolEnvironment.UNITTEST.setup();
|
||||||
|
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.UNITTEST);
|
||||||
|
|
||||||
|
RegistryToolEnvironment.ALPHA.setup();
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
|
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFromArgs_shortNotation_works() throws Exception {
|
||||||
|
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "alpha" }))
|
||||||
|
.isEqualTo(RegistryToolEnvironment.ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_longNotation_works() throws Exception {
|
public void testFromArgs_longNotation_works() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "--environment", "alpha" });
|
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "--environment", "alpha" }))
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
|
.isEqualTo(RegistryToolEnvironment.ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_uppercase_works() throws Exception {
|
public void testFromArgs_uppercase_works() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "QA" });
|
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "QA" }))
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.QA);
|
.isEqualTo(RegistryToolEnvironment.QA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_equalsNotation_works() throws Exception {
|
public void testFromArgs_equalsNotation_works() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e=sandbox" });
|
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e=sandbox" }))
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.SANDBOX);
|
.isEqualTo(RegistryToolEnvironment.SANDBOX);
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "--environment=sandbox" });
|
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "--environment=sandbox" }))
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.SANDBOX);
|
.isEqualTo(RegistryToolEnvironment.SANDBOX);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_envFlagAfterCommandName_getsIgnored() throws Exception {
|
public void testFromArgs_envFlagAfterCommandName_getsIgnored() throws Exception {
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] {
|
RegistryToolEnvironment.parseFromArgs(new String[] {
|
||||||
"registrar_activity_report",
|
"registrar_activity_report",
|
||||||
"-e", "1406851199"});
|
"-e", "1406851199"});
|
||||||
}
|
}
|
||||||
|
@ -66,30 +81,32 @@ public class RegistryToolEnvironmentTest {
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_missingEnvironmentFlag_throwsIae() throws Exception {
|
public void testFromArgs_missingEnvironmentFlag_throwsIae() throws Exception {
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] {});
|
RegistryToolEnvironment.parseFromArgs(new String[] {});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_extraEnvFlagAfterCommandName_getsIgnored() throws Exception {
|
public void testFromArgs_extraEnvFlagAfterCommandName_getsIgnored() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] {
|
String[] args = new String[] {
|
||||||
"-e", "alpha",
|
"-e", "alpha",
|
||||||
"registrar_activity_report",
|
"registrar_activity_report",
|
||||||
"-e", "1406851199"});
|
"-e", "1406851199"};
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
|
assertThat(RegistryToolEnvironment.parseFromArgs(args))
|
||||||
|
.isEqualTo(RegistryToolEnvironment.ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_loggingFlagWithUnderscores_isntConsideredCommand() throws Exception {
|
public void testFromArgs_loggingFlagWithUnderscores_isntConsideredCommand() throws Exception {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] {
|
String[] args = new String[] {
|
||||||
"--logging_properties_file", "my_file.properties",
|
"--logging_properties_file", "my_file.properties",
|
||||||
"-e", "alpha",
|
"-e", "alpha",
|
||||||
"list_tlds"});
|
"list_tlds"};
|
||||||
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
|
assertThat(RegistryToolEnvironment.parseFromArgs(args))
|
||||||
|
.isEqualTo(RegistryToolEnvironment.ALPHA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFromArgs_badName_throwsIae() throws Exception {
|
public void testFromArgs_badName_throwsIae() throws Exception {
|
||||||
thrown.expect(IllegalArgumentException.class);
|
thrown.expect(IllegalArgumentException.class);
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alphaville" });
|
RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "alphaville" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,8 +50,7 @@ public class RegistryToolTest {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
|
RegistryToolEnvironment.UNITTEST.setup();
|
||||||
RegistryToolEnvironment.get().setup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue