Coverage Report - org.crosswire.jsword.examples.ReadEverything
 
Classes in this File Line Coverage Branch Coverage Complexity
ReadEverything
0%
0/48
0%
0/20
3.4
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.examples;
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 import org.crosswire.jsword.book.Book;
 25  
 import org.crosswire.jsword.book.BookData;
 26  
 import org.crosswire.jsword.book.BookException;
 27  
 import org.crosswire.jsword.book.BookFilter;
 28  
 import org.crosswire.jsword.book.Books;
 29  
 import org.crosswire.jsword.passage.Key;
 30  
 import org.slf4j.Logger;
 31  
 import org.slf4j.LoggerFactory;
 32  
 
 33  
 /**
 34  
  * Test to check that all books can be read.
 35  
  * 
 36  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 37  
  * @author Joe Walker
 38  
  */
 39  
 public final class ReadEverything {
 40  
     /**
 41  
      * Prevent instantiation
 42  
      */
 43  0
     private ReadEverything() {
 44  0
     }
 45  
 
 46  
     /**
 47  
      * Read all the books that we can get our hands on.
 48  
      * @param args 
 49  
      */
 50  
     public static void main(String[] args) {
 51  
         // TODO(DMS): add the ability to pass a filter specification
 52  
         // Loop through all the Books
 53  0
         log.warn("*** Reading all installed Bibles");
 54  0
         BookFilter filter = null;
 55  
         //filter = BookFilters.getCustom("SourceType=ThML");
 56  
         //filter = BookFilters.getCustom("Description=Ergane Turkish to English Glossary");
 57  
         //filter = BookFilters.getCustom("ModDrv=zLD");
 58  
         //filter = BookFilters.getCustom("Initials=BosworthToller");
 59  
         //filter = BookFilters.getDictionaries();
 60  0
         List<Book> books = getBooks(filter);
 61  0
         for (Book book : books) {
 62  0
             if (book.isLocked()) {
 63  0
                 log.warn("****** Skipping: [{}] {} ({})", book.getInitials(), book.getName(), book.getBookCategory());
 64  0
                 continue;
 65  
             }
 66  0
             log.warn("****** Reading: [{}] {} ({})", book.getInitials(), book.getName(), book.getBookCategory());
 67  0
             Key set = book.getGlobalKeyList();
 68  0
             testReadMultiple(book, set);
 69  0
         }
 70  0
     }
 71  
 
 72  
     private static List<Book> getBooks(BookFilter filter) {
 73  0
         if (filter == null) {
 74  0
             return Books.installed().getBooks();
 75  
         }
 76  0
         return Books.installed().getBooks(filter);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Perform a test read on an iterator over a set of keys
 81  
      */
 82  
     private static void testReadMultiple(Book book, Key set) {
 83  
         // log.info("Testing: {}={}", bmd.getInitials(), bmd.getName());
 84  0
         long start = System.currentTimeMillis();
 85  0
         int entries = 0;
 86  0
         count = 0;
 87  0
         boolean first = true;
 88  0
         for (Key key : set) {
 89  
             // skip the first entry if the length is 0.
 90  0
             if (first) {
 91  0
                 first = false;
 92  0
                 if (key.getName().length() == 0) {
 93  0
                     continue;
 94  
                 }
 95  
             }
 96  0
             testReadSingle(book, key, entries);
 97  
 
 98  0
             entries++;
 99  
         }
 100  
 
 101  0
         long end = System.currentTimeMillis();
 102  0
         float time = (end - start) / 1000F;
 103  
 
 104  0
         if (count > 0) {
 105  0
             log.info("Tested: book={} entries={} time={}s errors={} ({}ms per entry)", book.getInitials(), Integer.toString(entries), Float.toString(time), Integer.toString(count), Float.toString(1000 * time / entries));
 106  
         } else {
 107  0
             log.info("Tested: book={} entries={} time={}s ({}ms per entry)", book.getInitials(), Integer.toString(entries), Float.toString(time), Float.toString(1000 * time / entries));
 108  
         }
 109  0
     }
 110  
 
 111  
     /**
 112  
      * Perform a test read on a single key
 113  
      */
 114  
     private static void testReadSingle(Book book, Key key, int entry) {
 115  0
         Throwable oops = null;
 116  
         try {
 117  0
             log.debug("reading: {}/{}:{}", book.getInitials(), Integer.toString(entry), key.getName());
 118  
 
 119  0
             BookData data = new BookData(book, key);
 120  0
             if (data.getOsisFragment() == null) {
 121  0
                 log.warn("No output from: {},{}", book.getInitials(), key.getOsisID());
 122  
             }
 123  
 
 124  
             // This might be a useful extra test, except that a failure gives
 125  
             // you no help at all.
 126  
             // data.validate();
 127  0
         } catch (BookException ex) {
 128  0
             oops = ex;
 129  0
         }
 130  0
         if (oops != null) {
 131  0
             ++count;
 132  0
             if (count < 5) {
 133  0
                 log.error("Unexpected error reading: {}, {}, {}, {}", book.getInitials(), Integer.toString(entry), key.getOsisID(), key.getClass().getName(), oops);
 134  
             }
 135  
         }
 136  0
     }
 137  
 
 138  
     private static int count;
 139  
 
 140  
     /**
 141  
      * The log stream
 142  
      */
 143  0
     private static final Logger log = LoggerFactory.getLogger(ReadEverything.class);
 144  
 }