From 07de1225289de385a58da016e1b8b9a40f2bab04 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 25 Jun 2020 13:03:15 +1200 Subject: [PATCH 1/3] Added fix for junit noclassdeffound error on Windows --- .../wgtn/yamf/checks/junit/JUnitActions.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java index b175029..87bb0b9 100644 --- a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java +++ b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java @@ -13,6 +13,7 @@ import java.util.Date; import java.util.stream.Collectors; import java.util.stream.Stream; +import java.lang.System; /** * Actions to run tests (usually acceptance tests). @@ -26,6 +27,9 @@ public class JUnitActions { public static final String JUNIT_REPORT_FOLDER = ".junit-reports"; public static final String JUPITER_REPORT_NAME = "TEST-junit-jupiter.xml"; public static final String VINTAGE_REPORT_NAME = "TEST-junit-vintage.xml"; + + private static bool isWindows = false; + private static bool osChecked = false; public static final DateFormat FOLDERNAME_FROM_TIMESTAMP_FORMAT = new java.text.SimpleDateFormat("yyyy-MM-dd--HH-mm-ss--SSS"); @@ -45,13 +49,19 @@ public static TestResults test (File junitRunner, String testClass, String class File junitReportFolder = new File(new File(JUNIT_REPORT_FOLDER),""+FOLDERNAME_FROM_TIMESTAMP_FORMAT.format(new Date())); // unique folder names junitReportFolder.mkdirs(); + + ProcessResult result = null; if (classpath==null) { result = OS.exe(new File("."), "java","-jar", junitRunner.getAbsolutePath(), "-reports-dir",junitReportFolder.getAbsolutePath(),"-c",testClass); } else { - result = OS.exe(new File("."), "java","-jar", junitRunner.getAbsolutePath(), "-reports-dir",junitReportFolder.getAbsolutePath(),"-cp",classpath,"-c",testClass); + if (checkOSisWindows()) { + result = adjustRunnerForWindows(classpath, junitRunner, junitReportFolder, testClass); + } else { + result = OS.exe(new File("."), "java","-jar", junitRunner.getAbsolutePath(), "-reports-dir",junitReportFolder.getAbsolutePath(),"-cp",classpath,"-c",testClass); + } } // parse results @@ -105,6 +115,38 @@ private static void extractStatsFromReport(File junitReport, TestResults testRes int testResultingInErrorCounts = XML.evalXPathSingleNodeAsInt(junitReport, "/testsuite/@errors"); testResults.addToTestsWithErrors(testResultingInErrorCounts); } + + private static ProcessResult adjustRunnerForWindows(String classpath, File junitRunner, File junitReportFolder, String testClass) { + //Prepend junit jar to classpath + classpath = junitRunner.getAbsolutePath()+File.pathSeparator+classpath; + + //Extract spring-mock dependency from classpath, and append to end + String[] cp = classpath.split(File.pathSeparator); + String cp2 = ""; + String mock = ""; + for (int i = 0; i < cp.length; i++) { + if (!cp[i].contains("spring-mock")) { + cp2 += cp[i] + File.pathSeparator; + } else { + mock = cp[i]; + } + } + cp2 += File.pathSeparator+mock; + + //Execute jar from classpath + result = OS.exe(new File("."), "java","-cp", cp2, "org.junit.platform.console.ConsoleLauncher", "-reports-dir",junitReportFolder.getAbsolutePath(),"-c",testClass); + } + + private static boolean checkOSisWindows() { + if (!osChecked) { + if (System.getProperty("os.name").toLowerCase().contains("win")) { + isWindows = true; + } + osChecked = true; + } + + return isWindows; + } } From c8af52e36e43f306b1d99a69e63dbb3cf13f31b5 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 25 Jun 2020 13:10:55 +1200 Subject: [PATCH 2/3] Minor bugfix --- .../java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java index 87bb0b9..6f0886c 100644 --- a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java +++ b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java @@ -28,8 +28,8 @@ public class JUnitActions { public static final String JUPITER_REPORT_NAME = "TEST-junit-jupiter.xml"; public static final String VINTAGE_REPORT_NAME = "TEST-junit-vintage.xml"; - private static bool isWindows = false; - private static bool osChecked = false; + private static boolean isWindows = false; + private static boolean osChecked = false; public static final DateFormat FOLDERNAME_FROM_TIMESTAMP_FORMAT = new java.text.SimpleDateFormat("yyyy-MM-dd--HH-mm-ss--SSS"); @@ -116,7 +116,7 @@ private static void extractStatsFromReport(File junitReport, TestResults testRes testResults.addToTestsWithErrors(testResultingInErrorCounts); } - private static ProcessResult adjustRunnerForWindows(String classpath, File junitRunner, File junitReportFolder, String testClass) { + private static ProcessResult adjustRunnerForWindows(String classpath, File junitRunner, File junitReportFolder, String testClass) throws Exception { //Prepend junit jar to classpath classpath = junitRunner.getAbsolutePath()+File.pathSeparator+classpath; @@ -134,7 +134,7 @@ private static ProcessResult adjustRunnerForWindows(String classpath, File junit cp2 += File.pathSeparator+mock; //Execute jar from classpath - result = OS.exe(new File("."), "java","-cp", cp2, "org.junit.platform.console.ConsoleLauncher", "-reports-dir",junitReportFolder.getAbsolutePath(),"-c",testClass); + return OS.exe(new File("."), "java","-cp", cp2, "org.junit.platform.console.ConsoleLauncher", "-reports-dir",junitReportFolder.getAbsolutePath(),"-c",testClass); } private static boolean checkOSisWindows() { From 1d114adfba1bc625971c8cc77a5d052e6121cb6c Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 25 Jun 2020 13:23:39 +1200 Subject: [PATCH 3/3] Minor bugfix --- .../main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java index 6f0886c..1b5e787 100644 --- a/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java +++ b/yamf-acceptancetests/src/main/java/nz/ac/wgtn/yamf/checks/junit/JUnitActions.java @@ -49,8 +49,6 @@ public static TestResults test (File junitRunner, String testClass, String class File junitReportFolder = new File(new File(JUNIT_REPORT_FOLDER),""+FOLDERNAME_FROM_TIMESTAMP_FORMAT.format(new Date())); // unique folder names junitReportFolder.mkdirs(); - - ProcessResult result = null; if (classpath==null) {