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.io.*;
026: import java.util.*;
027: import java.lang.reflect.*;
028: import junit.framework.*;
029: import drjava.smyle.*;
030: import drjava.smyle.testtypes.*;
031: import drjava.smyle.core.*;
032: import org.artsProject.mcop.*;
033: import drjava.util.*;
034:
035: /** tests how DiskStore interacts with the Disk */
036:
037: public class DiskStoreTest extends DiskStoreTestBase {
038: public DiskStoreTest(String name) {
039: super (name);
040: }
041:
042: public void testFailureWhileWritingMaster() throws Exception {
043: store.setWriteLatency(0);
044:
045: // add John
046: table.add(john);
047: snapshot.commitAndContinue();
048:
049: // try to remove John, but don't allow to write master
050: disk.sabotageMasterWrite = true;
051:
052: try {
053: table.clear();
054: snapshot.commit();
055: fail("Where's the exception?");
056: } catch (SmyleIOException e) {
057: // We simulate an application that catches the exception, displays an
058: // error or whatever and goes on
059: }
060: snapshot.forget();
061:
062: // assert there was a proper rollback
063: getImmutableSnapshot();
064: assertEquals(john, table.first());
065: }
066:
067: /** not a real test, but a utility method */
068: /*public void test_printMarshalledDisk() throws Exception {
069: table.add(john);
070: table.add(anne);
071: snapshot.commit();
072: Buffer buffer = new Buffer();
073: disk.marshal(buffer);
074: System.out.println("Disk: "+buffer);
075: }*/
076:
077: void testSerializedStore(String hex) throws Exception {
078: disk = new MemoryDisk(new Buffer(hex));
079:
080: int oldGCFreq = DiskStore.DEFAULT_GC_FREQUENCY;
081: DiskStore.DEFAULT_GC_FREQUENCY = 1;
082: // make sure GC occurs during conversion (exposes a certain bug)
083:
084: try {
085: createStore();
086: } finally {
087: DiskStore.DEFAULT_GC_FREQUENCY = oldGCFreq;
088: }
089:
090: table.add(maria);
091: snapshot.commit();
092: rotate();
093: assertEquals(table.size(), 3);
094: assertEquals(john, table.get(0));
095: assertEquals(anne, table.get(1));
096: assertEquals(maria, table.get(2));
097: }
098:
099: /** load a Smyle 0.1.0 store */
100: public void test010Store() throws Exception {
101: testSerializedStore("00000004000000040000001B00000001000000010000000300000001000000"
102: + "07706572736F6E00000000030000000C000000020000000100000002000000020000000D00000005"
103: + "416E6E650000000015000000010000000D000000054A6F686E00000000170000000400000004");
104: }
105:
106: /** load a Smyle 0.2 store */
107: public void test02Store() throws Exception {
108: testSerializedStore("00000005000000050000001B00000002000000010000000400000001000000"
109: + "07706572736F6E00000000040000001000000001000000020000000200000003000000030000000D"
110: + "00000005416E6E650000000015000000020000000D000000054A6F686E0000000017000000010000"
111: + "004B0000001253746F7265546573743A3A506572736F6E000000000200000007737472696E670000"
112: + "0000056E616D650000000000000000056C6F6E670000000004616765000000000000000000000000"
113: + "0500000005");
114: }
115:
116: /** load a Smyle 0.2.5 store */
117: public void test025Store() throws Exception {
118: testSerializedStore("00000003000000030000005C00000019000000050000000000000002000000"
119: + "000000004B00000000000000020000004B0000000D0000000000000002000000580000000D000000"
120: + "000000000200000065000000100000000000000002000000750000001B0000000500000002000000"
121: + "900000001253746F7265546573743A3A506572736F6E000000000200000007737472696E67000000"
122: + "00056E616D650000000000000000056C6F6E67000000000461676500000000000000000000000005"
123: + "4A6F686E000000001700000005416E6E650000000015000000010000000200000002000000030000"
124: + "001900000001000000040000000100000007706572736F6E00000000010000000C00000019000000"
125: + "00000000000000000300000003");
126: }
127:
128: /** load a Smyle 0.8 store */
129: public void test08Store() throws Exception {
130: testSerializedStore("00000003000000030000003400000123000000020000000300000000000000020000000000"
131: + "0000540000000000000002000000540000000C000000020000000500000002000000600000001F00"
132: + "0000000000000100000000000000400000000000000001000000400000000D000000000000000100"
133: + "00004D0000000D00000000000000010000005A000000180000000000000001000000720000001B00"
134: + "0000010000000100000001000000010000008D00000007506572736F6E0000000002000000077374"
135: + "72696E6700000000056E616D650000000000000000056C6F6E670000000004616765000000000000"
136: + "000000000000054A6F686E000000001700000005416E6E6500000000150000000100000002000000"
137: + "0200000003FFFFFFFF000000000000012300000001000000040000000100000007706572736F6E00"
138: + "0000000300000003");
139: }
140:
141: public void testDiskStoreIsSynchronized() throws Exception {
142: SynchronisationTester.assertPublicMethodsAreSynchronized(store
143: .getClass());
144: }
145:
146: public void testSnapshotIsSynchronized() throws Exception {
147: // Assert that SnapshotImpl methods called by TableImpl are synchronized too
148: SynchronisationTester
149: .assertPackageAndPublicMethodsAreSynchronized(snapshot
150: .getClass());
151: }
152:
153: // This doesn't work anymore because table methods synchronize on
154: // the snapshot now
155: /*public void testTableIsSynchronized() throws Exception {
156: SynchronisationTester.assertPackageAndPublicMethodsAreSynchronized(table.getClass(),
157: new SynchronisationTester.MethodPredicate() {
158: // exclude gj bridge methods
159: public boolean evaluate(Method m) {
160: if (m.getReturnType() == Object.class)
161: return false;
162: Class[] params = m.getParameterTypes();
163: for (int i = 0; i < params.length; i++)
164: if (params[i] == Object.class || params[i] == new Object[0].getClass())
165: return false;
166: return true;
167: }
168: });
169: }*/
170:
171: /*public void testMultipleProcessAccessDetected() throws Exception {
172: }*/
173:
174: public void testDeferredWrites() throws Exception {
175: store.setWriteLatency(50);
176: table.add(maria);
177: snapshot.commit();
178: Thread.sleep(200);
179: createStore();
180: assertEquals(1, table.size());
181: }
182:
183: public void testReadOnlyDisk() throws Exception {
184: tearDown();
185: disk = new MemoryDisk();
186:
187: // Trying to open a non-existing store in read-only mode should fail
188: try {
189: new DiskStore(disk, true);
190: fail("No exception");
191: } catch (StoreNotFoundException e) {
192: // ok
193: }
194:
195: // create a store on disk
196: new DiskStore(disk, false).close();
197:
198: disk.readOnly = true;
199: store = new DiskStore(disk, true);
200:
201: store.snapshot(); // immutable snapshots are ok
202:
203: try {
204: store.mutableSnapshot();
205: fail("No exception");
206: } catch (ReadOnlyException e) {
207: // ok
208: }
209:
210: store.close();
211: }
212: }
|