Source code: com/eireneh/bible/book/ZTestBibleBook.java
1
2 package com.eireneh.bible.book;
3
4 import java.io.*;
5 import java.util.*;
6 import java.net.*;
7
8 import com.eireneh.util.*;
9 import com.eireneh.bible.passage.*;
10
11 /**
12 * Attepmted 100% code coverage testing.
13 *
14 * <table border='1' cellPadding='3' cellSpacing='0' width="100%">
15 * <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
16 * Distribution Licence:<br />
17 * Project B is free software; you can redistribute it
18 * and/or modify it under the terms of the GNU General Public License,
19 * version 2 as published by the Free Software Foundation.<br />
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.<br />
24 * The License is available on the internet
25 * <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
26 * <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
28 * The copyright to this program is held by it's authors.
29 * </font></td></tr></table>
30 * @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
31 * @see docs.Licence
32 * @author Joe Walker
33 */
34 public class ZTestBibleBook extends TestBase
35 {
36 /**
37 * A basic are we OK type test, for the Passage package.
38 */
39 public void test(PrintWriter out, boolean fatal)
40 {
41 logPackageStart(out, fatal, ZTestBibleBook.class);
42
43 testVersion();
44 testBookUtil();
45
46 testDriverManager();
47
48 try
49 {
50 BibleDriver[] drivers = BibleDriverManager.getDrivers();
51 for (int i=0; i<drivers.length; i++)
52 {
53 testDriver(drivers[i]);
54 }
55 }
56 catch (Exception ex)
57 {
58 fail(ex);
59 }
60
61 testBibles();
62
63 try
64 {
65 String[] names = Bibles.getBibleNames();
66 for (int i=0; i<names.length; i++)
67 {
68 testBible(names[i]);
69 }
70 }
71 catch (Exception ex)
72 {
73 fail(ex);
74 }
75
76 logPackageStop();
77 }
78
79 /**
80 *
81 */
82 public static void testVersion()
83 {
84 try
85 {
86 log("VersionFactory.getVersion(String, String)");
87 Version niv1 = VersionFactory.getVersion("New International Version","Original");
88 Version niv2 = VersionFactory.getVersion("New International Version","Anglicized");
89 Version niv3 = VersionFactory.getVersion("New International Version","Inclusive Language");
90 Version av = VersionFactory.getVersion("King James Version","");
91 test(VersionFactory.getVersion("New International Version","Original"), niv1);
92 test(VersionFactory.getVersion("New International Version","Original"), niv1);
93 test(VersionFactory.getVersion("New International Version","Anglicized"), niv2);
94 test(VersionFactory.getVersion("King James Version",""), av);
95
96 log("VersionFactory.getVersion(String)");
97 test(VersionFactory.getVersion("New International Version,Original"), niv1);
98 test(VersionFactory.getVersion("New International Version, Original"), niv1);
99 test(VersionFactory.getVersion("New International Version, Original"), niv1);
100 test(VersionFactory.getVersion("New International Version, Anglicized"), niv2);
101 test(VersionFactory.getVersion("New International Version,Anglicized"), niv2);
102 test(VersionFactory.getVersion("King James Version"), av);
103 test(VersionFactory.getVersion("King James Version, "), av);
104 test(VersionFactory.getVersion("King James Version, "), av);
105
106 log("VersionFactory.decodeVersion(String, String)");
107 Version v1 = VersionFactory.decodeVersion("a, b", "initials,1 1 1990, PD, http://localhost/file.txt");
108 test(v1.getName(), "a");
109 test(v1.getEdition(), "b");
110 test(v1.getFirstPublished(), new Date(631152000000L)); // 1 1 1990
111 test(v1.getFullName(), "a,b");
112 test(v1.getInitials(), "initials");
113 test(v1.getLicence(), new URL("http://localhost/file.txt"));
114 test(v1.getOpenness(), Version.STATUS_PD);
115 v1 = VersionFactory.decodeVersion("a", "i, ,XXX, ");
116 test(v1.getName(), "a");
117 test(v1.getEdition(), "");
118 test(v1.getFirstPublished(), null);
119 test(v1.getFullName(), "a");
120 test(v1.getInitials(), "i");
121 test(v1.getLicence(), null);
122 test(v1.getOpenness(), Version.STATUS_UNKNOWN);
123 v1 = VersionFactory.decodeVersion("abcabc", " , , , ");
124 test(v1.getName(), "abcabc");
125 test(v1.getEdition(), "");
126 test(v1.getFirstPublished(), null);
127 test(v1.getFullName(), "abcabc");
128 test(v1.getInitials(), "a");
129 test(v1.getLicence(), null);
130 test(v1.getOpenness(), Version.STATUS_UNKNOWN);
131
132 log("VersionFactory.decodeStatus(String)");
133 test(VersionFactory.decodeStatus("PD"), Version.STATUS_PD);
134 test(VersionFactory.decodeStatus(" PD"), Version.STATUS_PD);
135 test(VersionFactory.decodeStatus("PD "), Version.STATUS_PD);
136 test(VersionFactory.decodeStatus(" PD "), Version.STATUS_PD);
137 test(VersionFactory.decodeStatus(" pd "), Version.STATUS_PD);
138 test(VersionFactory.decodeStatus(" commercial "), Version.STATUS_COMMERCIAL);
139 test(VersionFactory.decodeStatus(" copyable "), Version.STATUS_COPYABLE);
140 test(VersionFactory.decodeStatus("free"), Version.STATUS_FREE);
141 test(VersionFactory.decodeStatus("unknown"), Version.STATUS_UNKNOWN);
142 test(VersionFactory.decodeStatus(""), Version.STATUS_UNKNOWN);
143 test(VersionFactory.decodeStatus("XXX"), Version.STATUS_UNKNOWN);
144
145 log("VersionFactory.getName(String)");
146 test(VersionFactory.getName("a,b"), "a");
147 test(VersionFactory.getName(" a , b "), "a");
148 test(VersionFactory.getName(" ab , b "), "ab");
149 test(VersionFactory.getName(" ab "), "ab");
150
151 log("VersionFactory.getEdition(String)");
152 test(VersionFactory.getEdition("a,b"), "b");
153 test(VersionFactory.getEdition(" a , b "), "b");
154 test(VersionFactory.getEdition(" ab , b "), "b");
155 test(VersionFactory.getEdition(" ab "), "");
156
157 log("VersionFactory.getFullName(String, String)");
158 test(VersionFactory.getFullName("a", "b"), "a,b");
159 test(VersionFactory.getFullName("a", null), "a");
160 test(VersionFactory.getFullName("a", ""), "a");
161 test(VersionFactory.getFullName("a", " "), "a");
162 }
163 catch (Exception ex)
164 {
165 fail(ex);
166 }
167 }
168
169 /**
170 * Test the BookUtil class.
171 */
172 public static void testBookUtil()
173 {
174 try
175 {
176 log("BookUtil.updatePassageTally(Bible, PassageTally, String[])");
177 // BookUtil.updatePassageTally(version, tally, words);
178
179 log("BookUtil.updatePassageTallyFlat(Bible, PassageTally, String[])");
180 // BookUtil.updatePassageTallyFlat(version, tally, words);
181
182 log("BookUtil.getPassage(Bible, String[])");
183 // Passage ref = BookUtil.getPassage(version, words);
184
185 log("BookUtil.isNewPara(BibleEle)");
186 // boolean b = BookUtil.isNewPara(doc);
187
188 log("BookUtil.tokenize(String)");
189 String[] sa = BookUtil.tokenize("one two three");
190 test(sa.length, 3);
191 test(sa[0], "one ");
192 test(sa[1], "two ");
193 test(sa[2], "three");
194 sa = BookUtil.tokenize("!one two three ");
195 test(sa.length, 3);
196 test(sa[0], "!one ");
197 test(sa[1], "two ");
198 test(sa[2], "three ");
199 sa = BookUtil.tokenize("\"one-- two three ");
200 test(sa.length, 3);
201 test(sa[0], "\"one-- ");
202 test(sa[1], "two ");
203 test(sa[2], "three ");
204 sa = BookUtil.tokenize("-one--two three ");
205 test(sa.length, 3);
206 test(sa[0], "-one--");
207 test(sa[1], "two ");
208 test(sa[2], "three ");
209 sa = BookUtil.tokenize("one-two--three ");
210 test(sa.length, 2);
211 test(sa[0], "one-two--");
212 test(sa[1], "three ");
213 sa = BookUtil.tokenize("one!£ \"*(two-three");
214 test(sa.length, 2);
215 test(sa[0], "one!£ ");
216 test(sa[1], "\"*(two-three");
217
218 log("BookUtil.getWords(String)");
219 sa = BookUtil.getWords("One Two three");
220 test(sa.length, 3);
221 test(sa[0], "one");
222 test(sa[1], "two");
223 test(sa[2], "three");
224 sa = BookUtil.getWords("!one two three ");
225 test(sa.length, 3);
226 test(sa[0], "one");
227 test(sa[1], "two");
228 test(sa[2], "three");
229 sa = BookUtil.getWords("\"one-- two three ");
230 test(sa.length, 3);
231 test(sa[0], "one");
232 test(sa[1], "two");
233 test(sa[2], "three");
234 sa = BookUtil.getWords("-one--two three ");
235 test(sa.length, 3);
236 test(sa[0], "one");
237 test(sa[1], "two");
238 test(sa[2], "three");
239 sa = BookUtil.getWords("one-two--three ");
240 test(sa.length, 2);
241 test(sa[0], "one-two");
242 test(sa[1], "three");
243 sa = BookUtil.getWords("one!£ \"*(two-three");
244 test(sa.length, 2);
245 test(sa[0], "one");
246 test(sa[1], "two-three");
247
248 log("BookUtil.stripPunctuation(String[])");
249 // String[] sa = BookUtil.stripPunctuation(words);
250
251 log("BookUtil.stripPunctuationWord(String)");
252 test(BookUtil.stripPunctuationWord("test"), "test");
253 test(BookUtil.stripPunctuationWord(" test"), "test");
254 test(BookUtil.stripPunctuationWord("test-- "), "test");
255 test(BookUtil.stripPunctuationWord("test! "), "test");
256 test(BookUtil.stripPunctuationWord("test\" "), "test");
257 test(BookUtil.stripPunctuationWord("test... "), "test");
258 test(BookUtil.stripPunctuationWord("test's"), "test's");
259 test(BookUtil.stripPunctuationWord("test's "), "test's");
260 test(BookUtil.stripPunctuationWord("test's!"), "test's");
261 test(BookUtil.stripPunctuationWord("test's?"), "test's");
262 test(BookUtil.stripPunctuationWord("test!?;;'#\""), "test");
263 test(BookUtil.stripPunctuationWord("!\"£$test"), "test");
264 test(BookUtil.stripPunctuationWord(" test "), "test");
265 test(BookUtil.stripPunctuationWord("--test "), "test");
266 test(BookUtil.stripPunctuationWord("'test "), "test");
267 test(BookUtil.stripPunctuationWord("/?test "), "test");
268 test(BookUtil.stripPunctuationWord(" $%^\" test %^&"), "test");
269
270 log("BookUtil.stripWords(String[])");
271 // String[] sa = BookUtil.stripWords(words);
272
273 log("BookUtil.stripWords(String, String)");
274 // String s = BookUtil.stripWords(first, last);
275
276 log("BookUtil.getCases(String[])");
277 int[] ia = BookUtil.getCases(new String[] { "abc" });
278 test(ia[0], Books.CASE_LOWER);
279
280 log("BookUtil.firstLetter(String)");
281 test(BookUtil.firstLetter("abcde"), 0);
282 test(BookUtil.firstLetter(" abcde"), 1);
283 test(BookUtil.firstLetter(" \"£abcde"), 3);
284 test(BookUtil.firstLetter(" \"£abcde--! "), 3);
285
286 log("BookUtil.lastLetter(String)");
287 test(BookUtil.lastLetter("abcde"), 4);
288 test(BookUtil.lastLetter("abcde "), 4);
289 test(BookUtil.lastLetter("abcde\" "), 4);
290 test(BookUtil.lastLetter("abcde\"£$ "), 4);
291 test(BookUtil.lastLetter(" abcde"), 5);
292 test(BookUtil.lastLetter(" abcde "), 5);
293 test(BookUtil.lastLetter(" abcde\" "), 5);
294 test(BookUtil.lastLetter(" abcde\"£$ "), 5);
295 test(BookUtil.lastLetter(" abcde--\"£$ "), 5);
296 test(BookUtil.lastLetter(" abcde\"£$-- "), 5);
297 }
298 catch (Exception ex)
299 {
300 fail(ex);
301 }
302 }
303
304 /**
305 * Test the BookUtil class.
306 */
307 public static void testDriverManager()
308 {
309 try
310 {
311 String[] names = Bibles.getBibleNames();
312
313 log("BibleDriverManager.getDrivers()");
314 BibleDriver[] drivers = BibleDriverManager.getDrivers();
315 int before = drivers.length;
316 test(before, 3);
317
318 log("BibleDriverManager.getDriverForBible(String)");
319 for (int i=0; i<names.length; i++)
320 {
321 BibleDriver driver = BibleDriverManager.getDriverForBible(names[i]);
322 test(driver != null);
323 }
324 try { BibleDriverManager.getDriverForBible("NONE"); fail(); }
325 catch (BookException ex) { }
326
327 log("BibleDriverManager.unregisterDriver(BibleDriver)");
328 BibleDriverManager.unregisterDriver(drivers[0]);
329 BibleDriver[] d2 = BibleDriverManager.getDrivers();
330 int n2 = d2.length;
331 test(n2, 2);
332 BibleDriverManager.unregisterDriver(drivers[1]);
333 BibleDriver[] d3 = BibleDriverManager.getDrivers();
334 int n3 = d3.length;
335 test(n3, 1);
336 BibleDriverManager.unregisterDriver(drivers[2]);
337 BibleDriver[] d4 = BibleDriverManager.getDrivers();
338 int n4 = d4.length;
339 test(n4, 0);
340
341 log("BibleDriverManager.getDriverForBible(String)");
342 for (int i=0; i<names.length; i++)
343 {
344 try { BibleDriverManager.getDriverForBible(names[i]); fail(); }
345 catch (BookException ex) { }
346 }
347 try { BibleDriverManager.getDriverForBible("NONE"); fail(); }
348 catch (BookException ex) { }
349
350 log("BibleDriverManager.registerDriver(BibleDriver)");
351 BibleDriverManager.registerDriver(drivers[0]);
352 BibleDriver[] d5 = BibleDriverManager.getDrivers();
353 int n5 = d5.length;
354 test(n5, 1);
355 BibleDriverManager.registerDriver(drivers[1]);
356 BibleDriver[] d6 = BibleDriverManager.getDrivers();
357 int n6 = d6.length;
358 test(n6, 2);
359 BibleDriverManager.registerDriver(drivers[2]);
360 BibleDriver[] d7 = BibleDriverManager.getDrivers();
361 int n7 = d7.length;
362 test(n7, 3);
363
364 log("BibleDriverManager.getDriverForBible(String)");
365 for (int i=0; i<names.length; i++)
366 {
367 BibleDriver driver = BibleDriverManager.getDriverForBible(names[i]);
368 test(driver != null);
369 }
370 try { BibleDriverManager.getDriverForBible("NONE"); fail(); }
371 catch (BookException ex) { }
372 }
373 catch (Exception ex)
374 {
375 fail(ex);
376 }
377 }
378
379 /**
380 * Test the BibleDriver classes
381 */
382 public static void testDriver(BibleDriver driver)
383 {
384 try
385 {
386 String full = driver.getClass().getName();
387 String type = full.substring(full.lastIndexOf(".")+1);
388
389 log(type+".getDriverName()");
390 test(driver.getDriverName() != null);
391
392 log(type+".getBibleNames()");
393 String[] names = driver.getBibleNames();
394 test(names != null);
395 test(names.length > 0);
396
397 log(type+".countBibles()");
398 test(driver.countBibles(), names.length);
399
400 log(type+".exists(String)");
401 for (int i=0; i<names.length; i++)
402 {
403 test(driver.exists(names[i]));
404 }
405 test(!driver.exists("NONE"));
406
407 log(type+".getBible(String)");
408 for (int i=0; i<names.length; i++)
409 {
410 Bible b = driver.getBible(names[i]);
411 test(b != null);
412 }
413 try { Bible b = driver.getBible("NONE"); fail(); }
414 catch (BookException ex) { }
415
416 log(type+".createBible(String)");
417 log(type+".renameBible(String, String)");
418 log(type+".deleteBible(String)");
419 }
420 catch (Exception ex)
421 {
422 fail(ex);
423 }
424 }
425
426 /**
427 * Test the BookUtil class.
428 */
429 public static void testBibles()
430 {
431 try
432 {
433 log("Bibles.getBibleNames()");
434 String[] names = Bibles.getBibleNames();
435 test(names.length > 0);
436
437 log("Bibles.getBible(String)");
438 for (int i=0; i<names.length; i++)
439 {
440 Bible b = Bibles.getBible(names[i]);
441 test(b != null);
442 }
443 try { Bible b = Bibles.getBible("NONE"); fail(); }
444 catch (BookException ex) { }
445
446 log("Bibles.createBible(String, BibleDriver)");
447 // Bible b2 = Bibles.createBible("dest_name", dest_driver);
448
449 log("Bibles.setDefaultName(String)");
450 log("Bibles.getDefaultName()");
451 log("Bibles.getDefaultBible()");
452 String deft = Bibles.getDefaultName();
453 for (int i=0; i<names.length; i++)
454 {
455 Bibles.setDefaultName(names[0]);
456 test(Bibles.getDefaultName(), names[0]);
457 test(Bibles.getDefaultBible().getName(), names[0]);
458 }
459 try { Bibles.setDefaultName("NONE"); fail(); }
460 catch (BookException ex) { }
461 Bibles.setDefaultName(deft);
462
463 log("Bibles.getCacheingBibles()");
464 log("Bibles.setCacheingBibles(boolean)");
465 boolean cb = Bibles.getCacheingBibles();
466 Bibles.setCacheingBibles(true);
467 test(Bibles.getCacheingBibles());
468 Bibles.setCacheingBibles(false);
469 test(!Bibles.getCacheingBibles());
470 Bibles.setCacheingBibles(cb);
471 }
472 catch (Exception ex)
473 {
474 fail(ex);
475 }
476 }
477
478 /**
479 * Test the Bible classes
480 */
481 public static void testBible(String name)
482 {
483 try
484 {
485 log("Bibles.getBible(\""+name+"\")");
486 Bible ver = Bibles.getBible(name);
487 test(ver != null);
488
489 log("Bible.getDriver()");
490 BibleDriver drv = ver.getDriver();
491 String full = drv.getClass().getName();
492 String type = full.substring(full.lastIndexOf(".")+1);
493
494 log(type+".getVersion()");
495
496 log(type+".getName()");
497 test(name, ver.getName());
498
499 log(type+".getProperties()");
500 log(type+".getPropertiesURL()");
501
502 log(type+".getText(VerseRange)");
503 String fred;
504
505 log(type+".getDocument(BibleEle, Passage)");
506
507 log(type+".findPassage(String)");
508 Passage ref = ver.findPassage("aaron");
509 test(ref.countVerses() > 10);
510 ref = ver.findPassage("jerusalem");
511 test(ref.countVerses() > 10);
512 ref = ver.findPassage("god");
513 test(ref.countVerses() > 10);
514 ref = ver.findPassage("GOD");
515 test(ref.countVerses() > 10);
516 ref = ver.findPassage("brother's");
517 test(ref.countVerses() > 2);
518 ref = ver.findPassage("BROTHER'S");
519 test(ref.countVerses() > 2);
520
521 ref = ver.findPassage("maher-shalal-hash-baz");
522 if (ref.isEmpty())
523 ref = ver.findPassage("mahershalalhashbaz");
524 test(ref.countVerses(), 2);
525 test(ref.getVerseAt(0), new Verse("Isa 8:1"));
526 test(ref.getVerseAt(1), new Verse("Isa 8:3"));
527 ref = ver.findPassage("MAHER-SHALAL-HASH-BAZ");
528 if (ref.isEmpty())
529 ref = ver.findPassage("MAHERSHALALHASHBAZ");
530 test(ref.countVerses(), 2);
531 test(ref.getVerseAt(0), new Verse("Isa 8:1"));
532 test(ref.getVerseAt(1), new Verse("Isa 8:3"));
533
534 log(type+".listWords()");
535 Enumeration en = ver.listWords();
536 String word = (String) en.nextElement();
537 test(!word.equals(""));
538
539 log(type+".getStartsWith()");
540 String[] sa = ver.getStartsWith("jos");
541 test(sa.length > 5);
542 sa = ver.getStartsWith("jerusale");
543 test(sa[0], "jerusalem");
544 sa = ver.getStartsWith("maher-shalal");
545 if (sa.length == 0)
546 {
547 sa = ver.getStartsWith("mahershalal");
548 test(sa[0], "mahershalalhashbaz");
549 }
550 else
551 {
552 test(sa[0], "maher-shalal-hash-baz");
553 }
554 test(sa.length, 1);
555 sa = ver.getStartsWith("MAHER-SHALAL");
556 if (sa.length == 0)
557 {
558 sa = ver.getStartsWith("MAHERSHALAL");
559 test(sa[0], "mahershalalhashbaz");
560 }
561 else
562 {
563 test(sa[0], "maher-shalal-hash-baz");
564 }
565 test(sa.length, 1);
566 sa = ver.getStartsWith("XXX");
567 test(sa.length, 0);
568
569 log(type+".listWords()");
570 for (int i=0; i<1000; i++)
571 {
572 word = (String) en.nextElement();
573 if (word.equals("abimelech"))
574 break;
575
576 if (i == 1000)
577 fail();
578 }
579
580 log(type+".generate(Bible)");
581 log(type+".addProgressListener(ProgressListener)");
582 log(type+".removeProgressListener(ProgressListener)");
583 }
584 catch (Exception ex)
585 {
586 fail(ex);
587 }
588 }
589 }