001: /*
002: * $URL: DatabaseTest.java $
003: * $Rev: 3582 $
004: * $Date: 2007-11-25 14:29:06 +0300 (Ð’Ñ?, 25 ноÑ? 2007) $
005: *
006: * Copyright 2005 Netup, Inc. All rights reserved.
007: * URL: http://www.netup.biz
008: * e-mail: info@netup.biz
009: */
010:
011: package org.garret.perst;
012:
013: import junit.framework.*;
014:
015: import java.util.Iterator;
016:
017: /**
018: * These tests verifies an functionality of the <code>Database</code> class.
019: */
020: public class DatabaseTest extends TestCase {
021:
022: Storage storage;
023: Database database;
024:
025: public DatabaseTest(String testName) {
026: super (testName);
027: }
028:
029: public static junit.framework.Test suite() {
030: junit.framework.TestSuite suite = new junit.framework.TestSuite(
031: DatabaseTest.class);
032:
033: return suite;
034: }
035:
036: protected void setUp() throws java.lang.Exception {
037: storage = StorageFactory.getInstance().createStorage();
038: storage.open(new NullFile(), Storage.INFINITE_PAGE_POOL);
039: database = new Database(storage);
040: }
041:
042: protected void tearDown() throws java.lang.Exception {
043: if (storage.isOpened())
044: storage.close();
045: }
046:
047: /**
048: * <B>Goal:</B> To verify the functionality of the <CODE>createTable(...)</CODE> method.
049: * <P>
050: * <B>Conditions:</B>
051: * <ul>
052: * <li><code>createTable(Stored.class)</code> is invoked twice.</li>
053: * <li><code>Stored</code> class implements the <code>Persistent</code>
054: * interface.</li>
055: * </ul>
056: * <P>
057: * <B>Expected result:</B>
058: * <ul>
059: * <li>no exceptions are thrown.</li>
060: * <li>The first invocation of <code>createTable(...)</code> returned <i>true</i> and the
061: second invocation returned <i>false</i>.</li>
062: * </ul>
063: */
064: public void testCreateTable00() {
065: // test target
066: assertTrue(database.createTable(Stored.class));
067: assertFalse(database.createTable(Stored.class));
068: }
069:
070: /**
071: * <B>Goal:</B> To verify the functionality of the
072: * <CODE>addRecord(...)</CODE> method.
073: * <P>
074: * <B>Conditions:</B>
075: * <ul>
076: * <li>the Perst is able to store the <code>persistent</code> object.</li>
077: * <li>the <code>addRecord(null)</code>method is invoked.</li>
078: * </ul>
079: * <P>
080: * <B>Expected result:</B>
081: * <ul>
082: * <li><code>NullPointerException</code> was thrown.</li>
083: * </ul>
084: */
085: public void testAddRecord00() {
086: assertTrue(database.createTable(Stored.class));
087: try {
088: //test target
089: database.addRecord(null);
090: fail("NullPointerExceptions expected");
091: } catch (NullPointerException e) {
092: // expected exception
093: }
094: }
095:
096: /**
097: * <B>Goal:</B> To verify the functionality of the
098: * <CODE>addRecord(...)</CODE> method.
099: * <P>
100: * <B>Conditions:</B>
101: * <ul>
102: * <li>the Perst is able to store the <code>persistent</code> object.</li>
103: * <li>the <code>addRecord(persistent)</code>method is invoked.</li>
104: * <li><code>persistent</code> implements the <code>Persistent</code>
105: * interface.</li>
106: * </ul>
107: * <P>
108: * <B>Expected result:</B>
109: * <ul>
110: * <li>no exceptions are thrown.</li>
111: * </ul>
112: */
113: public void testAddRecord01() {
114: assertTrue(database.createTable(Stored.class));
115: // test target
116: database.addRecord(new Stored("asdf"));
117: }
118:
119: /**
120: * <b>Goal:</b> To verify the functionality of the <CODE>addRecode(...)</CODE>
121: * and <CODE>getRecords(...)</CODE> methods.
122: * <P>
123: * <B>Conditions:</B>
124: * <ul>
125: * <li>the Perst is able to store the <code>persistent</code> object.</li>
126: * <li><code>Stored</code> class implements the <code>Persistent</code>
127: * interface.</li>
128: * <li><code>createTable(Stored.class)</code> is invoked.</li>
129: * <li><code>addRecord(new Stored(...))</code> is invoked.</li>
130: * <li><code>getRecords(Stored.class)</code> is invoked.</li>
131: * </ul>
132: * <B>Expected result:</B>
133: * <ul>
134: * <li>no exceptions are thrown.</li>
135: * <li><code>getRecords(...)</code> returned added record.</li>
136: * </ul>
137: */
138: public void testAddRecordGetRecords() {
139: assertTrue(database.createTable(Stored.class));
140: Stored st = new Stored("qwe");
141: database.addRecord(st);
142: // test target
143: Iterator<IPersistent> i = database.getRecords(Stored.class);
144: assertEquals(st, i.next());
145: assertFalse(i.hasNext());
146: }
147:
148: /**
149: * <b>Goal:</b> To verify the functionality of the <CODE>addRecod(...)</CODE>,
150: * <CODE>deleteRecod(...)</CODE> and <CODE>getRecords(...)</CODE> methods.
151: * <P>
152: * <B>Conditions:</B>
153: * <ul>
154: * <li><code>createTable(Stored.class)</code> is invoked.</li>
155: * <li><code>stored</code> object implements the <code>Persistent</code>
156: * interface.</li>
157: * <li><code>addRecord(stored)</code> is invoked.</li>
158: * <li><code>deleteRecord(stored)</code> is invoked.</li>
159: * <li><code>getRecords(Stored.class)</code> is invoked.</li>
160: * </ul>
161: * <P>
162: * <B>Expected result:</B>
163: * <ul>
164: * <li>no exceptions are thrown.</li>
165: * <li><code>getRecords(...)</code> returned empty set.</li>
166: * </ul>
167: */
168: public void testAddRecordDeleteRecordGetRecords() {
169: assertTrue(database.createTable(Stored.class));
170: Stored st = new Stored("qwe");
171: database.addRecord(st);
172: database.deleteRecord(st);
173: Iterator<IPersistent> i = database.getRecords(Stored.class);
174: assertFalse(i.hasNext());
175: }
176:
177: /**
178: * Internal class
179: */
180: private static class Stored extends Persistent {
181: public String name;
182:
183: Stored(String name) {
184: this .name = name;
185: }
186:
187: public Stored() {
188: }
189: }
190:
191: }
|