001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.smyle.tests;
024:
025: import java.util.*;
026: import junit.framework.*;
027: import org.artsProject.mcop.*;
028: import drjava.smyle.core.*;
029:
030: public class ScalablePoolTest extends TestCase {
031: public ScalablePoolTest(String name) {
032: super (name);
033: }
034:
035: ChunkManager cm;
036: ScalablePool<String> pool;
037: int id1, id2;
038:
039: public void setUp() throws Exception {
040: cm = new DefaultChunkManager(new MemoryDisk(),
041: DiskStore.VERSION);
042: pool = new ScalablePool<String>(new StringMarDemar(), cm, null);
043: }
044:
045: public void testAddGetReplace() throws Exception {
046: for (int i = 0; i < 128; i++)
047: assertEquals(i, pool.insert(String.valueOf(i)));
048: for (int i = 0; i < 128; i++)
049: assertEquals(String.valueOf(i), pool.get(i));
050: for (int i = 0; i < 128; i++)
051: if ((i & 1) == 0)
052: pool.replace(i, String.valueOf(i * 2));
053: else
054: pool.release(i);
055:
056: checkAfterAddGetReplace();
057: }
058:
059: void checkAfterAddGetReplace() throws Exception {
060: for (int i = 0; i < 128; i++)
061: assertEquals((i & 1) == 0 ? String.valueOf(i * 2) : null,
062: pool.get(i));
063: assertEquals(128, pool.size());
064: // test get with a very high, not-existing index
065: assertEquals(null, pool.get(123456));
066: pool.checkConsistency();
067: }
068:
069: public void testReleaseWholeBlock() throws Exception {
070: for (int i = 0; i < 128; i++)
071: assertEquals(i, pool.insert(String.valueOf(i)));
072: for (int i = 31; i >= 0; i--)
073: pool.release(i);
074: assertEquals(null, pool.get(0));
075: for (int i = 0; i < 32; i++)
076: assertEquals(i, pool.insert(String.valueOf(i)));
077: }
078:
079: public void testNonExtantIds() {
080: for (int i = -10; i < 10; i++)
081: assertNull(pool.get(i));
082: }
083:
084: public void testInsert() {
085: int id = pool.insert("apple");
086: assertEquals("apple", pool.get(id));
087: }
088:
089: public void testInsertTwoObjects() {
090: id1 = pool.insert("apple");
091: id2 = pool.insert("pear");
092: assertEquals("apple", pool.get(id1));
093: assertEquals("pear", pool.get(id2));
094: }
095:
096: public void testGetIsRepeatable() {
097: testInsertTwoObjects();
098: assertEquals("apple", pool.get(id1));
099: assertEquals("pear", pool.get(id2));
100: }
101:
102: public void testReleasedSlotsContainNull() {
103: int id = pool.insert("banana");
104: pool.release(id);
105: assertNull(pool.get(id));
106: }
107:
108: public void testIdRecycling() {
109: int id = pool.insert("banana");
110: pool.release(id);
111: int id2 = pool.insert("pineapple");
112: assertEquals("ID", id, id2);
113: }
114:
115: /** more complicated recycling test
116: (fails if ids can only be recycled from the end) */
117: public void testIdRecycling2() {
118: testInsertTwoObjects();
119: pool.release(id1);
120: pool.release(id2);
121: int id3 = pool.insert("orange");
122: int id4 = pool.insert("grapefruit");
123: assertEquals("ID 1", id1, id3);
124: assertEquals("ID 2", id2, id4);
125: }
126:
127: /** this is needed for DispatcherTest */
128: public void testFirstIdIsZero() {
129: assertEquals(0, pool.insert("something"));
130: }
131:
132: public void testReplace() {
133: testInsertTwoObjects();
134: pool.replace(id1, "papaya");
135: assertEquals("papaya", pool.get(id1));
136: assertEquals("pear", pool.get(id2));
137: }
138:
139: public void testSaveAndLoad() throws Exception {
140: testAddGetReplace();
141: rotate();
142: checkAfterAddGetReplace();
143: }
144:
145: void rotate() throws Exception {
146: ChunkRef master = pool.flush();
147: pool = new ScalablePool<String>(new StringMarDemar(), cm,
148: master);
149: }
150: }
|