Changeset 6253


Ignore:
Timestamp:
05/10/10 13:34:40 (10 years ago)
Author:
bdezonia
Message:

added reflection code for testing private vars of ImagePlus

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/loci-plugins/test/loci/plugins/in/ImporterTest.java

    r6251 r6253  
    77 
    88import java.io.IOException; 
     9import java.lang.reflect.Field; 
    910 
    1011import loci.common.Region; 
     
    1718// TODO 
    1819//  - flesh out existing tests 
     20//      get reflection code working 
     21//      write test for open series code 
     22//      write tests for split options 
     23//      write tests for the color options 
     24//      concat follows - see my test to see if sufficient 
     25//      lowest priority - record modifications 
    1926//  - add some tests for combination of options 
    2027 
     
    150157  } 
    151158   
     159  private int getField(ImagePlus imp, String fieldName) { 
     160    Exception exc = null; 
     161    try { 
     162      Field field = ImagePlus.class.getDeclaredField(fieldName); 
     163      field.setAccessible(true); 
     164      Object value = field.get(imp); 
     165      return (Integer) value; 
     166    } 
     167    catch (SecurityException e) { exc = e; } 
     168    catch (NoSuchFieldException e) { exc = e; } 
     169    catch (IllegalArgumentException e) { exc = e; } 
     170    catch (IllegalAccessException e) { exc = e; } 
     171    exc.printStackTrace(); 
     172    return -1; 
     173  } 
     174   
     175  private int getSizeZ(ImagePlus imp) { return getField(imp, "nSlices"); } 
     176  private int getSizeT(ImagePlus imp) { return getField(imp, "nFrames"); } 
     177  private int getEffectiveSizeC(ImagePlus imp) { return getField(imp, "nChannels"); } 
     178   
     179  // ****** helper tests **************************************************************************************** 
     180   
    152181  private void defaultBehaviorTest(int pixType, int x, int y, int z, int c, int t) 
    153182  { 
     
    171200    assertEquals(x,ip.getWidth()); 
    172201    assertEquals(y,ip.getHeight()); 
     202    /* 
    173203    assertEquals(z,ip.getNSlices());    // tricky - these last 3 getters have side effects that change their output. 
    174204    assertEquals(c,ip.getNChannels());  // TODO - How to test? 
    175205    assertEquals(t,ip.getNFrames()); 
     206    */ 
     207    assertEquals(z,getSizeZ(ip)); 
     208    assertEquals(t,getSizeT(ip)); 
     209    assertEquals(c,getEffectiveSizeC(ip)); 
    176210  } 
    177211   
     
    279313  } 
    280314 
     315  private void datasetOpenAllSeriesTest() 
     316  { 
     317    // TODO - fill me in 
     318  } 
     319   
    281320  private void datasetConcatenateTest(int pixType, String order, 
    282321      int x, int y, int z, int c, int t, int s) 
     
    455494  public void testDatasetGroupFiles() 
    456495  { 
    457     // TODO - need to enhance FakeFiles first I think 
    458     //   This option kicks in when you have similarly named files. all the files get loaded 
    459     //   as one dataset. This relies on the filename differing only by an index. Not sure 
    460     //   what an index in a fake filename would do. Tried adding -1 before .fake to see what 
    461     //   would happen and BF crashes with negArraySizeExcep   
    462   } 
    463  
     496    String path = constructFakeFilename("group", FormatTools.INT16, 50, 50, 1, 1, 1, -1); 
     497    ImagePlus[] imps = null; 
     498    try { 
     499      ImporterOptions options = new ImporterOptions(); 
     500      options.setGroupFiles(true); 
     501      options.setId(path); 
     502      imps = BF.openImagePlus(options); 
     503    } 
     504    catch (IOException e) { 
     505      fail(e.getMessage()); 
     506    } 
     507    catch (FormatException e) { 
     508      fail(e.getMessage()); 
     509      } 
     510     
     511    // TODO - flesh this out and add asserts 
     512  } 
     513 
     514  @Test 
     515  public void testDatasetOpenFilesIndividually() 
     516  { 
     517    // TODO - do something. Did Curtis say this did not need testing. I didn't have it. 
     518    //   May have been an oversight but maybe not. 
     519  } 
     520   
    464521  @Test 
    465522  public void testDatasetSwapDims() 
     
    479536   
    480537  @Test 
     538  public void testDatasetOpenAllSeries() 
     539  { 
     540    // TODO - add cases 
     541    datasetOpenAllSeriesTest(); 
     542  } 
     543   
     544  @Test 
    481545  public void testDatasetConcatenate() 
    482546  { 
     
    547611    z=5; c=4; t=6; zFrom=1; zTo=4; zBy=2; cFrom=1; cTo=3; cBy=1; tFrom=2; tTo=6; tBy=2; 
    548612    memorySpecifyRangeTest(z,c,t,zFrom,zTo,zBy,cFrom,cTo,cBy,tFrom,tTo,tBy); 
     613     
     614    // TODO 
     615    //  1) more combos 
     616    //  2) noticed I am always setting from/to/by at the same time. test setting only one at a time (pass -1 to ignore) 
    549617  } 
    550618   
     
    588656    tester.testOutputStackOrder(); 
    589657    tester.testDatasetGroupFiles(); 
     658    tester.testDatasetOpenFilesIndividually(); 
    590659    tester.testDatasetSwapDims(); 
     660    tester.testDatasetOpenAllSeries(); 
    591661    tester.testDatasetConcatenate(); 
    592662    tester.testColorMerge(); 
     
    789859 
    790860*/ 
     861 
     862/*  notes 
     863 
     864 public static void main(String[] args) throws FormatException, IOException { 
     865   String[] files = { 
     866     "test_C1_TP1&sizeZ=7.fake", 
     867     "test_C2_TP1&sizeZ=7.fake", 
     868     "test_C3_TP1&sizeZ=7.fake", 
     869     "test_C1_TP2&sizeZ=7.fake", 
     870     "test_C2_TP2&sizeZ=7.fake", 
     871     "test_C3_TP2&sizeZ=7.fake", 
     872     "test_C1_TP3&sizeZ=7.fake", 
     873     "test_C2_TP3&sizeZ=7.fake", 
     874     "test_C3_TP3&sizeZ=7.fake", 
     875     "test_C1_TP4&sizeZ=7.fake", 
     876     "test_C2_TP4&sizeZ=7.fake", 
     877     "test_C3_TP4&sizeZ=7.fake", 
     878     "test_C1_TP5&sizeZ=7.fake", 
     879     "test_C2_TP5&sizeZ=7.fake", 
     880     "test_C3_TP5&sizeZ=7.fake", 
     881     "outlier.txt" 
     882   }; 
     883   for (String file : files) Location.mapId(file, "x"); // "x" is irrelevant 
     884 
     885   String id = files[0]; 
     886   IFormatReader reader = new FileStitcher(); 
     887   IMetadata meta = MetadataTools.createOMEXMLMetadata(); 
     888   reader.setMetadataStore(meta); 
     889   reader.setId(id); 
     890   String[] usedFiles = reader.getUsedFiles(); 
     891   int sizeX = reader.getSizeX(); 
     892   int sizeY = reader.getSizeY(); 
     893   int sizeZ = reader.getSizeZ(); 
     894   int sizeC = reader.getSizeC(); 
     895   int sizeT = reader.getSizeT(); 
     896   reader.close(); 
     897   System.out.println("Path = " + id); 
     898   System.out.println("X size = " + sizeX); 
     899   System.out.println("Y size = " + sizeY); 
     900   System.out.println("Z size = " + sizeZ); 
     901   System.out.println("C size = " + sizeC); 
     902   System.out.println("T size = " + sizeT); 
     903   System.out.println("Used files ="); 
     904   for (String used : usedFiles) System.out.println("\t" + used); 
     905 } 
     906That's how you synthesize a virtual directory on disk, essentially, for the file grouping logic to use. 
     907The key is to register each fake filename with "Location.mapId(filename, dummy)" where "dummy" doesn't matter. As long as it's not null. 
     908 
     909Then below, I call "getUsedFiles" to get a list of the constituent filenames, which you can compare against your expectations. 
     910 
     911 */ 
Note: See TracChangeset for help on using the changeset viewer.