1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 6 import com.tirsen.nanning.attribute.AbstractAttributesTest; 7 import com.tirsen.nanning.config.AspectSystem; 8 import com.tirsen.nanning.config.FindTargetMixinAspect; 9 10 import com.tirsen.nanning.samples.prevayler.BasicIdentifyingSystem; 11 12 public class IdentifyingMarshallerTest extends AbstractAttributesTest { 13 private IdentifyingMarshaller marshaller; 14 private long entityId; 15 private MyObject entity; 16 private AspectSystem aspectSystem; 17 private TestSystem system; 18 19 protected void setUp() throws Exception { 20 super.setUp(); 21 aspectSystem = new AspectSystem(); 22 aspectSystem.addAspect(new FindTargetMixinAspect()); 23 24 system = new TestSystem(); 25 entity = (MyObject) aspectSystem.newInstance(MyObject.class); 26 system.setEntity(entity); 27 CurrentPrevayler.enterTransaction(); 28 entityId = system.registerObjectID(entity); 29 CurrentPrevayler.exitTransaction(); 30 31 marshaller = new IdentifyingMarshaller(); 32 CurrentPrevayler.setSystem(system); 33 } 34 35 protected void tearDown() throws Exception { 36 CurrentPrevayler.setSystem(null); 37 while (CurrentPrevayler.isInTransaction()) { 38 CurrentPrevayler.exitTransaction(); 39 } 40 super.tearDown(); 41 } 42 43 public void testMarshalNull() { 44 testMarshalAndUnMarshalPrimitive(null); 45 } 46 47 public void testMarshalString() { 48 testMarshalAndUnMarshalPrimitive("blahonga"); 49 } 50 51 public void testMarshalInt() { 52 testMarshalAndUnMarshalPrimitive(new Integer (5)); 53 } 54 55 public void testMarshalDate() { 56 testMarshalAndUnMarshalPrimitive(new Date ()); 57 } 58 59 public void testMarshalList() { 60 testMarshalAndUnMarshalPrimitive(new ArrayList ()); 61 } 62 63 public void testMarshalRegisteredEntity() { 64 Identity identity = (Identity) marshaller.marshal(entity); 65 assertEquals("Marshaller generated invalid id", entityId, ((Long ) identity.getIdentifier()).longValue()); 66 assertEquals("Wrong class in identity", MyObject.class, identity.getObjectClass()); 67 68 assertSame("Entity unmarshalled incorrectly", entity, marshaller.unmarshal(identity)); 69 } 70 71 public void testMarshalUnregisteredEntity() { 72 Object object = aspectSystem.newInstance(MyObject.class); 73 assertSame("Unregistered entity should be marshalled by value", object, marshaller.marshal(object)); 74 assertFalse(system.hasObjectID(object)); 75 CurrentPrevayler.enterTransaction(); 76 assertSame("Unregistered entity should be marshalled by value", object, marshaller.unmarshal(object)); 77 CurrentPrevayler.exitTransaction(); 78 assertTrue(system.hasObjectID(object)); 79 } 80 81 public void testUnmarshalUnregisteredGraph() { 82 MyObject object1 = (MyObject) aspectSystem.newInstance(MyObject.class); 83 MyObject object2 = (MyObject) aspectSystem.newInstance(MyObject.class); 84 object1.setMyObject(object2); 85 86 marshaller.marshal(object1); 87 assertFalse(system.hasObjectID(object1)); 88 assertFalse(system.hasObjectID(object2)); 89 90 CurrentPrevayler.enterTransaction(); 91 marshaller.unmarshal(object1); 92 CurrentPrevayler.exitTransaction(); 93 assertTrue(system.hasObjectID(object1)); 94 assertTrue(system.hasObjectID(object2)); 95 } 96 97 public void TODOtestUnmarshalMixedGraph() { 98 this.getClass().getClassLoader().setDefaultAssertionStatus(true); 99 this.getClass().getClassLoader().setClassAssertionStatus(IdentifyingMarshaller.class.getName(), true); 100 101 MyObject object1 = (MyObject) aspectSystem.newInstance(MyObject.class); 102 MyObject object2 = (MyObject) aspectSystem.newInstance(MyObject.class); 103 object1.setMyObject(object2); 104 105 CurrentPrevayler.enterTransaction(); 106 system.registerObjectID(object2); 107 108 marshaller.marshal(object1); 109 assertFalse(system.hasObjectID(object1)); 110 assertTrue(system.hasObjectID(object2)); 111 112 try { 113 marshaller.unmarshal(object1); 114 fail("Should not be able to unmarshal a partially registered graph"); 115 } catch (AssertionError shouldHappen) { 116 } 117 } 118 119 public static class TestSystem extends BasicIdentifyingSystem { 120 private MyObject entity; 121 122 public MyObject getEntity() { 123 return entity; 124 } 125 126 public void setEntity(MyObject entity) { 127 this.entity = entity; 128 } 129 } 130 131 private void testMarshalAndUnMarshalPrimitive(Object object) { 132 assertSame("A non entity should not be identified", object, marshaller.marshal(object)); 133 assertSame("A non entity should not be identified", object, marshaller.unmarshal(object)); 134 assertFalse("Primitive should not become registered", system.hasObjectID(object)); 135 } 136 } 137 | Popular Tags |