1 package com.tirsen.nanning.samples.prevayler; 2 3 import java.io.*; 4 5 import com.tirsen.nanning.Aspects; 6 import com.tirsen.nanning.config.AspectSystem; 7 import com.tirsen.nanning.config.ClassIntroductor; 8 import junit.framework.TestCase; 9 10 public class BasicIdentifyingSystemTest extends TestCase { 11 private BasicIdentifyingSystem basicIdentifyingSystem; 12 private Object registredObject; 13 14 public static interface Interface {} 15 public static class Implementation implements Interface, Serializable {} 16 17 protected void setUp() throws Exception { 18 super.setUp(); 19 20 AspectSystem aspectSystem = new AspectSystem(); 21 aspectSystem.addAspect(new ClassIntroductor(Interface.class, Implementation.class)); 22 Aspects.setContextAspectFactory(aspectSystem); 23 24 registredObject = Aspects.getCurrentAspectFactory().newInstance(Interface.class); 25 26 basicIdentifyingSystem = new BasicIdentifyingSystem(); 27 28 CurrentPrevayler.enterTransaction(); 29 basicIdentifyingSystem.registerObjectID(registredObject); 30 CurrentPrevayler.exitTransaction(); 31 } 32 33 public void testSerialization() throws IOException, ClassNotFoundException { 34 ByteArrayOutputStream data = new ByteArrayOutputStream(); 35 ObjectOutputStream out = new ObjectOutputStream(data); 36 out.writeObject(basicIdentifyingSystem); 37 out.close(); 38 39 ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data.toByteArray())); 40 BasicIdentifyingSystem readSystem = (BasicIdentifyingSystem) in.readObject(); 41 42 assertEquals(1, readSystem.getAllRegisteredObjects().size()); 43 Object readObject = readSystem.getAllRegisteredObjects().iterator().next(); 44 assertTrue(readSystem.hasObjectID(readObject)); 45 assertEquals(basicIdentifyingSystem.getObjectID(registredObject), readSystem.getObjectID(readObject)); 46 assertSame(readObject, readSystem.getObjectWithID(readSystem.getObjectID(readObject))); 47 } 48 } 49 | Popular Tags |