From 8c822ac7b303bd2915640ff4f5a133763ca8fbf2 Mon Sep 17 00:00:00 2001 From: Jeanderson Barros Candido Date: Tue, 5 Jun 2018 05:40:12 -0300 Subject: [PATCH] Added tests to verify if resources exist in classpath (#76) Build tasks and tests to verify if resources exist in classpath * Added minimal Java main to debug Reporter (#75) * Added failing test due to missing .version file (#75) * Added generateVersion task on Gradle Build (#75) * Added test to check if hashes match with git output (#75) --- build.gradle | 18 +++++++- src/tests/ReporterResourcesTest.java | 69 ++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/tests/ReporterResourcesTest.java diff --git a/build.gradle b/build.gradle index 2254be0..3ab3ce0 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,18 @@ clean { group = "JPF Build" } +task generateVersion { + description = "Generates the .version file with the current revision hash" + group = "JPF Build Properties" + + doLast { + def revision = "git rev-parse HEAD".execute().text + new File(".version").withWriter("utf-8") { writer -> + writer.writeLine revision + } + } +} + task compile(type: Copy) { group = "JPF Build" description = "Compiles all JPF core sources." @@ -55,11 +67,15 @@ task compile(type: Copy) { // These are automatic generated tasks from the Java Gradle Plugin. // Gradle is able to infer the ordering of the source sets // due to the compileClasspath attribute - dependsOn compileTestJava, compileExamplesJava + dependsOn compileTestJava, compileExamplesJava, generateVersion // Copies build.properties file to the build directory from "build.properties" into sourceSets.main.java.outputDir.path + "/gov/nasa/jpf" + + // Copies .version file to the build directory + from ".version" + into sourceSets.main.java.outputDir.path + "/gov/nasa/jpf" } task jpfClassesJar(type: Jar) { diff --git a/src/tests/ReporterResourcesTest.java b/src/tests/ReporterResourcesTest.java new file mode 100644 index 0000000..0e5d3f7 --- /dev/null +++ b/src/tests/ReporterResourcesTest.java @@ -0,0 +1,69 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.junit.Before; +import org.junit.Test; + +import gov.nasa.jpf.Config; +import gov.nasa.jpf.JPF; +import gov.nasa.jpf.util.test.TestJPF; + +/** + * This is a plain JUnit test to check whether required resource files exist on + * JPF classpath. + * + * @author Jeanderson Candido + * + */ +public class ReporterResourcesTest extends TestJPF { + + private JPF jpf; + + @Before + public void setup() { + String[] configArgs = { "+vm.class=.vm.MultiProcessVM", "+target.1=HelloWorld", "+target.2=HelloWorld" }; + this.jpf = new JPF(new Config(configArgs)); + } + + @Test + public void checkResources() throws IOException { + assertNotNull("build.properties should exist on classpath", jpf.getClass().getResourceAsStream("build.properties")); + assertNotNull(".version should exist on classpath", jpf.getClass().getResourceAsStream(".version")); + } + + @Test + public void hashMustMatch() { + InputStream stream = jpf.getClass().getResourceAsStream(".version"); + assertEquals("Should have the same hash", fetchCurrentRevisionFromVCS().trim(), readContentFrom(stream).trim()); + } + + private String fetchCurrentRevisionFromVCS() { + String currentRevision = ""; + try { + Process process = Runtime.getRuntime().exec("git rev-parse HEAD"); + process.waitFor(); + InputStream output = process.getInputStream(); + currentRevision = readContentFrom(output); + + } catch (Exception e) { + e.printStackTrace(); + } + return currentRevision; + } + + private String readContentFrom(InputStream stream) { + BufferedReader buffer = new BufferedReader(new InputStreamReader(stream)); + StringBuilder output = new StringBuilder(); + try { + while (buffer.ready()) { + output.append(buffer.readLine().trim()).append("\n"); + } + } catch (IOException e) { + fail("Should not have failed while reading the file"); + } + return output.toString(); + } + +} -- 2.34.1