You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by kk...@apache.org on 2004/11/03 04:48:11 UTC
cvs commit: xml-xmlbeans/v2/test/src/xmlobject/schematypes/detailed ListAndUnionTests.java
kkrouse 2004/11/02 19:48:11
Modified: v2/src/typeimpl/org/apache/xmlbeans/impl/schema
StscJavaizer.java
v2/test/cases/xbean/xmlobject listandunion.xsd
v2/test/src/xmlobject/schematypes/detailed
ListAndUnionTests.java
Log:
fix for NPE in SchemaTypeCodePrinter for union of string enum types.
Revision Changes Path
1.7 +18 -0 xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/StscJavaizer.java
Index: StscJavaizer.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/StscJavaizer.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- StscJavaizer.java 5 May 2004 00:45:38 -0000 1.6
+++ StscJavaizer.java 3 Nov 2004 03:48:11 -0000 1.7
@@ -16,6 +16,7 @@
package org.apache.xmlbeans.impl.schema;
import org.apache.xmlbeans.impl.common.NameUtil;
+import org.apache.xmlbeans.impl.common.QNameHelper;
import org.apache.xmlbeans.impl.config.ExtensionHolder;
import org.apache.xmlbeans.impl.config.InterfaceExtension;
import org.apache.xmlbeans.QNameSetBuilder;
@@ -578,12 +579,16 @@
if (!sType.isSimpleType())
return SchemaProperty.XML_OBJECT;
+ SchemaType unionType = null;
if (sType.getSimpleVariety() == SchemaType.UNION)
{
// see if we can find an interesting common base type, e.g., for string enums
SchemaType baseType = sType.getUnionCommonBaseType();
if (baseType != null && !baseType.isURType())
+ {
+ unionType = sType;
sType = baseType;
+ }
else
return javaTypeCodeInCommon(sType.getUnionConstituentTypes());
}
@@ -644,7 +649,20 @@
case SchemaType.BTC_STRING:
if (isStringType(sType.getBaseEnumType()))
+ {
+ if (unionType != null)
+ {
+ // REVISIT: enum of union of string enum case
+ // set base enum type of unionType if it exists
+ // but only if unionType doesn't already have a base enum type.
+ // if the unionType does have a base enum type, treat as JAVA_STRING
+ if (unionType.getBaseEnumType() == null)
+ ((SchemaTypeImpl)unionType).setBaseEnumTypeRef(sType.getRef());
+ else
+ return SchemaProperty.JAVA_STRING;
+ }
return SchemaProperty.JAVA_ENUM;
+ }
return SchemaProperty.JAVA_STRING;
case SchemaType.BTC_DURATION:
1.2 +73 -0 xml-xmlbeans/v2/test/cases/xbean/xmlobject/listandunion.xsd
Index: listandunion.xsd
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/cases/xbean/xmlobject/listandunion.xsd,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- listandunion.xsd 8 Jun 2004 19:37:59 -0000 1.1
+++ listandunion.xsd 3 Nov 2004 03:48:11 -0000 1.2
@@ -65,4 +65,77 @@
</xs:complexType>
</xs:element>
+
+ <!-- get/set java type should be java.lang.String -->
+ <xs:element name="union-of-string">
+ <xs:simpleType>
+ <xs:union memberTypes="xs:string"/>
+ </xs:simpleType>
+ </xs:element>
+
+ <!-- get/set java type should be java.lang.Object -->
+ <xs:element name="union-of-date-and-string">
+ <xs:simpleType>
+ <xs:union memberTypes="xs:date xs:string"/>
+ </xs:simpleType>
+ </xs:element>
+
+ <!-- get/set java type should be java.util.Calendar -->
+ <xs:element name="union-of-time-and-date">
+ <xs:simpleType>
+ <xs:union memberTypes="xs:time xs:date"/>
+ </xs:simpleType>
+ </xs:element>
+
+ <!-- get/set java type should be org.openuri.lut.StringEnum.Enum -->
+ <xs:element name="union-of-string-enum" type="tns:union-of-string-enum"/>
+ <xs:simpleType name="union-of-string-enum">
+ <xs:union memberTypes="tns:string-enum"/>
+ </xs:simpleType>
+
+ <xs:simpleType name="string-enum">
+ <xs:restriction base="xs:string">
+ <xs:enumeration value="ABC"/>
+ <xs:enumeration value="DEF"/>
+ <xs:enumeration value="123"/>
+ <xs:enumeration value="456"/>
+ </xs:restriction>
+ </xs:simpleType>
+
+ <!-- get/set java type should be org.openuri.lut.StringEnum.Enum -->
+ <xs:element name="union-of-string-enum1-and-string-enum2">
+ <xs:simpleType>
+ <xs:union memberTypes="tns:string-enum-derived1 tns:string-enum-derived2"/>
+ </xs:simpleType>
+ </xs:element>
+
+
+ <xs:simpleType name="string-enum-derived1">
+ <xs:restriction base="tns:string-enum">
+ <xs:enumeration value="ABC"/>
+ <xs:enumeration value="DEF"/>
+ <xs:enumeration value="123"/>
+ </xs:restriction>
+ </xs:simpleType>
+
+ <xs:simpleType name="string-enum-derived2">
+ <xs:restriction base="tns:string-enum">
+ <xs:enumeration value="ABC"/>
+ <xs:enumeration value="DEF"/>
+ <xs:enumeration value="456"/>
+ </xs:restriction>
+ </xs:simpleType>
+
+ <!-- get/set java type should be java.lang.String ?? -->
+ <!--
+ <xs:element name="enum-of-union-of-string-enum">
+ <xs:simpleType>
+ <xs:restriction base="tns:union-of-string-enum">
+ <xs:enumeration value="ABC"/> <!- - only allowed value? - ->
+ <xs:enumeration value="XYZ"/> <!- - should never be a legal value? - ->
+ </xs:restriction>
+ </xs:simpleType>
+ </xs:element>
+ -->
+
</xs:schema>
1.5 +231 -0 xml-xmlbeans/v2/test/src/xmlobject/schematypes/detailed/ListAndUnionTests.java
Index: ListAndUnionTests.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/test/src/xmlobject/schematypes/detailed/ListAndUnionTests.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ListAndUnionTests.java 5 Oct 2004 20:43:31 -0000 1.4
+++ ListAndUnionTests.java 3 Nov 2004 03:48:11 -0000 1.5
@@ -22,6 +22,13 @@
import org.openuri.lut.UnionsDocument;
import org.openuri.lut.IncidentReportsDocument;
import org.openuri.lut.DateOrDateTime;
+import org.openuri.lut.UnionOfStringDocument;
+import org.openuri.lut.UnionOfStringEnumDocument;
+import org.openuri.lut.UnionOfDateAndStringDocument;
+import org.openuri.lut.UnionOfTimeAndDateDocument;
+import org.openuri.lut.UnionOfStringEnum;
+import org.openuri.lut.StringEnum;
+import org.openuri.lut.EnumOfUnionOfStringEnumDocument;
import java.util.List;
import java.util.Arrays;
@@ -35,6 +42,11 @@
import org.apache.xmlbeans.XmlDate;
import org.apache.xmlbeans.XmlDateTime;
import org.apache.xmlbeans.XmlCalendar;
+import org.apache.xmlbeans.XmlString;
+import org.apache.xmlbeans.SchemaProperty;
+import org.apache.xmlbeans.XmlAnySimpleType;
+import org.apache.xmlbeans.XmlTime;
+import org.apache.xmlbeans.impl.values.XmlObjectBase;
public class ListAndUnionTests extends TestCase
{
@@ -181,4 +193,223 @@
}
+ /**
+ * Testing a union of xs:string
+ */
+ public void testUnionOfString() throws Exception
+ {
+ // schema object model for union-of-string
+ SchemaType uniontype = UnionOfStringDocument.UnionOfString.type;
+ Assert.assertEquals(SchemaType.UNION, uniontype.getSimpleVariety());
+
+ SchemaType commonbase = uniontype.getUnionCommonBaseType();
+ Assert.assertEquals(XmlString.type, commonbase);
+
+ SchemaType[] constituents = uniontype.getUnionConstituentTypes();
+ Assert.assertEquals(1, constituents.length);
+ Assert.assertEquals(XmlString.type, constituents[0]);
+
+ SchemaProperty[] props = UnionOfStringDocument.type.getProperties();
+ Assert.assertEquals(1, props.length);
+ Assert.assertEquals(SchemaProperty.JAVA_STRING, props[0].getJavaTypeCode());
+
+ // create a document
+ UnionOfStringDocument doc = UnionOfStringDocument.Factory.parse(
+ "<lut:union-of-string xmlns:lut='http://openuri.org/lut'>kevin</lut:union-of-string>");
+ String value = doc.getUnionOfString();
+ Assert.assertEquals("kevin", value);
+ Assert.assertTrue(doc.validate());
+
+ // set value
+ doc.setUnionOfString("bob");
+ UnionOfStringDocument.UnionOfString union = doc.xgetUnionOfString();
+ Assert.assertEquals("bob", (String)union.getObjectValue());
+
+ // round trip to s text
+ String xtext = doc.xmlText();
+ UnionOfStringDocument docrt = UnionOfStringDocument.Factory.parse(xtext);
+ Assert.assertTrue(docrt.validate());
+
+ // verify contents
+ Assert.assertEquals("bob", docrt.getUnionOfString());
+ }
+
+ /**
+ * Testing a union of xs:date and xs:string
+ */
+ public void testUnionOfDateAndString() throws Exception
+ {
+ // schema object model for union-of-date-and-string
+ SchemaType uniontype = UnionOfDateAndStringDocument.UnionOfDateAndString.type;
+ Assert.assertEquals(SchemaType.UNION, uniontype.getSimpleVariety());
+
+ SchemaType commonbase = uniontype.getUnionCommonBaseType();
+ Assert.assertEquals(XmlAnySimpleType.type, commonbase);
+
+ SchemaType[] constituents = uniontype.getUnionConstituentTypes();
+ Assert.assertEquals(2, constituents.length);
+ Assert.assertEquals(XmlDate.type, constituents[0]);
+ Assert.assertEquals(XmlString.type, constituents[1]);
+
+ SchemaProperty[] props = UnionOfDateAndStringDocument.type.getProperties();
+ Assert.assertEquals(1, props.length);
+ Assert.assertEquals(SchemaProperty.JAVA_OBJECT, props[0].getJavaTypeCode());
+
+ // create a document
+ UnionOfDateAndStringDocument doc = UnionOfDateAndStringDocument.Factory.parse(
+ "<lut:union-of-date-and-string xmlns:lut='http://openuri.org/lut'>2001-08-06</lut:union-of-date-and-string>");
+ Calendar value = (Calendar)doc.getUnionOfDateAndString();
+ Assert.assertEquals(new XmlCalendar("2001-08-06"), value);
+ Assert.assertTrue(doc.validate());
+
+ // set value
+ doc.setUnionOfDateAndString("bob");
+ UnionOfDateAndStringDocument.UnionOfDateAndString union = doc.xgetUnionOfDateAndString();
+ Assert.assertEquals("bob", (String)union.getObjectValue());
+
+ // round trip
+ String xtext = doc.xmlText();
+ UnionOfDateAndStringDocument docrt = UnionOfDateAndStringDocument.Factory.parse(xtext);
+ Assert.assertTrue(docrt.validate());
+
+ // verify contents
+ Assert.assertEquals("bob", docrt.getUnionOfDateAndString());
+
+ // set value in original document and round trip again
+ doc.setUnionOfDateAndString(new XmlCalendar("2004-08-06"));
+ docrt = UnionOfDateAndStringDocument.Factory.parse(doc.xmlText());
+ Assert.assertTrue(docrt.validate());
+ Assert.assertEquals(new XmlCalendar("2004-08-06"), docrt.getUnionOfDateAndString());
+
+ }
+
+ /**
+ * Testing a union of xs:time and xs:date
+ */
+ public void testUnionOfTimeAndDate() throws Exception
+ {
+ // schema object model for union-of-time-and-date
+ SchemaType uniontype = UnionOfTimeAndDateDocument.UnionOfTimeAndDate.type;
+ Assert.assertEquals(SchemaType.UNION, uniontype.getSimpleVariety());
+
+ SchemaType commonbase = uniontype.getUnionCommonBaseType();
+ Assert.assertEquals(XmlAnySimpleType.type, commonbase);
+
+ SchemaType[] constituents = uniontype.getUnionConstituentTypes();
+ Assert.assertEquals(2, constituents.length);
+ Assert.assertEquals(XmlTime.type, constituents[0]);
+ Assert.assertEquals(XmlDate.type, constituents[1]);
+
+ SchemaProperty[] props = UnionOfTimeAndDateDocument.type.getProperties();
+ Assert.assertEquals(1, props.length);
+ Assert.assertEquals(SchemaProperty.JAVA_CALENDAR, props[0].getJavaTypeCode());
+
+ // create a document
+ UnionOfTimeAndDateDocument doc = UnionOfTimeAndDateDocument.Factory.parse(
+ "<lut:union-of-time-and-date xmlns:lut='http://openuri.org/lut'>2001-08-06</lut:union-of-time-and-date>");
+ Calendar value = doc.getUnionOfTimeAndDate();
+ Assert.assertEquals(new XmlCalendar("2001-08-06"), value);
+ Assert.assertTrue(doc.validate());
+
+ // set value
+ doc.setUnionOfTimeAndDate(new XmlCalendar("11:30:03"));
+ UnionOfTimeAndDateDocument.UnionOfTimeAndDate union = doc.xgetUnionOfTimeAndDate();
+ Assert.assertEquals(new XmlCalendar("11:30:03"), union.getObjectValue());
+
+ // round trip
+ String xtext = doc.xmlText();
+ UnionOfTimeAndDateDocument docrt = UnionOfTimeAndDateDocument.Factory.parse(xtext);
+ Assert.assertTrue(docrt.validate());
+
+ // verify contents
+ Assert.assertEquals(new XmlCalendar("11:30:03"), docrt.getUnionOfTimeAndDate());
+ }
+
+
+ /**
+ * Testing a union of enumerated xs:string
+ */
+ public void testUnionOfStringEnum() throws Exception
+ {
+ // schema object model for union-of-string-enum
+ SchemaType uniontype = UnionOfStringEnum.type;
+ Assert.assertEquals(SchemaType.UNION, uniontype.getSimpleVariety());
+
+ SchemaType commonbase = uniontype.getUnionCommonBaseType();
+ Assert.assertEquals(StringEnum.type, commonbase);
+
+ SchemaType[] constituents = uniontype.getUnionConstituentTypes();
+ Assert.assertEquals(1, constituents.length);
+ Assert.assertEquals(StringEnum.type, constituents[0]);
+
+ SchemaProperty[] props = UnionOfStringEnumDocument.type.getProperties();
+ Assert.assertEquals(1, props.length);
+ Assert.assertEquals(SchemaProperty.JAVA_ENUM, props[0].getJavaTypeCode());
+
+ // create a document
+ UnionOfStringEnumDocument doc = UnionOfStringEnumDocument.Factory.parse(
+ "<lut:union-of-string-enum xmlns:lut='http://openuri.org/lut'>ABC</lut:union-of-string-enum>");
+ StringEnum.Enum value = doc.getUnionOfStringEnum();
+ Assert.assertEquals(StringEnum.ABC, value);
+ Assert.assertTrue(doc.validate());
+
+ // set value
+ doc.setUnionOfStringEnum(StringEnum.X_123);
+ UnionOfStringEnum union = doc.xgetUnionOfStringEnum();
+ Assert.assertEquals(StringEnum.Enum.forString("123"), ((SimpleValue)union).getEnumValue());
+ Assert.assertEquals(StringEnum.X_123, ((SimpleValue)union).getEnumValue());
+ Assert.assertEquals("123", union.getObjectValue());
+
+ // round trip
+ String xtext = doc.xmlText();
+ UnionOfStringEnumDocument docrt = UnionOfStringEnumDocument.Factory.parse(xtext);
+ Assert.assertTrue(docrt.validate());
+
+ // verify contents
+ Assert.assertEquals(StringEnum.X_123, docrt.getUnionOfStringEnum());
+ }
+
+ /**
+ * Testing a enumerated union of enumerated xs:string
+ public void testEnumOfUnionOfStringEnum() throws Exception
+ {
+ // schema object model for union-of-string-enum
+ SchemaType uniontype = EnumOfUnionOfStringEnumDocument.EnumOfUnionOfStringEnum.type;
+ Assert.assertEquals(SchemaType.UNION, uniontype.getSimpleVariety());
+
+ SchemaType commonbase = uniontype.getUnionCommonBaseType();
+ Assert.assertEquals(StringEnum.type, commonbase);
+
+ SchemaType[] constituents = uniontype.getUnionConstituentTypes();
+ Assert.assertEquals(1, constituents.length);
+ Assert.assertEquals(StringEnum.type, constituents[0]);
+
+ SchemaProperty[] props = UnionOfStringEnumDocument.type.getProperties();
+ Assert.assertEquals(1, props.length);
+ Assert.assertEquals(SchemaProperty.JAVA_ENUM, props[0].getJavaTypeCode());
+
+ // create a document
+ EnumOfUnionOfStringEnumDocument doc = EnumOfUnionOfStringEnumDocument.Factory.parse(
+ "<lut:enum-of-union-of-string-enum xmlns:lut='http://openuri.org/lut'>ABC</lut:enum-of-union-of-string-enum>");
+ String value = doc.getEnumOfUnionOfStringEnum();
+ Assert.assertEquals("ABC", value);
+ Assert.assertTrue(doc.validate());
+
+ // set value
+ doc.setEnumOfUnionOfStringEnum("XYZ");
+ EnumOfUnionOfStringEnumDocument.EnumOfUnionOfStringEnum union = doc.xgetEnumOfUnionOfStringEnum();
+ Assert.assertEquals("XYZ", ((SimpleValue)union).getEnumValue());
+ Assert.assertEquals("XYZ", union.getObjectValue());
+ // XYZ is not a valid enumeration of the base union-of-string-enum type
+ Assert.assertFalse(doc.validate());
+
+ // round trip
+ String xtext = doc.xmlText();
+ EnumOfUnionOfStringEnumDocument docrt = EnumOfUnionOfStringEnumDocument.Factory.parse(xtext);
+ Assert.assertTrue(docrt.validate());
+
+ // verify contents
+ Assert.assertEquals("ABC", docrt.getEnumOfUnionOfStringEnum());
+ }
+ */
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org