001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.webcontainer.test;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.ObjectInputStream;
035: import java.io.ObjectOutputStream;
036: import java.io.Serializable;
037:
038: import nextapp.echo2.app.ApplicationInstance;
039: import nextapp.echo2.app.RenderIdSupport;
040: import nextapp.echo2.webcontainer.util.IdTable;
041: import junit.framework.TestCase;
042:
043: /**
044: *
045: */
046: public class IdTableTest extends TestCase {
047:
048: private static class TestObject implements RenderIdSupport,
049: Serializable {
050:
051: public String id = ApplicationInstance.generateSystemId();
052:
053: /**
054: * @see nextapp.echo2.app.RenderIdSupport#getRenderId()
055: */
056: public String getRenderId() {
057: return id;
058: }
059: }
060:
061: /**
062: * Tests to ensure references are released. This test relies on
063: * <code>System.gc()</code> actually causing weak references to
064: * be collected. It is not technically safe to rely on this,
065: * thus this test may randomly fail.
066: */
067: public void testReferenceRelease() {
068: IdTable idManager = new IdTable();
069: TestObject testObject = new TestObject();
070: String id = testObject.getRenderId();
071: idManager.register(testObject);
072: assertNotNull(idManager.getObject(id));
073:
074: testObject = null;
075: for (int i = 0; i < 10; ++i) {
076: System.gc();
077: }
078:
079: assertNull(idManager.getObject(id));
080: }
081:
082: /**
083: * Tests serialization of an <code>IdTable</code>.
084: */
085: public void testSerialization() throws Exception {
086: IdTable idTable = new IdTable();
087: TestObject testObject = new TestObject();
088: String id = testObject.getRenderId();
089: idTable.register(testObject);
090:
091: ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
092: ObjectOutputStream objectOut = new ObjectOutputStream(byteOut);
093: objectOut.writeObject(idTable);
094: objectOut.close();
095:
096: byte[] data = byteOut.toByteArray();
097:
098: ByteArrayInputStream byteIn = new ByteArrayInputStream(data);
099: ObjectInputStream objectIn = new ObjectInputStream(byteIn);
100: IdTable newIdTable = (IdTable) objectIn.readObject();
101: TestObject newTestObject = (TestObject) newIdTable
102: .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: }
|