geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r813877 - in /geronimo/xbean/trunk/xbean-spring/src: main/java/org/apache/xbean/spring/generator/ test/java/org/apache/xbean/spring/generator/ test/resources/org/apache/xbean/spring/generator/
Date Fri, 11 Sep 2009 15:24:42 GMT
Author: chirino
Date: Fri Sep 11 15:24:41 2009
New Revision: 813877

URL: http://svn.apache.org/viewvc?rev=813877&view=rev
Log:
XBEAN-139: XSD generation invalid: List fields declared as sequence of <xs:any namespace='##other'/>

Added:
    geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/
    geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/model-test-xsd-validation.xml
Modified:
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XsdGenerator.java
    geronimo/xbean/trunk/xbean-spring/src/test/java/org/apache/xbean/spring/generator/ModelTest.java

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XsdGenerator.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XsdGenerator.java?rev=813877&r1=813876&r2=813877&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XsdGenerator.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XsdGenerator.java
Fri Sep 11 15:24:41 2009
@@ -192,7 +192,8 @@
         }
         out.println("          <xs:complexType>");
         if (types.isEmpty()) {
-            out.println("            <xs:sequence minOccurs='0' maxOccurs='" + maxOccurs
+ "'><xs:any namespace='##other'/></xs:sequence>");
+            // We don't know the type because it's generic collection.  Allow folks to insert
objets from any namespace
+            out.println("            <xs:sequence minOccurs='0' maxOccurs='" + maxOccurs
+ "'><xs:any minOccurs='0' maxOccurs='unbounded'/></xs:sequence>");
         } else {
             out.println("            <xs:choice minOccurs='0' maxOccurs='" + maxOccurs
+ "'>");
             for (Iterator iterator = types.iterator(); iterator.hasNext();) {

Modified: geronimo/xbean/trunk/xbean-spring/src/test/java/org/apache/xbean/spring/generator/ModelTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/test/java/org/apache/xbean/spring/generator/ModelTest.java?rev=813877&r1=813876&r2=813877&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/test/java/org/apache/xbean/spring/generator/ModelTest.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/test/java/org/apache/xbean/spring/generator/ModelTest.java
Fri Sep 11 15:24:41 2009
@@ -16,20 +16,19 @@
  */
 package org.apache.xbean.spring.generator;
 
-import java.beans.PropertyEditorManager;
-import java.io.File;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicBoolean;
-
 import junit.framework.TestCase;
 import org.apache.xbean.spring.example.BeerService;
+import org.springframework.beans.factory.xml.PluggableSchemaResolver;
+import org.xml.sax.*;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import java.beans.PropertyEditorManager;
+import java.io.*;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * @author Dain Sundstrom
@@ -75,16 +74,16 @@
         final AtomicBoolean gotExpected = new AtomicBoolean(false);
         XsdGenerator generator = new XsdGenerator(null);
         generator.generateSchema(new PrintWriter("dummy") {
+            @Override
+            public void println(String x) {
+                if (x.indexOf("volumeWithPropertyEditor") != -1) {
+                    if (x.indexOf("xs:string") != -1) {
+                        gotExpected.set(true);
+                    }
+                }
+            }
+        }, defaultNamespace);
 
-			@Override
-			public void println(String x) {
-				if (x.indexOf("volumeWithPropertyEditor") != -1) {
-					if (x.indexOf("xs:string") != -1) {
-						gotExpected.set(true);
-					}
-				}
-			}}, defaultNamespace);
-        
         assertTrue("xsd with string got genereated", gotExpected.get());
     }
 
@@ -141,4 +140,73 @@
         PropertyEditorManager.setEditorSearchPath((String[]) editorSearchPath.toArray(new
String[editorSearchPath.size()]));
         assertTrue(Utils.isSimpleType(Type.newSimpleType("java.net.URI")));
     }
+
+    public void testXSDValidation() throws Exception{
+
+        InputStream xmlFile = ModelTest.class.getResourceAsStream("model-test-xsd-validation.xml");
+        File xsd = generateXSD();
+        validate(xmlFile, xsd);
+
+    }
+
+    private File generateXSD() throws IOException {
+        String basedir = System.getProperties().getProperty("basedir", ".");
+        final File targetXSD = new File(basedir, "target/test-data/model-test.xsd");
+        targetXSD.getParentFile().mkdirs();
+        QdoxMappingLoader mappingLoader = new QdoxMappingLoader(DEFAULT_NAMESPACE, new File[]
{ new File(basedir, "/src/test/java")}, null);
+        NamespaceMapping namespaceMapping = getDefaultNamespace(mappingLoader);
+        XsdGenerator generator = new XsdGenerator(targetXSD);
+        generator.setLog(new LogFacade() {
+            public void log(String message) {
+            }
+            public void log(String message, int level) {
+            }
+        });
+        generator.generate(namespaceMapping);
+        return targetXSD;
+    }
+
+    private void validate(InputStream xml, final File xsd) throws ParserConfigurationException,
SAXException, IOException {
+        assertNotNull(xml);
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setNamespaceAware(true);
+        factory.setValidating(true);
+        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
+
+        final AtomicReference<SAXParseException> error = new AtomicReference<SAXParseException>();
+
+        DocumentBuilder builder = factory.newDocumentBuilder();
+
+        builder.setErrorHandler(new ErrorHandler() {
+            public void warning(SAXParseException exception) throws SAXException {
+                error.set(exception);
+            }
+
+            public void error(SAXParseException exception) throws SAXException {
+                error.set(exception);
+            }
+
+            public void fatalError(SAXParseException exception) throws SAXException {
+                error.set(exception);
+            }
+        });
+        builder.setEntityResolver(new EntityResolver() {
+            public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
IOException {
+                PluggableSchemaResolver springResolver = new PluggableSchemaResolver(getClass().getClassLoader());
+                InputSource source = springResolver.resolveEntity(publicId, systemId);
+                if (source == null && "http://xbean.apache.org/test.xsd".equals(systemId))
{
+                    source = new InputSource(new FileInputStream(xsd));
+                    source.setPublicId(publicId);
+                    source.setSystemId(systemId);
+                }
+
+                return source;
+            }
+        });
+        builder.parse(xml);
+        if (error.get() != null) {
+            error.get().printStackTrace();
+            fail("Validateion failed: " + error.get().getMessage());
+        }
+    }
 }

Added: geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/model-test-xsd-validation.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/model-test-xsd-validation.xml?rev=813877&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/model-test-xsd-validation.xml
(added)
+++ geronimo/xbean/trunk/xbean-spring/src/test/resources/org/apache/xbean/spring/generator/model-test-xsd-validation.xml
Fri Sep 11 15:24:41 2009
@@ -0,0 +1,60 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You 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.
+
+-->
+<beans
+    xmlns="http://www.springframework.org/schema/beans"
+    xmlns:test="http://xbean.apache.org/test"
+    xmlns:util="http://www.springframework.org/schema/util"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="
+        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+        http://xbean.apache.org/test http://xbean.apache.org/test.xsd
+    ">
+
+  <!-- All the following bean declarations are equivalent  -->
+  <test:qname-service id="qname-1">
+    <test:list>
+      <bean class="org.apache.xbean.spring.example.PizzaService">
+        <property name="cheese" value="mucho"/>
+      </bean>
+    </test:list>
+  </test:qname-service>
+
+  <test:qname-service id="qname-2">
+    <test:list>
+      <test:pizza cheese="mucho"/>
+    </test:list>
+  </test:qname-service>
+
+  <test:qname-service id="qname-3">
+    <property name="list">
+      <list>
+        <test:pizza cheese="mucho"/>
+      </list>
+    </property>
+  </test:qname-service>
+
+
+  <test:recipe-service id="recipe-service-1">
+    <test:topRecipe>
+      <test:recipe instructions="lots of sugar"/>
+    </test:topRecipe>
+  </test:recipe-service>
+
+</beans>



Mime
View raw message