Changeset 6876


Ignore:
Timestamp:
09/03/10 06:01:34 (9 years ago)
Author:
callan
Message:

Some further test case cleanup. Added a check to ChannelFiller to handle cases where reading the first pixel of a file fails, which is now logged rather than being fatal. Also added a test case for the functionality. (Fixes #548)

Location:
trunk/components/bio-formats
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/components/bio-formats/src/loci/formats/ChannelFiller.java

    r6679 r6876  
    2525 
    2626import java.io.IOException; 
     27 
     28import org.slf4j.Logger; 
     29import org.slf4j.LoggerFactory; 
    2730 
    2831import loci.formats.meta.MetadataStore; 
     
    4144public class ChannelFiller extends ReaderWrapper { 
    4245 
     46  // -- Constants -- 
     47 
     48  private static final Logger LOGGER = 
     49    LoggerFactory.getLogger(ChannelFiller.class); 
     50 
    4351  // -- Utility methods -- 
    4452 
     
    260268    // NB: For some formats, LUTs are plane-specific and will 
    261269    // only be available after opening a particular image plane. 
    262     reader.openBytes(0, 0, 0, 1, 1); // read a single pixel, for performance 
     270    try { 
     271      reader.openBytes(0, 0, 0, 1, 1); // read a single pixel, for performance 
     272    } catch (Exception e) { 
     273      LOGGER.warn("Unable to read initial single pixel.", e); 
     274    } 
    263275    lut8 = reader.get8BitLookupTable(); 
    264276    if (lut8 != null) return lut8.length; 
  • trunk/components/bio-formats/test/loci/formats/utests/BaseModelMock.java

    r6860 r6876  
    3636 
    3737 
    38 import java.io.ByteArrayOutputStream; 
    3938import java.io.FileOutputStream; 
    4039import java.io.OutputStream; 
    41 import java.io.OutputStreamWriter; 
    42 import java.io.UnsupportedEncodingException; 
    4340 
    4441import javax.xml.parsers.DocumentBuilder; 
    4542import javax.xml.parsers.DocumentBuilderFactory; 
    46 import javax.xml.transform.OutputKeys; 
    47 import javax.xml.transform.Result; 
    48 import javax.xml.transform.Source; 
    49 import javax.xml.transform.Transformer; 
    50 import javax.xml.transform.TransformerException; 
    51 import javax.xml.transform.TransformerFactory; 
    52 import javax.xml.transform.dom.DOMSource; 
    53 import javax.xml.transform.stream.StreamResult; 
    5443 
    5544import org.w3c.dom.Document; 
    5645import org.w3c.dom.Element; 
    57 import org.w3c.dom.Node; 
    58 import org.w3c.dom.NodeList; 
    5946 
    6047import ome.xml.model.BinData; 
     
    181168  } 
    182169 
    183   public String asString(Document document) 
    184   throws TransformerException, UnsupportedEncodingException { 
    185     TransformerFactory transformerFactory = 
    186       TransformerFactory.newInstance(); 
    187     Transformer transformer = transformerFactory.newTransformer(); 
    188     //Setup indenting to "pretty print" 
    189     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    190     transformer.setOutputProperty( 
    191         "{http://xml.apache.org/xslt}indent-amount", "4"); 
    192     Source source = new DOMSource(document); 
    193     ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    194     Result result = new StreamResult(new OutputStreamWriter(os, "utf-8")); 
    195     transformer.transform(source, result); 
    196     return os.toString(); 
    197   } 
    198  
    199   public void postProcess(Element root, Document document) { 
    200     root.setAttribute("xmlns", XML_NS); 
    201     root.setAttribute("xmlns:xsi", XSI_NS); 
    202     root.setAttribute("xsi:schemaLocation", XML_NS + " " + SCHEMA_LOCATION); 
    203     document.appendChild(root); 
    204     // Put the planar data into each <BinData/> 
    205     NodeList binDataNodes = document.getElementsByTagName("BinData"); 
    206     for (int i = 0; i < binDataNodes.getLength(); i++) { 
    207       Node binDataNode = binDataNodes.item(i); 
    208       binDataNode.setTextContent(PLANE); 
    209     } 
    210   } 
    211  
    212170  public static void main(String[] args) throws Exception { 
    213171    BaseModelMock mock = new BaseModelMock(); 
     
    217175    // Produce a valid OME DOM element hierarchy 
    218176    Element root = mock.ome.asXMLElement(document); 
    219     mock.postProcess(root, document); 
     177    SPWModelMock.postProcess(root, document, true); 
    220178    // Produce string XML 
    221179    OutputStream outputStream = new FileOutputStream(args[0]); 
    222     outputStream.write(mock.asString(document).getBytes()); 
     180    outputStream.write(SPWModelMock.asString(document).getBytes()); 
    223181  } 
    224182 
  • trunk/components/bio-formats/test/loci/formats/utests/BaseModelReaderTest.java

    r6860 r6876  
    2929import java.io.File; 
    3030 
     31import loci.formats.ChannelFiller; 
     32import loci.formats.ChannelSeparator; 
    3133import loci.formats.IFormatReader; 
    3234import loci.formats.ImageReader; 
     
    6163    mock = new BaseModelMock(); 
    6264    temporaryFile = File.createTempFile(this.getClass().getName(), ".ome"); 
    63     SPWModelReaderTest.writeMockToFile(mock, temporaryFile); 
     65    SPWModelReaderTest.writeMockToFile(mock, temporaryFile, true); 
    6466  } 
    6567 
     
    7173  @Test 
    7274  public void testSetId() throws Exception { 
    73     reader = new MinMaxCalculator(new ImageReader()); 
     75    reader = new MinMaxCalculator(new ChannelSeparator( 
     76        new ChannelFiller(new ImageReader()))); 
    7477    metadata = new OMEXMLMetadataImpl(); 
    7578    reader.setMetadataStore(metadata); 
  • trunk/components/bio-formats/test/loci/formats/utests/ModelMock.java

    r6860 r6876  
    3636 
    3737 
    38 import java.io.UnsupportedEncodingException; 
    39  
    40 import javax.xml.transform.TransformerException; 
    41  
    42 import org.w3c.dom.Document; 
    43 import org.w3c.dom.Element; 
    44  
    4538import ome.xml.model.OME; 
    4639 
     
    5649  public OME getRoot(); 
    5750 
    58   public void postProcess(Element root, Document document); 
    59  
    60   public String asString(Document document) 
    61   throws TransformerException, UnsupportedEncodingException; 
    62  
    6351} 
  • trunk/components/bio-formats/test/loci/formats/utests/ObjectBasedOMEModelMock.java

    r6543 r6876  
    5454import ome.xml.model.LongAnnotation; 
    5555import ome.xml.model.OME; 
    56 import ome.xml.model.OMEModel; 
    5756import ome.xml.model.OTF; 
    5857import ome.xml.model.Objective; 
  • trunk/components/bio-formats/test/loci/formats/utests/SPWModelMock.java

    r6860 r6876  
    438438  } 
    439439 
    440   public String asString(Document document) 
     440  public static String asString(Document document) 
    441441  throws TransformerException, UnsupportedEncodingException { 
    442442    TransformerFactory transformerFactory = 
     
    454454  } 
    455455 
    456   public void postProcess(Element root, Document document) { 
     456  public static void postProcess(Element root, Document document, 
     457                                 boolean withBinData) { 
    457458    root.setAttribute("xmlns", XML_NS); 
    458459    root.setAttribute("xmlns:xsi", XSI_NS); 
     
    460461    document.appendChild(root); 
    461462    // Put the planar data into each <BinData/> 
    462     NodeList binDataNodes = document.getElementsByTagName("BinData"); 
    463     for (int i = 0; i < binDataNodes.getLength(); i++) { 
    464       Node binDataNode = binDataNodes.item(i); 
    465       binDataNode.setTextContent(PLANE); 
     463    if (withBinData) { 
     464      NodeList binDataNodes = document.getElementsByTagName("BinData"); 
     465      for (int i = 0; i < binDataNodes.getLength(); i++) { 
     466        Node binDataNode = binDataNodes.item(i); 
     467        binDataNode.setTextContent(PLANE); 
     468      } 
    466469    } 
    467470  } 
     
    474477    // Produce a valid OME DOM element hierarchy 
    475478    Element root = mock.ome.asXMLElement(document); 
    476     mock.postProcess(root, document); 
     479    SPWModelMock.postProcess(root, document, true); 
    477480    // Produce string XML 
    478481    OutputStream outputStream = new FileOutputStream(args[0]); 
    479     outputStream.write(mock.asString(document).getBytes()); 
     482    outputStream.write(SPWModelMock.asString(document).getBytes()); 
    480483  } 
    481484 
  • trunk/components/bio-formats/test/loci/formats/utests/SPWModelReaderTest.java

    r6871 r6876  
    3636import javax.xml.parsers.DocumentBuilderFactory; 
    3737 
     38import loci.formats.ChannelFiller; 
     39import loci.formats.ChannelSeparator; 
    3840import loci.formats.IFormatReader; 
    3941import loci.formats.ImageReader; 
     
    8183    temporaryFileWithNoLightSources =  
    8284      File.createTempFile(this.getClass().getName(), ".ome"); 
    83     writeMockToFile(mock, temporaryFile); 
    84     writeMockToFile(mockWithNoLightSources, temporaryFileWithNoLightSources); 
     85    writeMockToFile(mock, temporaryFile, true); 
     86    writeMockToFile(mockWithNoLightSources, temporaryFileWithNoLightSources, 
     87                    true); 
    8588  } 
    8689 
     
    8992   * @param mock Mock to build a DOM tree of and serialize to XML. 
    9093   * @param file File to write serialized XML to. 
     94   * @param withBinData Whether or not to do BinData post processing. 
    9195   * @throws Exception If there is an error writing the XML to the file. 
    9296   */ 
    93   public static void writeMockToFile(ModelMock mock, File file) 
    94   throws Exception { 
     97  public static void writeMockToFile(ModelMock mock, File file, 
     98  boolean withBinData) throws Exception { 
    9599    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    96100    DocumentBuilder parser = factory.newDocumentBuilder(); 
     
    98102    // Produce a valid OME DOM element hierarchy 
    99103    Element root = mock.getRoot().asXMLElement(document); 
    100     mock.postProcess(root, document); 
     104    SPWModelMock.postProcess(root, document, withBinData); 
    101105    // Write the OME DOM to the requested file 
    102106    OutputStream stream = new FileOutputStream(file); 
    103     stream.write(mock.asString(document).getBytes()); 
     107    stream.write(SPWModelMock.asString(document).getBytes()); 
    104108  } 
    105109 
     
    112116  @Test 
    113117  public void testSetId() throws Exception { 
    114     reader = new MinMaxCalculator(new ImageReader()); 
     118    reader = new MinMaxCalculator(new ChannelSeparator( 
     119        new ChannelFiller(new ImageReader()))); 
    115120    metadata = new OMEXMLMetadataImpl(); 
    116121    reader.setMetadataStore(metadata); 
     
    120125  @Test 
    121126  public void testSetIdWithNoLightSources() throws Exception { 
    122     readerWithNoLightSources = new MinMaxCalculator(new ImageReader()); 
     127    readerWithNoLightSources = new MinMaxCalculator(new ChannelSeparator( 
     128        new ChannelFiller(new ImageReader()))); 
    123129    metadataWithNoLightSources = new OMEXMLMetadataImpl(); 
    124130    readerWithNoLightSources.setMetadataStore(metadataWithNoLightSources); 
  • trunk/components/bio-formats/test/loci/formats/utests/testng.xml

    r6860 r6876  
    7979        <class name="loci.formats.utests.SPWModelReaderTest"/> 
    8080        <class name="loci.formats.utests.BaseModelReaderTest"/> 
     81        <class name="loci.formats.utests.BaseModelNoBinDataReaderTest"/> 
    8182      </classes> 
    8283    </test> 
Note: See TracChangeset for help on using the changeset viewer.