Remove TestConfig in favor of reworked Config ctors

This commit is contained in:
Chris Beams 2019-12-20 16:24:05 +01:00
parent 47794174a1
commit e67746b0a4
No known key found for this signature in database
GPG Key ID: 3D214F8F5BC5ED73
8 changed files with 81 additions and 84 deletions

View File

@ -14,6 +14,7 @@ import joptsimple.util.PathConverter;
import joptsimple.util.PathProperties; import joptsimple.util.PathProperties;
import joptsimple.util.RegexMatcher; import joptsimple.util.RegexMatcher;
import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -88,6 +89,7 @@ public class Config {
// default data dir properties // default data dir properties
private final String defaultAppName; private final String defaultAppName;
private final File defaultUserDataDir;
private final File defaultAppDataDir; private final File defaultAppDataDir;
private final File defaultConfigFile; private final File defaultConfigFile;
@ -159,15 +161,15 @@ public class Config {
private final OptionParser parser = new OptionParser(); private final OptionParser parser = new OptionParser();
public Config(String defaultAppName) { public Config(String... args) {
this(defaultAppName, new String[]{}); this(tempAppName(), tempUserDataDir(), args);
} }
public Config(String defaultAppName, String[] args) { public Config(String defaultAppName, File defaultUserDataDir, String... args) {
File defaultUserDataDir = getDefaultUserDataDir();
this.defaultAppName = defaultAppName; this.defaultAppName = defaultAppName;
this.defaultAppDataDir = new File(defaultUserDataDir, this.defaultAppName); this.defaultUserDataDir = defaultUserDataDir;
this.defaultConfigFile = new File(defaultAppDataDir, DEFAULT_CONFIG_FILE_NAME); this.defaultAppDataDir = new File(this.defaultUserDataDir, this.defaultAppName);
this.defaultConfigFile = new File(this.defaultAppDataDir, DEFAULT_CONFIG_FILE_NAME);
AbstractOptionSpec<Void> helpOpt = AbstractOptionSpec<Void> helpOpt =
parser.accepts("help", "Print this help text") parser.accepts("help", "Print this help text")
@ -184,7 +186,7 @@ public class Config {
parser.accepts("userDataDir", "User data directory") parser.accepts("userDataDir", "User data directory")
.withRequiredArg() .withRequiredArg()
.ofType(File.class) .ofType(File.class)
.defaultsTo(defaultUserDataDir); .defaultsTo(this.defaultUserDataDir);
ArgumentAcceptingOptionSpec<String> appNameOpt = ArgumentAcceptingOptionSpec<String> appNameOpt =
parser.accepts(APP_NAME, "Application name") parser.accepts(APP_NAME, "Application name")
@ -661,7 +663,7 @@ public class Config {
return defaultAppName; return defaultAppName;
} }
public File getDefaultUserDataDir() { public static File getOsUserDataDir() {
if (Utilities.isWindows()) if (Utilities.isWindows())
return new File(System.getenv("APPDATA")); return new File(System.getenv("APPDATA"));
@ -672,6 +674,28 @@ public class Config {
return Paths.get(System.getProperty("user.home"), ".local", "share").toFile(); return Paths.get(System.getProperty("user.home"), ".local", "share").toFile();
} }
private static File tempUserDataDir() {
try {
return Files.createTempDirectory("BisqTempUserData").toFile();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
private static String tempAppName() {
try {
File file = Files.createTempFile("Bisq", "Temp").toFile();
file.delete();
return file.toPath().getFileName().toString();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
public File getDefaultUserDataDir() {
return defaultUserDataDir;
}
public File getDefaultAppDataDir() { public File getDefaultAppDataDir() {
return defaultAppDataDir; return defaultAppDataDir;
} }

View File

@ -1,26 +0,0 @@
package bisq.common.config;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
public class TestConfig extends Config {
public TestConfig() {
super(generateTestAppName());
}
public TestConfig(String... args) {
super(generateTestAppName(), args);
}
private static String generateTestAppName() {
try {
File file = File.createTempFile("Bisq", "Test");
file.delete();
return file.toPath().getFileName().toString();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}

View File

@ -1,5 +1,7 @@
package bisq.common.config; package bisq.common.config;
import java.nio.file.Files;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -16,9 +18,9 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.isEmptyString; import static org.hamcrest.Matchers.isEmptyString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class ConfigTests { public class ConfigTests {
@ -30,30 +32,24 @@ public class ConfigTests {
// These options include appName, userDataDir, appDataDir, and configFile // These options include appName, userDataDir, appDataDir, and configFile
@Test @Test
public void whenTestConfigNoArgCtorIsCalled_thenDefaultAppNameIsSetToRandomValue() { public void whenNoArgCtorIsCalled_thenDefaultAppNameIsSetToTempValue() {
Config config = new TestConfig(); Config config = new Config();
String defaultAppName = config.getDefaultAppName(); String defaultAppName = config.getDefaultAppName();
String regex = "Bisq\\d{2,}Test"; String regex = "Bisq\\d{2,}Temp";
assertTrue(format("Test app name '%s' failed to match '%s'", defaultAppName, regex), assertTrue(format("Temp app name '%s' failed to match '%s'", defaultAppName, regex),
defaultAppName.matches(regex)); defaultAppName.matches(regex));
} }
@Test
public void whenStringConstructorIsCalled_thenDefaultAppNamePropertyIsAssignedToItsValue() {
Config config = new Config("Custom-Bisq");
assertThat(config.getDefaultAppName(), equalTo("Custom-Bisq"));
}
@Test @Test
public void whenAppNameOptionIsSet_thenAppNamePropertyDiffersFromDefaultAppNameProperty() { public void whenAppNameOptionIsSet_thenAppNamePropertyDiffersFromDefaultAppNameProperty() {
Config config = new TestConfig("--appName=My-Bisq"); Config config = new Config("--appName=My-Bisq");
assertThat(config.getAppName(), equalTo("My-Bisq")); assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getAppName(), not(equalTo(config.getDefaultAppName()))); assertThat(config.getAppName(), not(equalTo(config.getDefaultAppName())));
} }
@Test @Test
public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() { public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() {
Config config = new TestConfig(); Config config = new Config();
assertThat(config.getAppName(), equalTo(config.getDefaultAppName())); assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir())); assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(config.getDefaultAppDataDir())); assertThat(config.getAppDataDir(), equalTo(config.getDefaultAppDataDir()));
@ -62,7 +58,7 @@ public class ConfigTests {
@Test @Test
public void whenAppNameOptionIsSet_thenDataDirPropertiesReflectItsValue() { public void whenAppNameOptionIsSet_thenDataDirPropertiesReflectItsValue() {
Config config = new TestConfig("--appName=My-Bisq"); Config config = new Config("--appName=My-Bisq");
assertThat(config.getAppName(), equalTo("My-Bisq")); assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir())); assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), "My-Bisq"))); assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), "My-Bisq")));
@ -70,35 +66,38 @@ public class ConfigTests {
} }
@Test @Test
public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() { public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
Config config = new TestConfig("--appDataDir=/mydata/myapp"); File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = new Config("--appDataDir=" + appDataDir);
assertThat(config.getAppName(), equalTo(config.getDefaultAppName())); assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir())); assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File("/mydata/myapp"))); assertThat(config.getAppDataDir(), equalTo(appDataDir));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME))); assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
} }
@Test @Test
public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() { public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
Config config = new TestConfig("--userDataDir=/mydata"); File userDataDir = Files.createTempDirectory("myuserdata").toFile();
Config config = new Config("--userDataDir=" + userDataDir);
assertThat(config.getAppName(), equalTo(config.getDefaultAppName())); assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(new File("/mydata"))); assertThat(config.getUserDataDir(), equalTo(userDataDir));
assertThat(config.getAppDataDir(), equalTo(new File("/mydata", config.getDefaultAppName()))); assertThat(config.getAppDataDir(), equalTo(new File(userDataDir, config.getDefaultAppName())));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME))); assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
} }
@Test @Test
public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() { public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() throws IOException {
Config config = new TestConfig("--appName=My-Bisq", "--appDataDir=/mydata/myapp"); File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = new Config("--appName=My-Bisq", "--appDataDir=" + appDataDir);
assertThat(config.getAppName(), equalTo("My-Bisq")); assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir())); assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File("/mydata/myapp"))); assertThat(config.getAppDataDir(), equalTo(appDataDir));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME))); assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
} }
@Test @Test
public void whenConfFileOptionIsSetToNonExistentFile_thenConfFilePropertyFallsBackToDefaultValue() { public void whenConfFileOptionIsSetToNonExistentFile_thenConfFilePropertyFallsBackToDefaultValue() {
Config config = new TestConfig("--configFile=/tmp/bogus.properties"); Config config = new Config("--configFile=/tmp/bogus.properties");
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME))); assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
} }
@ -108,39 +107,39 @@ public class ConfigTests {
try (PrintWriter writer = new PrintWriter(configFile)) { try (PrintWriter writer = new PrintWriter(configFile)) {
writer.println("appName=Bisq-configFileValue"); writer.println("appName=Bisq-configFileValue");
} }
Config config = new TestConfig("--appName=Bisq-commandLineValue"); Config config = new Config("--appName=Bisq-commandLineValue");
assertThat(config.getAppName(), equalTo("Bisq-commandLineValue")); assertThat(config.getAppName(), equalTo("Bisq-commandLineValue"));
} }
@Test @Test
public void whenUnrecognizedOptionIsSet_thenThrowConfigException() { public void whenUnrecognizedOptionIsSet_thenConfigExceptionIsThrown() {
exceptionRule.expect(ConfigException.class); exceptionRule.expect(ConfigException.class);
exceptionRule.expectMessage("problem parsing option 'bogus': bogus is not a recognized option"); exceptionRule.expectMessage("problem parsing option 'bogus': bogus is not a recognized option");
new TestConfig("--bogus"); new Config("--bogus");
} }
@Test @Test
public void whenOptionFileArgumentDoesNotExist_thenThrowConfigException() { public void whenOptionFileArgumentDoesNotExist_thenConfigExceptionIsThrown() {
exceptionRule.expect(ConfigException.class); exceptionRule.expect(ConfigException.class);
exceptionRule.expectMessage("problem parsing option 'torrcFile': File [/does/not/exist] does not exist"); exceptionRule.expectMessage("problem parsing option 'torrcFile': File [/does/not/exist] does not exist");
new TestConfig("--torrcFile=/does/not/exist"); new Config("--torrcFile=/does/not/exist");
} }
@Test @Test
public void whenConfigFileOptionIsSetInConfigFile_thenDisallowedOptionExceptionisThrown() throws IOException { public void whenConfigFileOptionIsSetInConfigFile_thenConfigExceptionIsThrown() throws IOException {
File configFile = File.createTempFile("bisq", "properties"); File configFile = File.createTempFile("bisq", "properties");
try (PrintWriter writer = new PrintWriter(configFile)) { try (PrintWriter writer = new PrintWriter(configFile)) {
writer.println("configFile=/tmp/other.bisq.properties"); writer.println("configFile=/tmp/other.bisq.properties");
} }
exceptionRule.expect(IllegalArgumentException.class); exceptionRule.expect(ConfigException.class);
exceptionRule.expectMessage("The 'configFile' option is disallowed in config files"); exceptionRule.expectMessage("The 'configFile' option is disallowed in config files");
new TestConfig("--configFile=" + configFile.getAbsolutePath()); new Config("--configFile=" + configFile.getAbsolutePath());
} }
@Test @Test
public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException { public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException {
File configFile = File.createTempFile("bisq", "properties"); File configFile = File.createTempFile("bisq", "properties");
Config config = new TestConfig("--configFile=" + configFile.getAbsolutePath()); Config config = new Config("--configFile=" + configFile.getAbsolutePath());
assertThat(config.getConfigFile(), equalTo(configFile)); assertThat(config.getConfigFile(), equalTo(configFile));
} }
@ -150,7 +149,7 @@ public class ConfigTests {
try (PrintWriter writer = new PrintWriter(configFile)) { try (PrintWriter writer = new PrintWriter(configFile)) {
writer.println("appName=My-Bisq"); writer.println("appName=My-Bisq");
} }
Config config = new TestConfig("--configFile=" + configFile.getAbsolutePath()); Config config = new Config("--configFile=" + configFile.getAbsolutePath());
assertThat(config.getAppName(), equalTo("My-Bisq")); assertThat(config.getAppName(), equalTo("My-Bisq"));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir())); assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), config.getAppName()))); assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), config.getAppName())));
@ -159,14 +158,14 @@ public class ConfigTests {
@Test @Test
public void whenBannedBtcNodesOptionIsSet_thenBannedBtcNodesPropertyReturnsItsValue() { public void whenBannedBtcNodesOptionIsSet_thenBannedBtcNodesPropertyReturnsItsValue() {
Config config = new TestConfig("--bannedBtcNodes=foo.onion:8333,bar.onion:8333"); Config config = new Config("--bannedBtcNodes=foo.onion:8333,bar.onion:8333");
assertThat(config.getBannedBtcNodes(), contains("foo.onion:8333", "bar.onion:8333")); assertThat(config.getBannedBtcNodes(), contains("foo.onion:8333", "bar.onion:8333"));
} }
@Test @Test
public void whenHelpOptionIsSet_thenHelpRequestedIsThrown() { public void whenHelpOptionIsSet_thenIsHelpRequestedIsTrue() {
new TestConfig("--help"); assertFalse(new Config().isHelpRequested());
fail(); assertTrue(new Config("--help").isHelpRequested());
} }
@Test @Test

View File

@ -51,6 +51,8 @@ import java.io.IOException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import static bisq.common.config.Config.getOsUserDataDir;
@Slf4j @Slf4j
public abstract class BisqExecutable implements GracefulShutDownHandler, BisqSetup.BisqSetupListener { public abstract class BisqExecutable implements GracefulShutDownHandler, BisqSetup.BisqSetupListener {
@ -75,7 +77,7 @@ public abstract class BisqExecutable implements GracefulShutDownHandler, BisqSet
public void execute(String[] args) { public void execute(String[] args) {
try { try {
config = new Config(appName, args); config = new Config(appName, getOsUserDataDir(), args);
if (config.isHelpRequested()) { if (config.isHelpRequested()) {
config.printHelp(System.out, new BisqHelpFormatter(fullName, scriptName, version)); config.printHelp(System.out, new BisqHelpFormatter(fullName, scriptName, version));
System.exit(EXIT_SUCCESS); System.exit(EXIT_SUCCESS);

View File

@ -20,7 +20,6 @@ package bisq.core.network.p2p.seed;
import bisq.network.p2p.NodeAddress; import bisq.network.p2p.NodeAddress;
import bisq.common.config.Config; import bisq.common.config.Config;
import bisq.common.config.TestConfig;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -31,7 +30,7 @@ public class DefaultSeedNodeRepositoryTest {
@Test @Test
public void getSeedNodes() { public void getSeedNodes() {
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new TestConfig()); DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new Config());
Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty()); Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty());
} }
@ -39,8 +38,8 @@ public class DefaultSeedNodeRepositoryTest {
public void manualSeedNodes() { public void manualSeedNodes() {
String seed1 = "asdf:8001"; String seed1 = "asdf:8001";
String seed2 = "fdsa:6001"; String seed2 = "fdsa:6001";
String seedNodes = format("--%s=%s,%s", Config.SEED_NODES, seed1, seed2); String seedNodesOption= format("--%s=%s,%s", Config.SEED_NODES, seed1, seed2);
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new TestConfig(seedNodes)); DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new Config(seedNodesOption));
Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty()); Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty());
Assert.assertEquals(2, DUT.getSeedNodeAddresses().size()); Assert.assertEquals(2, DUT.getSeedNodeAddresses().size());
Assert.assertTrue(DUT.getSeedNodeAddresses().contains(new NodeAddress(seed1))); Assert.assertTrue(DUT.getSeedNodeAddresses().contains(new NodeAddress(seed1)));

View File

@ -25,7 +25,6 @@ import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res; import bisq.core.locale.Res;
import bisq.common.config.Config; import bisq.common.config.Config;
import bisq.common.config.TestConfig;
import bisq.common.storage.Storage; import bisq.common.storage.Storage;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
@ -60,7 +59,7 @@ public class PreferencesTest {
storage = mock(Storage.class); storage = mock(Storage.class);
preferences = new Preferences( preferences = new Preferences(
storage, new TestConfig(), null, null, Config.DEFAULT_FULL_DAO_NODE, storage, new Config(), null, null, Config.DEFAULT_FULL_DAO_NODE,
null, null, Config.UNSPECIFIED_PORT); null, null, Config.UNSPECIFIED_PORT);
} }

View File

@ -52,7 +52,7 @@ import bisq.network.p2p.network.BridgeAddressProvider;
import bisq.network.p2p.seed.SeedNodeRepository; import bisq.network.p2p.seed.SeedNodeRepository;
import bisq.common.ClockWatcher; import bisq.common.ClockWatcher;
import bisq.common.config.TestConfig; import bisq.common.config.Config;
import bisq.common.crypto.KeyRing; import bisq.common.crypto.KeyRing;
import bisq.common.crypto.KeyStorage; import bisq.common.crypto.KeyStorage;
import bisq.common.crypto.PubKeyRing; import bisq.common.crypto.PubKeyRing;
@ -80,7 +80,7 @@ public class GuiceSetupTest {
Res.setup(); Res.setup();
CurrencyUtil.setup(); CurrencyUtil.setup();
injector = Guice.createInjector(new BisqAppModule(new TestConfig())); injector = Guice.createInjector(new BisqAppModule(new Config()));
} }
@Test @Test

View File

@ -5,7 +5,7 @@ import bisq.core.app.misc.ModuleForAppWithP2p;
import bisq.core.locale.CurrencyUtil; import bisq.core.locale.CurrencyUtil;
import bisq.core.locale.Res; import bisq.core.locale.Res;
import bisq.common.config.TestConfig; import bisq.common.config.Config;
import com.google.inject.Guice; import com.google.inject.Guice;
@ -17,7 +17,7 @@ public class GuiceSetupTest {
Res.setup(); Res.setup();
CurrencyUtil.setup(); CurrencyUtil.setup();
ModuleForAppWithP2p module = new ModuleForAppWithP2p(new TestConfig()); ModuleForAppWithP2p module = new ModuleForAppWithP2p(new Config());
Guice.createInjector(module).getInstance(AppSetupWithP2PAndDAO.class); Guice.createInjector(module).getInstance(AppSetupWithP2PAndDAO.class);
} }
} }