Skip to content

Commit

Permalink
Removed duplicated code in CompileUploadCliTest
Browse files Browse the repository at this point in the history
  • Loading branch information
judovana committed Oct 9, 2021
1 parent 7f2058d commit fdbdcf5
Showing 1 changed file with 63 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,7 @@ void testDecompileJavap(String option) throws Exception {

void testOverwrite(String pucComponent) throws Exception {
createReplacement(NEW_GREETING);

String[] args = new String[]{
Cli.OVERWRITE,
pucComponent,
dummy.getFqn(),
dummy.getDotClassPath() // contains newGreeting because of try-catch above
};
Cli cli = new Cli(args, model);

Assertions.assertDoesNotThrow(() -> cli.consumeCli());
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, dummy.getDotClassPath()));
Assertions.assertTrue(streams.getOut().contains("success"));

// assert that change propagated, unfortunately we have to rely on another operation here
Expand Down Expand Up @@ -223,16 +214,7 @@ void testOverwriteWarning() {
} catch (IOException e) {
Assertions.fail("Failed to copy file.", e);
}

String[] args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
nonClassFile
};
Cli cli = new Cli(args, model);

Assertions.assertDoesNotThrow(() -> cli.consumeCli());
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, nonClassFile));
String output = streams.getErr();
Assertions.assertTrue(output.contains("WARNING:"));
}
Expand All @@ -252,17 +234,7 @@ public static boolean checkPlugin(String plugin, Model model) throws Exception {
void testDecompileCompileCfr() throws Exception {
final String plugin = "Cfr";
Assumptions.assumeTrue(checkPlugin(plugin, model), "plugin: " + plugin + " not available");
File decompiledFile = File.createTempFile("jrd", "test.java");
String[] args = new String[]{
Cli.DECOMPILE,
dummy.getPid(),
plugin,
dummy.getFqn(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, decompiledFile.getAbsolutePath()
};
Cli cli = new Cli(args, model);
cli.consumeCli();
File decompiledFile = decompile(plugin, dummy, model);
String sOrig = Files.readAllLines(
decompiledFile.toPath(), StandardCharsets.UTF_8).stream()
.collect(Collectors.joining("\n"));
Expand All @@ -273,49 +245,57 @@ void testDecompileCompileCfr() throws Exception {
assertEqualsWithTolerance(sOrig, sNoCommnets, 0.9);
assertEqualsWithTolerance(sNoCommnets, dummy.getDefaultContentWithPackage(), 0.85);

File compiledFile = File.createTempFile("jrd", "test.class");
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
decompiledFile.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();
File compiledFile = compile(null, dummy, model, decompiledFile);
String compiled = readBinaryAsString(compiledFile);
String original = readBinaryAsString(new File(dummy.getDotClassPath()));
assertEqualsWithTolerance(compiled, original, 0.9);

args = new String[]{
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, compiledFile));
Assertions.assertThrows(Exception.class, () -> overwrite(dummy, model, decompiledFile)); //src instead of bin == nonsense
}

private static void overwrite(AbstractSourceTestClass dummy, Model model, String bin) throws Exception {
overwrite(dummy, model, new File(bin));
}

private static void overwrite(AbstractSourceTestClass dummy, Model model, File bin) throws Exception {
String[] args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
compiledFile.getAbsolutePath()
bin.getAbsolutePath()
};
cli = new Cli(args, model);
Cli cli = new Cli(args, model);
cli.consumeCli();
}

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
decompiledFile.getAbsolutePath()//nonsense, will not be accepted
};
cli = new Cli(args, model);
Exception ex = null;
try {
cli.consumeCli();
} catch (Exception eex) {
ex = eex;
private static File compile(String plugin, AbstractSourceTestClass dummy, Model model, File src) throws Exception {
File compiledFile = File.createTempFile("jrd", "test.class");
String[] args;
if (plugin != null) {
args = new String[]{
Cli.COMPILE,
Cli.P, plugin,
Cli.CP, dummy.getPid(),
src.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
} else {
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
src.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
}
Assertions.assertNotNull(ex);
Cli cli = new Cli(args, model);
cli.consumeCli();
return compiledFile;
}

@Test
void testDecompileCompileJasm() throws Exception {
final String plugin = "jasm";
Assumptions.assumeTrue(checkPlugin(plugin, model), "plugin: " + plugin + " not available");
private static File decompile(String plugin, AbstractSourceTestClass dummy, Model model) throws Exception {
File decompiledFile = File.createTempFile("jrd", "test.java");
String[] args = new String[]{
Cli.DECOMPILE,
Expand All @@ -327,6 +307,14 @@ void testDecompileCompileJasm() throws Exception {
};
Cli cli = new Cli(args, model);
cli.consumeCli();
return decompiledFile;
}

@Test
void testDecompileCompileJasm() throws Exception {
final String plugin = "jasm";
Assumptions.assumeTrue(checkPlugin(plugin, model), "plugin: " + plugin + " not available");
File decompiledFile = decompile(plugin, dummy, model);
String sOrig = Files.readAllLines(decompiledFile.toPath(), StandardCharsets.UTF_8).stream().collect(Collectors.joining("\n"));
String sLine = Files.readAllLines(decompiledFile.toPath(), StandardCharsets.UTF_8).stream().collect(Collectors.joining(" "));
//unluckily there is nothing to compare to, unless we wish to call jasm from here "again"
Expand All @@ -345,102 +333,31 @@ void testDecompileCompileJasm() throws Exception {
Assertions.assertTrue(sLine.matches(".*getstatic\\s+Field\\s+java/lang/System.out:\"Ljava/io/PrintStream;\";.*"));
Assertions.assertTrue(sLine.matches(".*ldc\\s+String\\s+\"Hello\";.*"));

File compiledFile = File.createTempFile("jrd", "test.class");
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
Cli.P, plugin,
decompiledFile.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();
File compiledFile = compile(plugin, dummy, model, decompiledFile);
String compiled = readBinaryAsString(compiledFile);
String original = readBinaryAsString(new File(dummy.getDotClassPath()));
assertEqualsWithTolerance(compiled, original, 0.4); //yah, jasm performance is not greate
assertEqualsWithTolerance(compiled, original, 0.4); //yah, jasm performance is not great

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
decompiledFile.getAbsolutePath() //some nonsense, should fail
};
cli = new Cli(args, model);
Exception ex = null;
try {
cli.consumeCli();
} catch (Exception eex) {
ex = eex;
}
Assertions.assertNotNull(ex);
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, compiledFile));
Assertions.assertThrows(Exception.class, () -> overwrite(dummy, model, decompiledFile)); //src instead of bin == nonsense
}

@Test
void testDecompileCompileJcoder() throws Exception {
final String plugin = "jcoder";
Assumptions.assumeTrue(checkPlugin(plugin, model), "plugin: " + plugin + " not available");
File decompiledFile = File.createTempFile("jrd", "test.java");
String[] args = new String[]{
Cli.DECOMPILE,
dummy.getPid(),
plugin,
dummy.getFqn(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, decompiledFile.getAbsolutePath()
};
Cli cli = new Cli(args, model);
cli.consumeCli();
File decompiledFile = decompile(plugin, dummy, model);
String sOrig = Files.readAllLines(decompiledFile.toPath(), StandardCharsets.UTF_8).stream().collect(Collectors.joining("\n"));
//unluckily there is nothing to compare to, unless we wish to call jasm from here "again"
//unluckily there is nothing to compare to, unless we wish to call jcoder from here "again"
Assertions.assertTrue(sOrig.length() > 1000);

File compiledFile = File.createTempFile("jrd", "test.class");
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
Cli.P, plugin,
decompiledFile.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();
File compiledFile = compile(plugin, dummy, model, decompiledFile);
String compiled = readBinaryAsString(compiledFile);
String original = readBinaryAsString(new File(dummy.getDotClassPath()));
assertEqualsWithTolerance(compiled, original, 0.4); //yah, jasm performance is not greate

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
decompiledFile.getAbsolutePath() //some nonsense, should fail
};
cli = new Cli(args, model);
Exception ex = null;
try {
cli.consumeCli();
} catch (Exception eex) {
ex = eex;
}
Assertions.assertNotNull(ex);
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, compiledFile));
Assertions.assertThrows(Exception.class, () -> overwrite(dummy, model, decompiledFile)); //src instead of bin == nonsense
}


Expand All @@ -460,18 +377,9 @@ void testGlobalApi() throws Exception {

String withNonsense = dummy.getDefaultContentWithPackage().replace("/*API_PLACEHOLDER*/", "some nonsese\n");
Files.write(decompiledFile.toPath(), withNonsense.getBytes(StandardCharsets.UTF_8));
File compiledFile = File.createTempFile("jrd", "test.class");
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
decompiledFile.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
Exception expectedEx = null;
try {
cli.consumeCli();
File compiledFile = compile(null, dummy, model, decompiledFile);
} catch (Exception ex) {
String afterCompilationsOut = streams.getOut();
String afterCompilationsErr = streams.getErr();
Expand All @@ -485,33 +393,13 @@ void testGlobalApi() throws Exception {
"org.jrd.agent.api.Variables.Global.set(\"counter\", i);\n" +
"System.out.println(\"API: \"+i+\" had spoken\");\n");
Files.write(decompiledFile.toPath(), withApi.getBytes(StandardCharsets.UTF_8));
args = new String[]{
Cli.COMPILE,
Cli.CP, dummy.getPid(),
decompiledFile.getAbsolutePath(),
Cli.SAVE_LIKE, Cli.Saving.EXACT,
Cli.SAVE_AS, compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
try {
cli.consumeCli();
} catch (Exception ex) {
String afterCompilationsOut = streams.getOut();
String afterCompilationsErr = streams.getErr();
throw ex;
}
File compiledFile = compile(null, dummy, model, decompiledFile);
String compiled = readBinaryAsString(compiledFile);
String original = readBinaryAsString(new File(dummy.getDotClassPath()));
assertEqualsWithTolerance(compiled, original, 0.4);

args = new String[]{
Cli.OVERWRITE,
dummy.getPid(),
dummy.getFqn(),
compiledFile.getAbsolutePath()
};
cli = new Cli(args, model);
cli.consumeCli();
Assertions.assertDoesNotThrow(() -> overwrite(dummy, model, compiledFile));

Thread.sleep(1000);
String mainOutput = dummy.getOutString();
Assertions.assertTrue(mainOutput.contains("API: 1 had spoken"));
Expand Down

0 comments on commit fdbdcf5

Please sign in to comment.