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 HandlesTest extends TestCase {
031: public HandlesTest(String name) {
032: super (name);
033: }
034:
035: class HString implements Handled<HString> {
036: String s;
037: Handle<HString> handle;
038:
039: HString(String s) {
040: this .s = s;
041: }
042:
043: public void setHandle(Handle<HString> handle) {
044: this .handle = handle;
045: }
046: }
047:
048: class HStringMarDemar implements MarDemar<HString> {
049: public void marshal(Buffer b, HString s) {
050: b.writeString(s.s);
051: }
052:
053: public HString read(Buffer b) {
054: return new HString(b.readString());
055: }
056: }
057:
058: Handles<HString> handles;
059: MasterChunkManager cm;
060: BitSet whiteList;
061: HString hRed, hGreen;
062:
063: public void setUp() throws Exception {
064: cm = new DefaultChunkManager(new MemoryDisk(),
065: DiskStore.VERSION);
066: handles = new Handles<HString>(cm, new HStringMarDemar());
067: whiteList = new BitSet();
068: hRed = new HString("red");
069: hGreen = new HString("green");
070: }
071:
072: public void testAddHandle() throws Exception {
073: Handle<HString> handle = handles.add(hRed);
074: assertEquals(handle, hRed.handle);
075: assertEquals("red", handle.get().s);
076: }
077:
078: void rotate() throws Exception {
079: ChunkRef master = handles.flush();
080: BitSet oldWhiteList = (BitSet) whiteList.clone();
081: handles.collectChunks(whiteList);
082: cm.deleteEverythingBut(whiteList);
083: whiteList = oldWhiteList;
084: handles = new Handles<HString>(cm, new HStringMarDemar(),
085: master);
086: }
087:
088: public void testSaveAndReadHandles() throws Exception {
089: Handle<HString> red = handles.add(hRed), green = handles
090: .add(hGreen);
091:
092: // save
093: ChunkRef cRed = cm.createChunk(red), cGreen = cm
094: .createChunk(green);
095: whiteList.set(cRed.index);
096: whiteList.set(cGreen.index);
097:
098: // load
099: rotate();
100: green = handles.read(cm.readChunk(cGreen));
101: red = handles.read(cm.readChunk(cRed));
102:
103: // check
104: assertEquals("red", red.get().s);
105: assertEquals("green", green.get().s);
106: }
107:
108: public void testReplaceHandle() throws Exception {
109: Handle<HString> red = handles.add(hRed);
110: red.set(new HString("blue"));
111: ChunkRef cRed = cm.createChunk(red);
112: whiteList.set(cRed.index);
113: rotate();
114: red = handles.read(cm.readChunk(cRed));
115: assertEquals("blue", red.get().s);
116:
117: red.set(new HString("magenta"));
118: rotate();
119: red = handles.read(cm.readChunk(cRed));
120: assertEquals("magenta", red.get().s);
121: }
122:
123: public void testDisposeLastSlide() throws Exception {
124: Handle<HString> red = handles.add(hRed);
125: ChunkRef cRed = cm.createChunk(red);
126: red.dispose();
127: assertNull(hRed.handle);
128: try {
129: red = handles.read(cm.readChunk(cRed));
130: fail("No exception");
131: } catch (Exception e) {
132: // ok
133: }
134: }
135:
136: public void testDisposeSlide() throws Exception {
137: Handle<HString> red = handles.add(hRed);
138: Handle<HString> green = handles.add(hGreen);
139: Buffer b = new Buffer();
140: red.marshal(b);
141: red.dispose();
142: rotate();
143: try {
144: red = handles.read(b);
145: fail("No exception");
146: } catch (Exception e) {
147: // ok
148: }
149: }
150:
151: public void testInvalidate() throws Exception {
152: Handle<HString> red = handles.add(hRed);
153: handles.flush();
154: hRed.s = "blue";
155: red.invalidate();
156:
157: Buffer b = new Buffer();
158: red.marshal(b);
159: rotate();
160: red = handles.read(b);
161: assertEquals("blue", red.get().s);
162: }
163:
164: /** it must not be possible to assign two handles to one object */
165: // Doesn't work because there's not Handled.getHandle() method */
166: /*public void testHandleUniqueness() throws Exception {
167: Handle<HString> red = handles.add(hRed);
168:
169: try {
170: handles.add(hRed);
171: fail("No exception");
172: } catch (Exception e) {
173: // ok
174: }
175:
176: Handle<HString> green = handles.add(hGreen);
177: try {
178: green.set(hRed);
179: fail("No exception");
180: } catch (Exception e) {
181: // ok
182: }
183: }*/
184: }
|