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.RegexMatcher;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -88,6 +89,7 @@ public class Config {
// default data dir properties
private final String defaultAppName;
private final File defaultUserDataDir;
private final File defaultAppDataDir;
private final File defaultConfigFile;
@ -159,15 +161,15 @@ public class Config {
private final OptionParser parser = new OptionParser();
public Config(String defaultAppName) {
this(defaultAppName, new String[]{});
public Config(String... args) {
this(tempAppName(), tempUserDataDir(), args);
}
public Config(String defaultAppName, String[] args) {
File defaultUserDataDir = getDefaultUserDataDir();
public Config(String defaultAppName, File defaultUserDataDir, String... args) {
this.defaultAppName = defaultAppName;
this.defaultAppDataDir = new File(defaultUserDataDir, this.defaultAppName);
this.defaultConfigFile = new File(defaultAppDataDir, DEFAULT_CONFIG_FILE_NAME);
this.defaultUserDataDir = defaultUserDataDir;
this.defaultAppDataDir = new File(this.defaultUserDataDir, this.defaultAppName);
this.defaultConfigFile = new File(this.defaultAppDataDir, DEFAULT_CONFIG_FILE_NAME);
AbstractOptionSpec<Void> helpOpt =
parser.accepts("help", "Print this help text")
@ -184,7 +186,7 @@ public class Config {
parser.accepts("userDataDir", "User data directory")
.withRequiredArg()
.ofType(File.class)
.defaultsTo(defaultUserDataDir);
.defaultsTo(this.defaultUserDataDir);
ArgumentAcceptingOptionSpec<String> appNameOpt =
parser.accepts(APP_NAME, "Application name")
@ -661,7 +663,7 @@ public class Config {
return defaultAppName;
}
public File getDefaultUserDataDir() {
public static File getOsUserDataDir() {
if (Utilities.isWindows())
return new File(System.getenv("APPDATA"));
@ -672,6 +674,28 @@ public class Config {
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() {
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;
import java.nio.file.Files;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -16,9 +18,9 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.isEmptyString;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class ConfigTests {
@ -30,30 +32,24 @@ public class ConfigTests {
// These options include appName, userDataDir, appDataDir, and configFile
@Test
public void whenTestConfigNoArgCtorIsCalled_thenDefaultAppNameIsSetToRandomValue() {
Config config = new TestConfig();
public void whenNoArgCtorIsCalled_thenDefaultAppNameIsSetToTempValue() {
Config config = new Config();
String defaultAppName = config.getDefaultAppName();
String regex = "Bisq\\d{2,}Test";
assertTrue(format("Test app name '%s' failed to match '%s'", defaultAppName, regex),
String regex = "Bisq\\d{2,}Temp";
assertTrue(format("Temp app name '%s' failed to match '%s'", defaultAppName, regex),
defaultAppName.matches(regex));
}
@Test
public void whenStringConstructorIsCalled_thenDefaultAppNamePropertyIsAssignedToItsValue() {
Config config = new Config("Custom-Bisq");
assertThat(config.getDefaultAppName(), equalTo("Custom-Bisq"));
}
@Test
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(), not(equalTo(config.getDefaultAppName())));
}
@Test
public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() {
Config config = new TestConfig();
Config config = new Config();
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(config.getDefaultAppDataDir()));
@ -62,7 +58,7 @@ public class ConfigTests {
@Test
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.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), "My-Bisq")));
@ -70,35 +66,38 @@ public class ConfigTests {
}
@Test
public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() {
Config config = new TestConfig("--appDataDir=/mydata/myapp");
public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = new Config("--appDataDir=" + appDataDir);
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
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)));
}
@Test
public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() {
Config config = new TestConfig("--userDataDir=/mydata");
public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
File userDataDir = Files.createTempDirectory("myuserdata").toFile();
Config config = new Config("--userDataDir=" + userDataDir);
assertThat(config.getAppName(), equalTo(config.getDefaultAppName()));
assertThat(config.getUserDataDir(), equalTo(new File("/mydata")));
assertThat(config.getAppDataDir(), equalTo(new File("/mydata", config.getDefaultAppName())));
assertThat(config.getUserDataDir(), equalTo(userDataDir));
assertThat(config.getAppDataDir(), equalTo(new File(userDataDir, config.getDefaultAppName())));
assertThat(config.getConfigFile(), equalTo(new File(config.getAppDataDir(), DEFAULT_CONFIG_FILE_NAME)));
}
@Test
public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() {
Config config = new TestConfig("--appName=My-Bisq", "--appDataDir=/mydata/myapp");
public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() throws IOException {
File appDataDir = Files.createTempDirectory("myapp").toFile();
Config config = new Config("--appName=My-Bisq", "--appDataDir=" + appDataDir);
assertThat(config.getAppName(), equalTo("My-Bisq"));
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)));
}
@Test
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)));
}
@ -108,39 +107,39 @@ public class ConfigTests {
try (PrintWriter writer = new PrintWriter(configFile)) {
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"));
}
@Test
public void whenUnrecognizedOptionIsSet_thenThrowConfigException() {
public void whenUnrecognizedOptionIsSet_thenConfigExceptionIsThrown() {
exceptionRule.expect(ConfigException.class);
exceptionRule.expectMessage("problem parsing option 'bogus': bogus is not a recognized option");
new TestConfig("--bogus");
new Config("--bogus");
}
@Test
public void whenOptionFileArgumentDoesNotExist_thenThrowConfigException() {
public void whenOptionFileArgumentDoesNotExist_thenConfigExceptionIsThrown() {
exceptionRule.expect(ConfigException.class);
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
public void whenConfigFileOptionIsSetInConfigFile_thenDisallowedOptionExceptionisThrown() throws IOException {
public void whenConfigFileOptionIsSetInConfigFile_thenConfigExceptionIsThrown() throws IOException {
File configFile = File.createTempFile("bisq", "properties");
try (PrintWriter writer = new PrintWriter(configFile)) {
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");
new TestConfig("--configFile=" + configFile.getAbsolutePath());
new Config("--configFile=" + configFile.getAbsolutePath());
}
@Test
public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException {
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));
}
@ -150,7 +149,7 @@ public class ConfigTests {
try (PrintWriter writer = new PrintWriter(configFile)) {
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.getUserDataDir(), equalTo(config.getDefaultUserDataDir()));
assertThat(config.getAppDataDir(), equalTo(new File(config.getUserDataDir(), config.getAppName())));
@ -159,14 +158,14 @@ public class ConfigTests {
@Test
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"));
}
@Test
public void whenHelpOptionIsSet_thenHelpRequestedIsThrown() {
new TestConfig("--help");
fail();
public void whenHelpOptionIsSet_thenIsHelpRequestedIsTrue() {
assertFalse(new Config().isHelpRequested());
assertTrue(new Config("--help").isHelpRequested());
}
@Test

View File

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

View File

@ -20,7 +20,6 @@ package bisq.core.network.p2p.seed;
import bisq.network.p2p.NodeAddress;
import bisq.common.config.Config;
import bisq.common.config.TestConfig;
import org.junit.Assert;
import org.junit.Test;
@ -31,7 +30,7 @@ public class DefaultSeedNodeRepositoryTest {
@Test
public void getSeedNodes() {
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new TestConfig());
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new Config());
Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty());
}
@ -39,8 +38,8 @@ public class DefaultSeedNodeRepositoryTest {
public void manualSeedNodes() {
String seed1 = "asdf:8001";
String seed2 = "fdsa:6001";
String seedNodes = format("--%s=%s,%s", Config.SEED_NODES, seed1, seed2);
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new TestConfig(seedNodes));
String seedNodesOption= format("--%s=%s,%s", Config.SEED_NODES, seed1, seed2);
DefaultSeedNodeRepository DUT = new DefaultSeedNodeRepository(new Config(seedNodesOption));
Assert.assertFalse(DUT.getSeedNodeAddresses().isEmpty());
Assert.assertEquals(2, DUT.getSeedNodeAddresses().size());
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.common.config.Config;
import bisq.common.config.TestConfig;
import bisq.common.storage.Storage;
import javafx.collections.ObservableList;
@ -60,7 +59,7 @@ public class PreferencesTest {
storage = mock(Storage.class);
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);
}

View File

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

View File

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