stephan     2003/04/16 06:47:59

  Modified:    src/blocks/chaperon/test/org/apache/cocoon/transformation
                        LexicalTransformerTestCase.java
                        ParserTransformerTestCase.java
                        PatternTransformerTestCase.java
               src/test/org/apache/cocoon/transformation
                        AbstractTransformerTestCase.java
  Log:
  AbstractTransformerTestCase rewritten to be more intuitionally, and
  allow to test more than the generated document.
  The Chaperon testcases seems to be broken cause of the known NS problem.
  
  Revision  Changes    Path
  1.2       +15 -10    
cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/LexicalTransformerTestCase.java
  
  Index: LexicalTransformerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/LexicalTransformerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- LexicalTransformerTestCase.java   9 Apr 2003 12:24:35 -0000       1.1
  +++ LexicalTransformerTestCase.java   16 Apr 2003 13:47:57 -0000      1.2
  @@ -65,22 +65,27 @@
   
       public LexicalTransformerTestCase(String name) {
           super(name);
  +    }
   
  -        String transformer = "lexer";
  -        HashMap objectmodel = new HashMap();
  +    public void testLexicalTransformer1() {
           String src = 
"resource://org/apache/cocoon/transformation/lexertest-lexicon1.xml";
           Parameters parameters = new Parameters();
           String input = 
"resource://org/apache/cocoon/transformation/lexertest-input1.xml";
           String result = 
"resource://org/apache/cocoon/transformation/lexertest-result1.xml";
   
  -        addTestStep(transformer, objectmodel, src, parameters, input, result,
  -                    EQUAL);
  +        print(load(input));
  +        print(load(src));
   
  -        src = 
"resource://org/apache/cocoon/transformation/lexertest-lexicon2.xml";
  -        input = 
"resource://org/apache/cocoon/transformation/lexertest-input2.xml";
  -        result = 
"resource://org/apache/cocoon/transformation/lexertest-result2.xml";
  +        assertEqual(load(result), transform("lexer", src, parameters, 
load(input)));
  +    }
   
  -        addTestStep(transformer, objectmodel, src, parameters, input, result,
  -                    EQUAL);
  +    public void testLexicalTransformer2() {
  +
  +        String src = 
"resource://org/apache/cocoon/transformation/lexertest-lexicon2.xml";
  +        Parameters parameters = new Parameters();
  +        String input = 
"resource://org/apache/cocoon/transformation/lexertest-input2.xml";
  +        String result = 
"resource://org/apache/cocoon/transformation/lexertest-result2.xml";
  +
  +        assertEqual(load(result), transform("lexer", src, parameters, 
load(input)));
       }
   }
  
  
  
  1.2       +22 -17    
cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/ParserTransformerTestCase.java
  
  Index: ParserTransformerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/ParserTransformerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParserTransformerTestCase.java    9 Apr 2003 12:24:35 -0000       1.1
  +++ ParserTransformerTestCase.java    16 Apr 2003 13:47:57 -0000      1.2
  @@ -65,29 +65,34 @@
   
       public ParserTransformerTestCase(String name) {
           super(name);
  +    }
  +
  +    public void testParserTransformer1() {
   
  -        String transformer = "parser";
  -        HashMap objectmodel = new HashMap();
           String src = 
"resource://org/apache/cocoon/transformation/parsertest-grammar1.xml";
           Parameters parameters = new Parameters();
           String input = 
"resource://org/apache/cocoon/transformation/parsertest-input1.xml";
           String result = 
"resource://org/apache/cocoon/transformation/parsertest-result1.xml";
   
  -        addTestStep(transformer, objectmodel, src, parameters, input, result,
  -                    EQUAL);
  +        assertEqual(load(result), transform("parser", src, parameters, 
load(input)));
  +    }
  +
  +    public void testParserTransformer2() {
  +
  +        String src = 
"resource://org/apache/cocoon/transformation/parsertest-grammar1.xml";
  +        Parameters parameters = new Parameters();
  +        String input = 
"resource://org/apache/cocoon/transformation/parsertest-input2.xml";
  +        String result = 
"resource://org/apache/cocoon/transformation/parsertest-result2.xml";
  +
  +        assertEqual(load(result), transform("recovery-parser", src, 
parameters, load(input)));
  +    }
   
  -        transformer = "recovery-parser";
  -        src = 
"resource://org/apache/cocoon/transformation/parsertest-grammar1.xml";
  -        input = 
"resource://org/apache/cocoon/transformation/parsertest-input2.xml";
  -        result = 
"resource://org/apache/cocoon/transformation/parsertest-result2.xml";
  -
  -        addTestStep(transformer, objectmodel, src, parameters, input, result,
  -                    EQUAL);
  -
  -        src = 
"resource://org/apache/cocoon/transformation/parsertest-grammar1.xml";
  -        input = 
"resource://org/apache/cocoon/transformation/parsertest-input3.xml";
  -        result = 
"resource://org/apache/cocoon/transformation/parsertest-result3.xml";
  +    public void testParserTransformer3() {
  +        String src = 
"resource://org/apache/cocoon/transformation/parsertest-grammar1.xml";
  +        Parameters parameters = new Parameters();
  +        String input = 
"resource://org/apache/cocoon/transformation/parsertest-input3.xml";
  +        String result = 
"resource://org/apache/cocoon/transformation/parsertest-result3.xml";
   
  -        // addTestStep(transformer, objectmodel, src, parameters, input, 
result, EQUAL);
  +        assertEqual(load(result), transform("recovery-parser", src, 
parameters, load(input)));
       }
   }
  
  
  
  1.2       +5 -5      
cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/PatternTransformerTestCase.java
  
  Index: PatternTransformerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/transformation/PatternTransformerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PatternTransformerTestCase.java   9 Apr 2003 12:24:35 -0000       1.1
  +++ PatternTransformerTestCase.java   16 Apr 2003 13:47:57 -0000      1.2
  @@ -65,15 +65,15 @@
   
       public PatternTransformerTestCase(String name) {
           super(name);
  +    }
  +
  +    public void testPatternTransformer() {
   
  -        String transformer = "pattern";
  -        HashMap objectmodel = new HashMap();
           String src = 
"resource://org/apache/cocoon/transformation/patterntest-lexicon1.xml";
           Parameters parameters = new Parameters();
           String input = 
"resource://org/apache/cocoon/transformation/patterntest-input1.xml";
           String result = 
"resource://org/apache/cocoon/transformation/patterntest-result1.xml";
   
  -        addTestStep(transformer, objectmodel, src, parameters, input, result,
  -                    EQUAL);
  +        assertEqual(load(result), transform("pattern", src, parameters, 
load(input)));
       }
   }
  
  
  
  1.4       +139 -225  
cocoon-2.1/src/test/org/apache/cocoon/transformation/AbstractTransformerTestCase.java
  
  Index: AbstractTransformerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/transformation/AbstractTransformerTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractTransformerTestCase.java  9 Apr 2003 11:12:20 -0000       1.3
  +++ AbstractTransformerTestCase.java  16 Apr 2003 13:47:59 -0000      1.4
  @@ -53,7 +53,7 @@
   import java.io.IOException;
   import java.util.Enumeration;
   import java.util.Map;
  -import java.util.Vector;
  +import java.util.HashMap;
   
   import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
  @@ -69,6 +69,7 @@
   import org.apache.cocoon.components.source.SourceResolverAdapter;
   import org.apache.cocoon.xml.WhitespaceFilter;
   import org.apache.cocoon.xml.dom.DOMBuilder;
  +import org.apache.cocoon.xml.dom.DOMStreamer;
   import org.apache.excalibur.source.Source;
   import org.apache.excalibur.source.SourceResolver;
   import org.apache.excalibur.xml.sax.SAXParser;
  @@ -90,20 +91,7 @@
    */
   public abstract class AbstractTransformerTestCase extends ExcaliburTestCase
   {
  -
  -    /** If the result document should be equal. */
  -    public final static int EQUAL = 0;
  -
  -    /** If the result document should not be equal. */
  -    public final static int NOTEQUAL = 1;
  -
  -    /** If the result document should be identical. */
  -    public final static int IDENTICAL = 2;
  -
  -    /** If the result document should not be identical. */
  -    public final static int NOTIDENTICAL = 3;
  -
  -    private Vector teststeps = new Vector();
  +    private HashMap objectmodel = new HashMap();
   
       /**
        * Create a new transformer test case.
  @@ -114,47 +102,27 @@
           super(name);
       }
   
  -    /**
  -     * Add a new test step to the test case.
  -     *
  -     * @param transformer Hint of the transformer.
  -     * @param objectmodel Object model.
  -     * @param source Source for the transformer.
  -     * @param parameters Transformer parameters.
  -     * @param input Input XML document, which go throught
  -     *              the transformer.
  -     * @param assertion Assertion XML document.
  -     * @param assertiontype (EQUAL|NOTEQUAL|IDENTICAL|NOTIDENTICAL)
  -     */
  -    public final void addTestStep(String transformer, Map objectmodel,
  -                                  String source, Parameters parameters,
  -                                  String input, String assertion,
  -                                  int assertiontype) {
  -        TestStep test = new TestStep();
  -
  -        test.transformer = transformer;
  -        test.objectmodel = objectmodel;
  -        test.source = source;
  -        test.parameters = parameters;
  -        test.input = input;
  -        test.assertion = assertion;
  -        test.assertiontype = assertiontype;
  -
  -        teststeps.addElement(test);
  +    public final Map getObjectModel() {
  +        return objectmodel;
       }
   
  -    /**
  -     * Test the transformers and his output
  -     */
  -    public final void testTransformer() {
  +    /**     
  +     * Trannsform a document by a transformer
  +     *      
  +     * @param type Hint of the transformer. 
  +     * @param source Source for the transformer.
  +     * @param parameters Generator parameters.
  +     * @param input Input document.
  +     */ 
  +    public final Document transform(String type, String source, Parameters 
parameters, Document input) {
   
           ComponentSelector selector = null;
           Transformer transformer = null;
           SourceResolver resolver = null;
           SAXParser parser = null;
           Source inputsource = null;
  -        Source assertionsource = null;
   
  +        Document document = null;
           try {
               selector = (ComponentSelector) 
this.manager.lookup(Transformer.ROLE+
                   "Selector");
  @@ -166,115 +134,23 @@
               parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
               assertNotNull("Test lookup of parser", parser);
   
  -            TestStep test;
  -            int count = 0;
  -
  -            getLogger().debug("Count of test steps: "+teststeps.size());
   
  -            for (Enumeration e = teststeps.elements(); e.hasMoreElements(); 
) {
  -                test = (TestStep) e.nextElement();
  -                count++;
  -                getLogger().info(count+".Test step");
  -
  -                assertNotNull("Test if transformer name is not null",
  -                              test.transformer);
  -                transformer = (Transformer) 
selector.select(test.transformer);
  -                assertNotNull("Test lookup of transformer", transformer);
  -
  -                DOMBuilder builder = new DOMBuilder();
  -
  -                if ((test.assertiontype==EQUAL) ||
  -                    (test.assertiontype==NOTEQUAL)) {
  -                    transformer.setConsumer(new WhitespaceFilter(builder));
  -                } else {
  -                    transformer.setConsumer(builder);
  -                }
  -
  -                transformer.setup(new SourceResolverAdapter(resolver, 
this.manager),
  -                                  test.objectmodel, test.source,
  -                                  test.parameters);
  -
  -                assertNotNull("Test if input document is not null",
  -                              test.input);
  -                inputsource = resolver.resolveURI(test.input);
  -                assertNotNull("Test lookup of input source", inputsource);
  -
  -                assertNotNull("Test if inputstream of the input source is 
not null",
  -                              inputsource.getInputStream());
  -                parser.parse(new InputSource(inputsource.getInputStream()),
  -                             (ContentHandler) transformer,
  -                             (LexicalHandler) transformer);
  -
  -                Document document = builder.getDocument();
  -
  -                assertNotNull("Test for transformer document", document);
  -
  -                assertNotNull("Test if assertion document is not null",
  -                              test.assertion);
  -                assertionsource = resolver.resolveURI(test.assertion);
  -                assertNotNull("Test lookup of assertion source",
  -                              assertionsource);
  -
  -                assertNotNull("Test if inputstream of the assertion source 
is not null",
  -                              assertionsource.getInputStream());
  -                builder = new DOMBuilder();
  -                if ((test.assertiontype==EQUAL) ||
  -                    (test.assertiontype==NOTEQUAL)) {
  -                    parser.parse(new 
InputSource(assertionsource.getInputStream()),
  -                                 (ContentHandler) new 
WhitespaceFilter(builder),
  -                                 (LexicalHandler) builder);
  -                } else {
  -                    parser.parse(new 
InputSource(assertionsource.getInputStream()),
  -                                 (ContentHandler) builder,
  -                                 (LexicalHandler) builder);
  -                }
  -                Document assertiondocument = builder.getDocument();
  -
  -                assertNotNull("Test if assertion document exists", resolver);
  -
  -                assertTrue("Test if assertion type is correct",
  -                           (test.assertiontype>=EQUAL) &&
  -                           (test.assertiontype<=NOTIDENTICAL));
  -
  -                switch (test.assertiontype) {
  -                    case EQUAL :
  -                        document.getDocumentElement().normalize();
  -                        assertiondocument.getDocumentElement().normalize();
  -                        assertXMLEqual(compareXML(assertiondocument, 
document),
  -                                       true,
  -                                       "Test if the assertion document is 
equal");
  -                        break;
  -
  -                    case NOTEQUAL :
  -                        document.getDocumentElement().normalize();
  -                        assertiondocument.getDocumentElement().normalize();
  -                        assertXMLEqual(compareXML(assertiondocument, 
document),
  -                                       false,
  -                                       "Test if the assertion document is 
not equal");
  -                        break;
  -
  -                    case IDENTICAL :
  -                        assertXMLIdentical(compareXML(assertiondocument, 
document),
  -                                           true,
  -                                           "Test if the assertion document 
is identical");
  -                        break;
  -
  -                    case NOTIDENTICAL :
  -                        assertXMLIdentical(compareXML(assertiondocument, 
document),
  -                                           false,
  -                                           "Test if the assertion document 
is not identical");
  -                        break;
  -                }
  +            assertNotNull("Test if transformer name is not null", type);
  +            transformer = (Transformer) selector.select(type);
  +            assertNotNull("Test lookup of transformer", transformer);
  +
  +            DOMBuilder builder = new DOMBuilder();
  +            transformer.setConsumer(new WhitespaceFilter(builder));
  +
  +            transformer.setup(new SourceResolverAdapter(resolver, 
this.manager),
  +                                  objectmodel, source, parameters);
  +
  +            assertNotNull("Test if input document is not null", input);
  +            DOMStreamer streamer = new DOMStreamer(transformer);
  +            streamer.stream(input);
   
  -                selector.release(transformer);
  -                transformer = null;
  -
  -                resolver.release(inputsource);
  -                inputsource = null;
  -
  -                resolver.release(assertionsource);
  -                assertionsource = null;
  -            }
  +            document = builder.getDocument();
  +            assertNotNull("Test for transformer document", document);
   
           } catch (ComponentException ce) {
               getLogger().error("Could not retrieve transformer", ce);
  @@ -303,116 +179,154 @@
               if (inputsource!=null)
                   resolver.release(inputsource);
   
  -            if (inputsource!=null)
  -                resolver.release(assertionsource);
  -
               if (resolver!=null)
                   this.manager.release(resolver);
   
               if (parser!=null)
                   this.manager.release((Component) parser);
           }
  -    }
   
  -    /**
  -     * Compare two XML documents provided as strings
  -     * @param control Control document
  -     * @param test Document to test
  -     * @return Diff object describing differences in documents
  -     */
  -    public final Diff compareXML(Document control, Document test) {
  +        return document; 
  +    }
   
  -        /*TransformerFactory factory = (TransformerFactory) 
TransformerFactory.newInstance();
  +    public final void print(Document document) {
  +        TransformerFactory factory = (TransformerFactory) 
TransformerFactory.newInstance();
           try
           {
             javax.xml.transform.Transformer serializer = 
factory.newTransformer();
  -          System.out.println("Control document:");
  -          serializer.transform(new DOMSource(control), new 
StreamResult(System.out));
  -          System.out.println();
  -
  -          serializer = factory.newTransformer();
  -          System.out.println("Test document:");
  -          serializer.transform(new DOMSource(test), new 
StreamResult(System.out));
  +          serializer.transform(new DOMSource(document), new 
StreamResult(System.out));
             System.out.println();
           } 
           catch (TransformerException te)
           {
             te.printStackTrace();
  -        }*/
  -
  -        return new Diff(control, test);
  +        }
       }
   
       /**
  -     * Assert that the result of an XML comparison is or is not similar.
  -     * @param diff the result of an XML comparison
  -     * @param assertion true if asserting that result is similar
  +     * Compare two XML documents provided as strings
  +     * @param control Control document
  +     * @param test Document to test
  +     * @return Diff object describing differences in documents
        */
  -    public final void assertXMLEqual(Diff diff, boolean assertion) {
  -        XMLUnit.setIgnoreWhitespace(true);
  -        assertEquals(diff.toString(), assertion, diff.similar());
  +    public final Diff compareXML(Document control, Document test) {
  +
  +        return new Diff(control, test);
       }
   
  -    /**
  -     * Assert that the result of an XML comparison is or is not similar.
  -     * @param diff the result of an XML comparison
  -     * @param assertion true if asserting that result is similar
  -     * @param msg additional message to display if assertion fails
  -     */
  -    public final void assertXMLEqual(Diff diff, boolean assertion,
  -                                     String msg) {
  -        XMLUnit.setIgnoreWhitespace(true);
  -        assertEquals(msg+", "+diff.toString(), assertion, diff.similar());
  +    public final Document load(String source) {
  +
  +        SourceResolver resolver = null;
  +        SAXParser parser = null;
  +        Source assertionsource = null;
  +
  +        Document assertiondocument = null;
  +        try {
  +            resolver = (SourceResolver) 
this.manager.lookup(SourceResolver.ROLE);
  +            assertNotNull("Test lookup of source resolver", resolver);
  +
  +            parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
  +            assertNotNull("Test lookup of parser", parser);
  +
  +            assertNotNull("Test if assertion document is not null",
  +                          source);
  +            assertionsource = resolver.resolveURI(source);
  +            assertNotNull("Test lookup of assertion source",
  +                          assertionsource);
  +
  +            DOMBuilder builder = new DOMBuilder();
  +            assertNotNull("Test if inputstream of the assertion source is 
not null",
  +                          assertionsource.getInputStream());
  +
  +            parser.parse(new InputSource(assertionsource.getInputStream()),
  +                         new WhitespaceFilter(builder),
  +                         builder);
  +
  +            assertiondocument = builder.getDocument();
  +            assertNotNull("Test if assertion document exists", 
assertiondocument);
  +
  +        } catch (ComponentException ce) {
  +            getLogger().error("Could not retrieve generator", ce);
  +            fail("Could not retrieve generator: " + ce.toString());
  +        } catch (Exception e) {
  +            getLogger().error("Could not execute test", e);
  +            fail("Could not execute test: " + e);
  +        } finally {
  +            if (resolver != null) {
  +                resolver.release(assertionsource);
  +            }
  +            this.manager.release(resolver);
  +            this.manager.release((Component) parser);
  +        }
  +
  +        return assertiondocument;
       }
   
       /**
  -     * Assert that the result of an XML comparison is or is not identical
  -     * @param diff the result of an XML comparison
  -     * @param assertion true if asserting that result is identical
  +     * Assert that the result of an XML comparison is similar.
  +     *
  +     * @param msg The assertion message
  +     * @param expected The expected XML document
  +     * @param actual The actual XML Document
        */
  -    public final void assertXMLIdentical(Diff diff, boolean assertion) {
  -        XMLUnit.setIgnoreWhitespace(false);
  -        assertEquals(diff.toString(), assertion, diff.identical());
  +    public final void assertEqual(String msg, Document expected, Document 
actual) {
  +
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
  +
  +        Diff diff = compareXML(expected, actual);
  +
  +        assertEquals(msg + ", " + diff.toString(), true, diff.similar());
       }
   
       /**
  -     * Assert that the result of an XML comparison is or is not identical
  -     * @param diff the result of an XML comparison
  -     * @param assertion true if asserting that result is identical
  -     * @param msg additional message to display if assertion fails
  +     * Assert that the result of an XML comparison is similar.
  +     *
  +     * @param msg The assertion message
  +     * @param expected The expected XML document
  +     * @param actual The actual XML Document
        */
  -    public final void assertXMLIdentical(Diff diff, boolean assertion,
  -                                         String msg) {
  -        XMLUnit.setIgnoreWhitespace(false);
  -        assertEquals(msg+", "+diff.toString(), assertion, diff.identical());
  +    public final void assertEqual(Document expected, Document actual) {
  +
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
  +
  +        Diff diff = compareXML(expected, actual);
  +
  +        assertEquals("Test if the assertion document is equal, " + 
diff.toString(), true, diff.similar());
       }
   
       /**
  -     * Inner class for a test step.
  +     * Assert that the result of an XML comparison is identical.
  +     *
  +     * @param msg The assertion message
  +     * @param expected The expected XML document
  +     * @param actual The actual XML Document
        */
  -    private class TestStep
  -    {
  +    public final void assertIdentical(String msg, Document expected, 
Document actual) {
   
  -        /** Hint of the transformer. */
  -        public String transformer = null;
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
   
  -        /** Object model. */
  -        public Map objectmodel = null;
  +        Diff diff = compareXML(expected, actual);
   
  -        /** Source for the transformer. */
  -        public String source = null;
  +        assertEquals(msg + ", " + diff.toString(), true, diff.identical());
  +    }
   
  -        /** Transformer parameters. */
  -        public Parameters parameters = null;
  +    /**
  +     * Assert that the result of an XML comparison is identical.
  +     *
  +     * @param msg The assertion message
  +     * @param expected The expected XML document
  +     * @param actual The actual XML Document
  +     */
  +    public final void assertIdentical(Document expected, Document actual) {
   
  -        /** Input XML document, which go throught
  -         *  the transformer. */
  -        public String input = null;
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
   
  -        /** Assertion XML document. */
  -        public String assertion = null;
  +        Diff diff = compareXML(expected, actual);
   
  -        /** (EQUAL|NOTEQUAL|IDENTICAL|NOTIDENTICAL) */
  -        public int assertiontype = EQUAL;
  +        assertEquals("Test if the assertion document is equal, " + 
diff.toString(), true, diff.identical());
       }
   }
  
  
  

Reply via email to