package project; import gov.nasa.jpf.util.test.TestJPF; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import org.junit.Test; /** * Tests the CallMonitor listener. * * @author Franck van Breugel */ public class CallMonitorTest extends TestJPF { /** * JPF's application properties. */ private static String[] PROPERTIES = { "+listener=gov.nasa.jpf.listener.CallMonitor", "+classpath=C:/users/franck/workspace/project/bin/", "+native_classpath=C:/users/franck/workspace/bin/", "+@include=C:/users/franck/jpf/jpf-core/jpf.properties" }; /** * Tests the CallMonitor listener for an empty code block. */ @Test public void emptyTest() { if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { // do nothing } } /** * Method used in staticMethodTest. */ private static void staticMethod() {} /** * Tests the CallMonitor listener for a main method containing a call to staticMethod. */ @Test public void staticMethodTest() { PrintStream out = null; ByteArrayOutputStream stream = null; if (!TestJPF.isJPFRun()) { out = System.out; stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); } if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { CallMonitorTest.staticMethod(); } else { System.setOut(out); TestJPF.assertTrue("Invocation of staticMethod incorrect", stream.toString().contains("CallMonitorTest.staticMethod()")); } } /** * Method used in methodTest. */ private void method() {} /** * Tests the CallMonitor listener for a main method containing a call to method. */ @Test public void methodTest() { PrintStream out = null; ByteArrayOutputStream stream = null; if (!TestJPF.isJPFRun()) { out = System.out; stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); } if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { this.method(); } else { System.setOut(out); TestJPF.assertTrue("Invocation of method incorrect", stream.toString().contains("CallMonitorTest.method()")); } } /** * Method used in staticIntMethodTest. */ private static void staticIntMethod(int i) {} /** * Tests the CallMonitor listener for a main method containing a call to staticIntMethod. */ @Test public void staticIntMethodTest() { PrintStream out = null; ByteArrayOutputStream stream = null; if (!TestJPF.isJPFRun()) { out = System.out; stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); } if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { CallMonitorTest.staticIntMethod(0); } else { System.setOut(out); TestJPF.assertTrue("Invocation of staticIntMethod incorrect", stream.toString().contains("CallMonitorTest.staticIntMethod(0)")); } } /** * Method used in intMethodTes. */ private void intMethod(int i) {} /** * Tests the CallMonitor listener for a main method containing a call to intMethod. */ @Test public void intMethodTest() { PrintStream out = null; ByteArrayOutputStream stream = null; if (!TestJPF.isJPFRun()) { out = System.out; stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); } if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { this.intMethod(0); } else { System.setOut(out); TestJPF.assertTrue("Invocation of intMethod incorrect", stream.toString().contains("CallMonitorTest.intMethod(0)")); } } /** * Method used in staticDoubleMethodTest. */ private static void staticDoubleMethod(double d) {} /** * Tests the CallMonitor listener for a main method containing a call to staticDoubleMethod. */ @Test public void staticDoubleMethodTest() { PrintStream out = null; ByteArrayOutputStream stream = null; if (!TestJPF.isJPFRun()) { out = System.out; stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); } if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { CallMonitorTest.staticDoubleMethod(1.0); } else { System.setOut(out); TestJPF.assertTrue("Invocation of staticDoubleMethod incorrect", stream.toString().contains("CallMonitorTest.staticDoubleMethod(1.0)")); } } /** * Method used in doubleMethodTest. */ private void doubleMethod(double d) {} /** * Tests the CallMonitor listener for a main method containing a call to doubleMethod. */ @Test public void doubleMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (this.verifyNoPropertyViolation(CallMonitorTest.PROPERTIES)) { this.doubleMethod(1.0); } else { System.setOut(out); TestJPF.assertTrue("Invocation of doubleMethod incorrect", stream.toString().contains("CallMonitorTest.doubleMethod(1.0)")); } } // /** // * Method used in the next test. // */ // private static void staticNullMethod(Object o) {} // // @Test // public void staticNullMethodTest() { // PrintStream out = System.out; // ByteArrayOutputStream stream = new ByteArrayOutputStream(); // System.setOut(new PrintStream(stream)); // if (verifyNoPropertyViolation("+listener=CallMonitor", "+classpath=.", "+native_classpath=.")) { // staticNullMethod(null); // } else { // System.setOut(out); // assertTrue("Invocation of staticNullMethod incorrect", stream.toString().contains("0: CallMonitorTest.staticNullMethod(null)")); // } // } // // /** // * Method used in the next test. // */ // private void nullMethod(Object o) {} // // @Test // public void nullMethodTest() { // PrintStream out = System.out; // ByteArrayOutputStream stream = new ByteArrayOutputStream(); // System.setOut(new PrintStream(stream)); // if (verifyNoPropertyViolation("+listener=CallMonitor", "+classpath=.", "+native_classpath=.")) { // nullMethod(null); // } else { // System.setOut(out); // assertTrue("Invocation of nullMethod incorrect", stream.toString().contains("0: CallMonitorTest.nullMethod(null)")); // } // } // // /** // * Method used in the next test. // */ // private static void staticObjectMethod(Object o) {} // // @Test // public void staticObjectMethodTest() { // PrintStream out = System.out; // ByteArrayOutputStream stream = new ByteArrayOutputStream(); // System.setOut(new PrintStream(stream)); // if (verifyNoPropertyViolation("+listener=CallMonitor", "+classpath=.", "+native_classpath=.")) { // staticObjectMethod(new Object()); // } else { // System.setOut(out); // assertTrue("Invocation of staticObjectMethod incorrect" + stream.toString(), stream.toString().contains("CallMonitorTest.staticObjectMethod(java.lang.Object")); // } // } /** * Runs the test methods with the given names. If no names are given, all test methods are run. * * @param testMethods the names of the test methods to be run. */ public static void main(String[] testMethods) { runTestsOfThisClass(testMethods); } }