mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-08-05 21:24:19 -04:00
Bisq
This commit is contained in:
commit
8a38081c04
2800 changed files with 344130 additions and 0 deletions
153
common/src/test/java/bisq/common/app/CapabilitiesTest.java
Normal file
153
common/src/test/java/bisq/common/app/CapabilitiesTest.java
Normal file
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.common.app;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static bisq.common.app.Capability.DAO_FULL_NODE;
|
||||
import static bisq.common.app.Capability.SEED_NODE;
|
||||
import static bisq.common.app.Capability.TRADE_STATISTICS;
|
||||
import static bisq.common.app.Capability.TRADE_STATISTICS_2;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class CapabilitiesTest {
|
||||
|
||||
@Test
|
||||
public void testNoCapabilitiesAvailable() {
|
||||
Capabilities DUT = new Capabilities();
|
||||
|
||||
assertTrue(DUT.containsAll(new HashSet<>()));
|
||||
assertFalse(DUT.containsAll(new Capabilities(SEED_NODE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHasLess() {
|
||||
assertTrue(new Capabilities().hasLess(new Capabilities(SEED_NODE)));
|
||||
assertFalse(new Capabilities().hasLess(new Capabilities()));
|
||||
assertFalse(new Capabilities(SEED_NODE).hasLess(new Capabilities()));
|
||||
assertTrue(new Capabilities(SEED_NODE).hasLess(new Capabilities(DAO_FULL_NODE)));
|
||||
assertFalse(new Capabilities(DAO_FULL_NODE).hasLess(new Capabilities(SEED_NODE)));
|
||||
|
||||
Capabilities all = new Capabilities(
|
||||
TRADE_STATISTICS,
|
||||
TRADE_STATISTICS_2,
|
||||
Capability.ACCOUNT_AGE_WITNESS,
|
||||
Capability.ACK_MSG,
|
||||
Capability.PROPOSAL,
|
||||
Capability.BLIND_VOTE,
|
||||
Capability.DAO_STATE,
|
||||
Capability.BUNDLE_OF_ENVELOPES,
|
||||
Capability.MEDIATION,
|
||||
Capability.SIGNED_ACCOUNT_AGE_WITNESS,
|
||||
Capability.REFUND_AGENT,
|
||||
Capability.TRADE_STATISTICS_HASH_UPDATE
|
||||
);
|
||||
Capabilities other = new Capabilities(
|
||||
TRADE_STATISTICS,
|
||||
TRADE_STATISTICS_2,
|
||||
Capability.ACCOUNT_AGE_WITNESS,
|
||||
Capability.ACK_MSG,
|
||||
Capability.PROPOSAL,
|
||||
Capability.BLIND_VOTE,
|
||||
Capability.DAO_STATE,
|
||||
Capability.BUNDLE_OF_ENVELOPES,
|
||||
Capability.MEDIATION,
|
||||
Capability.SIGNED_ACCOUNT_AGE_WITNESS,
|
||||
Capability.REFUND_AGENT,
|
||||
Capability.TRADE_STATISTICS_HASH_UPDATE,
|
||||
Capability.NO_ADDRESS_PRE_FIX
|
||||
);
|
||||
|
||||
assertTrue(all.hasLess(other));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testO() {
|
||||
Capabilities DUT = new Capabilities(TRADE_STATISTICS);
|
||||
|
||||
assertTrue(DUT.containsAll(new HashSet<>()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleMatch() {
|
||||
Capabilities DUT = new Capabilities(TRADE_STATISTICS);
|
||||
|
||||
// single match
|
||||
assertTrue(DUT.containsAll(new Capabilities(TRADE_STATISTICS)));
|
||||
assertFalse(DUT.containsAll(new Capabilities(SEED_NODE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiMatch() {
|
||||
Capabilities DUT = new Capabilities(TRADE_STATISTICS, TRADE_STATISTICS_2);
|
||||
|
||||
assertTrue(DUT.containsAll(new Capabilities(TRADE_STATISTICS)));
|
||||
assertFalse(DUT.containsAll(new Capabilities(SEED_NODE)));
|
||||
assertTrue(DUT.containsAll(new Capabilities(TRADE_STATISTICS, TRADE_STATISTICS_2)));
|
||||
assertFalse(DUT.containsAll(new Capabilities(SEED_NODE, TRADE_STATISTICS_2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToIntList() {
|
||||
assertEquals(Collections.emptyList(), Capabilities.toIntList(new Capabilities()));
|
||||
assertEquals(Collections.singletonList(12), Capabilities.toIntList(new Capabilities(Capability.MEDIATION)));
|
||||
assertEquals(Arrays.asList(6, 12), Capabilities.toIntList(new Capabilities(Capability.MEDIATION, Capability.BLIND_VOTE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromIntList() {
|
||||
assertEquals(new Capabilities(), Capabilities.fromIntList(Collections.emptyList()));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromIntList(Collections.singletonList(12)));
|
||||
assertEquals(new Capabilities(Capability.BLIND_VOTE, Capability.MEDIATION), Capabilities.fromIntList(Arrays.asList(6, 12)));
|
||||
|
||||
assertEquals(new Capabilities(), Capabilities.fromIntList(Collections.singletonList(-1)));
|
||||
assertEquals(new Capabilities(), Capabilities.fromIntList(Collections.singletonList(99)));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromIntList(Arrays.asList(-6, 12)));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromIntList(Arrays.asList(12, 99)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToStringList() {
|
||||
assertEquals("", new Capabilities().toStringList());
|
||||
assertEquals("12", new Capabilities(Capability.MEDIATION).toStringList());
|
||||
assertEquals("6, 12", new Capabilities(Capability.BLIND_VOTE, Capability.MEDIATION).toStringList());
|
||||
// capabilities gets sorted, independent of our order
|
||||
assertEquals("6, 12", new Capabilities(Capability.MEDIATION, Capability.BLIND_VOTE).toStringList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromStringList() {
|
||||
assertEquals(new Capabilities(), Capabilities.fromStringList(null));
|
||||
assertEquals(new Capabilities(), Capabilities.fromStringList(""));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromStringList("12"));
|
||||
assertEquals(new Capabilities(Capability.BLIND_VOTE, Capability.MEDIATION), Capabilities.fromStringList("6,12"));
|
||||
assertEquals(new Capabilities(Capability.BLIND_VOTE, Capability.MEDIATION), Capabilities.fromStringList("12, 6"));
|
||||
assertEquals(new Capabilities(), Capabilities.fromStringList("a"));
|
||||
assertEquals(new Capabilities(), Capabilities.fromStringList("99"));
|
||||
assertEquals(new Capabilities(), Capabilities.fromStringList("-1"));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromStringList("12, a"));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromStringList("12, 99"));
|
||||
assertEquals(new Capabilities(Capability.MEDIATION), Capabilities.fromStringList("a,12, 99"));
|
||||
}
|
||||
}
|
53
common/src/test/java/bisq/common/app/VersionTest.java
Normal file
53
common/src/test/java/bisq/common/app/VersionTest.java
Normal file
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.common.app;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class VersionTest {
|
||||
|
||||
@Test
|
||||
public void testVersionNumber() {
|
||||
assertEquals(0, Version.getMajorVersion("0.0.0"));
|
||||
assertEquals(1, Version.getMajorVersion("1.0.0"));
|
||||
|
||||
assertEquals(0, Version.getMinorVersion("0.0.0"));
|
||||
assertEquals(5, Version.getMinorVersion("0.5.0"));
|
||||
|
||||
assertEquals(0, Version.getPatchVersion("0.0.0"));
|
||||
assertEquals(5, Version.getPatchVersion("0.0.5"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNewVersion() {
|
||||
assertFalse(Version.isNewVersion("0.0.0", "0.0.0"));
|
||||
assertTrue(Version.isNewVersion("0.1.0", "0.0.0"));
|
||||
assertTrue(Version.isNewVersion("0.0.1", "0.0.0"));
|
||||
assertTrue(Version.isNewVersion("1.0.0", "0.0.0"));
|
||||
assertTrue(Version.isNewVersion("0.5.1", "0.5.0"));
|
||||
assertFalse(Version.isNewVersion("0.5.0", "0.5.1"));
|
||||
assertTrue(Version.isNewVersion("0.6.0", "0.5.0"));
|
||||
assertTrue(Version.isNewVersion("0.6.0", "0.5.1"));
|
||||
assertFalse(Version.isNewVersion("0.5.0", "1.5.0"));
|
||||
assertFalse(Version.isNewVersion("0.4.9", "0.5.0"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
package bisq.common.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConfigFileEditorTests {
|
||||
|
||||
private File file;
|
||||
private PrintWriter writer;
|
||||
private ConfigFileReader reader;
|
||||
private ConfigFileEditor editor;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
file = File.createTempFile("bisq", "properties");
|
||||
reader = new ConfigFileReader(file);
|
||||
editor = new ConfigFileEditor(file);
|
||||
writer = new PrintWriter(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileDoesNotExist_thenSetOptionCreatesItAndAppendsOneLine() {
|
||||
writer.close();
|
||||
assertTrue(file.delete());
|
||||
|
||||
editor.setOption("opt1", "val1");
|
||||
|
||||
assertThat(reader.getLines(), contains("opt1=val1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileContainsOptionBeingSet_thenSetOptionOverwritesIt() {
|
||||
writer.println("opt1=val1");
|
||||
writer.println("opt2=val2");
|
||||
writer.println("opt3=val3");
|
||||
writer.flush();
|
||||
|
||||
editor.setOption("opt2", "newval2");
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"opt1=val1",
|
||||
"opt2=newval2",
|
||||
"opt3=val3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionBeingSetHasNoArg_thenSetOptionWritesItWithNoEqualsSign() {
|
||||
writer.println("opt1=val1");
|
||||
writer.println("opt2=val2");
|
||||
writer.flush();
|
||||
|
||||
editor.setOption("opt3");
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"opt1=val1",
|
||||
"opt2=val2",
|
||||
"opt3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileHasBlankOrCommentLines_thenTheyArePreserved() {
|
||||
writer.println("# Comment 1");
|
||||
writer.println("opt1=val1");
|
||||
writer.println();
|
||||
writer.println("# Comment 2");
|
||||
writer.println("opt2=val2");
|
||||
writer.flush();
|
||||
|
||||
editor.setOption("opt3=val3");
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"# Comment 1",
|
||||
"opt1=val1",
|
||||
"",
|
||||
"# Comment 2",
|
||||
"opt2=val2",
|
||||
"opt3=val3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileContainsOptionBeingCleared_thenClearOptionRemovesIt() {
|
||||
writer.println("opt1=val1");
|
||||
writer.println("opt2=val2");
|
||||
writer.flush();
|
||||
|
||||
editor.clearOption("opt2");
|
||||
|
||||
assertThat(reader.getLines(), contains("opt1=val1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileDoesNotContainOptionBeingCleared_thenClearOptionIsNoOp() {
|
||||
writer.println("opt1=val1");
|
||||
writer.println("opt2=val2");
|
||||
writer.flush();
|
||||
|
||||
editor.clearOption("opt3");
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"opt1=val1",
|
||||
"opt2=val2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileDoesNotExist_thenClearOptionIsNoOp() {
|
||||
writer.close();
|
||||
assertTrue(file.delete());
|
||||
editor.clearOption("opt1");
|
||||
assertFalse(file.exists());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package bisq.common.config;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class ConfigFileOptionTests {
|
||||
|
||||
@Test
|
||||
public void whenOptionHasWhitespaceAroundEqualsSign_thenItGetsTrimmed() {
|
||||
String value = "name1 = arg1";
|
||||
ConfigFileOption option = ConfigFileOption.parse(value);
|
||||
assertThat(option.name, equalTo("name1"));
|
||||
assertThat(option.arg, equalTo("arg1"));
|
||||
assertThat(option.toString(), equalTo("name1=arg1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionHasLeadingOrTrailingWhitespace_thenItGetsTrimmed() {
|
||||
String value = " name1=arg1 ";
|
||||
ConfigFileOption option = ConfigFileOption.parse(value);
|
||||
assertThat(option.name, equalTo("name1"));
|
||||
assertThat(option.arg, equalTo("arg1"));
|
||||
assertThat(option.toString(), equalTo("name1=arg1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionHasEscapedColons_thenTheyGetUnescaped() {
|
||||
String value = "host1=example.com\\:8080";
|
||||
ConfigFileOption option = ConfigFileOption.parse(value);
|
||||
assertThat(option.name, equalTo("host1"));
|
||||
assertThat(option.arg, equalTo("example.com:8080"));
|
||||
assertThat(option.toString(), equalTo("host1=example.com:8080"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionHasNoValue_thenItSetsEmptyValue() {
|
||||
String value = "host1=";
|
||||
ConfigFileOption option = ConfigFileOption.parse(value);
|
||||
assertThat(option.name, equalTo("host1"));
|
||||
assertThat(option.arg, equalTo(""));
|
||||
assertThat(option.toString(), equalTo("host1="));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package bisq.common.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ConfigFileReaderTests {
|
||||
|
||||
private File file;
|
||||
private PrintWriter writer;
|
||||
private ConfigFileReader reader;
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
file = File.createTempFile("bisq", "properties");
|
||||
reader = new ConfigFileReader(file);
|
||||
writer = new PrintWriter(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileDoesNotExist_thenGetLinesThrows() {
|
||||
writer.close();
|
||||
assertTrue(file.delete());
|
||||
|
||||
exception.expect(ConfigException.class);
|
||||
exception.expectMessage(containsString("Config file"));
|
||||
exception.expectMessage(containsString("does not exist"));
|
||||
|
||||
reader.getLines();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionHasWhitespaceAroundEqualsSign_thenGetLinesPreservesIt() {
|
||||
writer.println("name1 =arg1");
|
||||
writer.println("name2= arg2");
|
||||
writer.println("name3 = arg3");
|
||||
writer.flush();
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"name1 =arg1",
|
||||
"name2= arg2",
|
||||
"name3 = arg3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionHasEscapedColons_thenTheyGetUnescaped() {
|
||||
writer.println("host1=example.com\\:8080");
|
||||
writer.println("host2=example.org:8080");
|
||||
writer.flush();
|
||||
|
||||
assertThat(reader.getLines(), contains(
|
||||
"host1=example.com:8080",
|
||||
"host2=example.org:8080"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFileContainsNonOptionLines_getOptionLinesReturnsOnlyOptionLines() {
|
||||
writer.println("# Comment");
|
||||
writer.println("");
|
||||
writer.println("name1=arg1");
|
||||
writer.println("noArgOpt");
|
||||
writer.flush();
|
||||
|
||||
assertThat(reader.getOptionLines(), contains("name1=arg1", "noArgOpt"));
|
||||
}
|
||||
}
|
294
common/src/test/java/bisq/common/config/ConfigTests.java
Normal file
294
common/src/test/java/bisq/common/config/ConfigTests.java
Normal file
|
@ -0,0 +1,294 @@
|
|||
package bisq.common.config;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static bisq.common.config.Config.*;
|
||||
import static java.lang.String.format;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
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;
|
||||
|
||||
public class ConfigTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exceptionRule = ExpectedException.none();
|
||||
|
||||
// Note: "DataDirProperties" in the test method names below represent the group of
|
||||
// configuration options that influence the location of a Bisq node's data directory.
|
||||
// These options include appName, userDataDir, appDataDir and configFile
|
||||
|
||||
@Test
|
||||
public void whenNoArgCtorIsCalled_thenDefaultAppNameIsSetToTempValue() {
|
||||
Config config = new Config();
|
||||
String defaultAppName = config.defaultAppName;
|
||||
String regex = "Bisq\\d{2,}Temp";
|
||||
assertTrue(format("Temp app name '%s' failed to match '%s'", defaultAppName, regex),
|
||||
defaultAppName.matches(regex));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppNameOptionIsSet_thenAppNamePropertyDiffersFromDefaultAppNameProperty() {
|
||||
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"));
|
||||
assertThat(config.appName, equalTo("My-Bisq"));
|
||||
assertThat(config.appName, not(equalTo(config.defaultAppName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoOptionsAreSet_thenDataDirPropertiesEqualDefaultValues() {
|
||||
Config config = new Config();
|
||||
assertThat(config.appName, equalTo(config.defaultAppName));
|
||||
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
|
||||
assertThat(config.appDataDir, equalTo(config.defaultAppDataDir));
|
||||
assertThat(config.configFile, equalTo(config.defaultConfigFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppNameOptionIsSet_thenDataDirPropertiesReflectItsValue() {
|
||||
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"));
|
||||
assertThat(config.appName, equalTo("My-Bisq"));
|
||||
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
|
||||
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, "My-Bisq")));
|
||||
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
|
||||
File appDataDir = Files.createTempDirectory("myapp").toFile();
|
||||
Config config = configWithOpts(opt(APP_DATA_DIR, appDataDir));
|
||||
assertThat(config.appName, equalTo(config.defaultAppName));
|
||||
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
|
||||
assertThat(config.appDataDir, equalTo(appDataDir));
|
||||
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUserDataDirOptionIsSet_thenDataDirPropertiesReflectItsValue() throws IOException {
|
||||
File userDataDir = Files.createTempDirectory("myuserdata").toFile();
|
||||
Config config = configWithOpts(opt(USER_DATA_DIR, userDataDir));
|
||||
assertThat(config.appName, equalTo(config.defaultAppName));
|
||||
assertThat(config.userDataDir, equalTo(userDataDir));
|
||||
assertThat(config.appDataDir, equalTo(new File(userDataDir, config.defaultAppName)));
|
||||
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppNameAndAppDataDirOptionsAreSet_thenDataDirPropertiesReflectTheirValues() throws IOException {
|
||||
File appDataDir = Files.createTempDirectory("myapp").toFile();
|
||||
Config config = configWithOpts(opt(APP_NAME, "My-Bisq"), opt(APP_DATA_DIR, appDataDir));
|
||||
assertThat(config.appName, equalTo("My-Bisq"));
|
||||
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
|
||||
assertThat(config.appDataDir, equalTo(appDataDir));
|
||||
assertThat(config.configFile, equalTo(new File(config.appDataDir, DEFAULT_CONFIG_FILE_NAME)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionIsSetAtCommandLineAndInConfigFile_thenCommandLineValueTakesPrecedence() throws IOException {
|
||||
File configFile = File.createTempFile("bisq", "properties");
|
||||
try (PrintWriter writer = new PrintWriter(configFile)) {
|
||||
writer.println(new ConfigFileOption(APP_NAME, "Bisq-configFileValue"));
|
||||
}
|
||||
Config config = configWithOpts(opt(APP_NAME, "Bisq-commandLineValue"));
|
||||
assertThat(config.appName, equalTo("Bisq-commandLineValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUnrecognizedOptionIsSet_thenConfigExceptionIsThrown() {
|
||||
exceptionRule.expect(ConfigException.class);
|
||||
exceptionRule.expectMessage("problem parsing option 'bogus': bogus is not a recognized option");
|
||||
configWithOpts(opt("bogus"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUnrecognizedOptionIsSetInConfigFile_thenNoExceptionIsThrown() throws IOException {
|
||||
File configFile = File.createTempFile("bisq", "properties");
|
||||
try (PrintWriter writer = new PrintWriter(configFile)) {
|
||||
writer.println(new ConfigFileOption("bogusOption", "bogusValue"));
|
||||
writer.println(new ConfigFileOption(APP_NAME, "BisqTest"));
|
||||
}
|
||||
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
|
||||
assertThat(config.appName, equalTo("BisqTest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionFileArgumentDoesNotExist_thenConfigExceptionIsThrown() {
|
||||
String filepath = "/does/not/exist";
|
||||
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||
filepath = "C:\\does\\not\\exist";
|
||||
}
|
||||
exceptionRule.expect(ConfigException.class);
|
||||
exceptionRule.expectMessage(format("problem parsing option 'torrcFile': File [%s] does not exist", filepath));
|
||||
configWithOpts(opt(TORRC_FILE, filepath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigFileOptionIsSetToNonExistentFile_thenConfigExceptionIsThrown() {
|
||||
String filepath = "/no/such/bisq.properties";
|
||||
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||
filepath = "C:\\no\\such\\bisq.properties";
|
||||
}
|
||||
exceptionRule.expect(ConfigException.class);
|
||||
exceptionRule.expectMessage(format("The specified config file '%s' does not exist", filepath));
|
||||
configWithOpts(opt(CONFIG_FILE, filepath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigFileOptionIsSetInConfigFile_thenConfigExceptionIsThrown() throws IOException {
|
||||
File configFile = File.createTempFile("bisq", "properties");
|
||||
try (PrintWriter writer = new PrintWriter(configFile)) {
|
||||
writer.println(new ConfigFileOption(CONFIG_FILE, "/tmp/other.bisq.properties"));
|
||||
}
|
||||
exceptionRule.expect(ConfigException.class);
|
||||
exceptionRule.expectMessage(format("The '%s' option is disallowed in config files", CONFIG_FILE));
|
||||
configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigFileOptionIsSetToExistingFile_thenConfigFilePropertyReflectsItsValue() throws IOException {
|
||||
File configFile = File.createTempFile("bisq", "properties");
|
||||
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
|
||||
assertThat(config.configFile, equalTo(configFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigFileOptionIsSetToRelativePath_thenThePathIsPrefixedByAppDataDir() throws IOException {
|
||||
File configFile = Files.createTempFile("my-bisq", ".properties").toFile();
|
||||
File appDataDir = configFile.getParentFile();
|
||||
String relativeConfigFilePath = configFile.getName();
|
||||
Config config = configWithOpts(opt(APP_DATA_DIR, appDataDir), opt(CONFIG_FILE, relativeConfigFilePath));
|
||||
assertThat(config.configFile, equalTo(configFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppNameIsSetInConfigFile_thenDataDirPropertiesReflectItsValue() throws IOException {
|
||||
File configFile = File.createTempFile("bisq", "properties");
|
||||
try (PrintWriter writer = new PrintWriter(configFile)) {
|
||||
writer.println(new ConfigFileOption(APP_NAME, "My-Bisq"));
|
||||
}
|
||||
Config config = configWithOpts(opt(CONFIG_FILE, configFile.getAbsolutePath()));
|
||||
assertThat(config.appName, equalTo("My-Bisq"));
|
||||
assertThat(config.userDataDir, equalTo(config.defaultUserDataDir));
|
||||
assertThat(config.appDataDir, equalTo(new File(config.userDataDir, config.appName)));
|
||||
assertThat(config.configFile, equalTo(configFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBannedBtcNodesOptionIsSet_thenBannedBtcNodesPropertyReturnsItsValue() {
|
||||
Config config = configWithOpts(opt(BANNED_BTC_NODES, "foo.onion:8333,bar.onion:8333"));
|
||||
assertThat(config.bannedBtcNodes, contains("foo.onion:8333", "bar.onion:8333"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenHelpOptionIsSet_thenIsHelpRequestedIsTrue() {
|
||||
assertFalse(new Config().helpRequested);
|
||||
assertTrue(configWithOpts(opt(HELP)).helpRequested);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigIsConstructed_thenNoConsoleOutputSideEffectsShouldOccur() {
|
||||
PrintStream outOrig = System.out;
|
||||
PrintStream errOrig = System.err;
|
||||
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
|
||||
ByteArrayOutputStream errBytes = new ByteArrayOutputStream();
|
||||
try (PrintStream outTest = new PrintStream(outBytes);
|
||||
PrintStream errTest = new PrintStream(errBytes)) {
|
||||
System.setOut(outTest);
|
||||
System.setErr(errTest);
|
||||
new Config();
|
||||
assertThat(outBytes.toString(), isEmptyString());
|
||||
assertThat(errBytes.toString(), isEmptyString());
|
||||
} finally {
|
||||
System.setOut(outOrig);
|
||||
System.setErr(errOrig);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConfigIsConstructed_thenAppDataDirAndSubdirsAreCreated() {
|
||||
Config config = new Config();
|
||||
assertTrue(config.appDataDir.exists());
|
||||
assertTrue(config.keyStorageDir.exists());
|
||||
assertTrue(config.storageDir.exists());
|
||||
assertTrue(config.torDir.exists());
|
||||
assertTrue(config.walletDir.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppDataDirCannotBeCreated_thenUncheckedIoExceptionIsThrown() throws IOException {
|
||||
// set a userDataDir that is actually a file so appDataDir cannot be created
|
||||
File aFile = Files.createTempFile("A", "File").toFile();
|
||||
exceptionRule.expect(UncheckedIOException.class);
|
||||
exceptionRule.expectMessage(containsString("Application data directory"));
|
||||
exceptionRule.expectMessage(containsString("could not be created"));
|
||||
configWithOpts(opt(USER_DATA_DIR, aFile));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAppDataDirIsSymbolicLink_thenAppDataDirCreationIsNoOp() throws IOException {
|
||||
Path parentDir = Files.createTempDirectory("parent");
|
||||
Path targetDir = parentDir.resolve("target");
|
||||
Path symlink = parentDir.resolve("symlink");
|
||||
Files.createDirectory(targetDir);
|
||||
try {
|
||||
Files.createSymbolicLink(symlink, targetDir);
|
||||
} catch (Throwable ex) {
|
||||
// An error occurred trying to create a symbolic link, likely because the
|
||||
// operating system (e.g. Windows) does not support it, so we abort the test.
|
||||
return;
|
||||
}
|
||||
configWithOpts(opt(APP_DATA_DIR, symlink));
|
||||
}
|
||||
|
||||
|
||||
// == TEST SUPPORT FACILITIES ========================================================
|
||||
|
||||
static Config configWithOpts(Opt... opts) {
|
||||
String[] args = new String[opts.length];
|
||||
for (int i = 0; i < opts.length; i++)
|
||||
args[i] = opts[i].toString();
|
||||
return new Config(args);
|
||||
}
|
||||
|
||||
static Opt opt(String name) {
|
||||
return new Opt(name);
|
||||
}
|
||||
|
||||
static Opt opt(String name, Object arg) {
|
||||
return new Opt(name, arg.toString());
|
||||
}
|
||||
|
||||
static class Opt {
|
||||
private final String name;
|
||||
private final String arg;
|
||||
|
||||
public Opt(String name) {
|
||||
this(name, null);
|
||||
}
|
||||
|
||||
public Opt(String name, String arg) {
|
||||
this.name = name;
|
||||
this.arg = arg;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return format("--%s%s", name, arg != null ? ("=" + arg) : "");
|
||||
}
|
||||
}
|
||||
}
|
71
common/src/test/java/bisq/common/util/MathUtilsTest.java
Normal file
71
common/src/test/java/bisq/common/util/MathUtilsTest.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* Bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.common.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class MathUtilsTest {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testRoundDoubleWithInfiniteArg() {
|
||||
MathUtils.roundDouble(Double.POSITIVE_INFINITY, 2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testRoundDoubleWithNaNArg() {
|
||||
MathUtils.roundDouble(Double.NaN, 2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testRoundDoubleWithNegativePrecision() {
|
||||
MathUtils.roundDouble(3, -1);
|
||||
}
|
||||
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
||||
@Test
|
||||
public void testMovingAverageWithoutOutlierExclusion() {
|
||||
var values = new int[]{4, 5, 3, 1, 2, 4};
|
||||
// Moving average = 4, 4.5, 4, 3, 2, 7/3
|
||||
var movingAverage = new MathUtils.MovingAverage(3, 0);
|
||||
int i = 0;
|
||||
assertEquals(4, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals(4.5, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals(4, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals(3, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals(2, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals((double) 7 / 3, movingAverage.next(values[i]).get(),0.001);
|
||||
}
|
||||
|
||||
@SuppressWarnings("OptionalGetWithoutIsPresent")
|
||||
@Test
|
||||
public void testMovingAverageWithOutlierExclusion() {
|
||||
var values = new int[]{100, 102, 95, 101, 120, 115};
|
||||
// Moving average = N/A, N/A, 99, 99.333..., N/A, 103.666...
|
||||
var movingAverage = new MathUtils.MovingAverage(3, 0.2);
|
||||
int i = 0;
|
||||
assertFalse(movingAverage.next(values[i++]).isPresent());
|
||||
assertFalse(movingAverage.next(values[i++]).isPresent());
|
||||
assertEquals(99, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertEquals(99.333, movingAverage.next(values[i++]).get(),0.001);
|
||||
assertFalse(movingAverage.next(values[i++]).isPresent());
|
||||
assertEquals(103.666, movingAverage.next(values[i]).get(),0.001);
|
||||
}
|
||||
}
|
478
common/src/test/java/bisq/common/util/PermutationTest.java
Normal file
478
common/src/test/java/bisq/common/util/PermutationTest.java
Normal file
|
@ -0,0 +1,478 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.common.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PermutationTest {
|
||||
|
||||
|
||||
// @Test
|
||||
public void testGetPartialList() {
|
||||
String blindVote0 = "blindVote0";
|
||||
String blindVote1 = "blindVote1";
|
||||
String blindVote2 = "blindVote2";
|
||||
String blindVote3 = "blindVote3";
|
||||
String blindVote4 = "blindVote4";
|
||||
String blindVote5 = "blindVote5";
|
||||
|
||||
List<String> list = new ArrayList<>(Arrays.asList(blindVote0, blindVote1, blindVote2, blindVote3, blindVote4, blindVote5));
|
||||
List<Integer> indicesToRemove = Arrays.asList(0, 3);
|
||||
List<String> expected = new ArrayList<>(Arrays.asList(blindVote1, blindVote2, blindVote4, blindVote5));
|
||||
List<String> result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// remove nothing
|
||||
indicesToRemove = new ArrayList<>();
|
||||
expected = new ArrayList<>(list);
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// remove first
|
||||
indicesToRemove = Collections.singletonList(0);
|
||||
expected = new ArrayList<>(list);
|
||||
expected.remove(0);
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// remove last
|
||||
indicesToRemove = Collections.singletonList(5);
|
||||
expected = new ArrayList<>(list);
|
||||
expected.remove(5);
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// remove all
|
||||
indicesToRemove = Arrays.asList(0, 1, 2, 3, 4, 5);
|
||||
expected = new ArrayList<>();
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// wrong sorting of indices
|
||||
indicesToRemove = Arrays.asList(4, 0, 1);
|
||||
expected = expected = new ArrayList<>(Arrays.asList(blindVote2, blindVote3, blindVote5));
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// wrong sorting of indices
|
||||
indicesToRemove = Arrays.asList(0, 0);
|
||||
expected = new ArrayList<>(Arrays.asList(blindVote1, blindVote2, blindVote3, blindVote4, blindVote5));
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// don't remove as invalid index
|
||||
indicesToRemove = Collections.singletonList(9);
|
||||
expected = new ArrayList<>(list);
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// don't remove as invalid index
|
||||
indicesToRemove = Collections.singletonList(-2);
|
||||
expected = new ArrayList<>(list);
|
||||
result = PermutationUtil.getPartialList(list, indicesToRemove);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindMatchingPermutation() {
|
||||
String a = "A";
|
||||
String b = "B";
|
||||
String c = "C";
|
||||
String d = "D";
|
||||
String e = "E";
|
||||
int limit = 1048575;
|
||||
List<String> result;
|
||||
List<String> list;
|
||||
List<String> expected;
|
||||
BiFunction<String, List<String>, Boolean> predicate = (target, variationList) -> variationList.toString().equals(target);
|
||||
|
||||
list = Arrays.asList(a, b, c, d, e);
|
||||
|
||||
expected = Arrays.asList(a);
|
||||
result = PermutationUtil.findMatchingPermutation(expected.toString(), list, predicate, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
|
||||
expected = Arrays.asList(a, c, e);
|
||||
result = PermutationUtil.findMatchingPermutation(expected.toString(), list, predicate, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBreakAtLimit() {
|
||||
BiFunction<String, List<String>, Boolean> predicate =
|
||||
(target, variationList) -> variationList.toString().equals(target);
|
||||
var list = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o");
|
||||
var expected = Arrays.asList("b", "g", "m");
|
||||
|
||||
// Takes around 32508 tries starting from longer strings
|
||||
var limit = 100000;
|
||||
var result = PermutationUtil.findMatchingPermutation(expected.toString(), list, predicate, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
limit = 1000;
|
||||
result = PermutationUtil.findMatchingPermutation(expected.toString(), list, predicate, limit);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
// @Test
|
||||
public void testFindAllPermutations() {
|
||||
String blindVote0 = "blindVote0";
|
||||
String blindVote1 = "blindVote1";
|
||||
String blindVote2 = "blindVote2";
|
||||
String blindVote3 = "blindVote3";
|
||||
String blindVote4 = "blindVote4";
|
||||
|
||||
// Up to about 1M iterations performance is acceptable (0.5 sec)
|
||||
// findAllPermutations took 580 ms for 20 items and 1048575 iterations
|
||||
// findAllPermutations took 10 ms for 15 items and 32767 iterations
|
||||
// findAllPermutations took 0 ms for 10 items and 1023 iterations
|
||||
// int limit = 1048575;
|
||||
int limit = 1048575000;
|
||||
List<String> list;
|
||||
List<List<String>> expected;
|
||||
List<List<String>> result;
|
||||
List<String> subList;
|
||||
|
||||
|
||||
list = new ArrayList<>();
|
||||
/* for (int i = 0; i < 4; i++) {
|
||||
list.add("blindVote" + i);
|
||||
}*/
|
||||
|
||||
PermutationUtil.findAllPermutations(list, limit);
|
||||
|
||||
|
||||
list = new ArrayList<>();
|
||||
expected = new ArrayList<>();
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
list = new ArrayList<>(Arrays.asList(blindVote0));
|
||||
expected = new ArrayList<>();
|
||||
expected.add(list);
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// 2 items -> 3 variations
|
||||
list = new ArrayList<>(Arrays.asList(blindVote0, blindVote1));
|
||||
expected = new ArrayList<>();
|
||||
expected.add(Arrays.asList(list.get(0)));
|
||||
|
||||
expected.add(Arrays.asList(list.get(1)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
expected.add(subList);
|
||||
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// 3 items -> 7 variations
|
||||
list = new ArrayList<>(Arrays.asList(blindVote0, blindVote1, blindVote2));
|
||||
expected = new ArrayList<>();
|
||||
expected.add(Arrays.asList(list.get(0)));
|
||||
|
||||
expected.add(Arrays.asList(list.get(1)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(2)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
// 4 items -> 15 variations
|
||||
list = new ArrayList<>(Arrays.asList(blindVote0, blindVote1, blindVote2, blindVote3));
|
||||
expected = new ArrayList<>();
|
||||
expected.add(Arrays.asList(list.get(0)));
|
||||
|
||||
expected.add(Arrays.asList(list.get(1)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(2)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(3)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
|
||||
// 5 items -> 31 variations
|
||||
list = new ArrayList<>(Arrays.asList(blindVote0, blindVote1, blindVote2, blindVote3, blindVote4));
|
||||
expected = new ArrayList<>();
|
||||
expected.add(Arrays.asList(list.get(0)));
|
||||
|
||||
expected.add(Arrays.asList(list.get(1)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(2)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(3)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
expected.add(subList);
|
||||
|
||||
expected.add(Arrays.asList(list.get(4)));
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
subList = new ArrayList<>();
|
||||
subList.add(list.get(0));
|
||||
subList.add(list.get(1));
|
||||
subList.add(list.get(2));
|
||||
subList.add(list.get(3));
|
||||
subList.add(list.get(4));
|
||||
expected.add(subList);
|
||||
|
||||
result = PermutationUtil.findAllPermutations(list, limit);
|
||||
assertTrue(expected.toString().equals(result.toString()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package bisq.common.util;
|
||||
|
||||
import java.nio.file.Files;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static bisq.common.util.Preconditions.checkDir;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
public class PreconditionsTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exceptionRule = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void whenDirIsValid_thenDirIsReturned() throws IOException {
|
||||
File dir = Files.createTempDirectory("TestDir").toFile();
|
||||
File ret = checkDir(dir);
|
||||
assertSame(dir, ret);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDirDoesNotExist_thenThrow() {
|
||||
String filepath = "/does/not/exist";
|
||||
if (System.getProperty("os.name").startsWith("Windows")) {
|
||||
filepath = "C:\\does\\not\\exist";
|
||||
}
|
||||
exceptionRule.expect(IllegalArgumentException.class);
|
||||
exceptionRule.expectMessage(equalTo(String.format("Directory '%s' does not exist", filepath)));
|
||||
checkDir(new File(filepath));
|
||||
}
|
||||
}
|
71
common/src/test/java/bisq/common/util/UtilitiesTest.java
Normal file
71
common/src/test/java/bisq/common/util/UtilitiesTest.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* This file is part of Bisq.
|
||||
*
|
||||
* bisq is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* bisq is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with bisq. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package bisq.common.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class UtilitiesTest {
|
||||
|
||||
@Test
|
||||
public void testToStringList() {
|
||||
assertTrue(Utilities.commaSeparatedListToSet(null, false).isEmpty());
|
||||
assertTrue(Utilities.commaSeparatedListToSet(null, true).isEmpty());
|
||||
assertTrue(Utilities.commaSeparatedListToSet("", false).isEmpty());
|
||||
assertTrue(Utilities.commaSeparatedListToSet("", true).isEmpty());
|
||||
assertTrue(Utilities.commaSeparatedListToSet(" ", false).isEmpty());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet(" ", true).size());
|
||||
assertTrue(Utilities.commaSeparatedListToSet(",", false).isEmpty());
|
||||
assertTrue(Utilities.commaSeparatedListToSet(",", true).isEmpty());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet(",test1", false).size());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet(", , test1", false).size());
|
||||
assertEquals(2, Utilities.commaSeparatedListToSet(", , test1", true).size());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet("test1,", false).size());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet("test1, ,", false).size());
|
||||
assertEquals(1, Utilities.commaSeparatedListToSet("test1", false).size());
|
||||
assertEquals(2, Utilities.commaSeparatedListToSet("test1, test2", false).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIntegerToByteArray() {
|
||||
assertEquals("0000", Utilities.bytesAsHexString(Utilities.integerToByteArray(0, 2)));
|
||||
assertEquals("ffff", Utilities.bytesAsHexString(Utilities.integerToByteArray(65535, 2)));
|
||||
assertEquals("0011", Utilities.bytesAsHexString(Utilities.integerToByteArray(17, 2)));
|
||||
assertEquals("1100", Utilities.bytesAsHexString(Utilities.integerToByteArray(4352, 2)));
|
||||
assertEquals("dd22", Utilities.bytesAsHexString(Utilities.integerToByteArray(56610, 2)));
|
||||
assertEquals("7fffffff", Utilities.bytesAsHexString(Utilities.integerToByteArray(2147483647, 4))); // Integer.MAX_VALUE
|
||||
assertEquals("80000000", Utilities.bytesAsHexString(Utilities.integerToByteArray(-2147483648, 4))); // Integer.MIN_VALUE
|
||||
assertEquals("00110011", Utilities.bytesAsHexString(Utilities.integerToByteArray(1114129, 4)));
|
||||
assertEquals("ffeeffef", Utilities.bytesAsHexString(Utilities.integerToByteArray(-1114129, 4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testByteArrayToInteger() {
|
||||
assertEquals(0, Utilities.byteArrayToInteger(Utilities.decodeFromHex("0000")));
|
||||
assertEquals(65535, Utilities.byteArrayToInteger(Utilities.decodeFromHex("ffff")));
|
||||
assertEquals(4352, Utilities.byteArrayToInteger(Utilities.decodeFromHex("1100")));
|
||||
assertEquals(17, Utilities.byteArrayToInteger(Utilities.decodeFromHex("0011")));
|
||||
assertEquals(56610, Utilities.byteArrayToInteger(Utilities.decodeFromHex("dd22")));
|
||||
assertEquals(2147483647, Utilities.byteArrayToInteger(Utilities.decodeFromHex("7fffffff")));
|
||||
assertEquals(-2147483648, Utilities.byteArrayToInteger(Utilities.decodeFromHex("80000000")));
|
||||
assertEquals(1114129, Utilities.byteArrayToInteger(Utilities.decodeFromHex("00110011")));
|
||||
assertEquals(-1114129, Utilities.byteArrayToInteger(Utilities.decodeFromHex("ffeeffef")));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue