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:
nickfelt 2016-03-17 15:37:58 -07:00 committed by Justine Tunney
parent 14c794aa45
commit 5174c1c63f
7 changed files with 60 additions and 36 deletions

View file

@ -72,8 +72,7 @@ public final class GtechTool {
.build();
public static void main(String[] args) throws Exception {
RegistryToolEnvironment.loadFromArgs(args);
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.parseFromArgs(args).setup();
new RegistryCli().run("gtech_tool", args, COMMAND_MAP);
}
}

View file

@ -76,8 +76,7 @@ public final class RegistryTool {
.build();
public static void main(String[] args) throws Exception {
RegistryToolEnvironment.loadFromArgs(args);
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.parseFromArgs(args).setup();
new RegistryCli().run("registry_tool", args, COMMAND_MAP);
}
}

View file

@ -15,7 +15,7 @@
package com.google.domain.registry.tools;
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.ImmutableMap;
@ -60,21 +60,23 @@ enum RegistryToolEnvironment {
*
* @see #get()
*/
static void loadFromArgs(String[] args) {
instance = valueOf(getFlagValue(args, FLAGS).toUpperCase());
static RegistryToolEnvironment parseFromArgs(String[] args) {
return valueOf(getFlagValue(args, FLAGS).toUpperCase());
}
/**
* 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() {
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. */
void setup() {
instance = this;
System.setProperty(RegistryEnvironment.PROPERTY, actualEnvironment.name());
for (ImmutableMap.Entry<String, String> entry : extraProperties.entrySet()) {
System.setProperty(entry.getKey(), entry.getValue());

View file

@ -55,7 +55,7 @@ public abstract class CommandTestCase<C extends Command> {
private ByteArrayOutputStream stdout = new ByteArrayOutputStream();
C command = newCommandInstance();
protected C command;
@Rule
public final AppEngineRule appEngine = AppEngineRule.builder()
@ -70,12 +70,15 @@ public abstract class CommandTestCase<C extends Command> {
public TemporaryFolder tmpDir = new TemporaryFolder();
@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));
}
void runCommand(String... args) throws Exception {
RegistryToolEnvironment.UNITTEST.setup();
void runCommandInEnvironment(RegistryToolEnvironment env, String... args) throws Exception {
env.setup();
try {
JCommander jcommander = new JCommander(command);
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.
// This primarily matters for AutoTimestamp fields, which otherwise won't have updated values.
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. */
void runCommandForced(String... args) throws Exception {
runCommand(ObjectArrays.concat("--force", args));

View file

@ -51,8 +51,7 @@ public class GtechToolTest {
@Before
public void init() {
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.UNITTEST.setup();
}
@Test

View file

@ -30,35 +30,50 @@ public class RegistryToolEnvironmentTest {
public final ExpectedException thrown = ExpectedException.none();
@Test
public void testFromArgs_shortNotation_works() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
public void testGet_withoutSetup_throws() throws Exception {
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);
}
@Test
public void testFromArgs_shortNotation_works() throws Exception {
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "alpha" }))
.isEqualTo(RegistryToolEnvironment.ALPHA);
}
@Test
public void testFromArgs_longNotation_works() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] { "--environment", "alpha" });
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "--environment", "alpha" }))
.isEqualTo(RegistryToolEnvironment.ALPHA);
}
@Test
public void testFromArgs_uppercase_works() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "QA" });
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.QA);
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "QA" }))
.isEqualTo(RegistryToolEnvironment.QA);
}
@Test
public void testFromArgs_equalsNotation_works() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] { "-e=sandbox" });
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.SANDBOX);
RegistryToolEnvironment.loadFromArgs(new String[] { "--environment=sandbox" });
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.SANDBOX);
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "-e=sandbox" }))
.isEqualTo(RegistryToolEnvironment.SANDBOX);
assertThat(RegistryToolEnvironment.parseFromArgs(new String[] { "--environment=sandbox" }))
.isEqualTo(RegistryToolEnvironment.SANDBOX);
}
@Test
public void testFromArgs_envFlagAfterCommandName_getsIgnored() throws Exception {
thrown.expect(IllegalArgumentException.class);
RegistryToolEnvironment.loadFromArgs(new String[] {
RegistryToolEnvironment.parseFromArgs(new String[] {
"registrar_activity_report",
"-e", "1406851199"});
}
@ -66,30 +81,32 @@ public class RegistryToolEnvironmentTest {
@Test
public void testFromArgs_missingEnvironmentFlag_throwsIae() throws Exception {
thrown.expect(IllegalArgumentException.class);
RegistryToolEnvironment.loadFromArgs(new String[] {});
RegistryToolEnvironment.parseFromArgs(new String[] {});
}
@Test
public void testFromArgs_extraEnvFlagAfterCommandName_getsIgnored() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] {
String[] args = new String[] {
"-e", "alpha",
"registrar_activity_report",
"-e", "1406851199"});
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
"-e", "1406851199"};
assertThat(RegistryToolEnvironment.parseFromArgs(args))
.isEqualTo(RegistryToolEnvironment.ALPHA);
}
@Test
public void testFromArgs_loggingFlagWithUnderscores_isntConsideredCommand() throws Exception {
RegistryToolEnvironment.loadFromArgs(new String[] {
String[] args = new String[] {
"--logging_properties_file", "my_file.properties",
"-e", "alpha",
"list_tlds"});
assertThat(RegistryToolEnvironment.get()).isEqualTo(RegistryToolEnvironment.ALPHA);
"list_tlds"};
assertThat(RegistryToolEnvironment.parseFromArgs(args))
.isEqualTo(RegistryToolEnvironment.ALPHA);
}
@Test
public void testFromArgs_badName_throwsIae() throws Exception {
thrown.expect(IllegalArgumentException.class);
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alphaville" });
RegistryToolEnvironment.parseFromArgs(new String[] { "-e", "alphaville" });
}
}

View file

@ -50,8 +50,7 @@ public class RegistryToolTest {
@Before
public void init() {
RegistryToolEnvironment.loadFromArgs(new String[] { "-e", "alpha" });
RegistryToolEnvironment.get().setup();
RegistryToolEnvironment.UNITTEST.setup();
}
@Test