[jboss-svn-commits] JBoss Common SVN: r3325 - in jbossxb/trunk/src/test: java/org/jboss/test/xb/builder/object/attribute/test and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jul 2 17:55:50 EDT 2009
Author: alex.loubyansky at jboss.com
Date: 2009-07-02 17:55:49 -0400 (Thu, 02 Jul 2009)
New Revision: 3325
Added:
jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/support/DefaultAttribute.java
jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java
jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xml
jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd
Log:
testcase for default attribute value
Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/support/DefaultAttribute.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/support/DefaultAttribute.java (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/support/DefaultAttribute.java 2009-07-02 21:55:49 UTC (rev 3325)
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.object.attribute.support;
+
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
+
+/**
+ * A DefaultAttribute.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+ at XmlRootElement(name="default-attribute", namespace="xb:test:default-attribute")
+public class DefaultAttribute extends SimpleAttribute<Integer>
+{
+ @Override
+ @XmlAttribute
+ public Integer getAttribute()
+ {
+ return super.getAttribute();
+ }
+
+ @Override
+ public void setAttribute(Integer attribute)
+ {
+ super.setAttribute(attribute);
+ }
+}
Added: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java 2009-07-02 21:55:49 UTC (rev 3325)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.object.attribute.test;
+
+
+import org.jboss.test.xb.builder.AbstractBuilderTest;
+import org.jboss.test.xb.builder.object.attribute.support.DefaultAttribute;
+import org.jboss.util.xml.JBossEntityResolver;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
+
+/**
+ * A SchemaDefaultAttributeValueUnitTestCase.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class SchemaDefaultAttributeValueUnitTestCase extends AbstractBuilderTest
+{
+ public SchemaDefaultAttributeValueUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ /**
+ * This test demonstrates unmarshalling with default attribute values
+ *
+ * @throws Exception
+ */
+ public void testMain() throws Exception
+ {
+ UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
+ Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
+
+ // this is to make the SAX parser parse the XSD as well and include default attribute values in the startElement
+ unmarshaller.setSchemaValidation(true);
+
+ // this is EntityResolver that SAX parser (xerces) will use to resolve XSD location for the XML being parsed
+ JBossEntityResolver xmlResolver = new JBossEntityResolver();
+ // Here we map schema location specified in the XML file to the local schema location,
+ // which is a path relative to the resources directory visible in the classpath.
+ // Note, we have to specify here and XML the complete URL including the protocol part into the schemaLocation.
+ // The reason is that if we specify schemaLocation in XML as just a filename,
+ // the SAX parser will compose its schemaLocation by adding the protocol, baseURI (which will depend on the
+ // environment the code is run in) and the schemaLocation we specified in the XML and then
+ // will pass this new schemaLocation to the resolver.
+ xmlResolver.registerLocalEntity("http://www.hostame.org/SchemaDefaultAttributeValue.xsd", "org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd");
+ unmarshaller.setEntityResolver(xmlResolver);
+
+ // this is to resolve namespace to class mapping and build the SchemaBinding from the class
+ MultiClassSchemaResolver schemaBindingResolver = new MultiClassSchemaResolver();
+ schemaBindingResolver.mapURIToClass("xb:test:default-attribute", DefaultAttribute.class);
+ // the reason we configured a separate EntityResolver for unmarshaller above instead of
+ // calling schemaBindingResolver.mapSchemaLocation(nsURI, location) is that
+ // entity resolution inside schemaBindingResolver is used only for nsURI to class resolution (by XsdBinder)
+ // and is not related to SAX parser entity resolution and XML validation.
+
+ String xml = findXML("SchemaDefaultAttributeValue.xml");
+ Object result = unmarshaller.unmarshal(xml, schemaBindingResolver);
+
+ assertNotNull(result);
+ assertTrue(result instanceof DefaultAttribute);
+ DefaultAttribute da = (DefaultAttribute) result;
+ assertEquals(new Integer(123), da.getAttribute());
+ }
+}
Added: jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xml (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xml 2009-07-02 21:55:49 UTC (rev 3325)
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<default-attribute xmlns="xb:test:default-attribute"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="xb:test:default-attribute http://www.hostame.org/SchemaDefaultAttributeValue.xsd"/>
Added: jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd 2009-07-02 21:55:49 UTC (rev 3325)
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsd:schema
+ targetNamespace="xb:test:default-attribute"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ elementFormDefault="qualified"
+ attributeFormDefault="unqualified">
+
+ <xsd:element name="default-attribute">
+ <xsd:complexType>
+ <xsd:attribute name="attribute" type="xsd:int" default="123" />
+ </xsd:complexType>
+ </xsd:element>
+</xsd:schema>
\ No newline at end of file
More information about the jboss-svn-commits
mailing list