Changeset 2872


Ignore:
Timestamp:
06/15/07 14:01:28 (12 years ago)
Author:
melissa
Message:

Updated testing framework to use TestNG (instead of JUnit); added pixel consistency check.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/test/ConfigurationFiles.java

    r2455 r2872  
    103103          entry.type = new int[nSeries]; 
    104104          entry.littleEndian = new boolean[nSeries]; 
     105          entry.md5 = new String[nSeries]; 
    105106 
    106107          for (int i=0; i<nSeries; i++) { 
     
    141142            ndx = s.indexOf("little") + 7; 
    142143            entry.littleEndian[i] = s.substring(ndx).equals("true"); 
     144            ndx = s.indexOf("md5") + 4; 
     145            entry.md5[i] = s.substring(ndx, s.indexOf(" ", ndx)); 
    143146          } 
    144147 
     
    226229  } 
    227230 
     231  public String getMD5(String id) { 
     232    if (!initialized(id)) return null; 
     233    return ((ConfigEntry) entries.get(id)).md5[currentSeries]; 
     234  } 
     235 
    228236  public float getTimePerPlane(String id) { 
    229237    if (!initialized(id)) return 0; 
     
    253261    private int[] type; 
    254262    private boolean[] littleEndian; 
     263    private String[] md5;  
    255264    private float access; 
    256265    private int mem; 
  • trunk/loci/formats/test/ReaderTest.java

    r2857 r2872  
    2727import java.awt.image.BufferedImage; 
    2828import java.io.*; 
     29import java.security.MessageDigest; 
    2930import java.util.*; 
    30 import junit.framework.*; 
    3131import loci.formats.*; 
    3232import loci.formats.ome.OMEXMLMetadataStore; 
    33  
    34 /** 
    35  * JUnit tester for Bio-Formats file format readers. 
     33import org.testng.*; 
     34import org.testng.xml.*; 
     35 
     36/**  
     37 * TestNG tester for Bio-Formats file format readers. 
    3638 * Details on failed tests are written to a log file, for easier processing. 
    3739 * 
    38  * To test the framework, run this class from the command line with a command 
    39  * line argument indicating the root path of data files to be tested. The 
    40  * path will be scanned, and a list of files to test will be built. 
    41  * 
    42  * Unfortunately, it is not practical to construct one large JUnit test suite 
    43  * with a static suite() method, because many datasets are spread across 
    44  * multiple files. For example, a collection of 100 TIFF files numbered 
    45  * tiff001.tif through tiff100.tif should only be tested once, rather than 100 
    46  * times. To solve this problem, the list of files to test is whittled down 
    47  * dynamically -- after each file is tested, the Bio-Formats library reports 
    48  * all files that are part of that same dataset (i.e., essentially, a list of 
    49  * all files just tested). These files are all removed from the list, ensuring 
    50  * each dataset is only tested once. 
    51  * 
    52  * As such, this test case is not well suited for use with most JUnit tools, 
    53  * such as junit.awtui.TestRunner, junit.swingui.TestRunner, or 
    54  * junit.textui.TestRunner. If you are interested enough in unit testing to 
    55  * have read this explanation, and have any thoughts or suggestions for 
    56  * improvement, your thoughts would be most welcome. 
     40 * To run tests: 
     41 * java -ea -mx512m loci.formats.test.ReaderTest <test group> <directory> <time> 
    5742 */ 
    58 public class ReaderTest extends TestCase { 
    59  
    60   // -- Static fields -- 
    61  
    62   private static boolean writeConfigFiles = false; 
    63   private static boolean testXML = false; 
    64   private static float timeMultiplier = 1f; 
    65  
    66   private static StringBuffer configLine; 
     43public class ReaderTest { 
     44 
     45  // -- Fields -- 
     46 
     47  /** Root data directory. */ 
     48  public static String root; 
     49 
     50  /** Whether or not this is the first time calling the data provider. */ 
     51  private boolean isFirstTime = true; 
     52 
     53  /** List of files to test. */ 
     54  private static Vector toTest;  
     55 
     56  /** List of configuration files. */ 
    6757  private static Vector configFiles = new Vector(); 
     58 
     59  /** Configuration file reader. */ 
    6860  private static ConfigurationFiles config = ConfigurationFiles.newInstance(); 
     61 
     62  /** Current log file. */ 
    6963  private static FileWriter logFile; 
    70   private String[] used; 
    71  
    72   private static float averagePlaneAccess; 
    73   private static int maxMemory;  // maximum measured memory usage 
    74   private static int initialMemory;  // memory usage before opening the file 
    75   private static int finalMemory;  // memory usage after closing file 
    76  
    77   // -- Fields -- 
    78  
    79   private String id; 
    80   private FileStitcher reader; 
    81  
    82   // -- Constructor -- 
    83  
    84   public ReaderTest(String s) { 
    85     super(s); 
    86     throw new RuntimeException("Sorry, ReaderTest must be constructed with " + 
    87       "a filename to read for performing the tests. See the class javadoc " + 
    88       "for ReaderTest for more details."); 
    89   } 
    90  
    91   public ReaderTest(String s, String id) { 
    92     super(s); 
    93     this.id = id; 
    94   } 
    95  
    96   // -- ReaderTest API methods -- 
    97  
    98   /** Gets all constituent files in the tested dataset. */ 
    99   public String[] getUsedFiles() { return used; } 
    100  
    101   /** Closes the reader. */ 
    102   public void close() { 
    103     try { 
    104       reader.close(); 
    105     } 
    106     catch (Exception e) { } 
    107   } 
    108  
    109   // -- ReaderTest API methods - tests -- 
    110  
    111   /** 
    112    * Checks the SizeX and SizeY dimensions against the actual dimensions of 
    113    * the BufferedImages. 
    114    */ 
    115   public void testBufferedImageDimensions() { 
    116     boolean success = true; 
    117     Runtime rt = Runtime.getRuntime(); 
    118     initialMemory = (int) ((rt.totalMemory() - rt.freeMemory()) >> 20); 
    119     maxMemory = initialMemory; 
    120     try { 
    121       int planesRead = 0; 
    122       reader.setId(id); 
    123       long l1 = System.currentTimeMillis(); 
     64 
     65  // -- Data provider -- 
     66 
     67  /** 
     68   * @testng.data-provider name = "provider" 
     69   */ 
     70  public Object[][] createData() { 
     71    if (isFirstTime) { 
     72      toTest = new Vector();  
     73      getFiles(root, toTest); 
     74      isFirstTime = false;  
     75    } 
     76    String[] o = (String[]) toTest.toArray(new String[0]); 
     77    String[][] rtn = new String[o.length][1]; 
     78    for (int i=0; i<o.length; i++) { rtn[i][0] = o[i]; } 
     79    return rtn; 
     80  } 
     81 
     82  // -- Tests -- 
     83 
     84  /** 
     85   * @testng.test dataProvider = "provider" 
     86   *              groups = "all pixels" 
     87   */ 
     88  public void testBufferedImageDimensions(String file) { 
     89    try { 
     90      FileStitcher reader = new FileStitcher(); 
     91      reader.setId(file); 
     92 
     93      boolean success = true; 
    12494      for (int i=0; i<reader.getSeriesCount(); i++) { 
    125         int usedMemory = (int) (rt.totalMemory() - rt.freeMemory()) >> 20; 
    126         if (usedMemory > maxMemory) maxMemory = usedMemory; 
    12795        reader.setSeries(i); 
    128         int imageCount = reader.getImageCount(); 
    129         int sizeX = reader.getSizeX(); 
    130         int sizeY = reader.getSizeY(); 
    131  
    132         for (int j=0; j<imageCount; j++) { 
     96 
     97        int x = reader.getSizeX(); 
     98        int y = reader.getSizeY(); 
     99        int c = reader.getRGBChannelCount(); 
     100        int type = reader.getPixelType(); 
     101       
     102        for (int j=0; j<reader.getImageCount(); j++) { 
    133103          BufferedImage b = reader.openImage(j); 
    134           boolean failW = b.getWidth() != sizeX; 
    135           boolean failH = b.getHeight() != sizeY; 
    136           if (failW) writeLog(id + " failed width test"); 
    137           if (failH) writeLog(id + " failed height test"); 
    138           if (failW || failH) { 
     104          boolean failX = b.getWidth() != x; 
     105          boolean failY = b.getHeight() != y; 
     106          boolean failC = b.getRaster().getNumBands() <= c; 
     107          boolean failType = ImageTools.getPixelType(b) != type; 
     108         
     109          success = failX || failY || failC || failType; 
     110          if (!success) { 
     111            writeLog(file + " failed BufferedImage test"); 
     112            /* debug */ 
     113            System.out.println("failX : " + failX); 
     114            System.out.println("failY : " + failY); 
     115            System.out.println("failC : " + failC); 
     116            System.out.println("failType : " + failType); 
     117            /* end debug */ 
     118            break;  
     119          } 
     120        } 
     121      } 
     122      assert success;  
     123    } 
     124    catch (Exception exc) { 
     125      writeLog(file + " failed BufferedImage test"); 
     126      writeLog(exc); 
     127      assert false;  
     128    } 
     129  } 
     130 
     131  /** 
     132   * @testng.test dataProvider = "provider" 
     133   *              groups = "all pixels" 
     134   */ 
     135  public void testByteArrayDimensions(String file) { 
     136    try { 
     137      boolean success = true; 
     138      FileStitcher reader = new FileStitcher(); 
     139      reader.setId(file); 
     140      for (int i=0; i<reader.getSeriesCount(); i++) { 
     141        reader.setSeries(i); 
     142        int x = reader.getSizeX(); 
     143        int y = reader.getSizeY(); 
     144        int c = reader.getRGBChannelCount(); 
     145        int bytes = FormatTools.getBytesPerPixel(reader.getPixelType()); 
     146 
     147        int expected = x * y * c * bytes; 
     148 
     149        for (int j=0; j<reader.getImageCount(); j++) { 
     150          byte[] b = reader.openBytes(j); 
     151          if (b.length < expected) { 
    139152            success = false; 
    140             j = imageCount; 
    141             i = reader.getSeriesCount(); 
    142153            break; 
    143154          } 
    144155        } 
    145         planesRead += imageCount; 
    146       } 
    147       long l2 = System.currentTimeMillis(); 
    148       averagePlaneAccess = ((float) (l2 - l1)) / planesRead; 
     156      } 
     157      if (!success) writeLog(file + " failed byte array test"); 
     158      assert success; 
    149159    } 
    150160    catch (Exception exc) { 
    151       if (FormatHandler.debug) LogTools.trace(exc); 
    152       success = false; 
    153     } 
    154     if (!success) writeLog(id + " failed BufferedImage test"); 
    155     try { 
    156       reader.close(); 
    157       System.gc(); 
    158       Thread.sleep(100); 
    159       System.gc(); 
    160     } 
    161     catch (Exception e) { } 
    162     finalMemory = (int) (rt.totalMemory() - rt.freeMemory()) >> 20; 
    163     assertTrue(success); 
    164   } 
    165  
    166   /** 
    167    * Checks the SizeX and SizeY dimensions against the actual dimensions of 
    168    * the byte array returned by openBytes. 
    169    */ 
    170   public void testByteArrayDimensions() { 
    171     boolean success = true; 
    172     try { 
    173       reader.setId(id); 
     161      writeLog(file + " failed byte array test"); 
     162      writeLog(exc); 
     163      assert false;  
     164    } 
     165  } 
     166 
     167  /** 
     168   * @testng.test dataProvider = "provider" 
     169   *              groups = "all pixels" 
     170   */ 
     171  public void testThumbnailImageDimensions(String file) { 
     172    try { 
     173      FileStitcher reader = new FileStitcher(); 
     174      reader.setId(file); 
     175 
     176      boolean success = true; 
     177      for (int i=0; i<reader.getSeriesCount(); i++) { 
     178        reader.setSeries(i); 
     179 
     180        int x = reader.getThumbSizeX(); 
     181        int y = reader.getThumbSizeY(); 
     182        int c = reader.getRGBChannelCount(); 
     183        int type = reader.getPixelType(); 
     184       
     185        for (int j=0; j<reader.getImageCount(); j++) { 
     186          BufferedImage b = reader.openThumbImage(j); 
     187          boolean failX = b.getWidth() != x; 
     188          boolean failY = b.getHeight() != y; 
     189          boolean failC = b.getRaster().getNumBands() <= c; 
     190          boolean failType = ImageTools.getPixelType(b) != type; 
     191         
     192          success = failX || failY || failC || failType; 
     193          if (!success) { 
     194            writeLog(file + " failed thumbnail BufferedImage test"); 
     195            break;  
     196          } 
     197        } 
     198      } 
     199      assert success;  
     200    } 
     201    catch (Exception exc) { 
     202      writeLog(file + " failed thumbnail BufferedImage test"); 
     203      writeLog(exc); 
     204      assert false;  
     205    } 
     206  } 
     207 
     208  /** 
     209   * @testng.test dataProvider = "provider" 
     210   *              groups = "all pixels" 
     211   */ 
     212  public void testThumbnailByteArrayDimensions(String file) { 
     213    try { 
     214      boolean success = true; 
     215      FileStitcher reader = new FileStitcher(); 
     216      reader.setId(file); 
     217      for (int i=0; i<reader.getSeriesCount(); i++) { 
     218        reader.setSeries(i); 
     219        int x = reader.getThumbSizeX(); 
     220        int y = reader.getThumbSizeY(); 
     221        int c = reader.getRGBChannelCount(); 
     222        int bytes = FormatTools.getBytesPerPixel(reader.getPixelType()); 
     223 
     224        int expected = x * y * c * bytes; 
     225 
     226        for (int j=0; j<reader.getImageCount(); j++) { 
     227          byte[] b = reader.openThumbBytes(j); 
     228          if (b.length < expected) { 
     229            success = false; 
     230            break; 
     231          } 
     232        } 
     233      } 
     234      if (!success) writeLog(file + " failed thumbnail byte array test"); 
     235      assert success; 
     236    } 
     237    catch (Exception exc) { 
     238      writeLog(file + " failed thumbnail byte array test"); 
     239      writeLog(exc); 
     240      assert false;  
     241    } 
     242  } 
     243   
     244  /** 
     245   * @testng.test dataProvider = "provider" 
     246    *             groups = "all fast"  
     247   */ 
     248  public void testImageCount(String file) { 
     249    try { 
     250      FileStitcher reader = new FileStitcher(); 
     251      reader.setId(file); 
     252      boolean success = true;  
    174253      for (int i=0; i<reader.getSeriesCount(); i++) { 
    175254        reader.setSeries(i); 
    176255        int imageCount = reader.getImageCount(); 
    177         int sizeX = reader.getSizeX(); 
    178         int sizeY = reader.getSizeY(); 
    179         int bytesPerPixel = 
    180           FormatTools.getBytesPerPixel(reader.getPixelType()); 
    181         int sizeC = reader.getSizeC(); 
    182         boolean rgb = reader.isRGB(); 
    183  
    184         int expectedBytes = sizeX * sizeY * bytesPerPixel * (rgb ? sizeC : 1); 
    185  
    186         for (int j=0; j<imageCount; j++) { 
    187           byte[] b = reader.openBytes(j); 
    188           if (b.length != expectedBytes) { 
    189             success = false; 
    190             j = imageCount; 
    191             i = reader.getSeriesCount(); 
    192             break; 
    193           } 
    194         } 
    195       } 
     256        int z = reader.getSizeZ(); 
     257        int c = reader.getEffectiveSizeC(); 
     258        int t = reader.getSizeT(); 
     259        if (imageCount != z * c * t) { 
     260          success = false; 
     261          break;  
     262        }  
     263      }  
     264      reader.close();  
     265      if (!success) writeLog(file + " failed image count test");  
     266      assert success;  
    196267    } 
    197268    catch (Exception exc) { 
    198       if (FormatHandler.debug) LogTools.trace(exc); 
    199       success = false; 
    200     } 
    201     if (!success) writeLog(id + " failed byte array test"); 
    202     try { reader.close(true); } 
    203     catch (Exception e) { } 
    204     assertTrue(success); 
    205   } 
    206  
    207   /** 
    208    * Checks the reported thumbnail dimensions against the actual dimensions of 
    209    * the thumbnail BufferedImages. 
    210    */ 
    211   public void testThumbnailImageDimensions() { 
    212     boolean success = true; 
    213     try { 
    214       int planesRead = 0; 
    215       reader.setId(id); 
    216  
     269      writeLog(file + " failed image count test"); 
     270      writeLog(exc); 
     271      assert false;  
     272    } 
     273  }  
     274 
     275  /** 
     276   * @testng.test dataProvider = "provider" 
     277   *              groups = "all xml fast"  
     278   */ 
     279  public void testOMEXML(String file) { 
     280    try { 
     281      OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
     282      store.createRoot(); 
     283      FileStitcher reader = new FileStitcher(); 
     284      reader.setMetadataStore(store); 
     285      reader.setId(file); 
     286 
     287      boolean success = true; 
    217288      for (int i=0; i<reader.getSeriesCount(); i++) { 
    218289        reader.setSeries(i); 
    219         int imageCount = reader.getImageCount(); 
    220         int sizeX = reader.getThumbSizeX(); 
    221         int sizeY = reader.getThumbSizeY(); 
    222  
    223         for (int j=0; j<imageCount; j++) { 
    224           BufferedImage b = reader.openThumbImage(j); 
    225           boolean failW = b.getWidth() != sizeX; 
    226           boolean failH = b.getHeight() != sizeY; 
    227           if (failW) writeLog(id + " failed thumbnail width test"); 
    228           if (failH) writeLog(id + " failed thumbnail height test"); 
    229           if (failW || failH) { 
    230             success = false; 
    231             j = imageCount; 
    232             i = reader.getSeriesCount(); 
    233             break; 
    234           } 
    235         } 
    236         planesRead += imageCount; 
    237       } 
     290         
     291        Integer ii = new Integer(i); 
     292 
     293        String type = FormatTools.getPixelTypeString(reader.getPixelType()); 
     294 
     295        boolean failX = reader.getSizeX() != store.getSizeX(ii).intValue();  
     296        boolean failY = reader.getSizeY() != store.getSizeY(ii).intValue();  
     297        boolean failZ = reader.getSizeZ() != store.getSizeZ(ii).intValue();  
     298        boolean failC = reader.getSizeC() != store.getSizeC(ii).intValue();  
     299        boolean failT = reader.getSizeT() != store.getSizeT(ii).intValue();  
     300        boolean failBE =  
     301          reader.isLittleEndian() == store.getBigEndian(ii).booleanValue();  
     302        boolean failDE =  
     303          !reader.getDimensionOrder().equals(store.getDimensionOrder(ii));  
     304        boolean failType = !type.equalsIgnoreCase(store.getPixelType(ii));  
     305 
     306        if (success) { 
     307          success = failX || failY || failZ || failC || failT || failBE || 
     308            failType || failDE; 
     309        } 
     310        if (!success) break;  
     311      }  
     312      if (!success) writeLog(file + " failed OME-XML sanity test");  
     313      assert success;  
    238314    } 
    239315    catch (Exception exc) { 
    240       if (FormatHandler.debug) LogTools.trace(exc); 
    241       success = false; 
    242     } 
    243     if (!success) writeLog(id + " failed thumbnail BufferedImage test"); 
    244     assertTrue(success); 
    245   } 
    246  
    247   /** 
    248    * Checks the reported thumbnail dimensions against the size of the array 
    249    * returned by openThumbBytes. 
    250    */ 
    251   public void testThumbnailArrayDimensions() { 
    252     boolean success = true; 
    253     try { 
    254       reader.setId(id); 
    255       for (int i=0; i<reader.getSeriesCount(); i++) { 
    256         reader.setSeries(i); 
    257         int imageCount = reader.getImageCount(); 
    258         int sizeX = reader.getThumbSizeX(); 
    259         int sizeY = reader.getThumbSizeY(); 
    260         int sizeC = reader.getRGBChannelCount(); 
    261  
    262         int expectedBytes = sizeX * sizeY * sizeC; 
    263  
    264         for (int j=0; j<imageCount; j++) { 
    265           byte[] b = reader.openThumbBytes(j); 
    266           if (b.length != expectedBytes) { 
    267             success = false; 
    268             j = imageCount; 
    269             i = reader.getSeriesCount(); 
    270             break; 
    271           } 
    272         } 
    273       } 
     316      writeLog(file + " failed OME-XML sanity test"); 
     317      writeLog(exc); 
     318      assert false;  
     319    } 
     320  } 
     321 
     322  /** 
     323   * @testng.test dataProvider = "provider" 
     324   *              groups = "all fast"  
     325   */ 
     326  public void testConsistent(String file) { 
     327    try { 
     328      FileStitcher r = new FileStitcher(); 
     329      r.setId(file); 
     330 
     331      boolean success = true;; 
     332      boolean failSeries = r.getSeriesCount() != config.getNumSeries(file); 
     333        
     334      if (failSeries) { 
     335        writeLog(file + " failed consistent metadata (wrong series count)");  
     336        assert false; 
     337        return;  
     338      }  
     339 
     340      for (int i=0; i<r.getSeriesCount(); i++) { 
     341        r.setSeries(i); 
     342        config.setSeries(file, i); 
     343 
     344        boolean failX = config.getWidth(file) != r.getSizeX(); 
     345        boolean failY = config.getHeight(file) != r.getSizeY(); 
     346        boolean failZ = config.getZ(file) != r.getSizeZ(); 
     347        boolean failC = config.getC(file) != r.getSizeC(); 
     348        boolean failT = config.getT(file) != r.getSizeT(); 
     349        boolean failDim =  
     350          !config.getDimOrder(file).equals(r.getDimensionOrder());  
     351        boolean failInt = config.isInterleaved(file) != r.isInterleaved();  
     352        boolean failRGB = config.isRGB(file) != r.isRGB(); 
     353        boolean failTX = config.getThumbX(file) != r.getThumbSizeX(); 
     354        boolean failTY = config.getThumbY(file) != r.getThumbSizeY(); 
     355        boolean failType = config.getPixelType(file) != r.getPixelType(); 
     356        boolean failEndian = config.isLittleEndian(file) != r.isLittleEndian(); 
     357       
     358        success = failX || failY || failZ || failC || failT || failDim || 
     359          failInt || failRGB || failTX || failTY || failType || failEndian; 
     360      
     361        if (!success) { 
     362          writeLog(file + " failed consistent metadata test"); 
     363          assert false;  
     364          return;  
     365        } 
     366      } 
     367      assert true; 
    274368    } 
    275369    catch (Exception exc) { 
    276       if (FormatHandler.debug) LogTools.trace(exc); 
    277       success = false; 
    278     } 
    279     if (!success) writeLog(id + " failed thumbnail byte array test"); 
    280     try { reader.close(true); } 
    281     catch (Exception e) { } 
    282     assertTrue(success); 
    283   } 
    284  
    285   /** 
    286    * Checks the SizeZ, SizeC, and SizeT dimensions against the 
    287    * total image count. 
    288    */ 
    289   public void testImageCount() { 
    290     boolean success = true; 
    291     try { 
    292       reader.setId(id); 
    293       for (int i=0; i<reader.getSeriesCount(); i++) { 
    294         reader.setSeries(i); 
    295         int imageCount = reader.getImageCount(); 
    296         int sizeZ = reader.getSizeZ(); 
    297         int sizeC = reader.getEffectiveSizeC(); 
    298         int sizeT = reader.getSizeT(); 
    299         if (success) success = imageCount == sizeZ * sizeC * sizeT; 
    300         else break; 
    301       } 
    302     } 
    303     catch (Exception exc) { 
    304       if (FormatHandler.debug) LogTools.trace(exc); 
    305       success = false; 
    306     } 
    307     if (!success) writeLog(id + " failed image count test"); 
    308     try { reader.close(true); } 
    309     catch (Exception e) { } 
    310     assertTrue(success); 
    311   } 
    312  
    313   /** 
    314    * Checks that the OME-XML attribute values match the values of the core 
    315    * metadata (Size*, DimensionOrder, etc.). 
    316    */ 
    317   public void testOMEXML() { 
    318     boolean success = true; 
    319     try { 
    320       OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
    321       store.createRoot(); 
    322       reader.setMetadataStore(store); 
    323       reader.setId(id); 
    324  
    325       for (int i=0; i<reader.getSeries(); i++) { 
    326         reader.setSeries(i); 
    327         int sizeX = reader.getSizeX(); 
    328         int sizeY = reader.getSizeY(); 
    329         int sizeZ = reader.getSizeZ(); 
    330         int sizeC = reader.getSizeC(); 
    331         int sizeT = reader.getSizeT(); 
    332         boolean bigEndian = !reader.isLittleEndian(); 
    333         String type = FormatTools.getPixelTypeString(reader.getPixelType()); 
    334         String dimensionOrder = reader.getDimensionOrder(); 
    335  
    336         Integer ii = new Integer(i); 
    337         boolean failX = sizeX != store.getSizeX(ii).intValue(); 
    338         boolean failY = sizeY != store.getSizeY(ii).intValue(); 
    339         boolean failZ = sizeZ != store.getSizeZ(ii).intValue(); 
    340         boolean failC = sizeC != store.getSizeC(ii).intValue(); 
    341         boolean failT = sizeT != store.getSizeT(ii).intValue(); 
    342         boolean failBE = bigEndian != store.getBigEndian(ii).booleanValue(); 
    343         boolean failType = !type.equalsIgnoreCase(store.getPixelType(ii)); 
    344         boolean failDE = !dimensionOrder.equals(store.getDimensionOrder(ii)); 
    345         if (failX) writeLog(id + " failed OME-XML SizeX test"); 
    346         if (failY) writeLog(id + " failed OME-XML SizeY test"); 
    347         if (failZ) writeLog(id + " failed OME-XML SizeZ test"); 
    348         if (failC) writeLog(id + " failed OME-XML SizeC test"); 
    349         if (failT) writeLog(id + " failed OME-XML SizeT test"); 
    350         if (failBE) writeLog(id + " failed OME-XML BigEndian test"); 
    351         if (failType) writeLog(id + " failed OME-XML PixelType test"); 
    352         if (failDE) { 
    353           writeLog(id + " failed OME-XML DimensionOrder test"); 
    354         } 
    355  
    356         if (success) { 
    357           success = failX || failY || failZ || failC || 
    358             failT || failBE || failType || failDE; 
    359         } 
    360         else break; 
    361       } 
    362     } 
    363     catch (Exception exc) { 
    364       if (FormatHandler.debug) LogTools.trace(exc); 
    365       success = false; 
    366     } 
    367     if (!success) writeLog(id + " failed OME-XML sanity test"); 
    368     try { reader.close(true); } 
    369     catch (Exception e) { } 
    370     assertTrue(success); 
    371   } 
    372  
    373   /** 
    374    * Checks that the core metadata values match those given in 
    375    * the configuration file.  If there is no configuration file, this test 
    376    * is not run. 
    377    */ 
    378   public void testConsistent() { 
    379     boolean success = true; 
    380     try { 
    381       reader.setId(id); 
    382     } 
    383     catch (Exception exc) { 
    384       writeLog(id + " failed consistent metadata test"); 
    385       if (FormatHandler.debug) LogTools.trace(exc); 
    386       assertTrue(false); 
    387     } 
    388  
    389     if (writeConfigFiles) { 
    390       try { 
    391         // assemble the config line 
    392         configLine.append("\""); 
    393         configLine.append(new Location(id).getName()); 
    394         configLine.append("\" total_series="); 
    395         configLine.append(reader.getSeriesCount()); 
    396         for (int i=0; i<reader.getSeriesCount(); i++) { 
    397           reader.setSeries(i); 
    398           configLine.append(" [series="); 
    399           configLine.append(i); 
    400           configLine.append(" x="); 
    401           configLine.append(reader.getSizeX()); 
    402           configLine.append(" y="); 
    403           configLine.append(reader.getSizeY()); 
    404           configLine.append(" z="); 
    405           configLine.append(reader.getSizeZ()); 
    406           configLine.append(" c="); 
    407           configLine.append(reader.getSizeC()); 
    408           configLine.append(" t="); 
    409           configLine.append(reader.getSizeT()); 
    410           configLine.append(" order="); 
    411           configLine.append(reader.getDimensionOrder()); 
    412           configLine.append(" interleave="); 
    413           configLine.append(reader.isInterleaved()); 
    414           configLine.append(" rgb="); 
    415           configLine.append(reader.isRGB()); 
    416           configLine.append(" thumbx="); 
    417           configLine.append(reader.getThumbSizeX()); 
    418           configLine.append(" thumby="); 
    419           configLine.append(reader.getThumbSizeY()); 
    420           configLine.append(" type="); 
    421           configLine.append(FormatTools.getPixelTypeString( 
    422             reader.getPixelType())); 
    423           configLine.append(" little="); 
    424           configLine.append(reader.isLittleEndian()); 
    425           configLine.append("]"); 
    426         } 
    427         configLine.append(" access="); 
    428         configLine.append(averagePlaneAccess); 
    429         configLine.append(" mem="); 
    430         long len = 0; 
    431         RandomAccessStream ras = new RandomAccessStream(id); 
    432         configLine.append(ras.length()); 
    433         ras.close(); 
    434         configLine.append(" test=true\n"); 
    435  
    436         File f = new File(new Location(id).getParent(), ".bioformats"); 
    437         BufferedWriter w = new BufferedWriter(new FileWriter(f, true)); 
    438         w.write(configLine.toString()); 
    439         w.close(); 
    440       } 
    441       catch (Exception exc) { 
    442         if (FormatHandler.debug) LogTools.trace(exc); 
    443  
    444         configLine = new StringBuffer(); 
    445         configLine.append("\""); 
    446         configLine.append(new Location(id).getName()); 
    447         configLine.append("\" test=false\n"); 
    448  
    449         try { 
    450           File f = new File(new Location(id).getParent(), ".bioformats"); 
    451           BufferedWriter w = new BufferedWriter(new FileWriter(f, true)); 
    452           w.write(configLine.toString()); 
    453           w.close(); 
    454         } 
    455         catch (IOException exc2) { 
    456           if (FormatHandler.debug) LogTools.trace(exc2); 
    457           success = false; 
    458         } 
    459       } 
    460     } 
    461     else { 
    462       int nSeries = 0; 
    463       try { 
    464         nSeries = reader.getSeries(); 
    465         if (nSeries != config.getNumSeries(id)) { 
    466           success = false; 
    467           writeLog(id + " failed consistent series count check"); 
    468         } 
    469       } 
    470       catch (Exception exc) { 
    471         if (FormatHandler.debug) LogTools.trace(exc); 
     370      writeLog(file + " failed consistent metadata test"); 
     371      writeLog(exc); 
     372      assert false;  
     373    } 
     374  } 
     375 
     376  /** 
     377   * @testng.test dataProvider = "provider" 
     378   *              groups = "all"  
     379   */ 
     380  public void testMemoryUsage(String file) { 
     381    try { 
     382      Runtime r = Runtime.getRuntime(); 
     383      int maxMemory = (int) ((r.totalMemory() - r.freeMemory()) >> 20); 
     384      int initialMemory = maxMemory; 
     385 
     386      FileStitcher reader = new FileStitcher(); 
     387      reader.setId(file); 
     388      int mem = (int) ((r.totalMemory() - r.freeMemory()) >> 20); 
     389      if (mem > maxMemory) maxMemory = mem; 
     390 
     391      for (int i=0; i<reader.getImageCount(); i++) { 
     392        BufferedImage b = reader.openImage(i); 
     393        mem = (int) ((r.totalMemory() - r.freeMemory()) >> 20); 
     394        if (mem > maxMemory) maxMemory = mem; 
     395      }  
     396      int finalMemory = (int) ((r.totalMemory() - r.freeMemory()) >> 20);  
     397 
     398      boolean success = true; 
     399 
     400      // max memory usage should be no more than twice the file size  
     401      if (maxMemory - initialMemory > 2*(config.getFileSize(file) + 1)) { 
    472402        success = false; 
    473       } 
    474       if (success) { 
    475         try { 
    476           for (int i=0; i<nSeries; i++) { 
    477             config.setSeries(id, i); 
    478             reader.setSeries(i); 
    479             if (config.getWidth(id) != reader.getSizeX()) { 
    480               success = false; 
    481               writeLog(id + " failed consistent width check in series " + i); 
    482             } 
    483             if (config.getHeight(id) != reader.getSizeY()) { 
    484               success = false; 
    485               writeLog(id + " failed consistent height check in series " + i); 
    486             } 
    487             if (config.getZ(id) != reader.getSizeZ()) { 
    488               success = false; 
    489               writeLog(id + " failed consistent sizeZ check in series " + i); 
    490             } 
    491             if (config.getC(id) != reader.getSizeC()) { 
    492               success = false; 
    493               writeLog(id + " failed consistent sizeC check in series " + i); 
    494             } 
    495             if (config.getT(id) != reader.getSizeT()) { 
    496               success = false; 
    497               writeLog(id + " failed consistent sizeT check in series " + i); 
    498             } 
    499             if (!config.getDimOrder(id).equals(reader.getDimensionOrder())) { 
    500               success = false; 
    501               writeLog(id + 
    502                 " failed consistent dimension order check in series " + i); 
    503             } 
    504             if (config.isInterleaved(id) != reader.isInterleaved()) { 
    505               success = false; 
    506               writeLog(id + 
    507                 " failed consistent interleaving flag check in series " + i); 
    508             } 
    509             if (config.isRGB(id) != reader.isRGB()) { 
    510               success = false; 
    511               writeLog(id + " failed consistent RGB flag check in series " + i); 
    512             } 
    513             if (config.getThumbX(id) != reader.getThumbSizeX()) { 
    514               success = false; 
    515               writeLog(id + 
    516                 " failed consistent thumbnail width check in series " + i); 
    517             } 
    518             if (config.getThumbY(id) != reader.getThumbSizeY()) { 
    519               success = false; 
    520               writeLog(id + 
    521                 " failed consistent thumbnail height check in series " + i); 
    522             } 
    523             if (config.getPixelType(id) != reader.getPixelType()) { 
    524               success = false; 
    525               writeLog(id + 
    526                 " failed consistent pixel type check in series " + i); 
    527             } 
    528             if (config.isLittleEndian(id) != reader.isLittleEndian()) { 
    529               success = false; 
    530               writeLog(id + 
    531                 " failed consistent endianness flag check in series " + i); 
    532             } 
    533           } 
    534         } 
    535         catch (Exception exc) { 
    536           if (FormatHandler.debug) LogTools.trace(exc); 
    537           success = false; 
    538         } 
    539       } 
    540     } 
    541     assertTrue(success); 
    542   } 
    543  
    544   /** Check that the memory usage is acceptable. */ 
    545   public void testMemoryUsage() { 
    546     boolean success = true; 
    547  
    548     // we want the maximum usage to be no more than twice the file size 
    549     if (maxMemory - initialMemory > 2*(config.getFileSize(id)+1)) { 
    550       success = false; 
    551       writeLog(id + " failed maximum memory usage test (used " + 
    552         (maxMemory - initialMemory) + "MB; expected <= " + 
    553         (2*config.getFileSize(id) + 1) + "MB)"); 
    554     } 
    555  
    556     // check that the reader doesn't have any (significant) memory leaks 
    557     if (finalMemory - initialMemory > 10) { 
    558       success = false; 
    559       writeLog(id + " failed memory leak test (" + 
    560         (finalMemory - initialMemory) + "MB leaked)"); 
    561     } 
    562  
    563     assertTrue(success); 
    564   } 
    565  
    566   /** Check that the average access time per plane is reasonable. */ 
    567   public void testAccessTime() { 
    568     boolean success = true; 
    569     if (averagePlaneAccess - timeMultiplier*config.getTimePerPlane(id) > 20.0) { 
    570       success = false; 
    571       writeLog(id + " failed consistent access time test (got " + 
    572         averagePlaneAccess + " ms, expected " + config.getTimePerPlane(id) + 
    573         " ms)"); 
    574     } 
    575     assertTrue(success); 
    576   } 
    577  
    578   /** 
    579    * Check that the used file list produced by each file in a set is the same. 
    580    */ 
    581   public void testSaneUsedFiles() { 
    582     try { 
    583       reader.setId(id); 
     403        writeLog(file + " failed memory test (used " +  
     404          (maxMemory - initialMemory) + "MB; expected <= " +  
     405          (2*config.getFileSize(file) + 1) + "MB)"); 
     406      } 
     407    
     408      // check that the reader doesn't have any significant memory leaks  
     409      if (finalMemory - initialMemory > 10) { 
     410        success = false; 
     411        writeLog(file + " failed memory leak test (" + 
     412          (finalMemory - initialMemory) + "MB leaked)"); 
     413      } 
     414      assert success;  
     415    } 
     416    catch (Exception e) { 
     417      writeLog(file + " failed memory test"); 
     418      writeLog(e); 
     419      assert false;  
     420    } 
     421  } 
     422 
     423  /** 
     424   * @testng.test dataProvider = "provider" 
     425   *              groups = "all"  
     426   * @testng.parameters value = "timeMultiplier"  
     427   */ 
     428  public void testAccessTime(String file, float timeMultiplier) { 
     429    try { 
     430      FileStitcher reader = new FileStitcher(); 
     431      reader.setId(file); 
     432 
     433      long l1 = System.currentTimeMillis(); 
     434      for (int i=0; i<reader.getImageCount(); i++) { 
     435        reader.openImage(i); 
     436      } 
     437      long l2 = System.currentTimeMillis(); 
     438      if (((l2 - l1) / reader.getImageCount()) -  
     439        timeMultiplier*config.getTimePerPlane(file) > 20.0) 
     440      { 
     441        writeLog(file + " failed consistent access time test (got " + 
     442          ((l2 - l1) / reader.getImageCount()) + " ms, expected " + 
     443          config.getTimePerPlane(file) + " ms)"); 
     444        assert false;  
     445        return;  
     446      } 
     447      assert true;  
     448    } 
     449    catch (Exception e) { 
     450      writeLog(file + " failed consistent access time test"); 
     451      writeLog(e); 
     452      assert false;  
     453    } 
     454  } 
     455 
     456  /** 
     457   * @testng.test dataProvider = "provider" 
     458   *              groups = "all"  
     459   */ 
     460  public void testSaneUsedFiles(String file) { 
     461    try { 
     462      FileStitcher reader = new FileStitcher(); 
     463      reader.setId(file); 
     464       
    584465      String[] base = reader.getUsedFiles(); 
    585466      Arrays.sort(base); 
    586467 
    587       FileStitcher fs = new FileStitcher(); 
    588  
    589468      for (int i=0; i<base.length; i++) { 
    590         fs.setId(base[i]); 
    591         String[] comp = fs.getUsedFiles(); 
     469        reader.setId(base[i]); 
     470        String[] comp = reader.getUsedFiles(); 
    592471        Arrays.sort(comp); 
    593472        for (int j=0; j<comp.length; j++) { 
    594473          if (!comp[j].equals(base[j])) { 
    595             writeLog(id + " failed sane used files test (" + base[i] + ")"); 
    596             assertTrue(false); 
     474            writeLog(file + " failed sane used files test (" + base[i] + ")"); 
     475            assert false; 
     476            return;  
    597477          } 
    598478        } 
    599479      } 
    600  
    601       fs.close(); 
    602       assertTrue(true); 
    603     } 
    604     catch (Exception exc) { 
    605       if (FormatHandler.debug) LogTools.trace(exc); 
    606       writeLog(id + " failed sane used files test"); 
    607     } 
    608     assertTrue(false); 
    609   } 
    610  
    611   /** Tests that OME-XML is valid. */ 
    612   public void testValidXML() { 
     480      reader.close();  
     481      assert true;  
     482    } 
     483    catch (Exception e) { 
     484      writeLog(file + " failed sane used files test"); 
     485      writeLog(e); 
     486      assert false;  
     487    } 
     488  } 
     489 
     490  /** 
     491   * @testng.test dataProvider = "provider" 
     492   *              groups = "all xml fast"  
     493   */ 
     494  public void testValidXML(String file) { 
    613495    try { 
    614496      OMEXMLMetadataStore store = new OMEXMLMetadataStore(); 
    615497      store.createRoot(); 
     498      FileStitcher reader = new FileStitcher(); 
    616499      reader.setMetadataStore(store); 
    617       reader.setId(id); 
     500      reader.setId(file); 
    618501 
    619502      String xml = ((OMEXMLMetadataStore) reader.getMetadataStore()).dumpXML(); 
    620       if (xml == null) writeLog(id + " failed OME-XML validation"); 
    621       assertTrue(xml != null); 
     503      if (xml == null) writeLog(file + " failed OME-XML validation"); 
     504      reader.close();  
     505      assert xml != null;  
     506    } 
     507    catch (Exception e) { 
     508      writeLog(file + " failed OME-XML validation"); 
     509      writeLog(e); 
     510      assert false;  
     511    } 
     512  } 
     513 
     514  /** 
     515   * @testng.test dataProvider = "provider" 
     516   *              groups = "all pixels"  
     517   */ 
     518  public void testPixelsHashes(String file) { 
     519    try {  
     520      // check the MD5 of the first plane in each series  
     521  
     522      FileStitcher reader = new FileStitcher(); 
     523      reader.setId(file); 
     524 
     525      boolean success = false; 
     526      for (int i=0; i<reader.getSeriesCount(); i++) { 
     527        reader.setSeries(i); 
     528        config.setSeries(file, i); 
     529        String md5 = md5(reader.openBytes(0)); 
     530        if (!md5.equals(config.getMD5(file))) { 
     531          writeLog(file + " failed pixels consistency (series " + i + ")"); 
     532          success = false; 
     533          break; 
     534        } 
     535      } 
     536      assert success;  
    622537    } 
    623538    catch (Exception exc) { 
    624       writeLog(id + " failed OME-XML validation"); 
    625       LogTools.trace(exc); 
    626       assertTrue(false); 
    627     } 
    628   } 
    629  
    630   // -- TestCase API methods -- 
    631  
    632   /** Sets up the fixture. */ 
    633   protected void setUp() { 
    634     reader = new FileStitcher(); 
    635     configLine = new StringBuffer(); 
    636   } 
    637  
    638   /** Releases resources after tests have completed. */ 
    639   protected void tearDown() { 
    640     try { 
    641       reader.setId(id); 
    642       used = reader.getUsedFiles(); 
    643       reader.close(); 
    644     } 
    645     catch (FormatException exc) { 
    646       if (FormatHandler.debug) LogTools.trace(exc); 
    647     } 
    648     catch (IOException exc) { 
    649       if (FormatHandler.debug) LogTools.trace(exc); 
    650     } 
    651   } 
    652  
    653   // -- Static ReaderTest API methods -- 
    654  
    655   /** 
    656    * Creates a test suite for all ReaderTest tests, on the given file. 
    657    * This method is patterned after the suite() method for use with a 
    658    * TestRunner, but is distinct in that ReaderTest tests must be executed 
    659    * on a particular input file. 
    660    */ 
    661   public static TestSuite suite(String id) { 
    662     TestSuite suite = new TestSuite(); 
    663     if (testXML) suite.addTest(new ReaderTest("testValidXML", id)); 
    664     else { 
    665       suite.addTest(new ReaderTest("testBufferedImageDimensions", id)); 
    666       if (!writeConfigFiles) { 
    667         suite.addTest(new ReaderTest("testByteArrayDimensions", id)); 
    668         suite.addTest(new ReaderTest("testThumbnailImageDimensions", id)); 
    669         suite.addTest(new ReaderTest("testThumbnailArrayDimensions", id)); 
    670         suite.addTest(new ReaderTest("testImageCount", id)); 
    671         suite.addTest(new ReaderTest("testOMEXML", id)); 
    672         suite.addTest(new ReaderTest("testSaneUsedFiles", id)); 
    673         suite.addTest(new ReaderTest("testValidXML", id)); 
    674       } 
    675       if (config.initialized(id) || writeConfigFiles) { 
    676         suite.addTest(new ReaderTest("testConsistent", id)); 
    677       } 
    678       if (config.initialized(id) && !writeConfigFiles) { 
    679         suite.addTest(new ReaderTest("testMemoryUsage", id)); 
    680         suite.addTest(new ReaderTest("testAccessTime", id)); 
    681       } 
    682     } 
    683     return suite; 
    684   } 
    685  
    686   /** 
    687    * Determines if the given filename is a "bad" file. 
    688    * Bad files are skipped rather than tested. 
    689    */ 
    690   public static boolean isBadFile(String file) { 
    691     for (int i=0; i<configFiles.size(); i++) { 
    692       try { 
    693         String s = (String) configFiles.get(i); 
    694         if (!config.isParsed(s)) config.addFile(s); 
    695       } 
    696       catch (IOException exc) { 
    697         if (FormatHandler.debug) LogTools.trace(exc); 
    698       } 
    699     } 
    700     return !config.testFile(file) && !file.endsWith(".bioformats"); 
    701   } 
    702  
    703   /** Recursively generates a list of files to test. */ 
    704   public static void getFiles(String root, Vector files) { 
     539      writeLog(file + " failed pixels consistency"); 
     540      writeLog(exc); 
     541      assert false;  
     542    } 
     543  } 
     544 
     545  /** 
     546   * @testng.test dataProvider = "provider" 
     547   *              groups = "config"  
     548   */ 
     549  public void writeConfigFiles(String file) { 
     550    try {  
     551      FileStitcher reader = new FileStitcher(); 
     552      reader.setId(file); 
     553 
     554      StringBuffer line = new StringBuffer(); 
     555      line.append("\""); 
     556      line.append(new Location(file).getName()); 
     557      line.append("\" total_series="); 
     558      line.append(reader.getSeriesCount()); 
     559      long start = System.currentTimeMillis(); 
     560      int total = 0; 
     561      for (int i=0; i<reader.getSeriesCount(); i++) { 
     562        reader.setSeries(i); 
     563        line.append(" [series="); 
     564        line.append(i); 
     565        line.append(" x=" + reader.getSizeX()); 
     566        line.append(" y=" + reader.getSizeY()); 
     567        line.append(" z=" + reader.getSizeZ()); 
     568        line.append(" c=" + reader.getSizeC()); 
     569        line.append(" t=" + reader.getSizeT()); 
     570        line.append(" order=" + reader.getDimensionOrder()); 
     571        line.append(" interleave=" + reader.isInterleaved()); 
     572        line.append(" rgb=" + reader.isRGB()); 
     573        line.append(" thumbx=" + reader.getThumbSizeX()); 
     574        line.append(" thumby=" + reader.getThumbSizeY()); 
     575        line.append(" type=" +  
     576          FormatTools.getPixelTypeString(reader.getPixelType())); 
     577        line.append(" little=" + reader.isLittleEndian()); 
     578        line.append(" md5=" + md5(reader.openBytes(0))); 
     579        line.append("]"); 
     580     
     581        total += reader.getImageCount(); 
     582        for (int j=1; j<reader.getImageCount(); j++) reader.openImage(j);  
     583      } 
     584      long end = System.currentTimeMillis();  
     585   
     586      line.append(" access="); 
     587      line.append((end - start) / total); 
     588      line.append(" mem="); 
     589      line.append(new RandomAccessStream(file).length()); 
     590      line.append(" test=true\n"); 
     591 
     592      File f = new File(new Location(file).getParent(), ".bioformats"); 
     593      BufferedWriter w = new BufferedWriter(new FileWriter(f, true)); 
     594      w.write(line.toString()); 
     595      w.close(); 
     596      assert true;  
     597    } 
     598    catch (Exception e) { 
     599      writeLog(file + " failed to write config file"); 
     600      writeLog(e); 
     601    } 
     602    assert false;  
     603  } 
     604 
     605  // -- Helper methods -- 
     606 
     607  /** Calculate the MD5 of a byte array. */ 
     608  private static String md5(byte[] b) { 
     609    try {  
     610      MessageDigest md = MessageDigest.getInstance("MD5"); 
     611      md.reset(); 
     612      md.update(b); 
     613      byte[] digest = md.digest(); 
     614      StringBuffer sb = new StringBuffer();  
     615      for (int i=0; i<digest.length; i++) { 
     616        String a = Integer.toHexString(0xff & digest[i]); 
     617        if (a.length() == 1) a = "0" + a; 
     618        sb.append(a); 
     619      } 
     620      return sb.toString();  
     621    } 
     622    catch (Exception e) { 
     623      writeLog(e);  
     624    } 
     625    return null;  
     626  } 
     627 
     628  /** Writes the given message to the log file. */ 
     629  private static void writeLog(String s) { 
     630    if (logFile == null) createLogFile(); 
     631    LogTools.println(s); 
     632    try { logFile.flush(); } 
     633    catch (IOException exc) { } 
     634  } 
     635 
     636  /** Writes the given exception's stack trace to the log file. */ 
     637  private static void writeLog(Exception e) { 
     638    if (logFile == null) createLogFile(); 
     639    LogTools.trace(e); 
     640    try { logFile.flush(); } 
     641    catch (IOException exc) { } 
     642  } 
     643 
     644  /** Creates a new log file. */ 
     645  private static void createLogFile() { 
     646    try {  
     647      String date = new Date().toString().replaceAll(":", "-"); 
     648      logFile = new FileWriter("bio-formats-test-" + date + ".log"); 
     649      logFile.flush(); 
     650      TestLogger log = new TestLogger(logFile); 
     651      LogTools.setLog(log); 
     652    } 
     653    catch (IOException e) { 
     654 
     655    } 
     656  } 
     657 
     658  /** Recursively generate a list of files to test. */ 
     659  private static void getFiles(String root, Vector files) { 
    705660    Location f = new Location(root); 
    706661    String[] subs = f.list(); 
    707     f = null; 
    708662    Arrays.sort(subs); 
    709663 
     
    715669        subs[i] = tmp; 
    716670        break; 
    717       } 
    718     } 
    719  
     671      }  
     672    }  
     673   
    720674    if (subs == null) { 
    721675      LogTools.println("Invalid directory: " + root); 
    722676      return; 
    723677    } 
     678  
    724679    ImageReader ir = new ImageReader(); 
    725680    Vector similarFiles = new Vector(); 
     681 
    726682    for (int i=0; i<subs.length; i++) { 
    727       if (FormatHandler.debug) debug("Checking file " + subs[i]); 
    728       subs[i] = root + (root.endsWith(File.separator) ? "" : File.separator) + 
    729         subs[i]; 
    730       if (isBadFile(subs[i]) || similarFiles.contains(subs[i]) || 
    731         similarFiles.contains(new Location(root, subs[i]).getAbsolutePath())) 
    732       { 
    733         if (FormatHandler.debug) debug(subs[i] + " is a bad file"); 
     683      LogTools.println("Checking file " + subs[i]); 
     684      subs[i] = new Location(root, subs[i]).getAbsolutePath(); 
     685      if (isBadFile(subs[i]) || similarFiles.contains(subs[i])) { 
     686        LogTools.println(subs[i] + " is a bad file"); 
    734687        String[] matching = new FilePattern(subs[i]).getFiles(); 
    735688        for (int j=0; j<matching.length; j++) { 
    736689          similarFiles.add(new Location(root, matching[j]).getAbsolutePath()); 
    737690        } 
    738         continue; 
     691        continue;  
    739692      } 
    740693      Location file = new Location(subs[i]); 
     
    747700      else { 
    748701        if (ir.isThisType(subs[i])) { 
    749           if (FormatHandler.debug) debug("Adding " + subs[i]); 
     702          LogTools.println("Adding " + subs[i]); 
    750703          files.add(file.getAbsolutePath()); 
    751704        } 
    752         else if (FormatHandler.debug) debug(subs[i] + " has invalid type"); 
    753       } 
    754       file = null; 
    755     } 
    756   } 
    757  
    758   /** Writes the given message to the log file. */ 
    759   public static void writeLog(String s) { 
    760     if (logFile == null) { 
    761       try { 
    762         String date = new Date().toString().replaceAll(":", "-"); 
    763         logFile = new FileWriter("bio-formats-test-" + date + ".log"); 
    764         logFile.flush(); 
    765       } 
     705        else LogTools.println(subs[i] + " has invalid type");  
     706      } 
     707      file = null;  
     708    }   
     709  }  
     710 
     711  /** Determines if the given file is "bad" (bad files are not tested). */ 
     712  private static boolean isBadFile(String file) { 
     713    for (int i=0; i<configFiles.size(); i++) { 
     714      try {  
     715        String s = (String) configFiles.get(i); 
     716        if (!config.isParsed(s)) config.addFile(s); 
     717      }  
    766718      catch (IOException exc) { 
    767         if (FormatHandler.debug) LogTools.trace(exc); 
    768       } 
    769     } 
    770     try { 
    771       logFile.write(s + "\n"); 
    772       logFile.flush(); 
    773     } 
    774     catch (IOException exc) { 
    775       if (FormatHandler.debug) LogTools.trace(exc); 
    776     } 
    777   } 
    778  
    779   public static void debug(String s) { LogTools.println(s); } 
     719        LogTools.trace(exc); 
     720      } 
     721    } 
     722    return !config.testFile(file) && !file.endsWith(".bioformats");  
     723  } 
     724 
     725  // -- Helper class -- 
     726  
     727  public static class TestLogger extends Log { 
     728    private FileWriter writer;  
     729 
     730    public TestLogger(FileWriter writer) { 
     731      this.writer = writer; 
     732    } 
     733   
     734    public void print(String x) { 
     735      try {  
     736        if (writer != null) writer.write(x); 
     737      } 
     738      catch (IOException exc) { } 
     739    } 
     740  } 
    780741 
    781742  // -- Main method -- 
    782743 
    783744  public static void main(String[] args) { 
    784     if (args.length > 0) { 
    785       for (int i=1; i<args.length; i++) { 
    786         if (args[i].equals("-config")) ReaderTest.writeConfigFiles = true; 
    787         else if (args[i].equals("-debug")) FormatHandler.setDebug(true); 
    788         else if (args[i].equals("-xmlonly")) ReaderTest.testXML = true; 
    789         else if (args[i].equals("-time")) { 
    790           ReaderTest.timeMultiplier = Float.parseFloat(args[i+1]); 
    791         } 
    792       } 
    793     } 
    794     Vector files = new Vector(); 
    795     if (args == null || args.length == 0) { 
    796       LogTools.println( 
    797         "Please specify root folder to search for data files."); 
    798       System.exit(1); 
    799     } 
    800     LogTools.print("Building file list..."); 
    801     if (FormatHandler.debug) LogTools.println(); 
    802     getFiles(new Location(args[0]).getAbsolutePath(), files); 
    803     LogTools.println(files.size() + " found."); 
    804     while (files.size() > 0) { 
    805       String id = (String) files.elementAt(0); 
    806       String pattern = FilePattern.findPattern(new Location(id)); 
    807       if (pattern == null) pattern = id; 
    808       LogTools.println("Testing " + pattern); 
    809       TestResult result = new TestResult(); 
    810       TestSuite suite = suite(id); 
    811       suite.run(result); 
    812       int total = result.runCount(); 
    813       int failed = result.failureCount(); 
    814       float failPercent = (float) (100 * ((double) failed / (double) total)); 
    815       LogTools.println(id + " - " + failed + " failures in " + 
    816         total + " tests (" + failPercent + "% failed)"); 
    817  
    818       // remove files part of the just-tested dataset from the list 
    819       ReaderTest test = (ReaderTest) suite.testAt(0); 
    820       String[] used = test.getUsedFiles(); 
    821       if (used == null) { 
    822         LogTools.println("Warning: used files list is null for " + id); 
    823  
    824         used = new FilePattern(pattern).getFiles(); 
    825         if (used != null) { 
    826           for (int i=0; i<used.length; i++) { 
    827             if (FormatHandler.debug) LogTools.println("Removing " + used[i]); 
    828             files.removeElement(used[i]); 
    829           } 
    830         } 
    831         else files.removeElementAt(0); 
    832       } 
    833       else { 
    834         for (int i=0; i<used.length; i++) { 
    835           if (FormatHandler.debug) LogTools.println("Removing " + used[i]); 
    836           files.removeElement(used[i]); 
    837         } 
    838       } 
    839       while (files.contains(id)) files.remove(id); 
    840  
    841       test.close(); 
    842     } 
    843   } 
     745    if (args.length < 3) { 
     746      System.out.println("Usage:\njava loci.formats.test.ReaderTest " + 
     747        "<all | fast | xml | pixels | config> <data directory> " + 
     748        "<timing multiplier>"); 
     749    } 
     750 
     751    ReaderTest.root = args[1];  
     752 
     753    XmlSuite suite = new XmlSuite(); 
     754    suite.setName("bftest"); 
     755 
     756    Hashtable params = new Hashtable(); 
     757    params.put("timeMultiplier", args[2]); 
     758    suite.setParameters(params); 
     759 
     760    XmlTest test = new XmlTest(suite); 
     761    test.setName("bf-" + args[0]); 
     762    List classes = new ArrayList(); 
     763    classes.add(new XmlClass("loci.formats.test.ReaderTest")); 
     764    test.setXmlClasses(classes);  
     765    List groups = new ArrayList(); 
     766    groups.add(args[0]); 
     767    test.setIncludedGroups(groups); 
     768 
     769    List suites = new ArrayList(); 
     770    suites.add(suite); 
     771    TestNG tng = new TestNG(); 
     772    tng.setSourcePath(".");  
     773    tng.setXmlSuites(suites); 
     774    tng.run(); 
     775  } 
     776 
    844777} 
Note: See TracChangeset for help on using the changeset viewer.