1 | /*
|
---|
2 | * class: TDBJavaTest -- Test suite for this package
|
---|
3 | * Copyright (C) 2015 Greenstone Digital Library, University of Waikato
|
---|
4 | * $Id$
|
---|
5 | *
|
---|
6 | * Based on: javagdbm/GdbmTest.java
|
---|
7 | * Original author: Martin Pool, 1997 Pharos IP Pty Ltd
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU General Public License as published by
|
---|
11 | * the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with this program; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
22 | */
|
---|
23 |
|
---|
24 | package org.greenstone.tdbjava;
|
---|
25 |
|
---|
26 | import java.io.File;
|
---|
27 | import java.io.PrintStream;
|
---|
28 | import java.util.Enumeration;
|
---|
29 | import java.util.Random;
|
---|
30 |
|
---|
31 | import org.greenstone.tdbjava.TDBJava;
|
---|
32 | import org.greenstone.tdbjava.TDBJavaException;
|
---|
33 | import org.greenstone.tdbjava.Test;
|
---|
34 |
|
---|
35 | /** Test harness for the TDBJava/JNI layer.
|
---|
36 | *
|
---|
37 | * <P>The source of this class forms a useful demonstration of the
|
---|
38 | * TDBJava library and is commended to the intending programmer.
|
---|
39 | *
|
---|
40 | */
|
---|
41 | public class TDBJavaTest
|
---|
42 | {
|
---|
43 |
|
---|
44 | static final int MANY_RECORDS = 200;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Don't instantiate
|
---|
48 | */
|
---|
49 | private TDBJavaTest() {;}
|
---|
50 | /** TDBJavaTest() **/
|
---|
51 |
|
---|
52 |
|
---|
53 | /** @function testLongData(TDBJava)
|
---|
54 | */
|
---|
55 | private static void testLongData(TDBJava db)
|
---|
56 | throws Exception
|
---|
57 | {
|
---|
58 | // generate some long random content
|
---|
59 | StringBuffer buffer = new StringBuffer();
|
---|
60 | Random r = new Random();
|
---|
61 | for (int i = 0; i < 3600; i++) {
|
---|
62 | buffer.append((char)(r.nextInt(26) + 'a'));
|
---|
63 | }
|
---|
64 | String value = buffer.toString();
|
---|
65 | buffer = null;
|
---|
66 | String key = "LargeDatum";
|
---|
67 |
|
---|
68 | db.store(key, value);
|
---|
69 | String retrieved = db.fetch(key);
|
---|
70 |
|
---|
71 | Test.ok(160, retrieved.hashCode() == value.hashCode());
|
---|
72 | Test.ok(161, retrieved.equals(value));
|
---|
73 |
|
---|
74 | db.delete(key);
|
---|
75 | Test.ok(162, true);
|
---|
76 | }
|
---|
77 | /** testLongData(TDBJava) **/
|
---|
78 |
|
---|
79 |
|
---|
80 | /** @function testBigFile(TDBJava)
|
---|
81 | */
|
---|
82 | private static void testBigFile(TDBJava db)
|
---|
83 | throws Exception
|
---|
84 | {
|
---|
85 | String key;
|
---|
86 |
|
---|
87 | // Test a lot of data
|
---|
88 | byte[] data = new byte[10000];
|
---|
89 | for (int i = 0; i < data.length; i++) {
|
---|
90 | data[i] = (byte)((i & 63) + 32);
|
---|
91 | }
|
---|
92 | Test.ok(150, true);
|
---|
93 |
|
---|
94 | // Store a series of records in the file
|
---|
95 | for (int i = 0; i < MANY_RECORDS; i++) {
|
---|
96 | if ((i % 100) == 0) {
|
---|
97 | System.out.print(Integer.toString(i)+ " ");
|
---|
98 | }
|
---|
99 | db.store(Integer.toString(i, 16), data.toString());
|
---|
100 | }
|
---|
101 | System.out.println();
|
---|
102 | Test.ok(151, true);
|
---|
103 |
|
---|
104 | // Enumerate keys and values out of the file
|
---|
105 | int count = 0;
|
---|
106 | Enumeration keys = db.keys();
|
---|
107 | while ( keys.hasMoreElements() ) {
|
---|
108 | key = (String) keys.nextElement();
|
---|
109 | count++;
|
---|
110 | if ((count % 100) == 0) {
|
---|
111 | System.out.print(Integer.toString(count)+ " ");
|
---|
112 | }
|
---|
113 | }
|
---|
114 | System.out.println();
|
---|
115 | Test.ok(152, count==MANY_RECORDS);
|
---|
116 |
|
---|
117 | // Delete all the records from the file
|
---|
118 | count = 0;
|
---|
119 | while ( (key = (String) db.getFirstKey()) != null ) {
|
---|
120 | db.delete(key);
|
---|
121 | count++;
|
---|
122 | if ((count % 100) == 0) {
|
---|
123 | System.out.print(Integer.toString(count)+ " ");
|
---|
124 | }
|
---|
125 | }
|
---|
126 | System.out.println();
|
---|
127 | Test.ok(153, count==MANY_RECORDS);
|
---|
128 | }
|
---|
129 | /** testBigFile(TDBJava) **/
|
---|
130 |
|
---|
131 |
|
---|
132 | public static void ensureNoDatabase(String file_path)
|
---|
133 | {
|
---|
134 | try {
|
---|
135 | File delete_me = new File(file_path);
|
---|
136 | if (delete_me.exists()) {
|
---|
137 | delete_me.delete();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | catch (Exception e) {
|
---|
141 | System.err.println("Failed to remove existing database: " + file_path);
|
---|
142 | System.exit(0);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | /** ensureNoDatabase(String) **/
|
---|
146 |
|
---|
147 |
|
---|
148 | /** @function main()
|
---|
149 | */
|
---|
150 | public static void main(String[] argv)
|
---|
151 | {
|
---|
152 | final String db_path = "/tmp/java.tdb";
|
---|
153 | String key, value;
|
---|
154 | boolean found;
|
---|
155 |
|
---|
156 | // Test cases, inspired by db-hash.t in the Perl5.003_93
|
---|
157 | // distribution
|
---|
158 | System.out.println("===== TDBJava Database tests =====");
|
---|
159 | System.out.println(" - Library version: " + TDBJava.getLibraryVersion());
|
---|
160 | System.out.println(" - Driver version: " + TDBJava.getWrapperVersion());
|
---|
161 | ensureNoDatabase(db_path);
|
---|
162 |
|
---|
163 | try {
|
---|
164 | // Create a simple database
|
---|
165 | TDBJava db = new TDBJava(db_path);
|
---|
166 | Test.ok(1, db != null);
|
---|
167 |
|
---|
168 | // Check database is open
|
---|
169 | Test.ok(1.5, db.isOpen());
|
---|
170 |
|
---|
171 | // Store some data in the file
|
---|
172 | db.store("mountain", "Kosciusko");
|
---|
173 | Test.ok(2, true);
|
---|
174 |
|
---|
175 | // Store and read back data
|
---|
176 | db.store("abc", "ABC");
|
---|
177 | value = db.fetch("abc");
|
---|
178 | Test.ok(3, value.equals("ABC"));
|
---|
179 |
|
---|
180 | // Check whether keys exist or not
|
---|
181 | Test.ok(4, db.exists("abc"));
|
---|
182 | Test.ok(5, !db.exists("jimmy"));
|
---|
183 | Test.ok(6, !db.exists("abc\0"));
|
---|
184 |
|
---|
185 | // Delete those records
|
---|
186 | db.delete("mountain");
|
---|
187 | Test.ok(9, !db.exists("mountain"));
|
---|
188 | db.delete("abc");
|
---|
189 | Test.ok(10, !db.exists("abc"));
|
---|
190 |
|
---|
191 | // Store a series of records in the file
|
---|
192 | for (int i = 100; i < 200; i++) {
|
---|
193 | db.store(Integer.toString(i, Character.MAX_RADIX),
|
---|
194 | Integer.toString(i, Character.MIN_RADIX));
|
---|
195 | }
|
---|
196 |
|
---|
197 | // Enumerate keys and values out of the file
|
---|
198 | int count = 0;
|
---|
199 | Enumeration keys = db.keys();
|
---|
200 | while (keys.hasMoreElements()) {
|
---|
201 | key = (String) keys.nextElement();
|
---|
202 | // System.out.print(key + "=");
|
---|
203 | // value = (String) db.fetch(key);
|
---|
204 | // System.out.println(value);
|
---|
205 | count++;
|
---|
206 | }
|
---|
207 | Test.ok(20, count==100);
|
---|
208 |
|
---|
209 | // Delete all the records from the file
|
---|
210 | count = 0;
|
---|
211 | while ((key = (String) db.getFirstKey()) != null) {
|
---|
212 | db.delete(key);
|
---|
213 | count++;
|
---|
214 | }
|
---|
215 | Test.ok(30, count==100);
|
---|
216 |
|
---|
217 | // Try to fetch a non-existent key
|
---|
218 | boolean caught = false;
|
---|
219 | try {
|
---|
220 | db.fetch("larry!");
|
---|
221 | }
|
---|
222 | catch (Exception e) {
|
---|
223 | // TODO: Check the exception was the one we expected?
|
---|
224 | System.out.println("caught: " + e.toString());
|
---|
225 | caught = true;
|
---|
226 | }
|
---|
227 | Test.ok(40, caught);
|
---|
228 |
|
---|
229 | // Store some very large data
|
---|
230 | testLongData(db);
|
---|
231 |
|
---|
232 | // Store strings containing non-ASCII characters
|
---|
233 | final String euro_string = "Some funny characters: «¡ Ê Þ â à !»";
|
---|
234 | db.store("high ASCII", euro_string);
|
---|
235 | Test.ok(50, db.fetch("high ASCII").equals(euro_string));
|
---|
236 |
|
---|
237 | // Store for later
|
---|
238 | db.store("mountain", "Pirongia");
|
---|
239 |
|
---|
240 | // Close the database
|
---|
241 | db.close();
|
---|
242 | Test.ok(60, true);
|
---|
243 |
|
---|
244 | // Try to read from a database that is closed
|
---|
245 | caught = false;
|
---|
246 | try {
|
---|
247 | db.fetch("larry!");
|
---|
248 | }
|
---|
249 | catch (Exception e) {
|
---|
250 | System.out.println("caught: " + e.toString());
|
---|
251 | caught = true;
|
---|
252 | }
|
---|
253 | Test.ok(70, caught);
|
---|
254 |
|
---|
255 | // Try to open a non-existent database
|
---|
256 | String bogus_db_path = "/tmp/somenonexistentdatabase.tdb";
|
---|
257 | ensureNoDatabase(bogus_db_path);
|
---|
258 | caught = false;
|
---|
259 | try {
|
---|
260 | db = new TDBJava(bogus_db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
|
---|
261 | }
|
---|
262 | catch (Exception e) {
|
---|
263 | System.out.println("caught: " + e.toString());
|
---|
264 | caught = true;
|
---|
265 | }
|
---|
266 | Test.ok(80, caught);
|
---|
267 |
|
---|
268 | // Open a database read-only
|
---|
269 | db = new TDBJava(db_path, TDBJava.TDB_DEFAULT, TDBJava.O_READONLY);
|
---|
270 | Test.ok(90);
|
---|
271 |
|
---|
272 | // Try to insert
|
---|
273 | caught = false;
|
---|
274 | try {
|
---|
275 | db.store("apple", "crumble");
|
---|
276 | }
|
---|
277 | catch (Exception e) {
|
---|
278 | System.out.println("caught: " + e.toString());
|
---|
279 | caught = true;
|
---|
280 | }
|
---|
281 | Test.ok(91, caught);
|
---|
282 |
|
---|
283 | db.close();
|
---|
284 |
|
---|
285 | // Replace an existing database
|
---|
286 | db = new TDBJava(db_path, TDBJava.TDB_CLEAR_IF_FIRST);
|
---|
287 | Test.ok(100, !db.exists("mountain"));
|
---|
288 |
|
---|
289 | // Juggle three databases at once
|
---|
290 | final String db_path2 = "/tmp/java2.tdb";
|
---|
291 | ensureNoDatabase(db_path2);
|
---|
292 | TDBJava db2 = new TDBJava(db_path2);
|
---|
293 | final String db_path3 = "/tmp/java3.tdb";
|
---|
294 | ensureNoDatabase(db_path3);
|
---|
295 | TDBJava db3 = new TDBJava(db_path3);
|
---|
296 | Test.ok(110, true);
|
---|
297 |
|
---|
298 | keys = db.keys();
|
---|
299 | while (keys.hasMoreElements()) {
|
---|
300 | key = (String) keys.nextElement();
|
---|
301 | value = db.fetch(key);
|
---|
302 | db2.store(key, value);
|
---|
303 | db3.store(key, value);
|
---|
304 | }
|
---|
305 | Test.ok(120, true);
|
---|
306 |
|
---|
307 | keys = db2.keys();
|
---|
308 | found = true;
|
---|
309 | while (keys.hasMoreElements()) {
|
---|
310 | found &= db3.exists((String)keys.nextElement());
|
---|
311 | }
|
---|
312 | Test.ok(130, found);
|
---|
313 |
|
---|
314 | db2.close();
|
---|
315 | db3.close();
|
---|
316 |
|
---|
317 | Test.ok(140, true);
|
---|
318 |
|
---|
319 | testBigFile(db);
|
---|
320 |
|
---|
321 | db.close();
|
---|
322 | Test.ok(160, true);
|
---|
323 | }
|
---|
324 | catch (Exception e) {
|
---|
325 | e.printStackTrace();
|
---|
326 | }
|
---|
327 | Test.summary();
|
---|
328 | }
|
---|
329 | }
|
---|