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 { /** * Matches an arbitrary number of lines of text. */ private static final String LINES = "(.*\n)*"; /** * JPF's application properties. */ private static final String[] CONFIGURATION = { "+listener=CallMonitor", "+classpath=.", "+native_classpath=." }; /** * Tests the CallMonitor listener on an empty app. */ @Test public void emptyTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { // do nothing } else { System.setOut(out); assertTrue("", stream.toString().contains("0:")); } } /** * Method used in staticMethodTest. */ private static void staticMethod() {} /** * Tests the CallMonitor listener on an app containing a call to staticMethod. */ @Test public void staticMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { staticMethod(); } else { System.setOut(out); assertTrue("Invocation of staticMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.staticMethod\\(\\)" + LINES)); } } /** * Method used in methodTest. */ private void method() {} /** * Tests the CallMonitor listener on an app containing a call to method. */ @Test public void methodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { method(); } else { System.setOut(out); assertTrue("Invocation of method incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.method\\(\\)" + LINES)); } } /** * Method used in staticIntMethodTest. */ private static void staticIntMethod(int i) {} /** * Tests the CallMonitor listener on an app containing a call to staticIntMethod. */ @Test public void staticIntMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { staticIntMethod(0); } else { System.setOut(out); assertTrue("Invocation of staticIntMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.staticIntMethod\\(0\\)" + LINES)); } } /** * Method used in intMethodTest. */ private void intMethod(int i) {} /** * Tests the CallMonitor listener on an app containing a call to intMethod. */ @Test public void intMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { intMethod(0); } else { System.setOut(out); assertTrue("Invocation of intMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.intMethod\\(0\\)" + LINES)); } } /** * Method used in staticDoubleMethodTest. */ private static void staticDoubleMethod(double d) {} /** * Tests the CallMonitor listener on an app containing a call to staticDoubleMethod. */ @Test public void staticDoubleMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { staticDoubleMethod(1.0); } else { System.setOut(out); assertTrue("Invocation of staticDoubleMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.staticDoubleMethod\\(1\\.0\\)" + LINES)); } } /** * Method used in doubleMethodTest. */ private void doubleMethod(double d) {} /** * Tests the CallMonitor listener on an app containing a call to doubleMethod. */ @Test public void doubleMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { doubleMethod(1.0); } else { System.setOut(out); assertTrue("Invocation of doubleMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.doubleMethod\\(1\\.0\\)" + LINES)); } } /** * Method used in staticNullMethodTest */ private static void staticNullMethod(Object o) {} /** * Tests the CallMonitor listener on an app containing a call to staticNullMethod. */ @Test public void staticNullMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { staticNullMethod(null); } else { System.setOut(out); assertTrue("Invocation of staticNullMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.staticNullMethod\\(null\\)" + LINES)); } } /** * Method used in nullMethodTest. */ private void nullMethod(Object o) {} /** * Tests the CallMonitor listener on an app containing a call to nullMethod. */ @Test public void nullMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { nullMethod(null); } else { System.setOut(out); assertTrue("Invocation of nullMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.nullMethod\\(null\\)" + LINES)); } } /** * Method used in staticObjectMethodTest. */ private static void staticObjectMethod(Object o) {} /** * Tests the CallMonitor listener on an app containing a call to staticObjectMethod. */ @Test public void staticObjectMethodTest() { PrintStream out = System.out; ByteArrayOutputStream stream = new ByteArrayOutputStream(); System.setOut(new PrintStream(stream)); if (verifyNoPropertyViolation(CONFIGURATION)) { staticObjectMethod(new Object()); } else { System.setOut(out); assertTrue("Invocation of staticObjectMethod incorrect", stream.toString().matches(LINES + "0:.*CallMonitorTest\\.staticObjectMethod\\(java\\.lang\\.Object.*\\)" + LINES)); } } /** * 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); } }