Added tests to verify if resources exist in classpath (#76)
authorJeanderson Barros Candido <jeandersonbc@gmail.com>
Tue, 5 Jun 2018 08:40:12 +0000 (05:40 -0300)
committercyrille-artho <cyrille-artho@users.noreply.github.com>
Tue, 5 Jun 2018 08:40:12 +0000 (17:40 +0900)
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
src/tests/ReporterResourcesTest.java [new file with mode: 0644]

index 2254be02c5ba9bccde982a3cc6f919f3b14df387..3ab3ce00ca898956bbe40adddbc65e1ca751c1d9 100644 (file)
@@ -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 (file)
index 0000000..0e5d3f7
--- /dev/null
@@ -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();
+  }
+
+}