vgritsenko    2003/03/17 17:01:27

  Modified:    src/java/org/apache/cocoon/components/xmlform Form.java
               src/test/org/apache/cocoon/components/resolver/test
                        ResolverImplTestCase.java
                        ResolverImplTestCase.xtest
               src/test/org/apache/cocoon/components/source
                        SourceResolverAdapter.java
               src/test/org/apache/cocoon/components/validation/test
                        ZTestBean.java ZValidationTest.java
                        zxmlform-sch-report-test.xml
  Log:
  schematron validation test
  
  Revision  Changes    Path
  1.3       +5 -5      
cocoon-2.1/src/java/org/apache/cocoon/components/xmlform/Form.java
  
  Index: Form.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/java/org/apache/cocoon/components/xmlform/Form.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Form.java 16 Mar 2003 17:49:14 -0000      1.2
  +++ Form.java 18 Mar 2003 01:01:26 -0000      1.3
  @@ -73,7 +73,7 @@
    *  FormValidatingAction
    * </p>
    *
  - * NOTE: This class is NOT thread safe
  + * <b>NOTE: This class is NOT thread safe</b>
    *
    * @author Ivelin Ivanov, [EMAIL PROTECTED]
    * @author [EMAIL PROTECTED] 
  @@ -284,7 +284,7 @@
            * @return a Set of XPath strings pointing to 
            * each nodeset satisfying the nodeset selector
            *
  -     * <p>
  +         * <p>
            * TODO: the Collection return type should be replaced with a Set.
            * LinkedHashSet implementation should be used. All resolved 
            * nodes are unique in the resulting set, therefore Set is more appropriate.
  @@ -347,12 +347,12 @@
           /**
            * Populates an HTML Form POST into the XMLForm model (JavaBean or DOM 
node).
            *
  -     * <p>
  +         * <p>
            * Expects that all request parameter names are XPath expressions
            * to attributes of the model.
            * For each request parameter, finds and assigns its value to the
            * JavaBean property corresponding to the parameter's name
  -     * </p>
  +         * </p>
            *
            * TODO: provide a more sophisticated examples with checkboxes, multi 
choice,
            * radio button, text area, file upload, etc.
  
  
  
  1.3       +1 -3      
cocoon-2.1/src/test/org/apache/cocoon/components/resolver/test/ResolverImplTestCase.java
  
  Index: ResolverImplTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/resolver/test/ResolverImplTestCase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ResolverImplTestCase.java 12 Mar 2003 15:02:12 -0000      1.2
  +++ ResolverImplTestCase.java 18 Mar 2003 01:01:26 -0000      1.3
  @@ -254,7 +254,6 @@
        */
       public ResolverImplTestCase(String name) {
           super(name);
  -
       }
   
   
  @@ -444,4 +443,3 @@
           context.put(Constants.CONTEXT_ENVIRONMENT_CONTEXT, commandline_context);
       }
   }
  -
  
  
  
  1.2       +8 -15     
cocoon-2.1/src/test/org/apache/cocoon/components/resolver/test/ResolverImplTestCase.xtest
  
  Index: ResolverImplTestCase.xtest
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/resolver/test/ResolverImplTestCase.xtest,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResolverImplTestCase.xtest        9 Mar 2003 00:10:38 -0000       1.1
  +++ ResolverImplTestCase.xtest        18 Mar 2003 01:01:26 -0000      1.2
  @@ -4,12 +4,10 @@
   
   <!ELEMENT testcase (annotation?,logkit,context?,roles,components)>
   
  -<!-- annotation describes the testcase
  --->
  +<!-- annotation describes the testcase -->
   <!ELEMENT annotation (#PCDATA)>
   
  -<!-- logkit define the logkit in use
  --->
  +<!-- logkit define the logkit in use -->
   <!ELEMENT logkit (factories, targets, categories)>
   <!ELEMENT factories (factory+)>
   
  @@ -38,8 +36,7 @@
     id-ref CDATA #REQUIRED
   >
   
  -<!-- the context used by the components
  --->
  +<!-- the context used by the components -->
   <!ELEMENT context (entry*)>
   <!ELEMENT entry EMPTY>
   <!ATTLIST entry
  @@ -47,8 +44,7 @@
     value CDATA #REQUIRED
   >
   
  -<!-- the roles of the components
  --->
  +<!-- the roles of the components -->
   <!ELEMENT roles (role+)>
   <!ELEMENT role (hint*)>
   <!ATTLIST role
  @@ -63,8 +59,7 @@
     class CDATA #REQUIRED
   >
   
  -<!-- define components element, attributes, and its children
  --->
  +<!-- define components element, attributes, and its children -->
   <!ELEMENT components (resolver)>
   <!ATTLIST components 
     logger CDATA #IMPLIED
  @@ -80,9 +75,9 @@
     name CDATA #REQUIRED
     value CDATA #REQUIRED
   >
  -
   ]>
   
  +
   <testcase>
     <annotation>
       Test Cases: Catalog Entity Resolver 
  @@ -93,7 +88,7 @@
       </factories>
       <targets>
          <file id="root">
  -        <filename>resolver-test.log</filename>
  +        <filename>cocoon-resolver-test.log</filename>
           <format type="extended">
             %7.7{priority} %5.5{time}   [%8.8{category}] (%{context}): 
%{message}\n%{throwable}
           </format>
  @@ -115,8 +110,7 @@
       </role>
     </roles>
   
  -  <!--
  -  -->
  +  <!-- -->
     <components>
       <entity-resolver>
         <parameter name="verbosity" value="10"/>
  @@ -124,4 +118,3 @@
       </entity-resolver>
     </components>
   </testcase>
  -
  
  
  
  1.5       +3 -5      
cocoon-2.1/src/test/org/apache/cocoon/components/source/SourceResolverAdapter.java
  
  Index: SourceResolverAdapter.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/source/SourceResolverAdapter.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SourceResolverAdapter.java        16 Mar 2003 18:03:55 -0000      1.4
  +++ SourceResolverAdapter.java        18 Mar 2003 01:01:26 -0000      1.5
  @@ -84,7 +84,7 @@
       /**
        * Get a <code>Source</code> object.
        * This is a shortcut for <code>resolve(location, null, null)</code>
  -     * @throws SourceNotFoundException if the source cannot be found
  +     * @throws org.apache.excalibur.source.SourceException if the source cannot be 
resolved
        */
       public org.apache.excalibur.source.Source resolveURI( String location )
           throws MalformedURLException, IOException, 
org.apache.excalibur.source.SourceException {
  @@ -102,7 +102,7 @@
        *               is optional and can be <code>null</code>.
        * @param parameters - Additional parameters for the URI. The parameters
        *                     are specific to the used protocol.
  -     * @throws SourceNotFoundException if the source cannot be found
  +     * @throws org.apache.excalibur.source.SourceException if the source cannot be 
resolved
        */
       public org.apache.excalibur.source.Source resolveURI( String location,
                                                             String base,
  @@ -148,8 +148,6 @@
           throws SAXException, IOException, ProcessingException {
   
           SAXParser parser = null;
  -        org.apache.excalibur.source.Source assertionsource = null;
  -
           try {
               parser = (SAXParser) this.manager.lookup(SAXParser.ROLE);
   
  
  
  
  1.3       +2 -2      
cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/ZTestBean.java
  
  Index: ZTestBean.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/ZTestBean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ZTestBean.java    16 Mar 2003 18:03:55 -0000      1.2
  +++ ZTestBean.java    18 Mar 2003 01:01:27 -0000      1.3
  @@ -77,6 +77,7 @@
        public String getName() {
                return name;
        }
  +
        public void setName(String newName) {
                name = newName;
        }
  @@ -108,5 +109,4 @@
        public void incrementCount() {
                count++;
        }
  -
   }
  
  
  
  1.3       +129 -72   
cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/ZValidationTest.java
  
  Index: ZValidationTest.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/ZValidationTest.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ZValidationTest.java      16 Mar 2003 18:03:55 -0000      1.2
  +++ ZValidationTest.java      18 Mar 2003 01:01:27 -0000      1.3
  @@ -58,86 +58,143 @@
   
   import java.io.File;
   import java.io.FileInputStream;
  -import java.io.InputStream;
   import java.util.Iterator;
   import java.util.List;
  +import java.util.ArrayList;
  +
  +import junit.framework.TestCase;
  +import junit.swingui.TestRunner;
   
   /**
    * Test class for the Validation API
    *
  - * Takes 2 command line arguments.
  - *
  - * First is the location of a Schematron Schema file
  - * Second is the validation phase to use
  + * <p>Uses file 
src/test/org/apache/cocoon/components/validation/test/zxmlform-sch-report-test.xml.
    *
    * @version CVS $Id$
    */
  -public class ZValidationTest {
  -
  -     /**
  -      * Method main
  -      */
  -     public static void main(String args[]) throws Exception {
  -             System.out.println("\n=== Java based Schematron validation ===");
  -
  -             if (args.length < 1) {
  -                     System.err.println(
  -                             "Usage: java Schematron <schema.xml> " + "[phase] ");
  -                     return;
  -             }
  -
  -             // use custom schema
  -             File file = new File(args[0]);
  -             if (!file.exists())
  -                     throw new Exception("Error: schema file not found !");
  -             InputStream istrm = new FileInputStream(file);
  -             InputSource is = new InputSource(istrm);
  -             SchemaFactory schf =
  -                     SchemaFactory.lookup(SchemaFactory.NAMESPACE_SCHEMATRON);
  -             Schema sch = schf.compileSchema(is);
  -             Validator validator = sch.newValidator();
  -
  -             // set preprocessor parameters 
  -             if (args.length > 1)
  -                     validator.setProperty("phase", new String(args[1]));
  -
  -             ZTestBean tbean = new ZTestBean();
  -
  -             // measure validation speed
  -             long time = System.currentTimeMillis();
  -             int i = 0;
  -             List violations = null;
  -             for (; i < 100; i++) {
  -                     // perform validation
  -                     violations = validator.validate(tbean);
  -             }
  -             time = System.currentTimeMillis() - time;
  -             System.out.println("\nValidation performance:");
  -             System.out.println(
  -                     " validate() executed "
  -                             + i
  -                             + " times for a total of "
  -                             + time
  -                             + " ms");
  -             System.out.println("Avarage validation time: " + (time / i) + " ms ");
  -
  -             // everything ok?
  -             if (violations == null) {
  -                     System.out.println("\nValidation ok, no messages generated");
  -             } else {
  -                     System.out.println("Validation encountered errors. Messages 
:");
  -                     Iterator viter = violations.iterator();
  -                     while (viter.hasNext()) {
  -                             Violation v = (Violation) viter.next();
  -                             System.out.println(
  -                                     "Validity violation path: "
  -                                             + v.getPath()
  -                                             + ", message: "
  -                                             + v.getMessage());
  -                     }
  -             }
  +public class ZValidationTest extends TestCase {
   
  -             System.out.println("\n=== Schematron validation done ===");
  -     }
  +    private static final int count = 100;
   
  +    public ZValidationTest(String name) {
  +        super(name);
  +    }
  +
  +    private void testSchema(String schema, String phase, List violations) {
  +        // use custom schema
  +        File file = new File(schema);
  +        if (!file.exists()) {
  +            fail("Error: schema file " + schema + " not found");
  +        }
  +
  +        try {
  +            InputSource is = new InputSource(new FileInputStream(file));
  +            SchemaFactory schf =
  +                SchemaFactory.lookup(SchemaFactory.NAMESPACE_SCHEMATRON);
  +            Schema sch = schf.compileSchema(is);
  +            Validator validator = sch.newValidator();
  +
  +            // set preprocessor parameters
  +            if (phase != null) {
  +                validator.setProperty(Validator.PROPERTY_PHASE, phase);
  +            }
  +
  +            ZTestBean tbean = new ZTestBean();
  +
  +            // measure validation speed
  +            long time = System.currentTimeMillis();
  +            List vs = null;
  +            for (int i = 0; i < count; i++) {
  +                // perform validation
  +                vs = validator.validate(tbean);
  +            }
  +            time = System.currentTimeMillis() - time;
  +            System.out.println("Validation performed " + count
  +                               + " times for a total of " + time + " ms");
  +            System.out.println("Avarage validation time is "
  +                               + (time / count) + " ms ");
  +
  +            if (vs == null) {
  +                vs = new ArrayList();
  +            }
  +
  +            // everything ok?
  +            assertEquals("Violations count does not match", violations.size(), 
vs.size());
  +            for (Iterator i = violations.iterator(); i.hasNext();) {
  +                Violation v = (Violation) i.next();
  +                boolean gotit = false;
  +                for (Iterator j = vs.iterator(); j.hasNext();) {
  +                    Violation w = (Violation) j.next();
  +                    if (v.getPath().equals(w.getPath()) && 
v.getMessage().equals(w.getMessage())) {
  +                        gotit = true;
  +                        break;
  +                    }
  +                }
  +                assertTrue("Expected violation " + v.getPath() + " '" + 
v.getMessage() + "' not found", gotit);
  +            }
  +            for (Iterator i = vs.iterator(); i.hasNext();) {
  +                Violation v = (Violation) i.next();
  +                boolean gotit = false;
  +                for (Iterator j = violations.iterator(); j.hasNext();) {
  +                    Violation w = (Violation) j.next();
  +                    if (v.getPath().equals(w.getPath()) && 
v.getMessage().equals(w.getMessage())) {
  +                        gotit = true;
  +                        break;
  +                    }
  +                }
  +                assertTrue("Unexpected violation " + v.getPath() + " '" + 
v.getMessage() + "' found", gotit);
  +            }
  +        } catch (Exception e) {
  +            fail("Got an exception " + e);
  +        }
  +    }
  +
  +    public void testSchema() {
  +        Violation violation;
  +        List violations = new ArrayList();
  +
  +        violation = new Violation();
  +        violation.setPath("/scope");
  +        violation.setMessage("Scope should be request or session.");
  +        violations.add(violation);
  +
  +        violation = new Violation();
  +        violation.setPath("/name");
  +        violation.setMessage("Animal name should be at least 4 characters.");
  +        violations.add(violation);
  +
  +        violation = new Violation();
  +        violation.setPath("/count");
  +        violation.setMessage("The counter should be > 0.");
  +        violations.add(violation);
  +
  +        
testSchema("src/test/org/apache/cocoon/components/validation/test/zxmlform-sch-report-test.xml",
  +                   null, violations);
  +    }
  +
  +    public void testSchema_PhaseNew() {
  +        Violation violation;
  +        List violations = new ArrayList();
  +
  +        violation = new Violation();
  +        violation.setPath("/scope");
  +        violation.setMessage("Scope should be request or session.");
  +        violations.add(violation);
  +
  +        violation = new Violation();
  +        violation.setPath("/name");
  +        violation.setMessage("Animal name should be at least 4 characters.");
  +        violations.add(violation);
  +
  +        
testSchema("src/test/org/apache/cocoon/components/validation/test/zxmlform-sch-report-test.xml",
  +                   "New", violations);
  +    }
  +
  +    /**
  +     * The main program for this test.
  +     */
  +    public static void main(final String[] args) throws Exception {
  +        final String[] testCaseName = { ZValidationTest.class.getName() };
  +        TestRunner.main(testCaseName);
  +    }
   }
  
  
  
  1.2       +56 -49    
cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/zxmlform-sch-report-test.xml
  
  Index: zxmlform-sch-report-test.xml
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/test/org/apache/cocoon/components/validation/test/zxmlform-sch-report-test.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- zxmlform-sch-report-test.xml      9 Mar 2003 00:10:39 -0000       1.1
  +++ zxmlform-sch-report-test.xml      18 Mar 2003 01:01:27 -0000      1.2
  @@ -1,58 +1,65 @@
   <?xml version="1.0" ?>
  -<!--
  -     Sample Schematron schema for the ZValidationTest
   
  -    Pass this file as a first argument on the command line to ZValidationTest
  -
  -     Ivelin Ivanov, [EMAIL PROTECTED]
  --->
  +<!--+
  +    | Sample Schematron schema for the ZValidationTest
  +    |
  +    | Pass this file as a first argument on the command line to ZValidationTest
  +    |
  +    | Ivelin Ivanov, [EMAIL PROTECTED]
  +    | CVS $Id$
  +    +-->
   
   <schema ns="http://xml.apache.cocoon/xmlform";  
xmlns="http://www.ascc.net/xml/schematron";>
  -     <title>Schema for the XML Form example</title>
  -        <phase id="New">
  -                <p>For creating new documents.</p>
  -                <active pattern="mini"/>
  -        </phase>
  -        <phase id="Draft">
  -                <p>For fast validation of draft documents.</p>
  -                <active pattern="required" />
  -        </phase>
  -        <phase id="Full">
  -                <p>For final validation and tracking some tricky problems.</p>
  -                <active pattern="mini" />
  -                <active pattern="required" />
  -                <active pattern="extra" />
  -        </phase>
  -     <pattern name="A Simple Validation Pattern" id="mini">
  -             <rule context="name">
  -                     <assert  test="string-length(.) > 3" diagnostics="dname 
dcount">Animal name should be at least 4 characters.</assert>
  -                     <assert  test="string-length(.) &lt; 10">Animal name should be 
less than 10 characters.</assert>
  -             </rule>
  -             <rule context="scope">
  -                     <assert test="normalize-space(.) = 'session' or 
normalize-space(.) = 'request'">Scope should be request or session.</assert>
  -             </rule>
  -     </pattern>
  -     <pattern name="Required Validation Pattern" id="required">
  -             <rule context="count">
  -                     <assert  test="number(.) > 0" diagnostics="dcount"> The 
counter should be > 0.</assert>
  -             </rule>
  -     </pattern>
  -     <pattern name="Extra Simple Validation Pattern" id="extra">
  +    <title>Schema for the XML Form example</title>
  +
  +    <phase id="New">
  +        <p>For creating new documents.</p>
  +        <active pattern="mini"/>
  +    </phase>
  +    <phase id="Draft">
  +        <p>For fast validation of draft documents.</p>
  +        <active pattern="required" />
  +    </phase>
  +    <phase id="Full">
  +        <p>For final validation and tracking some tricky problems.</p>
  +        <active pattern="mini" />
  +        <active pattern="required" />
  +        <active pattern="extra" />
  +    </phase>
  +
  +    <pattern name="A Simple Validation Pattern" id="mini">
  +        <rule context="name">
  +            <assert  test="string-length(.) > 3" diagnostics="dname dcount">Animal 
name should be at least 4 characters.</assert>
  +            <assert  test="string-length(.) &lt; 10">Animal name should be less 
than 10 characters.</assert>
  +        </rule>
  +        <rule context="scope">
  +            <assert test="normalize-space(.) = 'session' or normalize-space(.) = 
'request'">Scope should be request or session.</assert>
  +        </rule>
  +    </pattern>
  +
  +    <pattern name="Required Validation Pattern" id="required">
  +        <rule context="count">
  +            <assert  test="number(.) > 0" diagnostics="dcount">The counter should 
be > 0.</assert>
  +        </rule>
  +    </pattern>
  +
  +    <pattern name="Extra Simple Validation Pattern" id="extra">
             a pattern
  -             <rule context="/personalInfo/type"> a rule 
  -                     <assert  test="text(.) = 'mammal'"> Animal type should be 
mammal</assert>
  -                     <report  test="text(.) != 'mammal'"> Animal is not 
mammal</report>
  -             </rule>
  -     </pattern>
  +        <rule context="/personalInfo/type"> a rule 
  +            <assert  test="text(.) = 'mammal'">Animal type should be 
mammal.</assert>
  +            <report  test="text(.) != 'mammal'">Animal is not mammal.</report>
  +        </rule>
  +    </pattern>
  +
       <diagnostics>
  -            <diagnostic id="dname">
  -                Animals rarely have names shorter than 5 characters.
  -                Dog and Cat are among the rare exceptions.
  -                All right, all right, don't hold me to these words, this is just a 
sloppy test after all.
  -            </diagnostic>
  -            <diagnostic id="dcount">
  -                The animal counter simply keeps track of the number of times this 
animal was visited .
  -            </diagnostic>
  +        <diagnostic id="dname">
  +            Animals rarely have names shorter than 5 characters.
  +            Dog and Cat are among the rare exceptions.
  +            All right, all right, don't hold me to these words, this is just a 
sloppy test after all.
  +        </diagnostic>
  +        <diagnostic id="dcount">
  +            The animal counter simply keeps track of the number of times this 
animal was visited .
  +        </diagnostic>
       </diagnostics>
   </schema>
   
  
  
  

Reply via email to