source: main/trunk/greenstone3/src/packages/javagdbm/java/au/com/pharos/gdbm/GdbmTest.java@ 25159

Last change on this file since 25159 was 25159, checked in by jmt12, 12 years ago

Recoding this file as UTF8 since it contains characters that cause java (1.7u3) to choke otherwise

  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/*
2 * module: pip/java/gdbm -- A Java interface to the GDBM library
3 * class: GdbmTest -- Test suite for this package
4 * Copyright (C) 1997 Pharos IP Pty Ltd
5 * $Id: GdbmTest.java 25159 2012-02-29 00:20:47Z jmt12 $
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22package au.com.pharos.gdbm;
23
24import java.io.PrintStream;
25
26import java.util.Enumeration;
27
28import au.com.pharos.test.Test;
29import au.com.pharos.packing.Packing;
30import au.com.pharos.packing.StringPacking;
31import au.com.pharos.packing.RawPacking;
32
33/** Test harness for the Gdbm library.
34 *
35 * <P>The source of this class forms a useful demonstration of the
36 * JavaGDBM library and is commended to the intending programmer.
37 *
38 * <P>More test cases would be very welcome.
39 *
40 * @see au.com.pharos.gdbm.GdbmFile
41 * @see au.com.pharos.packing.Packing
42 * @author Martin Pool
43 * @version $Revision: 25159 $
44 */
45public class GdbmTest {
46 // Don't instantiate
47 private GdbmTest() { ; }
48
49 private static void testLongData(GdbmFile db)
50 throws Exception
51 {
52 StringBuffer lgBuffer = new StringBuffer();
53 for (int i = 0; i < 100; i++)
54 lgBuffer.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890" +
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890" +
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890");
57
58 String lgString = lgBuffer.toString();
59 lgBuffer = null;
60 String smallKey = "largeDatum";
61
62 db.store(smallKey, lgString);
63 String retr = (String) db.fetch(smallKey);
64
65 Test.ok(9, retr.hashCode() == lgString.hashCode());
66 Test.ok(10, retr.equals(lgString));
67
68 db.delete(smallKey);
69 Test.ok(11, true);
70
71 db.reorganize();
72 Test.ok(12, true);
73 }
74
75
76 static final int MANY_RECORDS = 200;
77
78 private static void testBigFile(GdbmFile db)
79 throws Exception
80 {
81 String key;
82
83 // Test a lot of data
84 byte[] bigData = new byte[10000];
85 for (int i = 0; i < bigData.length; i++)
86 bigData[i] = (byte)((i & 63) + 32);
87 db.setValuePacking(new RawPacking());
88 Test.ok(150, true);
89
90 // Store a series of records in the file
91 for (int i = 0; i < MANY_RECORDS; i++) {
92 if ((i % 100) == 0)
93 System.out.print(Integer.toString(i)+ " ");
94 db.store(Integer.toString(i, 16), bigData);
95 }
96 System.out.println();
97 Test.ok(151, true);
98
99 // Enumerate keys and values out of the file
100 int count = 0;
101 Enumeration keys = db.keys();
102 while ( keys.hasMoreElements() ) {
103 key = (String) keys.nextElement();
104 count++;
105 if ((count % 100) == 0)
106 System.out.print(Integer.toString(count)+ " ");
107 }
108 System.out.println();
109 Test.ok(152, count==MANY_RECORDS);
110
111 // Delete all the records from the file
112 count = 0;
113 while ( (key = (String) db.getFirstKey()) != null ) {
114 db.delete(key);
115 count++;
116 if ((count % 100) == 0)
117 System.out.print(Integer.toString(count)+ " ");
118 }
119 System.out.println();
120 Test.ok(153, count==MANY_RECORDS);
121
122 db.reorganize();
123 Test.ok(154, true);
124 }
125
126 public static void main(String[] argv) {
127 try {
128 final String dbFile = "/tmp/java.gdbm";
129 String key, value;
130 boolean found;
131
132 // Test cases, inspired by db-hash.t in the Perl5.003_93
133 // distribution
134 System.out.print("GDBM Database tests:\n\t" +
135 GdbmFile.getLibraryVersion() + "\n\t" +
136 GdbmFile.getWrapperVersion() + "\n");
137
138 // Create a simple database
139 GdbmFile db = new GdbmFile(dbFile, GdbmFile.NEWDB);
140 Test.ok(1, db != null);
141
142 // Set the packing strategy
143 db.setKeyPacking(new StringPacking());
144 db.setValuePacking(new StringPacking());
145
146 // Store some data in the file
147 db.store("mountain", "Kosciusko");
148 Test.ok(2, true);
149
150 // Store and read back data
151 db.storeNoReplace("abc", "ABC");
152 Test.ok(3, ((String) db.fetch("abc")).equals("ABC"));
153
154 // Check whether keys exist or not
155 Test.ok(4, db.exists("abc"));
156 Test.ok(5, !db.exists("jimmy"));
157 Test.ok(6, !db.exists("abc\0"));
158
159 // Delete those records
160 db.delete("abc");
161 db.delete("mountain");
162
163 Test.ok(10, !db.exists("abc"));
164 Test.ok(11, !db.exists("mountain"));
165
166 // Store a series of records in the file
167 for (int i = 100; i < 200; i++)
168 db.storeNoReplace(Integer.toString(i, Character.MAX_RADIX),
169 Integer.toString(i, Character.MIN_RADIX));
170
171 // Enumerate keys and values out of the file
172 int count = 0;
173 Enumeration keys = db.keys();
174 while ( keys.hasMoreElements() ) {
175 key = (String) keys.nextElement();
176 // System.out.print(key + "=");
177 // value = (String) db.fetch(key);
178 // System.out.println(value);
179 count++;
180 }
181 Test.ok(20, count==100);
182
183 // Delete all the records from the file
184 count = 0;
185 while ( (key = (String) db.getFirstKey()) != null ) {
186 db.delete(key);
187 count++;
188 }
189 Test.ok(30, count==100);
190
191 // Try to fetch a non-existent key
192 boolean caught = false;
193 try {
194 db.fetch("larry!");
195 } catch ( GdbmException e ) {
196 // TODO: Check the exception was the one we expected?
197 caught = true;
198 }
199 Test.ok(40, caught);
200
201 // Store some very large data
202 testLongData(db);
203
204 // Store strings containing non-ASCII characters
205 final String euroString = "Some funny characters: «¡ Ê Þ â ß !»";
206 db.store("high ASCII", euroString);
207 Test.ok(50, db.fetch("high ASCII").equals(euroString));
208
209 // Close the database
210 db.close();
211 Test.ok(60, true);
212
213 // Try to read from a database that is closed
214 caught = false;
215 try {
216 db.fetch("larry!");
217 } catch ( Exception e ) {
218 System.out.println("caught: " + e.toString());
219 caught = true;
220 }
221 Test.ok(70, caught);
222
223 // Try to open a non-existent database
224 caught = false;
225 try {
226 db = new GdbmFile("/tmp/somenonexistentdatabase.gdbm",
227 GdbmFile.READER);
228 } catch ( GdbmException e ) {
229 System.out.println("caught: " + e.toString());
230 caught = true;
231 }
232 Test.ok(80, caught);
233
234 // Open a database read-only
235 db = new GdbmFile(dbFile, GdbmFile.READER);
236 db.setKeyPacking(new StringPacking());
237 db.setValuePacking(new StringPacking());
238 Test.ok(90);
239
240 // Try to insert
241 caught = false;
242 try {
243 db.store("apple", "crumble");
244 } catch ( GdbmException e ) {
245 System.out.println("caught: " + e.toString());
246 caught = true;
247 }
248 Test.ok(91, caught);
249
250 // Replace an existing database
251 db.close();
252 db = new GdbmFile(dbFile, GdbmFile.NEWDB);
253 db.setKeyPacking(new StringPacking());
254 db.setValuePacking(new StringPacking());
255 Test.ok(100, !db.exists("camel"));
256
257 // Sync to disk
258 db.sync();
259 Test.ok(105, true);
260
261 // Juggle three databases at once
262 final String dbFile2 = "/tmp/java2.gdbm";
263 GdbmFile db2 = new GdbmFile(dbFile2, GdbmFile.NEWDB);
264 final String dbFile3 = "/tmp/java3.gdbm";
265 GdbmFile db3 = new GdbmFile(dbFile3, GdbmFile.NEWDB);
266 Test.ok(110, true);
267
268 keys = db.keys();
269 while (keys.hasMoreElements()) {
270 key = (String) keys.nextElement();
271 value = (String) db.fetch(key);
272 db2.store(key.getBytes(), value.getBytes());
273 db3.store(key.getBytes(), value.getBytes());
274 }
275 Test.ok(120, true);
276
277 keys = db2.keys();
278 found = true;
279 while (keys.hasMoreElements())
280 found &= db3.exists(keys.nextElement());
281 Test.ok(130, found);
282
283 db2.close();
284 db3.close();
285
286 Test.ok(140, true);
287
288 testBigFile(db);
289
290 db.close();
291 Test.ok(160, true);
292 } catch (Exception e) {
293 e.printStackTrace();
294 }
295 }
296}
Note: See TracBrowser for help on using the repository browser.