diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java index aa79127a..e5a3a3ff 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/EnumUtil.java @@ -30,7 +30,12 @@ private EnumUtil() { } static > T valueOf(Class enumClass, String name) { - return Enum.valueOf(enumClass, name.toUpperCase(Locale.ENGLISH)); + try { + return Enum.valueOf(enumClass, name.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + throw new XmlSchemaException("Invalid value \"" + name + "\" for " + + enumClass.getSimpleName() + ".", e); + } } } diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java index 66418f23..bbd215fa 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/SchemaBuilder.java @@ -584,7 +584,7 @@ public void validate(XmlSchema pSchema) { if (isEmpty(uri)) { valid = isEmpty(pSchema.getSyntacticalTargetNamespace()); } else { - valid = pSchema.getSyntacticalTargetNamespace().equals(uri); + valid = uri.equals(pSchema.getSyntacticalTargetNamespace()); } if (!valid) { throw new XmlSchemaException("An imported schema was announced to have the namespace " @@ -931,7 +931,7 @@ private QName getRefQName(String pName, NamespaceContext pContext) { } if (uri == null || Constants.NULL_NS_URI.equals(uri)) { - throw new IllegalStateException("The prefix " + prefix + " is not bound."); + throw new XmlSchemaException("The prefix " + prefix + " is not bound."); } localName = pName.substring(offset + 1); } @@ -1355,6 +1355,10 @@ private XmlSchemaComplexContentRestriction handleComplexContentRestriction(XmlSc } if (constraintEl.hasAttribute("refer")) { + if (!(constraint instanceof XmlSchemaKeyref)) { + throw new XmlSchemaException("A \"refer\" attribute is only permitted on xs:keyref," + + " not on xs:" + constraintEl.getLocalName() + "."); + } String name = constraintEl.getAttribute("refer"); ((XmlSchemaKeyref)constraint).refer = getRefQName(name, constraintEl); } diff --git a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java index dfe59fb3..1383ee30 100644 --- a/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java +++ b/xmlschema-core/src/main/java/org/apache/ws/commons/schema/XmlSchemaCollection.java @@ -714,10 +714,10 @@ public String toString() { void addSchema(SchemaKey pKey, XmlSchema pSchema) { if (schemas.containsKey(pKey)) { throw - new IllegalStateException("A schema with target namespace " - + pKey.getNamespace() - + " and system ID " - + pKey.getSystemId() + " is already present."); + new XmlSchemaException("A schema with target namespace " + + pKey.getNamespace() + + " and system ID " + + pKey.getSystemId() + " is already present."); } schemas.put(pKey, pSchema); } diff --git a/xmlschema-core/src/test/java/tests/ExceptionContractTest.java b/xmlschema-core/src/test/java/tests/ExceptionContractTest.java new file mode 100644 index 00000000..5e029f05 --- /dev/null +++ b/xmlschema-core/src/test/java/tests/ExceptionContractTest.java @@ -0,0 +1,82 @@ +/** + * 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. + */ + +package tests; + +import java.io.StringReader; + +import org.apache.ws.commons.schema.XmlSchemaCollection; +import org.apache.ws.commons.schema.XmlSchemaException; + +import org.junit.Assert; +import org.junit.Test; + +public class ExceptionContractTest extends Assert { + + private void assertRejectedWithXmlSchemaException(String schemaBody) { + String schema = + "" + schemaBody + ""; + XmlSchemaCollection collection = new XmlSchemaCollection(); + try { + collection.read(new StringReader(schema)); + fail("The crafted schema should have been rejected: " + schemaBody); + } catch (XmlSchemaException expected) { + // Expected documented failure surface. + } + } + + @Test + public void testUndeclaredPrefixInTypeReference() { + assertRejectedWithXmlSchemaException(""); + } + + @Test + public void testReferOnKeyConstraint() { + assertRejectedWithXmlSchemaException( + "" + + "" + + "" + + ""); + } + + @Test + public void testInvalidFormAttributeValue() { + assertRejectedWithXmlSchemaException( + "" + + "" + + ""); + } + + @Test + public void testInvalidUseAttributeValue() { + assertRejectedWithXmlSchemaException( + "" + + "" + + ""); + } + + @Test + public void testInvalidProcessContentsValue() { + assertRejectedWithXmlSchemaException( + "" + + "" + + ""); + } +} \ No newline at end of file