cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From step...@apache.org
Subject cvs commit: cocoon-2.1/src/test/org/apache/cocoon/generation AbstractGeneratorTestCase.java FileGeneratorTestCase.java
Date Wed, 16 Apr 2003 10:56:45 GMT
stephan     2003/04/16 03:56:45

  Modified:    src/blocks/chaperon/test/org/apache/cocoon/generation
                        TextGeneratorTestCase.java
               src/test/org/apache/cocoon/generation
                        AbstractGeneratorTestCase.java
                        FileGeneratorTestCase.java
  Log:
  AbstractGeneratorTestCase rewritten to wrote more intuitionally, and
  allow to test more than the generated document.
  
  Revision  Changes    Path
  1.2       +11 -7     cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/generation/TextGeneratorTestCase.java
  
  Index: TextGeneratorTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/chaperon/test/org/apache/cocoon/generation/TextGeneratorTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TextGeneratorTestCase.java	9 Apr 2003 12:24:34 -0000	1.1
  +++ TextGeneratorTestCase.java	16 Apr 2003 10:56:44 -0000	1.2
  @@ -67,18 +67,22 @@
   
       public TextGeneratorTestCase(String name) {
           super(name);
  +    }
   
  -        String generator = "text";
  -        HashMap objectmodel = new HashMap();
  +    public void testTextGenerator1() {
           String src = "resource://org/apache/cocoon/generation/texttest-input1.txt";
           Parameters parameters = new Parameters();
           String result = "resource://org/apache/cocoon/generation/texttest-result1.xml";
   
  -        addTestStep(generator, objectmodel, src, parameters, result, EQUAL);
  +        assertEqual(load(result), generate("text", src, parameters));
  +    }
   
  -        src = "resource://org/apache/cocoon/generation/texttest-input2.txt";
  -        result = "resource://org/apache/cocoon/generation/texttest-result2.xml";
  +    public void testTextGenerator2() {
   
  -        addTestStep(generator, objectmodel, src, parameters, result, EQUAL);
  +        String src = "resource://org/apache/cocoon/generation/texttest-input2.txt";
  +        Parameters parameters = new Parameters();
  +        String result = "resource://org/apache/cocoon/generation/texttest-result2.xml";
  +
  +        assertEqual(load(result), generate("text", src, parameters));
       }
   }
  
  
  
  1.4       +121 -178  cocoon-2.1/src/test/org/apache/cocoon/generation/AbstractGeneratorTestCase.java
  
  Index: AbstractGeneratorTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/test/org/apache/cocoon/generation/AbstractGeneratorTestCase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractGeneratorTestCase.java	18 Mar 2003 01:33:38 -0000	1.3
  +++ AbstractGeneratorTestCase.java	16 Apr 2003 10:56:44 -0000	1.4
  @@ -53,7 +53,7 @@
   
   import java.util.Enumeration;
   import java.util.Map;
  -import java.util.Vector;
  +import java.util.HashMap;
   
   import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
   import org.apache.avalon.framework.component.Component;
  @@ -79,20 +79,7 @@
    */
   public abstract class AbstractGeneratorTestCase 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 generator test case.
  @@ -103,35 +90,18 @@
           super(name);
       }
   
  -    /**
  -     * Add a new test step to the test case.
  -     *
  -     * @param generator Hint of the generator. 
  -     * @param objectmodel Object model.
  -     * @param source Source for the transformer.
  -     * @param parameters Generator parameters.
  -     * @param assertion Assertion XML document. 
  -     * @param assertiontype (EQUAL|NOTEQUAL|IDENTICAL|NOTIDENTICAL)
  -     */
  -    public final void addTestStep(String generator, Map objectmodel,
  -                                  String source, Parameters parameters,
  -                                  String assertion, int assertiontype) {
  -        TestStep test = new TestStep();
  -
  -        test.generator = generator;
  -        test.objectmodel = objectmodel;
  -        test.source = source;
  -        test.parameters = parameters;
  -        test.assertion = assertion;
  -        test.assertiontype = assertiontype;
  -
  -        teststeps.addElement(test);
  +    public final Map getObjectModel() {
  +        return objectmodel;
       }
   
       /**
  -     * Test the generators and his output
  +     * Generate the generator output.
  +     *
  +     * @param type Hint of the generator. 
  +     * @param source Source for the generator.
  +     * @param parameters Generator parameters.
        */
  -    public final void testGenerator() {
  +    public final Document generate(String type, String source, Parameters parameters) {
   
           ComponentSelector selector = null;
           Generator generator = null;
  @@ -139,6 +109,7 @@
           SAXParser parser = null;
           Source assertionsource = null;
   
  +        Document document = null;
           try {
               selector = (ComponentSelector) this.manager.lookup(Generator.ROLE +
                   "Selector");
  @@ -150,100 +121,22 @@
               parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
               assertNotNull("Test lookup of parser", parser);
   
  -            TestStep test;
  -            int count = 0;
  +            assertNotNull("Test if generator name is not null", type);
   
  -            for (Enumeration e = teststeps.elements(); e.hasMoreElements(); ) {
  -                test = (TestStep) e.nextElement();
  -                count++;
  -                getLogger().info(count+".Test step");
  -
  -                assertNotNull("Test if generator name is not null",
  -                              test.generator);
  -                generator = (Generator) selector.select(test.generator);
  -                assertNotNull("Test lookup of generator", generator);
  -
  -                DOMBuilder builder = new DOMBuilder();
  -
  -                if ((test.assertiontype==EQUAL) ||
  -                    (test.assertiontype==NOTEQUAL)) {
  -                    generator.setConsumer(new WhitespaceFilter(builder));
  -                } else {
  -                    generator.setConsumer(builder);
  -                }
  -
  -                generator.setup(new SourceResolverAdapter(resolver, this.manager),
  -                                test.objectmodel, test.source,
  -                                test.parameters);
  -                generator.generate();
  -
  -                Document document = builder.getDocument();
  -
  -                assertNotNull("Test for generator document", document);
  -
  -                assertNotNull("Test if assertion document is not null",
  -                              test.assertion);
  -                assertionsource = resolver.resolveURI(test.assertion);
  -                assertNotNull("Test lookup of assertion source",
  -                              assertionsource);
  -
  -                builder = new DOMBuilder();
  -                assertNotNull("Test if inputstream of the assertion source is not null",
  -                              assertionsource.getInputStream());
  -                if ((test.assertiontype==EQUAL) ||
  -                    (test.assertiontype==NOTEQUAL)) {
  -                    parser.parse(new InputSource(assertionsource.getInputStream()),
  -                                 new WhitespaceFilter(builder),
  -                                 builder);
  -                } else {
  -                    parser.parse(new InputSource(assertionsource.getInputStream()),
  -                                 builder,
  -                                 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;
  -                }
  +            generator = (Generator) selector.select(type);
  +            assertNotNull("Test lookup of generator", generator);
   
  -                selector.release(generator);
  -                generator = null;
  +            DOMBuilder builder = new DOMBuilder();
  +            generator.setConsumer(new WhitespaceFilter(builder));
  +
  +            generator.setup(new SourceResolverAdapter(resolver, this.manager),
  +                            objectmodel, source, parameters);
  +            generator.generate();
  +
  +            document = builder.getDocument();
  +
  +            assertNotNull("Test for generator document", document);
   
  -                resolver.release(assertionsource);
  -                assertionsource = null;
  -            }
           } catch (ComponentException ce) {
               getLogger().error("Could not retrieve generator", ce);
               fail("Could not retrieve generator: " + ce.toString());
  @@ -256,11 +149,58 @@
               }
               this.manager.release(selector);
               this.manager.release(resolver);
  +            this.manager.release((Component) parser);
  +        }
  +
  +        return document;
  +    }
  +
  +    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;
       }
   
       /**
  @@ -274,67 +214,70 @@
       }
   
       /**
  -     * 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
  +     * 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 assertXMLEqual(Diff diff, boolean assertion) {
  -        assertEquals(diff.toString(), assertion, diff.similar());
  -    }
  +    public final void assertEqual(String msg, Document expected, Document actual) {
   
  -    /**
  -     * 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) {
  -        assertEquals(msg + ", " + diff.toString(), assertion, diff.similar());
  -    }
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
   
  -    /**
  -     * 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
  -     */
  -    public final void assertXMLIdentical(Diff diff, boolean assertion) {
  -        assertEquals(diff.toString(), assertion, diff.identical());
  +        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
  -     */
  -    public final void assertXMLIdentical(Diff diff, boolean assertion,
  -                                         String msg) {
  -        assertEquals(msg + ", " + diff.toString(), assertion, diff.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 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) {
  +
  +        expected.getDocumentElement().normalize();
  +        actual.getDocumentElement().normalize();
   
  -        /** Hint of the generator. */
  -        public String generator = null;
  +        Diff diff = compareXML(expected, actual);
   
  -        /** Object model. */
  -        public Map objectmodel = null;
  +        assertEquals(msg + ", " + diff.toString(), true, diff.identical());
  +    }
   
  -        /** Source for the transformer. */
  -        public String source = 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) {
   
  -        /** Generator parameters. */
  -        public Parameters parameters = 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());
       }
   }
  
  
  
  1.3       +7 -5      cocoon-2.1/src/test/org/apache/cocoon/generation/FileGeneratorTestCase.java
  
  Index: FileGeneratorTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/test/org/apache/cocoon/generation/FileGeneratorTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FileGeneratorTestCase.java	16 Mar 2003 18:03:56 -0000	1.2
  +++ FileGeneratorTestCase.java	16 Apr 2003 10:56:44 -0000	1.3
  @@ -63,14 +63,16 @@
   {
     public FileGeneratorTestCase(String name)
     {
  -    super(name==null?"FileGenerator Testcase":name);
  +    super(name);
  +  }
   
  -    String generator = "file";
  -    HashMap objectmodel = new HashMap();
  +  public void testFileGenerator() {
  +
  +    String type = "file";
       String src = "resource://org/apache/cocoon/generation/FileGeneratorTestCase.source.xml";
       Parameters parameters = new Parameters();
       String result = "resource://org/apache/cocoon/generation/FileGeneratorTestCase.source.xml";
   
  -    addTestStep(generator, objectmodel, src, parameters, result, EQUAL);
  +    assertEqual(load(result), generate(type, src, parameters));
     }
   }
  
  
  

Mime
View raw message