1 29 30 package nextapp.echo2.webcontainer.test; 31 32 import java.io.ByteArrayInputStream ; 33 import java.io.ByteArrayOutputStream ; 34 import java.io.ObjectInputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.io.Serializable ; 37 38 import nextapp.echo2.app.ApplicationInstance; 39 import nextapp.echo2.app.RenderIdSupport; 40 import nextapp.echo2.webcontainer.util.IdTable; 41 import junit.framework.TestCase; 42 43 46 public class IdTableTest extends TestCase { 47 48 private static class TestObject 49 implements RenderIdSupport, Serializable { 50 51 public String id = ApplicationInstance.generateSystemId(); 52 53 56 public String getRenderId() { 57 return id; 58 } 59 } 60 61 67 public void testReferenceRelease() { 68 IdTable idManager = new IdTable(); 69 TestObject testObject = new TestObject(); 70 String id = testObject.getRenderId(); 71 idManager.register(testObject); 72 assertNotNull(idManager.getObject(id)); 73 74 testObject = null; 75 for (int i = 0; i < 10; ++i) { 76 System.gc(); 77 } 78 79 assertNull(idManager.getObject(id)); 80 } 81 82 85 public void testSerialization() 86 throws Exception { 87 IdTable idTable = new IdTable(); 88 TestObject testObject = new TestObject(); 89 String id = testObject.getRenderId(); 90 idTable.register(testObject); 91 92 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 93 ObjectOutputStream objectOut = new ObjectOutputStream (byteOut); 94 objectOut.writeObject(idTable); 95 objectOut.close(); 96 97 byte[] data = byteOut.toByteArray(); 98 99 ByteArrayInputStream byteIn = new ByteArrayInputStream (data); 100 ObjectInputStream objectIn = new ObjectInputStream (byteIn); 101 IdTable newIdTable = (IdTable) objectIn.readObject(); 102 TestObject newTestObject = (TestObject) newIdTable.getObject(id); 103 assertEquals(id, newTestObject.getRenderId()); 104 objectIn.close(); 105 106 testObject = null; 107 for (int i = 0; i < 10; ++i) { 108 System.gc(); 109 } 110 111 assertNull(idTable.getObject(id)); 112 assertNotNull(newIdTable.getObject(id)); 113 114 newTestObject = null; 115 for (int i = 0; i < 10; ++i) { 116 System.gc(); 117 } 118 119 assertNull(newIdTable.getObject(id)); 120 } 121 } 122 | Popular Tags |