[jboss-cvs] JBossAS SVN: r80315 - in projects/metadata/trunk/src/test/java/org/jboss/test/metadata: binding and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Oct 31 10:21:05 EDT 2008


Author: alex.loubyansky at jboss.com
Date: 2008-10-31 10:21:05 -0400 (Fri, 31 Oct 2008)
New Revision: 80315

Added:
   projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/
   projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/SchemaBindingValidationUnitTestCase.java
Log:
JBMETA-142

Added: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/SchemaBindingValidationUnitTestCase.java
===================================================================
--- projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/SchemaBindingValidationUnitTestCase.java	                        (rev 0)
+++ projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/SchemaBindingValidationUnitTestCase.java	2008-10-31 14:21:05 UTC (rev 80315)
@@ -0,0 +1,328 @@
+/*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+package org.jboss.test.metadata.binding;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.TestCase;
+
+import org.apache.xerces.xs.XSAttributeDeclaration;
+import org.apache.xerces.xs.XSAttributeUse;
+import org.apache.xerces.xs.XSComplexTypeDefinition;
+import org.apache.xerces.xs.XSConstants;
+import org.apache.xerces.xs.XSElementDeclaration;
+import org.apache.xerces.xs.XSModel;
+import org.apache.xerces.xs.XSModelGroup;
+import org.apache.xerces.xs.XSNamedMap;
+import org.apache.xerces.xs.XSObjectList;
+import org.apache.xerces.xs.XSParticle;
+import org.apache.xerces.xs.XSSimpleTypeDefinition;
+import org.apache.xerces.xs.XSTerm;
+import org.apache.xerces.xs.XSTypeDefinition;
+import org.apache.xerces.xs.XSWildcard;
+import org.jboss.metadata.ejb.jboss.JBoss50MetaData;
+import org.jboss.xb.binding.Constants;
+import org.jboss.xb.binding.Util;
+import org.jboss.xb.binding.sunday.unmarshalling.AllBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ChoiceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
+
+/**
+ * This test case validates various SchemaBinding's against their corresponding XSD's
+ * and vice versa XSD's vs SchemaBinding's to make sure the schemas and metadata API are consistent.
+ * 
+ * Currently, it's only validating SchemaBinding's against their XSD's. At the moment, it's only
+ * the most important stuff, some details are left behind for now. See the TODOs in-line.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class SchemaBindingValidationUnitTestCase extends TestCase
+{
+   public void testJBoss50() throws Exception
+   {
+      assertEquivalent("jboss_5_0.xsd", JBoss50MetaData.class);
+   }
+
+   public void assertEquivalent(String xsdName, Class<?> cls) throws IOException
+   {
+      URL xsdUrl = Thread.currentThread().getContextClassLoader().getResource("schema/" + xsdName);
+      assertNotNull(xsdUrl);
+
+      DefaultSchemaResolver resolver = new DefaultSchemaResolver();
+      resolver.addClassBindingForLocation(xsdName, cls);
+      SchemaBinding binding = resolver.resolve("", null, xsdName);
+
+      XSModel xsModel = Util.loadSchema(xsdUrl.openStream(), null, resolver);
+
+      assertEquivalent(xsModel, binding);
+   }
+
+   public static void assertEquivalent(XSModel xsSchema, SchemaBinding schemaBinding)
+   {
+      /* TODO groups are not properly bound
+      XSNamedMap groups = xsSchema.getComponents(XSConstants.MODEL_GROUP_DEFINITION);
+      for(int i = 0; i < groups.getLength(); ++i)
+      {
+         XSModelGroupDefinition xsGroupDef = (XSModelGroupDefinition)groups.item(i);
+         System.out.println(xsGroupDef.getName());
+         QName groupQName = new QName(xsGroupDef.getNamespace(), xsGroupDef.getName());
+         ModelGroupBinding groupBinding = schemaBinding.getGroup(groupQName);
+         assertNotNull("Group " + groupQName + " exists in the schema binding.", groupBinding);
+      }
+      */
+
+      XSNamedMap types = xsSchema.getComponents(XSConstants.TYPE_DEFINITION);
+      for (int i = 0; i < types.getLength(); ++i)
+      {
+         XSTypeDefinition xsType = (XSTypeDefinition) types.item(i);
+         if (Constants.NS_XML_SCHEMA.equals(xsType.getNamespace()))
+            continue;
+
+         QName typeQName = new QName(xsType.getNamespace(), xsType.getName());
+         TypeBinding typeBinding = schemaBinding.getType(typeQName);
+         if (typeBinding == null)
+         {
+            boolean ignoreIfNotFound = false;
+            if (xsType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE)
+            {
+               ignoreIfNotFound = true;
+            }
+            else
+            {
+               XSComplexTypeDefinition xsComplexType = (XSComplexTypeDefinition) xsType;
+               if (xsComplexType.getContentType() == XSComplexTypeDefinition.CONTENTTYPE_SIMPLE)
+               {
+                  XSObjectList attributeUses = xsComplexType.getAttributeUses();
+                  if(attributeUses.getLength() == 0)
+                  {
+                     ignoreIfNotFound = true;
+                  }   
+                  else if (attributeUses.getLength() == 1)
+                  {
+                     XSAttributeUse xsAttrUse = (XSAttributeUse) attributeUses.item(0);
+                     XSAttributeDeclaration xsAttr = xsAttrUse.getAttrDeclaration();
+                     if(xsAttr.getNamespace() == null && "id".equals(xsAttr.getName()))
+                        ignoreIfNotFound = true;
+                  }
+               }
+            }
+            
+            if(!ignoreIfNotFound)
+            {
+/*               for(Iterator<TypeBinding> iter = schemaBinding.getTypes(); iter.hasNext();)
+               {
+                  TypeBinding type = iter.next();
+                  System.out.println(type.getQName());
+               }
+*/
+               fail("Type " + typeQName + " defined in schema binding.");
+            }
+         }
+         else
+         {
+            assertEquivalent(xsType, typeBinding);
+         }
+      }
+
+      XSNamedMap elements = xsSchema.getComponents(XSConstants.ELEMENT_DECLARATION);
+      for (int i = 0; i < elements.getLength(); ++i)
+      {
+         XSElementDeclaration xsElement = (XSElementDeclaration) elements.item(i);
+         QName elementQName = new QName(xsElement.getNamespace(), xsElement.getName());
+         ElementBinding elementBinding = schemaBinding.getElement(elementQName);
+         assertNotNull("ElementBinding " + elementQName + " exists", elementBinding);
+      }
+   }
+
+   public static void assertEquivalent(XSElementDeclaration xsElement, ElementBinding elementBinding)
+   {
+      QName xsQName = new QName(xsElement.getNamespace(), xsElement.getName());
+      assertEquals("ElementBinding QName.", xsQName, elementBinding.getQName());
+      assertEquivalent(xsElement.getTypeDefinition(), elementBinding.getType());
+   }
+
+   public static void assertEquivalent(XSTypeDefinition xsType, TypeBinding typeBinding)
+   {
+      if(xsType.getName() == null)
+         assertNull("TypeBinding is anonymous.", typeBinding.getQName());
+      else
+      {
+         QName xsQName = new QName(xsType.getNamespace(), xsType.getName());
+         assertEquals("TypeBinding QName.", xsQName, typeBinding.getQName());
+      }
+      
+      if(xsType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE)
+         assertEquivalent((XSSimpleTypeDefinition)xsType, typeBinding);
+      else
+         assertEquivalent((XSComplexTypeDefinition)xsType, typeBinding);         
+   }
+   
+   public static void assertEquivalent(XSSimpleTypeDefinition xsType, TypeBinding typeBinding)
+   {
+      // TODO there could xsd types that are mapped to String which is bound by default to xsd:string
+      //QName xsQName = xsType.getName() == null ? null : new QName(xsType.getNamespace(), xsType.getName());
+      //assertEquals("Simple type expected to be " + (xsType == null ? "anonymous" : "named '" + xsQName + "'"), xsQName, typeBinding.getQName());
+      
+      assertTrue("Type " + typeBinding.getQName() + " is simple", typeBinding.isSimple());
+      // TODO the rest of the simple type stuff?
+   }
+
+   public static void assertEquivalent(XSComplexTypeDefinition xsType, TypeBinding typeBinding)
+   {
+      //System.out.println("assert complex type: " + typeBinding.getQName());
+
+      QName xsQName = xsType.getName() == null ? null : new QName(xsType.getNamespace(), xsType.getName());
+      assertEquals("Complex type is " + (xsType == null ? "anonymous" : "named '" + xsQName + "'"), xsQName, typeBinding.getQName());
+
+      XSObjectList xsAttrUses = xsType.getAttributeUses();
+      if(xsAttrUses.getLength() == 0)
+      {
+         // TODO missing id attributes in the schema
+         //assertTrue("Type " + typeBinding.getQName() + " has no attributes in the schema", typeBinding.getAttributes().isEmpty());
+      }
+      else
+      {
+         for(int i = 0; i < xsAttrUses.getLength(); ++i)
+         {
+            XSAttributeDeclaration xsAttr = ((XSAttributeUse)xsAttrUses.item(i)).getAttrDeclaration();
+            QName xsAttrQName = new QName(xsAttr.getNamespace(), xsAttr.getName());
+            AttributeBinding attrBinding = typeBinding.getAttribute(xsAttrQName);
+            assertNotNull("Type " + typeBinding.getQName() + " declares attribute " + xsAttrQName, attrBinding);
+            assertEquivalent(xsAttr.getTypeDefinition(), attrBinding.getType());
+         }
+      }
+      
+      XSWildcard xsAttrWildcard = xsType.getAttributeWildcard();
+      if(xsAttrWildcard != null)
+         assertNotNull("Type " + typeBinding.getQName() + " has AnyAttributeBinding", typeBinding.getAnyAttribute());
+      
+      XSSimpleTypeDefinition xsSimpleType = xsType.getSimpleType();
+      if(xsSimpleType != null)
+      {
+         TypeBinding simpleTypeBinding = typeBinding.getSimpleType();
+         assertNotNull("Type " + typeBinding.getQName() + " has simple TypeBinding", simpleTypeBinding);
+         assertEquivalent(xsSimpleType, simpleTypeBinding);
+      }
+      
+      XSParticle xsParticle = xsType.getParticle();
+      if(xsParticle != null)
+      {
+         ParticleBinding particleBinding = typeBinding.getParticle();
+         assertNotNull("Type " + xsQName + " has a ParticleBinding", particleBinding);
+         assertEquivalent(xsParticle, particleBinding);
+      }
+   }
+   
+   public static void assertEquivalent(XSParticle xsParticle, ParticleBinding particleBinding)
+   {
+      assertEquals("ParticleBinding min occurs.", xsParticle.getMinOccurs(), particleBinding.getMinOccurs());
+      
+      if(xsParticle.getMaxOccursUnbounded())
+         assertTrue("ParticleBinding has max occurs unbounded.", particleBinding.getMaxOccursUnbounded());
+      else
+         assertEquals("ParticleBinding max occurs.", xsParticle.getMaxOccurs(), particleBinding.getMaxOccurs());
+      
+      XSTerm xsTerm = xsParticle.getTerm();
+      TermBinding termBinding = particleBinding.getTerm();
+      assertNotNull("Particle binding has a term", termBinding);
+      short xsTermType = xsTerm.getType();
+      if(xsTermType == XSConstants.MODEL_GROUP)
+      {
+         if(!termBinding.isModelGroup())
+         {
+            String groupType = "sequence";
+            short xsModelGroupCompositor = ((XSModelGroup)xsTerm).getCompositor();
+            if(XSModelGroup.COMPOSITOR_CHOICE == xsModelGroupCompositor)
+               groupType = "choice";
+            else if(XSModelGroup.COMPOSITOR_ALL == xsModelGroupCompositor)
+               groupType = "all";
+            fail("TermBinding expected to be a " + groupType + " but was " + termBinding);
+         }
+         assertEquivalent((XSModelGroup) xsTerm, (ModelGroupBinding) termBinding);
+      }
+      else if(xsTermType == XSConstants.ELEMENT_DECLARATION)
+      {
+         XSElementDeclaration xsElement = (XSElementDeclaration) xsTerm;
+         QName xsElementName = new QName(xsElement.getNamespace(), xsElement.getName());
+         assertTrue("TermBinding expected to be an element " + xsElementName + " but was " + termBinding, termBinding.isElement());         
+      }
+      else if(xsTermType == XSConstants.WILDCARD)
+      {
+         assertTrue("TermBinding expected to be a wildcard but was " + termBinding, termBinding.isWildcard());
+         XSWildcard xsWildcard = (XSWildcard) xsTerm;
+         WildcardBinding wildcardBinding = (WildcardBinding) termBinding;
+         assertEquals("WildcardBinding process content.", xsWildcard.getProcessContents(), wildcardBinding.getProcessContents());
+      }
+      else
+         fail("Unexpected XSTerm type: " + xsTermType);
+   }
+   
+   public static void assertEquivalent(XSModelGroup xsModelGroup, ModelGroupBinding modelGroupBinding)
+   {
+      short xsCompositor = xsModelGroup.getCompositor();
+      if(xsCompositor == XSModelGroup.COMPOSITOR_SEQUENCE)
+         assertTrue("ModelGroupBinding expected to be a sequence but was " + modelGroupBinding, modelGroupBinding instanceof SequenceBinding);
+      else if(xsCompositor == XSModelGroup.COMPOSITOR_CHOICE)
+      {
+         System.out.println("choice");
+         assertTrue("ModelGroupBinding expected to be a choice but was " + modelGroupBinding, modelGroupBinding instanceof ChoiceBinding);
+      }
+      else if(xsCompositor == XSModelGroup.COMPOSITOR_ALL)
+      {
+         System.out.println("all");
+         assertTrue("ModelGroupBinding expected to be an all but was " + modelGroupBinding, modelGroupBinding instanceof AllBinding);
+      }
+      else
+         fail("Unexpected compositor type for model group " + xsCompositor);
+      
+      XSObjectList xsParticles = xsModelGroup.getParticles();
+      Collection<ParticleBinding> particleBindings = modelGroupBinding.getParticles();
+      if(xsParticles.getLength() > 0)
+      {
+         assertTrue("ModelGroupBinding has particles.", particleBindings != null);
+         assertEquals("ModelGroupBinding particles total.", xsParticles.getLength(), particleBindings.size());
+      }
+      
+      // TODO fir choice and all the order maybe different
+      Iterator<ParticleBinding> iter = particleBindings.iterator();
+      for(int i = 0; i < xsParticles.getLength(); ++i)
+      {
+         XSParticle xsParticle = (XSParticle) xsParticles.item(i);
+         assertEquivalent(xsParticle, iter.next());
+      }
+   }
+}


Property changes on: projects/metadata/trunk/src/test/java/org/jboss/test/metadata/binding/SchemaBindingValidationUnitTestCase.java
___________________________________________________________________
Name: svn:executable
   + *




More information about the jboss-cvs-commits mailing list