001: /**
002: * JDBM LICENSE v1.00
003: *
004: * Redistribution and use of this software and associated documentation
005: * ("Software"), with or without modification, are permitted provided
006: * that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain copyright
009: * statements and notices. Redistributions must also contain a
010: * copy of this document.
011: *
012: * 2. Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and the
014: * following disclaimer in the documentation and/or other
015: * materials provided with the distribution.
016: *
017: * 3. The name "JDBM" must not be used to endorse or promote
018: * products derived from this Software without prior written
019: * permission of Cees de Groot. For written permission,
020: * please contact cg@cdegroot.com.
021: *
022: * 4. Products derived from this Software may not be called "JDBM"
023: * nor may "JDBM" appear in their names without prior written
024: * permission of Cees de Groot.
025: *
026: * 5. Due credit should be given to the JDBM Project
027: * (http://jdbm.sourceforge.net/).
028: *
029: * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
033: * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040: * OF THE POSSIBILITY OF SUCH DAMAGE.
041: *
042: * Copyright 2000 (C) Cees de Groot. All Rights Reserved.
043: * Contributions are Copyright (C) 2000 by their associated contributors.
044: *
045: */package jdbm.htree;
046:
047: import jdbm.RecordManager;
048:
049: import jdbm.recman.BaseRecordManager;
050: import jdbm.recman.TestRecordFile;
051:
052: import junit.framework.*;
053:
054: /**
055: * Test cases for HTree rollback
056: */
057: public class TestRollback extends TestCase {
058:
059: public TestRollback(String name) {
060: super (name);
061: }
062:
063: public void setUp() {
064: TestRecordFile.deleteTestFile();
065: }
066:
067: public void tearDown() {
068: TestRecordFile.deleteTestFile();
069: }
070:
071: /**
072: * Test case courtesy of Derek Dick (mailto:ddick@users.sourceforge.net)
073: */
074: public void testRollback1() throws Exception {
075: RecordManager recman;
076: long root;
077:
078: // Note: We start out with an empty file
079: recman = new BaseRecordManager(TestRecordFile.testFileName);
080:
081: root = recman.getNamedObject("xyz");
082:
083: HTree tree = null;
084: if (root == 0) {
085: // create a new one
086: tree = HTree.createInstance(recman);
087: root = tree.getRecid();
088: recman.setNamedObject("xyz", root);
089: recman.commit();
090: } else {
091: tree = HTree.load(recman, root);
092: }
093:
094: tree.put("Foo", "Bar");
095: tree.put("Fo", "Fum");
096:
097: recman.commit();
098:
099: tree.put("Hello", "World");
100:
101: recman.rollback();
102:
103: tree = HTree.load(recman, root);
104: assertTrue(tree.get("Foo").equals("Bar"));
105: assertTrue(tree.get("Fo").equals("Fum"));
106: assertTrue(tree.get("Hello") == null);
107: }
108:
109: /**
110: * Test case courtesy of Derek Dick (mailto:ddick@users.sourceforge.net)
111: */
112: public void testRollback2() throws Exception {
113: RecordManager recman;
114: long root;
115:
116: // Note: We start out with an empty file
117: recman = new BaseRecordManager(TestRecordFile.testFileName);
118:
119: root = recman.getNamedObject("xyz");
120:
121: HTree tree = null;
122: if (root == 0) {
123: // create a new one
124: tree = HTree.createInstance(recman);
125: root = tree.getRecid();
126: recman.setNamedObject("xyz", root);
127: recman.commit();
128: } else {
129: tree = HTree.load(recman, root);
130: }
131:
132: tree.put("hello", "world");
133: tree.put("goodnight", "gracie");
134: recman.commit();
135:
136: tree.put("derek", "dick");
137: recman.rollback();
138:
139: assertTrue(tree.get("derek") == null);
140: assertTrue(tree.get("goodnight").equals("gracie"));
141: assertTrue(tree.get("hello").equals("world"));
142: }
143:
144: /**
145: * Runs all tests in this class
146: */
147: public static void main(String[] args) {
148: junit.textui.TestRunner.run(new TestSuite(TestRollback.class));
149: }
150: }
|