Merge branch 'master' into gradle
[jpf-core.git] / src / tests / ReporterResourcesTest.java
1 import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.io.InputStreamReader;
5
6 import org.junit.Before;
7 import org.junit.Test;
8
9 import gov.nasa.jpf.Config;
10 import gov.nasa.jpf.JPF;
11 import gov.nasa.jpf.util.test.TestJPF;
12
13 /**
14  * This is a plain JUnit test to check whether required resource files exist on
15  * JPF classpath.
16  *
17  * @author Jeanderson Candido
18  *
19  */
20 public class ReporterResourcesTest extends TestJPF {
21
22   private JPF jpf;
23
24   @Before
25   public void setup() {
26     String[] configArgs = { "+vm.class=.vm.MultiProcessVM", "+target.1=HelloWorld", "+target.2=HelloWorld" };
27     this.jpf = new JPF(new Config(configArgs));
28   }
29
30   @Test
31   public void checkResources() throws IOException {
32     assertNotNull("build.properties should exist on classpath", jpf.getClass().getResourceAsStream("build.properties"));
33     assertNotNull(".version should exist on classpath", jpf.getClass().getResourceAsStream(".version"));
34   }
35
36   @Test
37   public void hashMustMatch() {
38     InputStream stream = jpf.getClass().getResourceAsStream(".version");
39     assertEquals("Should have the same hash", fetchCurrentRevisionFromVCS().trim(), readContentFrom(stream).trim());
40   }
41
42   private String fetchCurrentRevisionFromVCS() {
43     String currentRevision = "";
44     try {
45       Process process = Runtime.getRuntime().exec("git rev-parse HEAD");
46       process.waitFor();
47       InputStream output = process.getInputStream();
48       currentRevision = readContentFrom(output);
49
50     } catch (Exception e) {
51       e.printStackTrace();
52     }
53     return currentRevision;
54   }
55
56   private String readContentFrom(InputStream stream) {
57     BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
58     StringBuilder output = new StringBuilder();
59     try {
60       while (buffer.ready()) {
61         output.append(buffer.readLine().trim()).append("\n");
62       }
63     } catch (IOException e) {
64       fail("Should not have failed while reading the file");
65     }
66     return output.toString();
67   }
68
69 }