ugo         2003/10/11 08:57:00

  Modified:    .        gump.xml
               src/blocks/woody/java/org/apache/cocoon/woody/datatype
                        DynamicSelectionList.java SelectionList.java
               src/webapp/samples/flow/calc sitemap.xmap
  Added:       src/blocks/woody/test/org/apache/cocoon/woody/datatype
                        DynamicSelectionListTestCase.java
                        DynamicSelectionListTestCase.source.xml
  Log:
  Added a testcase for Woody's DynamicSelectionList and did a small refactoring 
of it to ease testability.
  
  Revision  Changes    Path
  1.90      +2 -1      cocoon-2.1/gump.xml
  
  Index: gump.xml
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/gump.xml,v
  retrieving revision 1.89
  retrieving revision 1.90
  diff -u -r1.89 -r1.90
  --- gump.xml  11 Oct 2003 01:20:18 -0000      1.89
  +++ gump.xml  11 Oct 2003 15:57:00 -0000      1.90
  @@ -761,6 +761,7 @@
       <depend project="xreporter-expression"/>
       <depend project="jakarta-oro"/>
   
  +    <work nested="build/cocoon-@@DATE@@/blocks/woody/test"/>
       <work nested="tools/anttasks"/>
       <home nested="build/cocoon-@@DATE@@"/>
   
  
  
  
  1.1                  
cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.java
  
  Index: DynamicSelectionListTestCase.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
      [EMAIL PROTECTED]
  
   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  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  
  package org.apache.cocoon.woody.datatype;
  
  import java.io.IOException;
  import java.io.InputStream;
  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 junit.framework.TestCase;
  
  import org.apache.cocoon.woody.datatype.typeimpl.IntegerType;
  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.xml.sax.ContentHandler;
  import org.xml.sax.SAXException;
  
  /**
   * Test case for Woody's DynamicSelectionList datatype.
   * @version CVS $Id: DynamicSelectionListTestCase.java,v 1.1 2003/10/11 
15:57:00 ugo Exp $
   */
  public class DynamicSelectionListTestCase extends TestCase {
  
      /**
       * Construct a new test case.
       * @param name The test case's name.
       */
      public DynamicSelectionListTestCase(String name) {
          super(name);
      }
      
      /**
       * Test the generateSaxFragment method.
       * @throws MalformedURLException
       * @throws ParserConfigurationException
       */
      public void testGenerateSaxFragment() throws Exception {
          DynamicSelectionList list = 
              new DynamicSelectionList(new IntegerType(), null, null);
          DOMBuilder dest = new DOMBuilder();
          XMLizableSource source =
              new 
XMLizableSource("resource://org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.source.xml");
          list.generateSaxFragment(dest, Locale.ENGLISH, source);
          assertEqual("Test if input is equal to output",
              source.getDocument(), 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();
          // DIRTY HACK WARNING: we remove the "xmlns:wd" attribute reported
          // by DOM, as expected, but not generated by the method under test,
          // otherwise the comparison would fail. 
          expected.getDocumentElement().removeAttribute("xmlns:wd");
          Diff diff =  new Diff(expected, actual);
          assertTrue(message + ", " + diff.toString(), diff.similar());
      }
      
      /**
       * A class that implements both the 
       * [EMAIL PROTECTED] org.apache.excalibur.xml.sax.XMLizable} and 
       * [EMAIL PROTECTED] org.apache.excalibur.source.Source} interfaces by 
delegating to a 
       * [EMAIL PROTECTED] org.apache.cocoon.xml.domDocumentWrapper} and to a 
       * [EMAIL PROTECTED] org.apache.excalibur.source.impl.ResourceSource},
       * respectively.
       * 
       * @version CVS $Id: DynamicSelectionListTestCase.java,v 1.1 2003/10/11 
15:57:00 ugo Exp $
       */
      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);
              DocumentBuilderFactory factory = 
DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder builder = factory.newDocumentBuilder();
              document = builder.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.1                  
cocoon-2.1/src/blocks/woody/test/org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.source.xml
  
  Index: DynamicSelectionListTestCase.source.xml
  ===================================================================
  <?xml version="1.0"?>
  
  <!--+
      |   Source file for DynamicSelectionList test case.
      |   $Id: DynamicSelectionListTestCase.source.xml,v 1.1 2003/10/11 
15:57:00 ugo Exp $
      +-->
  
  <wd:selection-list xmlns:wd="http://cocoon.apache.org/woody/definition/1.0";>
    <wd:convertor type="formatting">
      <wd:patterns>
        <wd:pattern>yyyy-MM-dd</wd:pattern>
      </wd:patterns>
    </wd:convertor>
    <wd:item value="2003-10-11"/>
    <wd:item value="1963-02-04"><wd:label>My birthdate</wd:label></wd:item>
  </wd:selection-list>
  
  
  1.4       +13 -3     
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DynamicSelectionList.java
  
  Index: DynamicSelectionList.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/DynamicSelectionList.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DynamicSelectionList.java 16 Jul 2003 13:59:18 -0000      1.3
  +++ DynamicSelectionList.java 11 Oct 2003 15:57:00 -0000      1.4
  @@ -56,6 +56,7 @@
   import org.apache.avalon.framework.service.ServiceManager;
   import org.apache.excalibur.source.SourceResolver;
   import org.apache.excalibur.source.Source;
  +import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.source.SourceUtil;
   import org.apache.cocoon.xml.AttributesImpl;
   import org.apache.cocoon.xml.AbstractXMLPipe;
  @@ -65,6 +66,7 @@
   import org.apache.cocoon.woody.datatype.convertor.DefaultFormatCache;
   import org.w3c.dom.Element;
   
  +import java.io.IOException;
   import java.util.Locale;
   
   /**
  @@ -89,15 +91,23 @@
           return datatype;
       }
   
  +    /*
  +     * This method is only used by a test case and by the public version
  +     * of generateSaxFragment. 
  +     */
  +    void generateSaxFragment(ContentHandler contentHandler, Locale locale, 
Source source) throws ProcessingException, SAXException, IOException {
  +        SelectionListHandler handler = new SelectionListHandler(locale);
  +        handler.setContentHandler(contentHandler);
  +        SourceUtil.toSAX(source, handler);
  +    }
  +
       public void generateSaxFragment(ContentHandler contentHandler, Locale 
locale) throws SAXException {
           SourceResolver sourceResolver = null;
           Source source = null;
           try {
               sourceResolver = 
(SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
               source = sourceResolver.resolveURI(src);
  -            SelectionListHandler handler = new SelectionListHandler(locale);
  -            handler.setContentHandler(contentHandler);
  -            SourceUtil.toSAX(source, handler);
  +            generateSaxFragment(contentHandler, locale, source);
           } catch (Exception e) {
               throw new SAXException("Error while generating selection list: " 
+ e.getMessage(), e);
           } finally {
  
  
  
  1.4       +2 -2      
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/SelectionList.java
  
  Index: SelectionList.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/datatype/SelectionList.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SelectionList.java        25 Sep 2003 17:37:30 -0000      1.3
  +++ SelectionList.java        11 Oct 2003 15:57:00 -0000      1.4
  @@ -50,10 +50,10 @@
   */
   package org.apache.cocoon.woody.datatype;
   
  +import java.util.Locale;
  +
   import org.xml.sax.ContentHandler;
   import org.xml.sax.SAXException;
  -
  -import java.util.Locale;
   
   /**
    * Interface to be implemented by selection lists.
  
  
  
  1.5       +6 -0      cocoon-2.1/src/webapp/samples/flow/calc/sitemap.xmap
  
  Index: sitemap.xmap
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/webapp/samples/flow/calc/sitemap.xmap,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- sitemap.xmap      14 Jul 2003 09:30:45 -0000      1.4
  +++ sitemap.xmap      11 Oct 2003 15:57:00 -0000      1.5
  @@ -5,6 +5,7 @@
     <!-- indicates what flowscript to attach to this sitemap -->
     <map:flow language="javascript">
       <map:script src="calc.js"/>
  +    <map:script src="test.js"/>
     </map:flow>
   
     <map:pipelines>
  @@ -47,6 +48,11 @@
         <map:match pattern="">
           <map:call function="calculator"/>
         </map:match>
  +
  +      <map:match pattern="test">
  +        <map:call function="test"/>
  +      </map:match>
  +
       </map:pipeline>
     </map:pipelines>
   </map:sitemap>
  
  
  

Reply via email to