| 1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.io.*; 4 import java.lang.reflect.Method ; 5 import java.util.Collection ; 6 import java.util.IdentityHashMap ; 7 8 import com.tirsen.nanning.AspectFactory; 9 import com.tirsen.nanning.AspectInstance; 10 import com.tirsen.nanning.Aspects; 11 import com.tirsen.nanning.MixinInstance; 12 import com.tirsen.nanning.attribute.AbstractAttributesTest; 13 import com.tirsen.nanning.attribute.Attributes; 14 import com.tirsen.nanning.config.AspectSystem; 15 import com.tirsen.nanning.config.FindTargetMixinAspect; 16 import org.apache.commons.collections.CollectionUtils; 17 import org.apache.commons.collections.Predicate; 18 import org.prevayler.implementation.CheckpointPrevayler; 19 20 public class PrevaylerTest extends AbstractAttributesTest { 21 22 private AspectFactory aspectFactory; 23 24 private File prevaylerDir; 25 26 protected void setUp() throws Exception { 27 super.setUp(); 28 29 AspectSystem aspectSystem = new AspectSystem(); 30 aspectSystem.addAspect(new FindTargetMixinAspect()); 31 aspectSystem.addAspect(new PrevaylerAspect()); 32 33 aspectFactory = aspectSystem; 34 Aspects.setContextAspectFactory(aspectFactory); 35 36 prevaylerDir = File.createTempFile("test", ""); 37 prevaylerDir.delete(); 38 prevaylerDir.mkdirs(); 39 prevaylerDir.deleteOnExit(); 40 } 41 42 protected void tearDown() throws Exception { 43 prevaylerDir.delete(); 44 } 45 46 public void test() throws IOException, ClassNotFoundException { 47 final CountingPrevayler prevayler = newPrevayler(); 48 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 49 public void run() { 50 MyObject insideObject = currentMySystem().createMyObject(); 51 prevayler.assertNumberOfCommands("create should not result in a command", 0); 52 currentMySystem().setMyObject(insideObject); 53 prevayler.assertNumberOfCommands("set should result in one command only", 1); 54 55 insideObject.setValue("oldValue"); 56 prevayler.assertNumberOfCommands(2); 57 insideObject.setValue("newValue"); 58 prevayler.assertNumberOfCommands(3); 59 60 MyObject outsideObject = (MyObject) aspectFactory.newInstance(MyObject.class); 61 prevayler.assertNumberOfCommands("no command when object created outside prevayler", 3); 62 63 MyObject outsideNestedObject = (MyObject) aspectFactory.newInstance(MyObject.class); 64 prevayler.assertNumberOfCommands("no command when object created outside prevayler", 3); 65 66 outsideObject.setMyObject(outsideNestedObject); 67 prevayler.assertNumberOfCommands("commands operating on objects outside prevayler still generates " + 68 "commands (they shouldn't really but we haven't implemented that yet)", 69 4); 70 71 76 insideObject.setMyObject(outsideObject); 77 prevayler.assertNumberOfCommands( 78 "command when operating on object inside prevayler with object outside prevayler", 5); 79 80 Collection objects = currentMySystem().getAllObjects(); 81 assertEquals("objects not created ", 3, objects.size()); 82 final MyObject objectToFind = insideObject; 83 insideObject = (MyObject) CollectionUtils.find(objects, new Predicate() { 84 public boolean evaluate(Object o) { 85 return o == objectToFind; 86 } 87 }); 88 assertNotNull(insideObject); 89 assertEquals("value not correct", "newValue", insideObject.getValue()); 90 assertNotNull(insideObject.getMyObject()); 91 } 92 }); 93 94 checkMySystem(); 96 97 ((CheckpointPrevayler) prevayler.getWrappedPrevayler()).checkpoint(); 99 checkMySystem(); 100 } 101 102 private void checkMySystem() throws IOException, ClassNotFoundException { 103 final CountingPrevayler prevayler = newPrevayler(); 104 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 105 public void run() { 106 Collection objects = currentMySystem().getAllObjects(); 107 assertEquals("objects not persisted", 3, objects.size()); 108 MyObject myObject = currentMySystem().getMyObject(); 109 assertEquals("property not correct value", "newValue", myObject.getValue()); 110 prevayler.assertNumberOfCommands("just checking, should be no commands", 0); 111 112 } 117 }); 118 } 119 120 public void testABC() throws ClassNotFoundException , IOException, NoSuchMethodException { 121 AspectInstance aspectInstance = Aspects.getAspectInstance(aspectFactory.newInstance(MyObject.class)); 122 MixinInstance mixinInstance = (MixinInstance) aspectInstance.getMixins().iterator().next(); 123 124 Method setValue = MyObject.class.getDeclaredMethod("setValue", new Class []{String .class}); 125 assertEquals(2, mixinInstance.getInterceptorsForMethod(setValue).size()); 126 127 Method setABC = MyObject.class.getDeclaredMethod("setABC", new Class []{String [].class}); 128 assertTrue(Attributes.getAttributes(MyObject.class).hasAttribute(setABC, "transaction")); 129 assertTrue(Attributes.hasAttribute(setABC, "transaction")); 130 assertEquals(2, mixinInstance.getInterceptorsForMethod(setABC).size()); 131 132 final CountingPrevayler prevayler = newPrevayler(); 133 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 134 public void run() { 135 MyObject myObject = currentMySystem().createMyObject(); 136 prevayler.assertNumberOfCommands(0); 137 myObject.setABC(null); 138 prevayler.assertNumberOfCommands(1); 139 } 140 }); 141 } 142 143 public void testGarbageCollect() throws IOException, ClassNotFoundException { 144 CountingPrevayler prevayler = newPrevayler(); 145 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 146 public void run() { 147 MyObject myObject = (MyObject) aspectFactory.newInstance(MyObject.class); 148 currentMySystem().setMyObject(myObject); 149 myObject.setMyObject((MyObject) aspectFactory.newInstance(MyObject.class)); 150 assertEquals("two MyObjects should have been created", 2, currentMySystem().getAllObjects().size()); 151 } 152 }); 153 154 ((CheckpointPrevayler) prevayler.getWrappedPrevayler()).checkpoint(); 156 prevayler = newPrevayler(); 157 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 158 public void run() { 159 assertEquals("two MyObjects should be left", 2, currentMySystem().getAllObjects().size()); 160 assertNotNull(currentMySystem().getMyObject().getMyObject()); 162 currentMySystem().getMyObject().setMyObject(null); 163 } 164 }); 165 166 ((CheckpointPrevayler) prevayler.getWrappedPrevayler()).checkpoint(); 168 169 prevayler = newPrevayler(); 170 CurrentPrevayler.withPrevayler(prevayler, new Runnable () { 171 public void run() { 172 assertNull(currentMySystem().getMyObject().getMyObject()); 173 assertEquals("1 MyObjects should be left, one garbage collected", 1, currentMySystem().getAllObjects().size()); 174 } 175 }); 176 } 177 178 public void testSerialization() throws IOException, ClassNotFoundException { 179 MyObject myObject = (MyObject) aspectFactory.newInstance(MyObject.class); 180 myObject.setValue("value"); 181 IdentityHashMap identityHashMap = new IdentityHashMap (); 182 identityHashMap.put(myObject, new Long (1)); 183 184 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 185 ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); 186 objectOutputStream.writeObject(identityHashMap); 187 objectOutputStream.close(); 188 ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray())); 189 identityHashMap = (IdentityHashMap ) objectInputStream.readObject(); 190 191 myObject = (MyObject) identityHashMap.keySet().iterator().next(); 192 assertEquals("value", myObject.getValue()); 193 } 194 195 212 private static MySystem currentMySystem() { 213 return (MySystem) CurrentPrevayler.getSystem(); 214 } 215 216 private CountingPrevayler newPrevayler() throws IOException, ClassNotFoundException { 217 CountingPrevayler prevayler = new CountingPrevayler(new CheckpointPrevayler(aspectFactory.newInstance(MySystem.class), 218 prevaylerDir.getAbsolutePath())); 219 return prevayler; 220 } 221 222 public void testUnsupportedTransaction() { 223 assertTrue(CheckTransactionUnsupportedInterceptor.isTransactionsSupported()); 224 TestUnsupportedTransaction testUnsupportedTransaction = 225 (TestUnsupportedTransaction) aspectFactory.newInstance(TestUnsupportedTransaction.class); 226 try { 227 testUnsupportedTransaction.callWithUnsupportedTransaction(); 228 fail(); 229 } catch (IllegalStateException shouldHappen) { 230 } 231 assertTrue(CheckTransactionUnsupportedInterceptor.isTransactionsSupported()); 232 } 233 234 public void testIsTransactional() throws NoSuchMethodException { 235 assertFalse(PrevaylerInterceptor.isTransactional(null)); 236 assertFalse(PrevaylerInterceptor.isTransactional(Object .class)); 237 assertTrue(PrevaylerInterceptor.isTransactional(MySystem.class)); 238 assertFalse(PrevaylerInterceptor.transactionalReturnValue( 239 MySystem.class.getMethod("setMyObject", new Class []{MyObject.class}))); 240 assertTrue(PrevaylerInterceptor.transactionalReturnValue(MySystem.class.getMethod("createMyObject", null))); 241 } 242 } 243 | Popular Tags |