xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ce...@apache.org
Subject cvs commit: xml-xmlbeans/v2/test/src/schemas selectChldAttTest.xsd
Date Mon, 05 Apr 2004 17:26:07 GMT
cezar       2004/04/05 10:26:07

  Modified:    v2/test/src/drt/drtcases SmokeTests.java
  Added:       v2/test/src/drt/drtcases SelectChldAttTests.java
               v2/test/src/schemas selectChldAttTest.xsd
  Log:
  Drt test case for selectChildren/Attributes feature.
  
  DRT: passes
  
  Revision  Changes    Path
  1.10      +1 -0      xml-xmlbeans/v2/test/src/drt/drtcases/SmokeTests.java
  
  Index: SmokeTests.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/SmokeTests.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SmokeTests.java	29 Feb 2004 16:38:25 -0000	1.9
  +++ SmokeTests.java	5 Apr 2004 17:26:07 -0000	1.10
  @@ -50,6 +50,7 @@
           suite.addTest(RichParserTests.suite());
           suite.addTest(NumeralsTests.suite());
           suite.addTest(ValidatingXMLStreamReaderTests.suite());
  +        suite.addTest(SelectChldAttTests.suite());
           return suite;
       }
   }
  
  
  
  1.1                  xml-xmlbeans/v2/test/src/drt/drtcases/SelectChldAttTests.java
  
  Index: SelectChldAttTests.java
  ===================================================================
  /*   Copyright 2004 The Apache Software Foundation
   *
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
   *   Unless required by applicable law or agreed to in writing, software
   *   distributed under the License is distributed on an "AS IS" BASIS,
   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *   See the License for the specific language governing permissions and
   *  limitations under the License.
   */
  package drtcases;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import junit.framework.TestCase;
  import junit.framework.Assert;
  import org.apache.xmlbeans.XmlException;
  import org.apache.xmlbeans.XmlOptions;
  import org.apache.xmlbeans.XmlObject;
  import org.apache.xmlbeans.QNameSet;
  import org.apache.xmlbeans.QNameSetBuilder;
  import org.apache.xml.test.selectChldAtt.DocDocument;
  import org.apache.xml.test.selectChldAtt.TypeExtendedC;
  
  import javax.xml.namespace.QName;
  import java.util.Collection;
  import java.util.ArrayList;
  import java.util.Iterator;
  
  /**
   * @author Cezar Andrei (cezar.andrei at bea.com)
   * Date: Apr 5, 2004
   */
  public class SelectChldAttTests extends TestCase
  {
      public SelectChldAttTests(String name) { super(name); }
      public static Test suite() { return new TestSuite(SelectChldAttTests.class); }
  
      private static final String XSI_URI="http://www.w3.org/2001/XMLSchema-instance";
  
      public void testSelect() throws XmlException
      {
          String uri = "http://xml.apache.org/test/selectChldAtt";
  
          String xml = "<doc xmlns='" + uri + "'>\n" +
              "  <int>7</int>\n" +
              "  <string> ... some text ... </string>\n" +
  
              "  <elemA price='4.321'>\n" +
              "    <topLevelElement> this is wildcard bucket </topLevelElement>\n"
+
              "  </elemA>\n" +
  
              "  <elemB xmlns:p='uri:other_namespace' \n" +
              "       p:att='attribute in #other namespace'>\n" +
              "    <someElement>2</someElement>\n" +
              "    <p:otherElement> element in #other namespace </p:otherElement>\n"
+
              "  </elemB>\n" +
  
              "  <elemC xmlns:xsi='" + XSI_URI + "' \n" +
              "         xmlns:p='uri_other_namespace' \n" +
              "         xsi:type='typeExtendedC' \n" +
              "         att1='attribute from typeC' \n" +
              "         aditionalAtt='attribute added in type extension' \n" +
              "         p:validAtt='attribute in any bucket' >\n" +
              "    <someElement> element from typeC </someElement>\n" +
              "    <p:validElement> element in the 'any' bucket for typeExtendedC </p:validElement>\n"
+
              "    <aditionalElement> element from typeExtendedC </aditionalElement>\n"
+
              "  </elemC>\n" +
              "</doc>";
  
          DocDocument document = DocDocument.Factory.parse(xml);
          DocDocument.Doc doc = document.getDoc();
          Collection errors = new ArrayList();
          Assert.assertTrue("Valid instance", doc.validate(new XmlOptions().setErrorListener(errors)));
          printErrors(errors);
  
          XmlObject xo;
          XmlObject[] xos;
  
          // select a known element
          xos = doc.selectChildren(new QName(uri, "int"));
          //print("1 selectChildren 'int' : ", xos);
          Assert.assertTrue("1 selectChildren 'int' : ", verifyResult(xos, new String[]{"<xml-fragment>7</xml-fragment>"}));
  
          xos = doc.selectChildren(uri, "string");
          //print("2 selectChildren 'string' : ", xos);
          Assert.assertTrue("2 selectChildren 'string' : ", verifyResult(xos, new String[]{"<xml-fragment>...
some text ...</xml-fragment>"}));
  
          // elemA
          xos = doc.selectChildren(new QName(uri, "elemA"));
          //print("3 selectChildren 'elemA' : ", xos);
          Assert.assertTrue("3 selectChildren 'elemA' : ",
              verifyResult(xos, new String[]{"<xml-fragment price=\"4.321\" xmlns:sel=\""
+ uri +"\">\n" +
              "  <sel:topLevelElement>this is wildcard bucket</sel:topLevelElement>\n"
+
              "</xml-fragment>"}));
  
          // select a known attribute
          xo = xos[0].selectAttribute(new QName("", "price"));
          //print("4     selectAttribute 'price' : ", xo);
          Assert.assertTrue("4     selectAttribute 'price' : ",
              verifyResult(xo, "<xml-fragment>4.321</xml-fragment>"));
  
          // select all attributes
          QNameSet qns = QNameSet.forWildcardNamespaceString("##any", uri);
          xos = xos[0].selectAttributes(qns);
          //print("5     selectAttributes set'##any' :", xos);
          Assert.assertTrue("5     selectAttributes set'##any' :",
              verifyResult(xos, new String[]{"<xml-fragment>4.321</xml-fragment>"}));
  
          // elemB
          xos = doc.selectChildren(new QName(uri, "elemB"));
          //print("6 selectChildren 'elemB' : ", xos);
  
          //print("7     selectChildren set'##other' : " , xos[0].selectChildren(QNameSet.forWildcardNamespaceString("##other",
uri)));
          Assert.assertTrue("7     selectChildren set'##other' : ",
              verifyResult( xos[0].selectChildren(QNameSet.forWildcardNamespaceString("##other",
uri))
              , new String[]{"<xml-fragment xmlns:p=\"uri:other_namespace\">element
in #other namespace</xml-fragment>"}));
          //print("8     selectAttributes set'##other' : ", xos[0].selectAttributes(QNameSet.forWildcardNamespaceString("##other",
uri)));
          Assert.assertTrue("8     selectAttributes set'##other' : ",
              verifyResult(xos[0].selectAttributes(QNameSet.forWildcardNamespaceString("##other",
uri)),
              new String[]{"<xml-fragment>attribute in #other namespace</xml-fragment>"}));
  
          // elemC
          xos = doc.selectChildren(new QName(uri, "elemC"));
          //print("9 selectChildren 'elemC' : ", xos);
          //print("10    selectChildren set'##any' : " , xos[0].selectChildren(QNameSet.forWildcardNamespaceString("##any",
uri)));
          Assert.assertTrue("10    selectChildren set'##any' : ",
              verifyResult(xos[0].selectChildren(QNameSet.forWildcardNamespaceString("##any",
uri))
              , new String[]{"<xml-fragment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:p=\"uri_other_namespace\">element from typeC</xml-fragment>",
              "<xml-fragment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"uri_other_namespace\">element
in the 'any' bucket for typeExtendedC</xml-fragment>",
              "<xml-fragment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"uri_other_namespace\">element
from typeExtendedC</xml-fragment>"}));
  
          // select elements in the any bucket by excluding the the known elements
          QNameSetBuilder qnsb = new QNameSetBuilder();
          qnsb.add(new QName(uri, "someElement"));
          qnsb.add(new QName(uri, "aditionalElement"));
          qnsb.invert();
  
          //print("11a    selectChildren in the any bucket for typeExtendedC: " , xos[0].selectChildren(qnsb.toQNameSet()));
          Assert.assertTrue("11a    selectChildren in the any bucket for typeExtendedC: ",
              verifyResult(xos[0].selectChildren(qnsb.toQNameSet()),
              new String[]{"<xml-fragment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:p=\"uri_other_namespace\">element in the 'any' bucket for typeExtendedC</xml-fragment>"}));
  
          //print("11b    selectChildren in the any bucket for typeExtendedC: " , xos[0].selectChildren(TypeExtendedC.type.qnameSetForWildcardElements()));
          Assert.assertTrue("11b    selectChildren in the any bucket for typeExtendedC: ",
              verifyResult(xos[0].selectChildren(TypeExtendedC.type.qnameSetForWildcardElements()),
              new String[]{"<xml-fragment xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:p=\"uri_other_namespace\">element in the 'any' bucket for typeExtendedC</xml-fragment>"}));
  
          // select attributes in the any bucket by excluding the the known attributes
          qnsb = new QNameSetBuilder();
          qnsb.add(new QName("", "att1"));
          qnsb.add(new QName("", "aditionalAtt"));
          qnsb.add(new QName(XSI_URI, "type"));
          qnsb.invert();
  
          //print("12a    selectChildren in the any bucket for typeExtendedC: " , xos[0].selectAttributes(qnsb.toQNameSet()));
          Assert.assertTrue("12a    selectChildren in the any bucket for typeExtendedC: ",
              verifyResult(xos[0].selectAttributes(qnsb.toQNameSet()),
              new String[]{"<xml-fragment>attribute in any bucket</xml-fragment>"}));
          //print("12b    selectChildren in the any bucket for typeExtendedC: " , xos[0].selectAttributes(TypeExtendedC.type.qnameSetForWildcardAttributes()));
          Assert.assertTrue("12b    selectChildren in the any bucket for typeExtendedC: ",
              verifyResult(xos[0].selectAttributes(TypeExtendedC.type.qnameSetForWildcardAttributes()),
              new String[]{"<xml-fragment>typeExtendedC</xml-fragment>",
              "<xml-fragment>attribute in any bucket</xml-fragment>"}));
      }
  
      private static void print(String msg, XmlObject[] xos)
      {
          System.out.println(msg + "   " + xos.length);
          for (int i=0; i<xos.length; i++)
          {
              System.out.println("      " + i + " :" + xos[i]);
          }
      }
  
      private static void print(String msg, XmlObject xo)
      {
          System.out.println(msg + "   " + xo);
      }
  
      private static void printErrors(Collection errors)
      {
          for (Iterator i = errors.iterator(); i.hasNext();)
          {
              System.out.println("ERROR: " + i.next());
          }
      }
  
      private static boolean verifyResult(XmlObject[] xos, String[] expected)
      {
          if (xos==null && expected==null)
              return true;
  
          if (xos==null || expected==null)
              return false;
  
          if (xos.length != expected.length)
              return false;
  
          for (int i = 0; i < xos.length; i++)
          {
              XmlObject xo = xos[i];
              if (!equalsIgnoreNewLine(xo.toString(),expected[i]))
              {
                  System.out.println("ERROR:\n    Actual:\n" + xo.toString() + "\n   Expected:\n"
+ expected[i]);
                  return false;
              }
          }
          return true;
      }
  
      private static boolean verifyResult(XmlObject xo, String expected)
      {
          if (xo==null && expected==null)
              return true;
  
          if (xo==null || expected==null)
              return false;
  
          if (!xo.toString().equals(expected))
          {
              System.out.println("ERROR:\n    Actual:\n" + xo.toString() + "\n   Expected:\n"
+ expected);
              return false;
          }
          else
              return true;
      }
  
      private static boolean equalsIgnoreNewLine(String s1, String s2)
      {
          if (s1==null && s2==null)
              return true;
  
          if (s1==null || s2==null)
              return false;
  
          int i1 = 0, i2 = 0;
          while (i1<s1.length() || i2<s2.length())
          {
              if (s1.charAt(i1)=='\n' || s1.charAt(i1)=='\r')
              {
                  i1++;
                  continue;
              }
  
              if (s2.charAt(i2)=='\n' || s2.charAt(i2)=='\r')
              {
                  i2++;
                  continue;
              }
  
              if (s1.charAt(i1)!=s2.charAt(i2))
                  return false;
  
              i1++;
              i2++;
          }
  
          if ( (i1 == s1.length()) && (i2 == s2.length()))
              return true;
          else
              return false;
      }
  }
  
  
  
  1.1                  xml-xmlbeans/v2/test/src/schemas/selectChldAttTest.xsd
  
  Index: selectChldAttTest.xsd
  ===================================================================
  <xs:schema
     xmlns:xs='http://www.w3.org/2001/XMLSchema'
     xmlns='http://xml.apache.org/test/selectChldAtt'
     targetNamespace='http://xml.apache.org/test/selectChldAtt'
     elementFormDefault='qualified'>
  
    <xs:element name='doc'>
      <xs:complexType>
        <xs:sequence>
          <xs:choice minOccurs='0' maxOccurs='unbounded'>
            <xs:element name='int' type='xs:int' />
            <xs:element name='string' type='xs:string' />
            <xs:element name='elemA' type='typeA' />
            <xs:element name='elemB' type='typeB' />
            <xs:element name='elemC' type='typeC' />
          </xs:choice>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  
    <xs:complexType name="typeA">
      <xs:sequence>
        <xs:any namespace="##any" processContents="lax" minOccurs='0' maxOccurs='unbounded'
/>
      </xs:sequence>
      <xs:anyAttribute namespace="##any" processContents="lax" />
    </xs:complexType>
  
    <xs:complexType name="typeB">
      <xs:sequence>
        <xs:element name="someElement" type='xs:int' />
  
        <xs:any namespace="##other" processContents="lax" minOccurs='0' maxOccurs='unbounded'
/>
      </xs:sequence>
  
  	<xs:attribute name='att1' type='xs:string' />
  
      <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
  
    <xs:complexType name="typeC">
      <xs:sequence>
        <xs:element name="someElement" type='xs:string' />
  
        <xs:any namespace="##other" processContents="lax" minOccurs='0' maxOccurs='unbounded'
 />
      </xs:sequence>
  	<xs:attribute name='att1' type='xs:string' />
  
      <xs:anyAttribute namespace="##other" processContents="lax" />
    </xs:complexType>
  
    <xs:complexType name="typeExtendedC">
      <xs:complexContent>
        <xs:extension base='typeC'>
          <xs:sequence>
            <xs:element name="aditionalElement" type='xs:string' />
          </xs:sequence>
          <xs:attribute name='aditionalAtt' type='xs:string' />
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  
    <xs:element name='topLevelElement' type='xs:string' />
  
    <xs:attribute name='price' type='xs:float' />
    <xs:attribute name='quant' type='xs:byte' />
  
  </xs:schema>
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org


Mime
View raw message