1 7 package ch.ethz.prose.crosscut; 8 9 import java.lang.reflect.Method ; 11 import java.util.*; 12 13 import junit.framework.*; 14 import ch.ethz.prose.ProseSystem; 15 import ch.ethz.prose.filter.PointCutter; 16 import ch.ethz.prose.filter.Within; 17 18 24 public 25 class FunctionalCrosscutTest extends TestCase { 26 27 class FunctionalCrosscutExample extends MethodCut 29 { 30 void METHOD_ARGS(ANY thisObject,REST params) 31 { 32 System.err.println(">>>" + thisObject.getObject() + "/" + params.getObject()); 33 } 34 35 protected PointCutter pointCutter() 36 { 37 return null; 38 } 39 } 40 41 45 public FunctionalCrosscutTest(String name) 46 { 47 super(name); 48 } 49 50 List lastEACall; 51 MethodCut exampleCrosscut1 = null; 52 MethodCut exampleCrosscut2 = null; 53 MethodCut exampleCrosscut3 = null; 54 MethodCut exampleCrosscut4 = null; 55 MethodCut exampleCrosscut5 = null; 56 MethodCut exampleCrosscut6 = null; 57 58 61 protected void setUp() { 62 try 63 { 64 exampleCrosscut1 = new FunctionalCrosscutExample(); 65 66 exampleCrosscut2 = new MethodCut() { 67 protected PointCutter pointCutter() 68 { 69 return Within.method("someMethod"); 70 } 71 72 public void METHOD_ARGS(ANY thisObject, REST params) { } 73 }; 74 75 exampleCrosscut4 = new MethodCut() { 76 public void METHOD_ARGS(String receiver,REST params) { } 77 78 protected PointCutter pointCutter() 79 { 80 return Within.method("someOtherMethod") ; 81 } 82 }; 83 84 exampleCrosscut5 = new MethodCut() { 85 public void METHOD_ARGS(Vector receiver,int param) { } 86 87 protected PointCutter pointCutter() 88 { 89 return null; 90 } 91 }; 92 93 exampleCrosscut6 = new MethodCut() { 94 public void METHOD_ARGS(Vector receiver,int param, REST theRestArgs) { } 95 96 protected PointCutter pointCutter() 97 { 98 return null; 99 } 100 }; 101 102 ProseSystem.startup(); 103 } 104 catch ( Throwable e ) 105 { 106 e.printStackTrace(); 107 Assert.fail("ProseSystem.startup() failed."); 108 } 109 110 try 111 { 112 exampleCrosscut1.insertionAction(true); 113 exampleCrosscut2.insertionAction(true); 114 exampleCrosscut4.insertionAction(true); 115 exampleCrosscut5.insertionAction(true); 116 exampleCrosscut6.insertionAction(true); 117 } 118 catch (Exception e) 119 { 120 } 124 125 lastEACall = null; 126 } 127 128 protected void tearDown() 129 { 130 try 131 { ProseSystem.teardown(); } 132 catch (Exception e) 133 { 134 Assert.fail("ProseSystem.teardown() failed"); } 135 } 136 137 public void testAdviceMethod() 138 { 139 Method meth1 = ((MethodCutSignaturePattern)(exampleCrosscut1.adviceSignature)).methodObj; 140 assertNotNull(meth1); 141 assertEquals(meth1.getName(),"METHOD_ARGS"); 142 Method meth2 = ((MethodCutSignaturePattern)(exampleCrosscut2.adviceSignature)).methodObj; 143 assertNotNull(meth2); 144 145 Exception thrownException = null; 146 try 147 { 148 exampleCrosscut3 = new MethodCut() 149 { 150 protected PointCutter pointCutter() 151 { 152 return null; 153 } 154 }; 155 } 156 catch (MissingInformationException e) 157 { 158 thrownException=e; 159 } 160 161 assertNotNull("thrown exception should be non-null",thrownException); 162 } 163 164 public void testPotentialCroscutClasses() 165 { 166 assertTrue("ANY matches at least 100 classes",exampleCrosscut1.potentialCrosscutClasses().length > 100); 167 assertTrue(exampleCrosscut4.potentialCrosscutClasses()[0]==String .class); 168 } 169 170 public void testDoCreateRequest() throws Exception 171 { 172 try 173 { 174 CrosscutRequest cr1 = exampleCrosscut1.doCreateRequest(Vector.class); 175 assertTrue("list of public method in vector in non-empty", cr1.size() > 0); 176 177 CrosscutRequest cr5 = exampleCrosscut5.doCreateRequest(Vector.class); 178 179 CrosscutRequest cr6 = exampleCrosscut6.doCreateRequest(Vector.class); 180 181 assertTrue("more methods in cr6",cr6.size() > cr5.size()); 182 } 183 catch(Exception e) 184 { 185 e.printStackTrace(); 186 throw e; 187 } 188 } 189 190 public void exampleAdvice(ANY c, int a, REST params) 191 { 192 lastEACall = new Vector(); 193 lastEACall.add(c.getObject()); 194 lastEACall.add(new Integer (a)); 195 lastEACall.add(params.getObject()); 196 } 197 198 199 public void exampleLocalCall(int a, String b, Date c) 200 { 201 } 202 203 204 207 public void _testDoCreateLocalAction() throws Exception 208 { 209 try 210 { 211 Method eaMethod = getClass().getMethod("exampleAdvice",new Class []{ANY.class,Integer.TYPE,REST.class}); 212 assertNotNull(eaMethod); 213 Object [] localParams = new Object []{"RECEIVER",new Integer (5),"hahah", new Date()}; 214 215 220 assertEquals(lastEACall.get(0),"RECEIVER"); 221 assertEquals(lastEACall.get(1),new Integer (5)); 222 assertEquals(((Object [])(lastEACall.get(2)))[0],"hahah"); 223 } 224 catch (Exception e) 225 { 226 e.printStackTrace(); 227 throw e; 228 } 229 } 230 231 236 public static 237 Test suite() 238 { 239 return new TestSuite(FunctionalCrosscutTest.class); 240 } 241 } 242 243 244 | Popular Tags |