Browse Source

Polish Jaxb2MarshallerTests

pull/26893/head
Sam Brannen 5 years ago
parent
commit
a28511cdbd
  1. 80
      spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

80
spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -78,7 +78,7 @@ import static org.xmlunit.diff.DifferenceEvaluators.downgradeDifferencesToEqual;
* @author Biju Kunjummen * @author Biju Kunjummen
* @author Sam Brannen * @author Sam Brannen
*/ */
public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshaller> { class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshaller> {
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test"; private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
@ -104,7 +104,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
@Test @Test
public void marshalSAXResult() throws Exception { void marshalSAXResult() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class); ContentHandler contentHandler = mock(ContentHandler.class);
SAXResult result = new SAXResult(contentHandler); SAXResult result = new SAXResult(contentHandler);
marshaller.marshal(flights, result); marshaller.marshal(flights, result);
@ -124,7 +124,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test @Test
public void lazyInit() throws Exception { void lazyInit() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(CONTEXT_PATH); marshaller.setContextPath(CONTEXT_PATH);
marshaller.setLazyInit(true); marshaller.setLazyInit(true);
@ -137,48 +137,44 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test @Test
public void properties() throws Exception { void properties() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(CONTEXT_PATH); marshaller.setContextPath(CONTEXT_PATH);
marshaller.setMarshallerProperties( marshaller.setMarshallerProperties(
Collections.<String, Object>singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
Boolean.TRUE));
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
} }
@Test @Test
public void noContextPathOrClassesToBeBound() throws Exception { void noContextPathOrClassesToBeBound() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
assertThatIllegalArgumentException().isThrownBy( assertThatIllegalArgumentException().isThrownBy(marshaller::afterPropertiesSet);
marshaller::afterPropertiesSet);
} }
@Test @Test
public void testInvalidContextPath() throws Exception { void testInvalidContextPath() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("ab"); marshaller.setContextPath("ab");
assertThatExceptionOfType(UncategorizedMappingException.class).isThrownBy( assertThatExceptionOfType(UncategorizedMappingException.class).isThrownBy(marshaller::afterPropertiesSet);
marshaller::afterPropertiesSet);
} }
@Test @Test
public void marshalInvalidClass() throws Exception { void marshalInvalidClass() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(FlightType.class); marshaller.setClassesToBeBound(FlightType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
Result result = new StreamResult(new StringWriter()); Result result = new StreamResult(new StringWriter());
Flights flights = new Flights(); Flights flights = new Flights();
assertThatExceptionOfType(XmlMappingException.class).isThrownBy(() -> assertThatExceptionOfType(XmlMappingException.class).isThrownBy(() -> marshaller.marshal(flights, result));
marshaller.marshal(flights, result));
} }
@Test @Test
public void supportsContextPath() throws Exception { void supportsContextPath() throws Exception {
testSupports(); testSupports();
} }
@Test @Test
public void supportsClassesToBeBound() throws Exception { void supportsClassesToBeBound() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Flights.class, FlightType.class); marshaller.setClassesToBeBound(Flights.class, FlightType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
@ -186,7 +182,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test @Test
public void supportsPackagesToScan() throws Exception { void supportsPackagesToScan() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan(CONTEXT_PATH); marshaller.setPackagesToScan(CONTEXT_PATH);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
@ -224,11 +220,11 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
private void testSupportsPrimitives() { private void testSupportsPrimitives() {
final Primitives primitives = new Primitives(); final Primitives primitives = new Primitives();
ReflectionUtils.doWithMethods(Primitives.class, new ReflectionUtils.MethodCallback() { ReflectionUtils.doWithMethods(Primitives.class, method -> {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Type returnType = method.getGenericReturnType(); Type returnType = method.getGenericReturnType();
assertThat(marshaller.supports(returnType)).as("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">").isTrue(); assertThat(marshaller.supports(returnType))
.as("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">")
.isTrue();
try { try {
// make sure the marshalling does not result in errors // make sure the marshalling does not result in errors
Object returnValue = method.invoke(primitives); Object returnValue = method.invoke(primitives);
@ -237,22 +233,18 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
throw new AssertionError(e.getMessage(), e); throw new AssertionError(e.getMessage(), e);
} }
} },
}, new ReflectionUtils.MethodFilter() { method -> method.getName().startsWith("primitive")
@Override );
public boolean matches(Method method) {
return method.getName().startsWith("primitive");
}
});
} }
private void testSupportsStandardClasses() throws Exception { private void testSupportsStandardClasses() throws Exception {
final StandardClasses standardClasses = new StandardClasses(); final StandardClasses standardClasses = new StandardClasses();
ReflectionUtils.doWithMethods(StandardClasses.class, new ReflectionUtils.MethodCallback() { ReflectionUtils.doWithMethods(StandardClasses.class, method -> {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
Type returnType = method.getGenericReturnType(); Type returnType = method.getGenericReturnType();
assertThat(marshaller.supports(returnType)).as("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">").isTrue(); assertThat(marshaller.supports(returnType))
.as("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">")
.isTrue();
try { try {
// make sure the marshalling does not result in errors // make sure the marshalling does not result in errors
Object returnValue = method.invoke(standardClasses); Object returnValue = method.invoke(standardClasses);
@ -261,17 +253,13 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
catch (InvocationTargetException e) { catch (InvocationTargetException e) {
throw new AssertionError(e.getMessage(), e); throw new AssertionError(e.getMessage(), e);
} }
} },
}, new ReflectionUtils.MethodFilter() { method -> method.getName().startsWith("standardClass")
@Override );
public boolean matches(Method method) {
return method.getName().startsWith("standardClass");
}
});
} }
@Test @Test
public void supportsXmlRootElement() throws Exception { void supportsXmlRootElement() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class); marshaller.setClassesToBeBound(DummyRootElement.class, DummyType.class);
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
@ -284,7 +272,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
@Test @Test
public void marshalAttachments() throws Exception { void marshalAttachments() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(BinaryObject.class); marshaller.setClassesToBeBound(BinaryObject.class);
marshaller.setMtomEnabled(true); marshaller.setMtomEnabled(true);
@ -304,7 +292,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test // SPR-10714 @Test // SPR-10714
public void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception { void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception {
marshaller = new Jaxb2Marshaller(); marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("org.springframework.oxm.jaxb"); marshaller.setPackagesToScan("org.springframework.oxm.jaxb");
marshaller.afterPropertiesSet(); marshaller.afterPropertiesSet();
@ -318,7 +306,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test // SPR-10806 @Test // SPR-10806
public void unmarshalStreamSourceWithXmlOptions() throws Exception { void unmarshalStreamSourceWithXmlOptions() throws Exception {
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override @Override
@ -352,7 +340,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
} }
@Test // SPR-10806 @Test // SPR-10806
public void unmarshalSaxSourceWithXmlOptions() throws Exception { void unmarshalSaxSourceWithXmlOptions() throws Exception {
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override @Override

Loading…
Cancel
Save