package xml.bug1865; import java.io.InputStream; import java.net.URL; import org.exolab.castor.mapping.Mapping; import org.exolab.castor.xml.MarshalException; import org.exolab.castor.xml.Unmarshaller; import org.xml.sax.InputSource; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; public class TestRequired extends TestCase { public static Test suite() { return new TestSuite(TestRequired.class); } public void testDog1() throws Exception { unmarshalResource(Dog.class, "dog1.xml"); } public void testDog2() throws Exception { unmarshalResource(Dog.class, "dog2.xml"); } public void testKennel1() throws Exception { unmarshalResource(Kennel.class, "kennel1.xml"); } public void testKennel2() throws Exception { unmarshalResource(Kennel.class, "kennel2.xml"); } private void unmarshalResource(Class clazz, String resource) throws Exception { Unmarshaller unmarshaller = new Unmarshaller(clazz); unmarshaller.setIgnoreExtraElements(false); unmarshaller.setIgnoreExtraAttributes(false); unmarshaller.setValidation(true); URL mappingURL = getClass().getResource("mapping.xml"); Mapping mapping = new Mapping(); mapping.loadMapping(mappingURL); unmarshaller.setMapping(mapping); InputStream xmlStream = getClass().getResourceAsStream(resource); InputSource xml = new InputSource(xmlStream); try { unmarshaller.unmarshal(xml); fail("XML is missing required attributes and should fail unmarshalling"); } catch (MarshalException e) { assertTrue(e.getMessage().indexOf("required") != -1); } } }