Skip to content

Commit

Permalink
Added tests to verify if resources exist in classpath (#76)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jeandersonbc authored and cyrille-artho committed Jun 5, 2018
1 parent dda52e5 commit 8c822ac
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
18 changes: 17 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,34 @@ 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."

// 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) {
Expand Down
69 changes: 69 additions & 0 deletions src/tests/ReporterResourcesTest.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit 8c822ac

Please sign in to comment.