Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Patching CLI tests to prevent littering daemon.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Sep 13, 2014
1 parent e7c41bb commit 54781d5
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void testCliInitAndConnect() throws Exception {
"--plugin", "local",
"--plugin-option", "path=" + clientA.get("repopath"),
"--no-encryption",
"--no-compression"
"--no-compression",
"--no-daemon"
};

logger.log(Level.INFO, "Running syncany with argument: " + StringUtil.join(initArgs, " "));
Expand All @@ -66,6 +67,7 @@ public void testCliInitAndConnect() throws Exception {
"--localdir", clientB.get("localdir"),
"--plugin", "local",
"--plugin-option", "path=" + clientB.get("repopath"),
"--no-daemon",
};

logger.log(Level.INFO, "Running syncany with argument: " + StringUtil.join(connectArgs, " "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public void testCliConnectInteractiveWithParamsEnc() throws Exception {
TestCliUtil.setCurrentDirectory(localDirB);

String[] connectArgs = new String[] {
"connect"
"connect",
"--no-daemon"
};

System.out.println("repopath = " + clientB.get("repopath"));
Expand Down Expand Up @@ -132,6 +133,7 @@ public void testCliConnectInteractiveWithLinkEnc() throws Exception {

String[] connectArgs = new String[] {
"connect",
"--no-daemon",
initializedRepoConnectLink
};

Expand Down Expand Up @@ -187,7 +189,8 @@ private void initInteractiveWithEncryptionWithParams() throws Exception {
TestCliUtil.setCurrentDirectory(tempLocalDirA);

String[] initArgs = new String[] {
"init"
"init",
"--no-daemon"
};

systemInMock.provideText(StringUtil.join(new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
import org.syncany.tests.util.TestConfigUtil;
import org.syncany.tests.util.TestFileUtil;

public class GenlinkCommandTest {
public class GenlinkCommandTest {
@Rule
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();

// TODO [low] TextFromStandardInputStream is not thread-safe. This leads to failures from time to time.

@Test
Expand All @@ -42,72 +43,72 @@ public void testGenlinkCommandShortNotEncrypted() throws Exception {
Map<String, String> connectionSettings = TestConfigUtil.createTestLocalConnectionSettings();
Map<String, String> clientA = TestCliUtil.createLocalTestEnvAndInit("A", connectionSettings);
File clientLocalDirB = TestFileUtil.createTempDirectoryInSystemTemp();

String[] cliOutA = TestCliUtil.runAndCaptureOutput(new CommandLineClient(new String[] {
"--localdir", clientA.get("localdir"),
"genlink",
"-s"
"--localdir", clientA.get("localdir"),
"genlink",
"-s"
}));

assertEquals("Different number of output lines expected.", 1, cliOutA.length);
String createdLink = cliOutA[0];

String[] cliOutB = TestCliUtil.runAndCaptureOutput(new CommandLineClient(new String[] {
"--localdir", clientLocalDirB.getAbsolutePath(),
"connect",
"--no-daemon",
createdLink
"--localdir", clientLocalDirB.getAbsolutePath(),
"--no-daemon",
"connect",
createdLink
}));

assertEquals("Different number of output lines expected.", 3, cliOutB.length);
assertEquals("Repository connected, and local folder initialized.", cliOutB[1]);

TestCliUtil.deleteTestLocalConfigAndData(clientA);
TestFileUtil.deleteDirectory(clientLocalDirB);
}
TestFileUtil.deleteDirectory(clientLocalDirB);
}

@Test
public void testGenlinkCommandShortEncrypted() throws Exception {
// Setup
Map<String, String> connectionSettings = TestConfigUtil.createTestLocalConnectionSettings();
Map<String, String> clientA = TestCliUtil.createLocalTestEnv("A", connectionSettings);
Map<String, String> clientA = TestCliUtil.createLocalTestEnv("A", connectionSettings);
File clientLocalDirB = TestFileUtil.createTempDirectoryInSystemTemp();

systemInMock.provideText("somelongpassword\nsomelongpassword\nsomelongpassword\n");
systemInMock.provideText("somelongpassword\nsomelongpassword\nsomelongpassword\n");

// Run Init
String[] initArgs = new String[] {
"--localdir", clientA.get("localdir"),
"init",
"--no-daemon",
"--plugin", "local",
"--plugin-option", "path=" + clientA.get("repopath"),
"--no-compression"
};
String[] initArgs = new String[] {
"--localdir", clientA.get("localdir"),
"init",
"--no-daemon",
"--plugin", "local",
"--plugin-option", "path=" + clientA.get("repopath"),
"--no-compression"
};

new CommandLineClient(initArgs).start();

// Run Genlink (on A)
String[] cliOutA = TestCliUtil.runAndCaptureOutput(new CommandLineClient(new String[] {
"--localdir", clientA.get("localdir"),
"genlink",
"-s"
"--localdir", clientA.get("localdir"),
"genlink",
"-s"
}));

assertEquals("Different number of output lines expected.", 1, cliOutA.length);
String createdLink = cliOutA[0];

String[] cliOutB = TestCliUtil.runAndCaptureOutput(new CommandLineClient(new String[] {
"--localdir", clientLocalDirB.getAbsolutePath(),
"connect",
"--no-daemon",
createdLink
"--localdir", clientLocalDirB.getAbsolutePath(),
"--no-daemon",
"connect",
createdLink
}));

assertEquals("Different number of output lines expected.", 4, cliOutB.length);
assertEquals("Repository connected, and local folder initialized.", cliOutB[2]);

TestCliUtil.deleteTestLocalConfigAndData(clientA);
TestFileUtil.deleteDirectory(clientLocalDirB);
}
TestFileUtil.deleteDirectory(clientLocalDirB);
}
}
123 changes: 66 additions & 57 deletions syncany-cli/src/test/java/org/syncany/tests/util/TestCliUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,104 +34,113 @@

public class TestCliUtil {
private static final Logger logger = Logger.getLogger(TestCliUtil.class.getSimpleName());

public static Map<String, String> createLocalTestEnv(String machineName, Map<String, String> connectionProperties) throws Exception {
Map<String, String> clientSettings = new HashMap<String, String>();

File tempLocalDir = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("client-"+machineName, connectionProperties));

File tempLocalDir = TestFileUtil.createTempDirectoryInSystemTemp(TestConfigUtil.createUniqueName("client-" + machineName,
connectionProperties));
tempLocalDir.mkdirs();

// Client settings
clientSettings.put("machinename", machineName);
clientSettings.put("localdir", tempLocalDir.getAbsolutePath());
clientSettings.put("repopath", connectionProperties.get("path"));
clientSettings.put("repopath", connectionProperties.get("path"));

return clientSettings;
}
}

public static Map<String, String> createLocalTestEnvAndInit(String machineName, Map<String, String> connectionProperties) throws Exception {
Map<String, String> client = createLocalTestEnv(machineName, connectionProperties);

// Init
String[] initArgs = new String[] {
"--localdir", client.get("localdir"),
"init",
"--plugin", "local",
"--plugin-option", "path="+client.get("repopath"),
"--no-encryption",
"--no-compression"
};

logger.log(Level.INFO, "Running syncany with argument: "+StringUtil.join(initArgs, " "));
String[] initArgs = new String[] {
"--localdir", client.get("localdir"),
"init",
"--no-daemon",
"--plugin", "local",
"--plugin-option", "path=" + client.get("repopath"),
"--no-encryption",
"--no-compression",
};

logger.log(Level.INFO, "Running syncany with argument: " + StringUtil.join(initArgs, " "));
new CommandLineClient(initArgs).start();

fixMachineName(client);

return client;
}
}

public static Map<String, String> createLocalTestEnvAndConnect(String machineName, Map<String, String> connectionProperties) throws Exception {
Map<String, String> client = createLocalTestEnv(machineName, connectionProperties);

// Init
String[] connectArgs = new String[] {
"--localdir", client.get("localdir"),
"connect",
"--plugin", "local",
"--plugin-option", "path="+client.get("repopath")
};

logger.log(Level.INFO, "Running syncany with argument: "+StringUtil.join(connectArgs, " "));
String[] connectArgs = new String[] {
"--localdir", client.get("localdir"),
"connect",
"--no-daemon",
"--plugin", "local",
"--plugin-option", "path=" + client.get("repopath")
};

logger.log(Level.INFO, "Running syncany with argument: " + StringUtil.join(connectArgs, " "));
new CommandLineClient(connectArgs).start();

fixMachineName(client);

return client;
}
}

public static String[] runAndCaptureOutput(CommandLineClient cli) throws Exception {
ByteArrayOutputStream bufferedCliOut = new ByteArrayOutputStream();

cli.setOut(new SplitOutputStream(bufferedCliOut, System.out));
cli.start();

logger.log(Level.INFO, "CLI output: ");
logger.log(Level.INFO, toString(bufferedCliOut));

return toStringArray(bufferedCliOut);
}

private static void fixMachineName(Map<String, String> client) throws Exception {
File configFile = new File(client.get("localdir")+"/"+Config.DIR_APPLICATION+"/"+Config.FILE_CONFIG);
Serializer serializer = new Persister();
ConfigTO configTO = serializer.read(ConfigTO.class, configFile);
File configFile = new File(client.get("localdir") + "/" + Config.DIR_APPLICATION + "/" + Config.FILE_CONFIG);
Serializer serializer = new Persister();

ConfigTO configTO = serializer.read(ConfigTO.class, configFile);
configTO.setMachineName(client.get("machinename"));

serializer.write(configTO, configFile);
}

public static void deleteTestLocalConfigAndData(Map<String, String> clientSettings) throws ConfigException {
if (clientSettings.get("localdir") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("localdir")));
if (clientSettings.get("cachedir") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("cachedir")));
if (clientSettings.get("databasedir") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("databasedir")));
if (clientSettings.get("configfile") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("configfile")));
if (clientSettings.get("appdir") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("appdir")));
if (clientSettings.get("repopath") != null) TestFileUtil.deleteDirectory(new File(clientSettings.get("repopath")));
}
public static void deleteTestLocalConfigAndData(Map<String, String> clientSettings) throws ConfigException {
if (clientSettings.get("localdir") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("localdir")));
if (clientSettings.get("cachedir") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("cachedir")));
if (clientSettings.get("databasedir") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("databasedir")));
if (clientSettings.get("configfile") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("configfile")));
if (clientSettings.get("appdir") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("appdir")));
if (clientSettings.get("repopath") != null)
TestFileUtil.deleteDirectory(new File(clientSettings.get("repopath")));
}

public static String toString(ByteArrayOutputStream bos) {
return new String(bos.toByteArray());
}
public static String[] toStringArray(ByteArrayOutputStream bos) {

public static String[] toStringArray(ByteArrayOutputStream bos) {
return toString(bos).split("[\\r\\n]|[\\n\\r]|[\\n]");
}

public static boolean setCurrentDirectory(File newDirectory) {
boolean result = false;
boolean result = false;
File directory = newDirectory.getAbsoluteFile();

if (directory.exists() || directory.mkdirs()) {
result = (System.setProperty("user.dir", directory.getAbsolutePath()) != null);
}
Expand Down

0 comments on commit 54781d5

Please sign in to comment.