You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ug...@apache.org on 2003/10/24 00:08:14 UTC

cvs commit: cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype FlowJXPathSelectionListTestCase.xtest FlowJXPathSelectionListTestCase.java FlowJXPathSelectionListTestCase.dest.xml FlowJXPathSelectionListTestCase.source.xml DynamicSelectionListTestCase.java DynamicSelectionListTestCase.xtest

ugo         2003/10/23 15:08:13

  Modified:    src/blocks/woody/test/org/apache/cocoon/woody/datatype
                        DynamicSelectionListTestCase.java
                        DynamicSelectionListTestCase.xtest
  Added:       src/blocks/woody/test/org/apache/cocoon/woody/datatype
                        FlowJXPathSelectionListTestCase.xtest
                        FlowJXPathSelectionListTestCase.java
                        FlowJXPathSelectionListTestCase.dest.xml
                        FlowJXPathSelectionListTestCase.source.xml
  Log:
  [Woody] Added testcase for FlowJXPathSelectionList.
  
  Revision  Changes    Path
  1.3       +14 -117   cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.java
  
  Index: DynamicSelectionListTestCase.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DynamicSelectionListTestCase.java	19 Oct 2003 17:20:56 -0000	1.2
  +++ DynamicSelectionListTestCase.java	23 Oct 2003 22:08:13 -0000	1.3
  @@ -51,16 +51,11 @@
   
   package org.apache.cocoon.woody.datatype;
   
  -import java.io.IOException;
  -import java.io.InputStream;
   import java.io.Writer;
   import java.util.Locale;
   
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
  -import javax.xml.parsers.FactoryConfigurationError;
  -import javax.xml.parsers.ParserConfigurationException;
  -import javax.xml.transform.TransformerException;
   import javax.xml.transform.TransformerFactory;
   import javax.xml.transform.dom.DOMSource;
   import javax.xml.transform.stream.StreamResult;
  @@ -70,17 +65,10 @@
   import org.apache.avalon.framework.service.WrapperServiceManager;
   import org.apache.cocoon.woody.Constants;
   import org.apache.cocoon.xml.dom.DOMBuilder;
  -import org.apache.cocoon.xml.dom.DocumentWrapper;
  -import org.apache.excalibur.source.Source;
  -import org.apache.excalibur.source.SourceNotFoundException;
  -import org.apache.excalibur.source.SourceValidity;
   import org.apache.excalibur.source.impl.ResourceSource;
  -import org.apache.excalibur.xml.sax.XMLizable;
   import org.custommonkey.xmlunit.Diff;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
  -import org.xml.sax.ContentHandler;
  -import org.xml.sax.SAXException;
   
   /**
    * Test case for Woody's DynamicSelectionList datatype.
  @@ -129,9 +117,10 @@
        */
       public void testGenerateSaxFragment() throws Exception {
           DOMBuilder dest = new DOMBuilder();
  -        XMLizableSource source =
  -            new XMLizableSource("resource://org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.source.xml");
  -        Element datatypeElement = (Element) source.getDocument().getElementsByTagNameNS(Constants.WD_NS, "convertor").item(0);
  +        ResourceSource source = 
  +            new ResourceSource("resource://org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.source.xml");
  +        Document sourceDoc = parser.parse(source.getInputStream());
  +        Element datatypeElement = (Element) sourceDoc.getElementsByTagNameNS(Constants.WD_NS, "convertor").item(0);
           Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
           DynamicSelectionList list = 
               new DynamicSelectionList(datatype, null, serviceManager);
  @@ -168,107 +157,15 @@
        */
       public final void print(Document document, Writer out) {
           TransformerFactory factory = TransformerFactory.newInstance();
  -        try
  -        {
  -          javax.xml.transform.Transformer serializer = factory.newTransformer();
  -          serializer.transform(new DOMSource(document), new StreamResult(out));
  -        } 
  -        catch (TransformerException te)
  -        {
  -          te.printStackTrace();
  +        try {
  +            javax.xml.transform.Transformer serializer =
  +                factory.newTransformer();
  +            serializer.transform(
  +                new DOMSource(document),
  +                new StreamResult(out));
  +            out.write('\n');
  +        } catch (Exception e) {
  +            e.printStackTrace();
           }
  -    }
  -    
  -    /**
  -     * A class that implements both the 
  -     * {@link org.apache.excalibur.xml.sax.XMLizable} and 
  -     * {@link org.apache.excalibur.source.Source} interfaces by delegating to a 
  -     * {@link org.apache.cocoon.xml.domDocumentWrapper} and to a 
  -     * {@link org.apache.excalibur.source.impl.ResourceSource},
  -     * respectively.
  -     */
  -    class XMLizableSource implements XMLizable, Source {
  -
  -        private ResourceSource source;
  -        private Document document;
  -        private DocumentWrapper wrapper;
  -        
  -        /**
  -         * Create a new XMLizableSource from the provided resource URI.
  -         * @param uri An URI of the form "resource://..."
  -         * @throws SAXException
  -         * @throws IOException
  -         * @throws ParserConfigurationException
  -         * @throws FactoryConfigurationError
  -         */
  -        public XMLizableSource(String uri) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
  -            source = new ResourceSource(uri);
  -            document = parser.parse(source.getInputStream());
  -            wrapper = new DocumentWrapper(document);
  -        }
  -        
  -        /**
  -         * Return the document parsed from the source.
  -         * @return A DOM Document.
  -         */
  -        public Document getDocument() {
  -            return document;
  -        }
  -
  -        // The following methods delegate either to the DocumentWrapper
  -        // or to the Source.        
  -        public void toSAX(ContentHandler handler) throws SAXException {
  -            wrapper.toSAX(handler);
  -        }
  -
  -        public boolean equals(Object obj) {
  -            return source.equals(obj);
  -        }
  -
  -        public boolean exists() {
  -            return source.exists();
  -        }
  -
  -        public long getContentLength() {
  -            return source.getContentLength();
  -        }
  -
  -        public InputStream getInputStream()
  -            throws IOException, SourceNotFoundException {
  -            return source.getInputStream();
  -        }
  -
  -        public long getLastModified() {
  -            return source.getLastModified();
  -        }
  -
  -        public String getMimeType() {
  -            return source.getMimeType();
  -        }
  -
  -        public String getScheme() {
  -            return source.getScheme();
  -        }
  -
  -        public String getURI() {
  -            return source.getURI();
  -        }
  -
  -        public SourceValidity getValidity() {
  -            return source.getValidity();
  -        }
  -
  -        public int hashCode() {
  -            return source.hashCode();
  -        }
  -
  -        public void refresh() {
  -            source.refresh();
  -        }
  -
  -        public String toString() {
  -            return source.toString();
  -        }
  -
       }
   }
  
  
  
  1.2       +9 -1      cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.xtest
  
  Index: DynamicSelectionListTestCase.xtest
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.xtest,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DynamicSelectionListTestCase.xtest	19 Oct 2003 17:20:56 -0000	1.1
  +++ DynamicSelectionListTestCase.xtest	23 Oct 2003 22:08:13 -0000	1.2
  @@ -1,7 +1,7 @@
   <?xml version="1.0" ?>
   <testcase>
    <annotation>
  -  Test Cases: Woody Dynamic Selection List
  +  Test Cases: Woody Flow Model Selection List
    </annotation>
   
    <logkit>
  @@ -34,6 +34,14 @@
     <role name="org.apache.cocoon.woody.expression.ExpressionManager"
       shorthand="woody-expression"
       default-class="org.apache.cocoon.woody.expression.DefaultExpressionManager"/>
  +
  +  <role name="org.apache.excalibur.xmlizer.XMLizer"
  +        shorthand="xmlizer"
  +        default-class="org.apache.excalibur.xmlizer.DefaultXMLizer"/>
  +
  +  <role name="org.apache.excalibur.xml.sax.SAXParser"
  +        shorthand="xml-parser"
  +        default-class="org.apache.excalibur.xml.impl.JaxpParser"/>
   
    </roles>
   
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.xtest
  
  Index: FlowJXPathSelectionListTestCase.xtest
  ===================================================================
  <?xml version="1.0" ?>
  <testcase>
   <annotation>
    Test Cases: Woody Dynamic Selection List
   </annotation>
  
   <logkit>
    <factories>
     <factory type="stream" class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/>
    </factories>
    <targets>
     <stream id="root">
      <stream>System.out</stream>
      <format type="extended">
       %7.7{priority} %5.5{time}   [%9.9{category}] (%{context}): %{message}\n%{throwable}
      </format>
     </stream>
    </targets>
    <categories>
     <category name="test" log-level="ERROR">
      <log-target id-ref="root"/>
     </category>
    </categories>
   </logkit>
  
   <context/>
  
   <roles>
  
    <role name="org.apache.cocoon.woody.datatype.DatatypeManager"
      shorthand="woody-datatype"
      default-class="org.apache.cocoon.woody.datatype.DefaultDatatypeManager"/>
  
    <role name="org.apache.cocoon.woody.expression.ExpressionManager"
      shorthand="woody-expression"
      default-class="org.apache.cocoon.woody.expression.DefaultExpressionManager"/>
  
   </roles>
  
   <components>
    <woody-datatype logger="woody">
      <datatypes>
        <datatype name="string" src="org.apache.cocoon.woody.datatype.typeimpl.StringTypeBuilder">
          <convertors default="dummy" plain="dummy">
            <convertor name="dummy" src="org.apache.cocoon.woody.datatype.convertor.DummyStringConvertorBuilder"/>
          </convertors>
        </datatype>
        <datatype name="long" src="org.apache.cocoon.woody.datatype.typeimpl.LongTypeBuilder">
          <convertors default="formatting" plain="plain">
            <convertor name="plain" src="org.apache.cocoon.woody.datatype.convertor.PlainLongConvertorBuilder"/>
            <convertor name="formatting" src="org.apache.cocoon.woody.datatype.convertor.FormattingLongConvertorBuilder"/>
          </convertors>
        </datatype>
        <datatype name="decimal" src="org.apache.cocoon.woody.datatype.typeimpl.DecimalTypeBuilder">
          <convertors default="formatting" plain="plain">
            <convertor name="plain" src="org.apache.cocoon.woody.datatype.convertor.PlainDecimalConvertorBuilder"/>
            <convertor name="formatting" src="org.apache.cocoon.woody.datatype.convertor.FormattingDecimalConvertorBuilder"/>
          </convertors>
        </datatype>
        <datatype name="date" src="org.apache.cocoon.woody.datatype.typeimpl.DateTypeBuilder">
          <convertors default="formatting" plain="millis">
            <convertor name="formatting" src="org.apache.cocoon.woody.datatype.convertor.FormattingDateConvertorBuilder"/>
            <convertor name="millis" src="org.apache.cocoon.woody.datatype.convertor.MillisDateConvertorBuilder"/>
          </convertors>
        </datatype>
        <datatype name="boolean" src="org.apache.cocoon.woody.datatype.typeimpl.BooleanTypeBuilder">
          <convertors default="plain" plain="plain">
            <convertor name="plain" src="org.apache.cocoon.woody.datatype.convertor.PlainBooleanConvertorBuilder"/>
          </convertors>
        </datatype>  
        <datatype name="integer" src="org.apache.cocoon.woody.datatype.typeimpl.IntegerTypeBuilder">
          <convertors default="formatting" plain="plain">
            <convertor name="plain" src="org.apache.cocoon.woody.datatype.convertor.PlainIntegerConvertorBuilder"/>
            <convertor name="formatting" src="org.apache.cocoon.woody.datatype.convertor.FormattingIntegerConvertorBuilder"/>
          </convertors>
        </datatype>  
      </datatypes>
      <validation-rules>
        <validation-rule name="length" src="org.apache.cocoon.woody.datatype.validationruleimpl.LengthValidationRuleBuilder"/>
        <validation-rule name="email" src="org.apache.cocoon.woody.datatype.validationruleimpl.EmailValidationRuleBuilder"/>
        <validation-rule name="value-count" src="org.apache.cocoon.woody.datatype.validationruleimpl.ValueCountValidationRuleBuilder"/>
        <validation-rule name="range" src="org.apache.cocoon.woody.datatype.validationruleimpl.RangeValidationRuleBuilder"/>
        <validation-rule name="assert" src="org.apache.cocoon.woody.datatype.validationruleimpl.AssertValidationRuleBuilder"/>
        <validation-rule name="mod10" src="org.apache.cocoon.woody.datatype.validationruleimpl.Mod10ValidationRuleBuilder"/>
        <validation-rule name="regexp" src="org.apache.cocoon.woody.datatype.validationruleimpl.RegExpValidationRuleBuilder"/>
      </validation-rules>
    </woody-datatype>
  
    <woody-expression logger="woody.expression"/>
  
   </components>
  
  </testcase>
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.java
  
  Index: FlowJXPathSelectionListTestCase.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.cocoon.woody.datatype;
  
  import java.io.Writer;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Locale;
  import java.util.Map;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.transform.stream.StreamResult;
  
  import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
  import org.apache.avalon.framework.context.Context;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.WrapperServiceManager;
  import org.apache.cocoon.components.ContextHelper;
  import org.apache.cocoon.components.flow.FlowHelper;
  import org.apache.cocoon.environment.ObjectModelHelper;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.mock.MockRequest;
  import org.apache.cocoon.woody.Constants;
  import org.apache.cocoon.xml.dom.DOMBuilder;
  import org.apache.excalibur.source.Source;
  import org.apache.excalibur.source.impl.ResourceSource;
  import org.custommonkey.xmlunit.Diff;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  
  /**
   * Test case for Woody's FlowModelSelectionList datatype.
   * @version CVS $Id: FlowJXPathSelectionListTestCase.java,v 1.1 2003/10/23 22:08:13 ugo Exp $
   */
  public class FlowJXPathSelectionListTestCase extends ExcaliburTestCase {
  
      protected ServiceManager serviceManager;
      protected DatatypeManager datatypeManager;
      protected DocumentBuilder parser;
  
      /**
       * Construct a new test case.
       * @param name The test case's name.
       */
      public FlowJXPathSelectionListTestCase(String name) {
          super(name);
      }
  
      /* (non-Javadoc)
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception {
          super.setUp();
          serviceManager = new WrapperServiceManager(manager); 
          datatypeManager = (DatatypeManager) serviceManager.lookup(DatatypeManager.ROLE);
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setNamespaceAware(true);
          parser = factory.newDocumentBuilder();
      }
      
      /* (non-Javadoc)
       * @see junit.framework.TestCase#tearDown()
       */
      protected void tearDown() throws Exception {
          if (datatypeManager != null) {
              serviceManager.release(datatypeManager);
          }
          super.tearDown();
      }
      
      /**
       * Test the generateSaxFragment method.
       */
      public void testGenerateSaxFragment() throws Exception {
          List beans = new ArrayList(2);
          beans.add(new TestBean("1", "One"));
          beans.add(new TestBean("2", "Two"));
          Map flowContextObject = new HashMap();
          flowContextObject.put("beans", beans);
          Request request = new MockRequest();
          request.setAttribute(FlowHelper.CONTEXT_OBJECT, flowContextObject);
          Map objectModel = new HashMap();
          objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
          Map contextObjectModel = new HashMap();
          contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel);
          Context context = new DefaultContext(contextObjectModel);
          Source sampleSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
          Document sample = parser.parse(sampleSource.getInputStream());
          Element datatypeElement = (Element) sample.getElementsByTagNameNS(Constants.WD_NS, "datatype").item(0);
          Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
          FlowJXPathSelectionList list = new FlowJXPathSelectionList
              (context, "beans", "key", "value", datatype);
          DOMBuilder dest = new DOMBuilder();
          list.generateSaxFragment(dest, Locale.ENGLISH);
          Source expectedSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.dest.xml");
          Document expected = parser.parse(expectedSource.getInputStream());
          assertEqual("Test if generated list matches expected",
              expected, dest.getDocument());
      }
  
      /**
       * Check is the source document is equal to the one produced by the method under test.
       * @param message A message to print in case of failure.
       * @param expected The expected (source) document.
       * @param actual The actual (output) document.
       */
      private void assertEqual(String message, Document expected, Document actual) {
          expected.getDocumentElement().normalize();
          actual.getDocumentElement().normalize();
          // DIRTY HACK WARNING: we add the "xmlns:wi" attribute reported
          // by DOM, as expected, but not generated by the method under test,
          // otherwise the comparison would fail. 
          actual.getDocumentElement().setAttribute(Constants.WI_PREFIX,
                  Constants.WI_NS);
          Diff diff =  new Diff(expected, actual);
          assertTrue(message + ", " + diff.toString(), diff.similar());
      }
  
      /**
       * Print a document to a writer for debugging purposes.
       * @param document The document to print.
       * @param out The writer to write to.
       */
      public final void print(Document document, Writer out) {
          TransformerFactory factory = TransformerFactory.newInstance();
          try {
              javax.xml.transform.Transformer serializer =
                  factory.newTransformer();
              serializer.transform(
                  new DOMSource(document),
                  new StreamResult(out));
              out.write('\n');
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      
      public static class TestBean {
          private String key;
          private String value;
  
          public TestBean(String key, String value) {
              this.key = key;
              this.value = value;
          }
          
          public String getKey() {
              return key;
          }
          
          public String getValue() {
              return value;
          }
          
          public String toString() {
              return "{ " + key + " : " + value + " }";
          }
      }
  }
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.dest.xml
  
  Index: FlowJXPathSelectionListTestCase.dest.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <wi:selection-list xmlns:wi="http://apache.org/cocoon/woody/instance/1.0"><wi:item value="1"><wi:label>One</wi:label></wi:item><wi:item value="2"><wi:label>Two</wi:label></wi:item></wi:selection-list>
  
  
  1.1                  cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml
  
  Index: FlowJXPathSelectionListTestCase.source.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      |   Source file for FlowModelSelectionList test case.
      |   $Id: FlowJXPathSelectionListTestCase.source.xml,v 1.1 2003/10/23 22:08:13 ugo Exp $
      +-->
  
  <wd:field xmlns:wd="http://apache.org/cocoon/woody/definition/1.0"
    id="sample" required="false">
    <wd:datatype base="string"/>
    <wd:selection-list model="beans"/>
  </wd:field>