1 8 package test.aj; 9 10 import junit.framework.TestCase; 11 12 15 public class Test extends TestCase { 16 17 private static String s_logString = ""; 18 19 public Test(String name) { 20 super(name); 21 } 22 23 public void testBefore() throws Exception { 24 s_logString = ""; 25 before(); 26 assertEquals("before before() ", s_logString); 27 } 28 29 public void testAfterFinally() throws Exception { 30 s_logString = ""; 31 afterFinally(); 32 assertEquals("afterFinally() after-finally ", s_logString); 33 } 34 35 public void testAfterReturning() throws Exception { 36 s_logString = ""; 37 afterReturning(); 38 assertEquals("afterReturning() after-returning ", s_logString); 39 } 40 41 public void testAfterThrowing() throws Exception { 42 s_logString = ""; 43 try { 44 afterThrowing(); 45 } catch (RuntimeException e) { 46 assertEquals("afterThrowing() after-throwing ", s_logString); 47 return; 48 } 49 fail("RuntimeException should have been catched"); 50 } 51 52 public void testAround() throws Exception { 53 s_logString = ""; 54 around(); 55 assertEquals("before-around around() after-around ", s_logString); 56 } 57 58 public static void main(String [] args) { 59 junit.textui.TestRunner.run(suite()); 60 } 61 62 public static junit.framework.Test suite() { 63 return new junit.framework.TestSuite(Test.class); 64 } 65 66 public static void log(final String wasHere) { 67 s_logString += wasHere; 68 } 69 70 public long around() { 71 log("around() "); 72 return 0x1L; 73 } 74 75 public int before() { 76 log("before() "); 77 return -0x1; 78 } 79 80 public String afterThrowing() { 81 log("afterThrowing() "); 82 throw new RuntimeException (); 83 } 84 85 public Object afterReturning() { 86 log("afterReturning() "); 87 return "string"; 88 } 89 90 public void afterFinally() { 91 log("afterFinally() "); 92 } 93 } 94 | Popular Tags |