[jboss-svn-commits] JBoss Common SVN: r2956 - in jbossxb/trunk/src: test/java/org/jboss/test/xb/builder and 3 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Jan 14 10:49:13 EST 2009


Author: alex.loubyansky at jboss.com
Date: 2009-01-14 10:49:13 -0500 (Wed, 14 Jan 2009)
New Revision: 2956

Added:
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootAnotherNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootDefaultNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootMainNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootDefaultNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootMainNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/YetAnotherRootMainNs.java
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/test/
   jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/test/MultipleRootClassesUnitTestCase.java
Modified:
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBBuilder.java
   jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
Log:
JBXB-169

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBBuilder.java	2009-01-13 11:15:50 UTC (rev 2955)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBBuilder.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -25,6 +25,7 @@
 import java.security.PrivilegedAction;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.Set;
 
 import javax.xml.XMLConstants;
 
@@ -82,10 +83,54 @@
    {
       return new BuilderSchemaBindingInitializer<T>(root);
    }
+
+   /**
+    * Calls build(false, roots).
+    * 
+    * @param roots
+    * @return
+    */
+   public static SchemaBinding build(Class<?>... roots)
+   {
+      return build(false, roots);
+   }
    
    /**
-    * Build from a preparsed schema binding
+    * Builds a schema binding from an array of classes. The classes must be mapped to the same namespace.
+    * SchemaBinding for the first class will be built by calling build(root, rebuildFirst).
+    * For each subsequent class build(SchemaBinding, root) will be called passing in the schema binding
+    * created for the first root class.
     * 
+    * @param rebuildFirst
+    * @param roots
+    * @return
+    */
+   public static SchemaBinding build(boolean rebuildFirst, Class<?>... roots)
+   {
+      if(roots == null || roots.length == 0)
+         throw new IllegalArgumentException("There has to be at least one root class in the arguments.");
+      
+      Class<?> root = roots[0];
+      if(root == null)
+         throw new IllegalArgumentException("Root class can't be null.");
+      SchemaBinding schema = build(root, rebuildFirst);
+      for(int i = 1; i < roots.length; ++i)
+      {
+         root = roots[i];
+         if(root == null)
+            throw new IllegalArgumentException("Root class can't be null.");
+         build(schema, root);
+      }
+      
+      return schema;
+   }
+
+   /**
+    * Build from a preparsed schema binding. The target namespaces of the SchemaBinding and the class must be equal.
+    * Otherwise, an exception will be thrown. Schema properties defined with annotations on the class will be ignored
+    * and won't override the ones in the SchemaBinding instance (except for the prefix mappings which unless a conflict
+    * found will be added to the SchemaBinding and in case of prefix mapping conflict an exception will be thrown).
+    * 
     * @param schemaBinding the schema binding
     * @param root the root
     * @throws IllegalArgumentException for a null schema binding or root
@@ -97,7 +142,51 @@
       if (root == null)
          throw new IllegalArgumentException("Null root");
 
-      // TODO build
+      ClassInfo classInfo = JBossXBBuilder.configuration.getClassInfo(root);
+
+      // add prefix mappings
+      JBossXmlSchema schema = classInfo.getUnderlyingAnnotation(JBossXmlSchema.class);
+      PackageInfo packageInfo = classInfo.getPackage();
+      if (schema == null && packageInfo != null)
+      {
+         schema = packageInfo.getUnderlyingAnnotation(JBossXmlSchema.class);
+      }
+
+      String classNamespace = XMLConstants.NULL_NS_URI;
+      Set<String> schemaNamespaces = schemaBinding.getNamespaces();
+      String schemaNamespace = schemaNamespaces.iterator().next();
+      if(schema != null)
+      {
+         // check the default namespaces are equal
+         if(!JBossXmlConstants.DEFAULT.equals(schema.namespace()))
+            classNamespace = schema.namespace();
+
+         // add prefix mappings
+         if (schema.xmlns().length > 0)
+         {
+            for(int i = 0; i < schema.xmlns().length; ++i)
+            {
+               String prefix = schema.xmlns()[i].prefix();
+               String existingMapping = schemaBinding.getNamespace(prefix);
+               if(existingMapping != null)
+               {
+                  String newMapping = schema.xmlns()[i].namespaceURI();
+                  if(!existingMapping.equals(newMapping))
+                     throw new IllegalStateException("Class " + root.getName() + " maps prefix '" + prefix +
+                           "' to namespace '" + newMapping + "' while in the schema binding it is mapped to '" + existingMapping + "'");
+               }
+               else
+                  schemaBinding.addPrefixMapping(prefix, schema.xmlns()[i].namespaceURI());
+            }
+         }
+      }
+
+      if(!classNamespace.equals(schemaNamespace))
+         throw new IllegalStateException("SchemaBinding namespace '" + schemaNamespace + "' does not match class namespace '" + classNamespace + "'");
+         
+      
+      JBossXBNoSchemaBuilder builder = new JBossXBNoSchemaBuilder(classInfo);
+      builder.build(schemaBinding);
    }
    
    /**
@@ -135,7 +224,7 @@
       }
       return binding;
    }
-   
+
    /**
     * Initialize the schema binding from the root
     * 

Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2009-01-13 11:15:50 UTC (rev 2955)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -193,19 +193,35 @@
     */
    public SchemaBinding build()
    {
-      initSchema();
+      // Initialize the schema
+      schemaBinding = new SchemaBinding();
+      JBossXBBuilder.initSchema(schemaBinding, root);
+      initBuilder();
       createRootElements();
       return schemaBinding;
    }
 
    /**
-    * Initialise the schema
+    * Builds schema binding components from the class and adds them to the SchemaBinding
+    * passed in the argument.
+    * Note, schema initialization step (processing of schema-related class- and package-level annotations) will be skipped.
+    * 
+    * @param schema  SchemaBinding to add the built binding components to
     */
-   protected void initSchema()
+   public void build(SchemaBinding schema)
    {
-      // Initialize the schema
-      schemaBinding = new SchemaBinding();
-      JBossXBBuilder.initSchema(schemaBinding, root);
+      if(schema == null)
+         throw new IllegalArgumentException("Null schema");
+      schemaBinding = schema;
+      initBuilder();
+      createRootElements();
+   }
+
+   /**
+    * Initialise the builder
+    */
+   protected void initBuilder()
+   {
       if (trace)
          log.trace("Building schema for " + root.getName() + " schemaBinding=" + schemaBinding);
 

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootAnotherNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootAnotherNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootAnotherNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A MainRoot.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="another-root")
+ at JBossXmlSchema(namespace="another.namespace", strict=true)
+public class AnotherRootAnotherNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootDefaultNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootDefaultNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootDefaultNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A AnotherRootDefaultNs.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="another-root")
+ at JBossXmlSchema(strict=true)
+public class AnotherRootDefaultNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootMainNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootMainNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/AnotherRootMainNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A MainRoot.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="another-root")
+ at JBossXmlSchema(namespace="main.namespace", strict=true)
+public class AnotherRootMainNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootDefaultNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootDefaultNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootDefaultNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A MainRootDefaultNs.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="main-root")
+ at JBossXmlSchema(strict=false)
+public class MainRootDefaultNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootMainNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootMainNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/MainRootMainNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A MainRoot.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="main-root")
+ at JBossXmlSchema(namespace="main.namespace", strict=false)
+public class MainRootMainNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/YetAnotherRootMainNs.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/YetAnotherRootMainNs.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/support/YetAnotherRootMainNs.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,39 @@
+/*
+ * 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.xb.builder.multiplerootclasses.support;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.jboss.xb.annotations.JBossXmlSchema;
+
+/**
+ * A MainRoot.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="yet-another-root")
+ at JBossXmlSchema(namespace="main.namespace")
+public class YetAnotherRootMainNs
+{
+
+}

Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/test/MultipleRootClassesUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/test/MultipleRootClassesUnitTestCase.java	                        (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/multiplerootclasses/test/MultipleRootClassesUnitTestCase.java	2009-01-14 15:49:13 UTC (rev 2956)
@@ -0,0 +1,255 @@
+/*
+ * 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.xb.builder.multiplerootclasses.test;
+
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.test.xb.builder.AbstractBuilderTest;
+import org.jboss.test.xb.builder.multiplerootclasses.support.AnotherRootAnotherNs;
+import org.jboss.test.xb.builder.multiplerootclasses.support.AnotherRootDefaultNs;
+import org.jboss.test.xb.builder.multiplerootclasses.support.AnotherRootMainNs;
+import org.jboss.test.xb.builder.multiplerootclasses.support.MainRootDefaultNs;
+import org.jboss.test.xb.builder.multiplerootclasses.support.MainRootMainNs;
+import org.jboss.test.xb.builder.multiplerootclasses.support.YetAnotherRootMainNs;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.builder.JBossXBBuilder;
+
+/**
+ * A MultipleRootClassesUnitTestCase.
+ * 
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class MultipleRootClassesUnitTestCase extends AbstractBuilderTest
+{
+   public MultipleRootClassesUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   public void testMainNsAnotherNs() throws Exception
+   {
+      SchemaBinding mainSchema = JBossXBBuilder.build(MainRootMainNs.class, true);
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      Iterator<ElementBinding> elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      ElementBinding e = elements.next();
+      assertEquals(new QName("main.namespace", "main-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      SchemaBinding anotherSchema = JBossXBBuilder.build(AnotherRootAnotherNs.class);
+      assertTrue(anotherSchema.isStrictSchema());
+      assertEquals(1, anotherSchema.getNamespaces().size());
+      elements = anotherSchema.getElements();
+      assertTrue(elements.hasNext());
+      e = elements.next();
+      assertEquals(new QName("another.namespace", "another-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      try
+      {
+         JBossXBBuilder.build(mainSchema, AnotherRootAnotherNs.class);
+         fail("Different namespaces can not be merged.");
+      }
+      catch (IllegalStateException ise)
+      {
+         assertEquals("SchemaBinding namespace 'main.namespace' does not match class namespace 'another.namespace'",
+               ise.getMessage());
+      }
+   }
+
+   public void testDefaultNsAnotherNs() throws Exception
+   {
+      SchemaBinding mainSchema = JBossXBBuilder.build(MainRootDefaultNs.class, true);
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      Iterator<ElementBinding> elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      ElementBinding e = elements.next();
+      assertEquals(new QName("main-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      SchemaBinding anotherSchema = JBossXBBuilder.build(AnotherRootAnotherNs.class);
+      assertTrue(anotherSchema.isStrictSchema());
+      assertEquals(1, anotherSchema.getNamespaces().size());
+      elements = anotherSchema.getElements();
+      assertTrue(elements.hasNext());
+      e = elements.next();
+      assertEquals(new QName("another.namespace", "another-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      try
+      {
+         JBossXBBuilder.build(mainSchema, AnotherRootAnotherNs.class);
+         fail("Different namespaces can not be merged.");
+      }
+      catch (IllegalStateException ise)
+      {
+         assertEquals("SchemaBinding namespace '' does not match class namespace 'another.namespace'",
+               ise.getMessage());
+      }
+   }
+
+   public void testMainNsDefaultNs() throws Exception
+   {
+      SchemaBinding mainSchema = JBossXBBuilder.build(MainRootMainNs.class, true);
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      Iterator<ElementBinding> elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      ElementBinding e = elements.next();
+      assertEquals(new QName("main.namespace", "main-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      SchemaBinding anotherSchema = JBossXBBuilder.build(AnotherRootDefaultNs.class);
+      assertTrue(anotherSchema.isStrictSchema());
+      assertEquals(1, anotherSchema.getNamespaces().size());
+      elements = anotherSchema.getElements();
+      assertTrue(elements.hasNext());
+      e = elements.next();
+      assertEquals(new QName("another-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      try
+      {
+         JBossXBBuilder.build(mainSchema, AnotherRootDefaultNs.class);
+         fail("Different namespaces can not be merged.");
+      }
+      catch (IllegalStateException ise)
+      {
+         assertEquals("SchemaBinding namespace 'main.namespace' does not match class namespace ''",
+               ise.getMessage());
+      }
+   }
+   
+   public void testDefaultNsDefaultNs() throws Exception
+   {
+      SchemaBinding mainSchema = JBossXBBuilder.build(MainRootDefaultNs.class, true);
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      Iterator<ElementBinding> elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      ElementBinding e = elements.next();
+      assertEquals(new QName("main-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      SchemaBinding anotherSchema = JBossXBBuilder.build(AnotherRootDefaultNs.class);
+      assertTrue(anotherSchema.isStrictSchema());
+      assertEquals(1, anotherSchema.getNamespaces().size());
+      elements = anotherSchema.getElements();
+      assertTrue(elements.hasNext());
+      e = elements.next();
+      assertEquals(new QName("another-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      JBossXBBuilder.build(mainSchema, AnotherRootDefaultNs.class);
+
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertFalse(elements.hasNext());
+      e = mainSchema.getElement(new QName("main-root"));
+      assertNotNull(e);
+      e = mainSchema.getElement(new QName("another-root"));
+      assertNotNull("another-root is added to the main schema", e);
+   }
+
+   public void testMainNsMainNs() throws Exception
+   {
+      SchemaBinding mainSchema = JBossXBBuilder.build(MainRootMainNs.class, true);
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      Iterator<ElementBinding> elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      ElementBinding e = elements.next();
+      assertEquals(new QName("main.namespace", "main-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      SchemaBinding anotherSchema = JBossXBBuilder.build(AnotherRootMainNs.class);
+      assertTrue(anotherSchema.isStrictSchema());
+      assertEquals(1, anotherSchema.getNamespaces().size());
+      elements = anotherSchema.getElements();
+      assertTrue(elements.hasNext());
+      e = elements.next();
+      assertEquals(new QName("main.namespace", "another-root"), e.getQName());
+      assertFalse(elements.hasNext());
+
+      JBossXBBuilder.build(mainSchema, AnotherRootMainNs.class);
+
+      assertFalse(mainSchema.isStrictSchema());
+      assertEquals(1, mainSchema.getNamespaces().size());
+      elements = mainSchema.getElements();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertFalse(elements.hasNext());
+      e = mainSchema.getElement(new QName("main.namespace", "main-root"));
+      assertNotNull(e);
+      e = mainSchema.getElement(new QName("main.namespace", "another-root"));
+      assertNotNull("another-root is added to the main schema", e);
+   }
+   
+   public void testBuildArrayMainNsDefaultNs() throws Exception
+   {
+      try
+      {
+         JBossXBBuilder.build(MainRootMainNs.class, YetAnotherRootMainNs.class, AnotherRootDefaultNs.class);
+         fail("Different namespaces can not be merged.");
+      }
+      catch (IllegalStateException ise)
+      {
+         assertEquals("SchemaBinding namespace 'main.namespace' does not match class namespace ''",
+               ise.getMessage());
+      }      
+   }
+
+   public void testBuildArrayMainNs() throws Exception
+   {
+      SchemaBinding schema = JBossXBBuilder.build(MainRootMainNs.class, YetAnotherRootMainNs.class, AnotherRootMainNs.class);
+      
+      assertFalse(schema.isStrictSchema());
+      assertEquals(1, schema.getNamespaces().size());
+      Iterator<ElementBinding> elements = schema.getElements();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertTrue(elements.hasNext());
+      elements.next();
+      assertFalse(elements.hasNext());
+      ElementBinding e = schema.getElement(new QName("main.namespace", "main-root"));
+      assertNotNull(e);
+      e = schema.getElement(new QName("main.namespace", "another-root"));
+      assertNotNull("another-root is added to the main schema", e);
+      e = schema.getElement(new QName("main.namespace", "yet-another-root"));
+      assertNotNull("yet-another-root is added to the main schema", e);
+   }
+}




More information about the jboss-svn-commits mailing list