Browse Source

Polish

pull/31924/head
Stéphane Nicoll 2 years ago
parent
commit
153f8895cb
  1. 21
      spring-oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTests.java
  2. 24
      spring-oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTests.java
  3. 8
      spring-oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTests.java
  4. 4
      spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java
  5. 19
      spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java
  6. 5
      spring-oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java
  7. 23
      spring-oxm/src/test/java/org/springframework/oxm/xstream/XStreamUnmarshallerTests.java

21
spring-oxm/src/test/java/org/springframework/oxm/AbstractMarshallerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.oxm; @@ -18,6 +18,7 @@ package org.springframework.oxm;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -66,7 +67,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -66,7 +67,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
protected abstract Object createFlights();
@Test
public void marshalDOMResult() throws Exception {
void marshalDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
@ -89,7 +90,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -89,7 +90,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalEmptyDOMResult() throws Exception {
void marshalEmptyDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
@ -114,7 +115,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -114,7 +115,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalStreamResultWriter() throws Exception {
void marshalStreamResultWriter() throws Exception {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flights, result);
@ -122,15 +123,15 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -122,15 +123,15 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalStreamResultOutputStream() throws Exception {
void marshalStreamResultOutputStream() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
marshaller.marshal(flights, result);
assertThat(XmlContent.of(new String(os.toByteArray(), "UTF-8"))).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
assertThat(XmlContent.of(os.toString(StandardCharsets.UTF_8))).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
public void marshalStaxResultStreamWriter() throws Exception {
void marshalStaxResultStreamWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter writer = new StringWriter();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
@ -140,7 +141,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -140,7 +141,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalStaxResultEventWriter() throws Exception {
void marshalStaxResultEventWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter writer = new StringWriter();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
@ -150,7 +151,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -150,7 +151,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalJaxp14StaxResultStreamWriter() throws Exception {
void marshalJaxp14StaxResultStreamWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter writer = new StringWriter();
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
@ -160,7 +161,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> { @@ -160,7 +161,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
}
@Test
public void marshalJaxp14StaxResultEventWriter() throws Exception {
void marshalJaxp14StaxResultEventWriter() throws Exception {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
StringWriter writer = new StringWriter();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);

24
spring-oxm/src/test/java/org/springframework/oxm/AbstractUnmarshallerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,6 +18,7 @@ package org.springframework.oxm; @@ -18,6 +18,7 @@ package org.springframework.oxm;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
@ -69,7 +70,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -69,7 +70,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
protected abstract void testFlight(Object o);
@Test
public void unmarshalDomSource() throws Exception {
void unmarshalDomSource() throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.newDocument();
Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
@ -86,21 +87,22 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -86,21 +87,22 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalStreamSourceReader() throws Exception {
void unmarshalStreamSourceReader() throws Exception {
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
@Test
public void unmarshalStreamSourceInputStream() throws Exception {
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
void unmarshalStreamSourceInputStream() throws Exception {
StreamSource source = new StreamSource(new ByteArrayInputStream(
INPUT_STRING.getBytes(StandardCharsets.UTF_8)));
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);
}
@Test
public void unmarshalSAXSource() throws Exception {
void unmarshalSAXSource() throws Exception {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
SAXParser saxParser = saxParserFactory.newSAXParser();
@ -111,7 +113,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -111,7 +113,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
void unmarshalStaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
Source source = StaxUtils.createStaxSource(streamReader);
@ -120,7 +122,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -120,7 +122,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalStaxSourceXmlEventReader() throws Exception {
void unmarshalStaxSourceXmlEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
Source source = StaxUtils.createStaxSource(eventReader);
@ -129,7 +131,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -129,7 +131,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
void unmarshalJaxp14StaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(streamReader);
@ -138,7 +140,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -138,7 +140,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
void unmarshalJaxp14StaxSourceXmlEventReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
StAXSource source = new StAXSource(eventReader);
@ -147,7 +149,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> { @@ -147,7 +149,7 @@ public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
}
@Test
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
protected void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
streamReader.nextTag(); // skip to flights

8
spring-oxm/src/test/java/org/springframework/oxm/config/OxmNamespaceHandlerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,20 +31,20 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -31,20 +31,20 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Jakub Narloch
* @author Sam Brannen
*/
public class OxmNamespaceHandlerTests {
class OxmNamespaceHandlerTests {
private final ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass());
@Test
public void jaxb2ContextPathMarshaller() {
void jaxb2ContextPathMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ContextPathMarshaller", Jaxb2Marshaller.class);
assertThat(jaxb2Marshaller).isNotNull();
}
@Test
public void jaxb2ClassesToBeBoundMarshaller() {
void jaxb2ClassesToBeBoundMarshaller() {
Jaxb2Marshaller jaxb2Marshaller = applicationContext.getBean("jaxb2ClassesMarshaller", Jaxb2Marshaller.class);
assertThat(jaxb2Marshaller).isNotNull();
}

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

@ -148,13 +148,13 @@ class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshaller> { @@ -148,13 +148,13 @@ class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshaller> {
}
@Test
void noContextPathOrClassesToBeBound() throws Exception {
void noContextPathOrClassesToBeBound() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
assertThatIllegalArgumentException().isThrownBy(marshaller::afterPropertiesSet);
}
@Test
void testInvalidContextPath() throws Exception {
void testInvalidContextPath() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("ab");
assertThatExceptionOfType(UncategorizedMappingException.class).isThrownBy(marshaller::afterPropertiesSet);

19
spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java

@ -48,7 +48,7 @@ import static org.mockito.Mockito.mock; @@ -48,7 +48,7 @@ import static org.mockito.Mockito.mock;
* @author Biju Kunjummen
* @author Sam Brannen
*/
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marshaller> {
class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marshaller> {
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
@ -78,7 +78,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh @@ -78,7 +78,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh
}
@Test
public void marshalAttachments() throws Exception {
void marshalAttachments() throws Exception {
unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(BinaryObject.class);
unmarshaller.setMtomEnabled(true);
@ -102,12 +102,11 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh @@ -102,12 +102,11 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh
StringReader reader = new StringReader(content);
Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
boolean condition = result instanceof BinaryObject;
assertThat(condition).as("Result is not a BinaryObject").isTrue();
BinaryObject object = (BinaryObject) result;
assertThat(object.getBytes()).as("bytes property not set").isNotNull();
assertThat(object.getBytes()).as("bytes property not set").isNotEmpty();
assertThat(object.getSwaDataHandler()).as("datahandler property not set").isNotNull();
assertThat(result).isInstanceOfSatisfying(BinaryObject.class, object -> {
assertThat(object.getBytes()).as("bytes property not set").isNotNull();
assertThat(object.getBytes()).as("bytes property not set").isNotEmpty();
assertThat(object.getSwaDataHandler()).as("datahandler property not set").isNotNull();
});
}
@Test
@ -129,7 +128,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh @@ -129,7 +128,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh
public void unmarshalAnXmlReferringToAWrappedXmlElementDecl() throws Exception {
// SPR-10714
unmarshaller = new Jaxb2Marshaller();
unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" });
unmarshaller.setPackagesToScan("org.springframework.oxm.jaxb");
unmarshaller.afterPropertiesSet();
Source source = new StreamSource(new StringReader(
"<brand-airplane><name>test</name></brand-airplane>"));
@ -138,7 +137,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh @@ -138,7 +137,7 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marsh
}
@Test
public void unmarshalFile() throws IOException {
void unmarshalFile() throws IOException {
Resource resource = new ClassPathResource("jaxb2.xml", getClass());
File file = resource.getFile();

5
spring-oxm/src/test/java/org/springframework/oxm/xstream/XStreamMarshallerTests.java

@ -21,6 +21,7 @@ import java.io.Reader; @@ -21,6 +21,7 @@ import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -149,7 +150,7 @@ class XStreamMarshallerTests { @@ -149,7 +150,7 @@ class XStreamMarshallerTests {
ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
marshaller.marshal(flight, result);
String s = os.toString("UTF-8");
String s = os.toString(StandardCharsets.UTF_8);
assertThat(XmlContent.of(s)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@ -206,7 +207,7 @@ class XStreamMarshallerTests { @@ -206,7 +207,7 @@ class XStreamMarshallerTests {
}
@Test
void converters() throws Exception {
void converters() {
marshaller.setConverters(new EncodedByteArrayConverter());
byte[] buf = {0x1, 0x2};

23
spring-oxm/src/test/java/org/springframework/oxm/xstream/XStreamUnmarshallerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -44,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Arjen Poutsma
* @author Juergen Hoeller
*/
public class XStreamUnmarshallerTests {
class XStreamUnmarshallerTests {
protected static final String INPUT_STRING = "<flight><flightNumber>42</flightNumber></flight>";
@ -52,7 +52,7 @@ public class XStreamUnmarshallerTests { @@ -52,7 +52,7 @@ public class XStreamUnmarshallerTests {
@BeforeEach
public void createUnmarshaller() {
void createUnmarshaller() {
unmarshaller = new XStreamMarshaller();
unmarshaller.setTypePermissions(AnyTypePermission.ANY);
Map<String, Class<?>> aliases = new HashMap<>();
@ -62,7 +62,7 @@ public class XStreamUnmarshallerTests { @@ -62,7 +62,7 @@ public class XStreamUnmarshallerTests {
@Test
public void unmarshalDomSource() throws Exception {
void unmarshalDomSource() throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
DOMSource source = new DOMSource(document);
@ -71,7 +71,7 @@ public class XStreamUnmarshallerTests { @@ -71,7 +71,7 @@ public class XStreamUnmarshallerTests {
}
@Test
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
void unmarshalStaxSourceXmlStreamReader() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
Source source = StaxUtils.createStaxSource(streamReader);
@ -80,14 +80,14 @@ public class XStreamUnmarshallerTests { @@ -80,14 +80,14 @@ public class XStreamUnmarshallerTests {
}
@Test
public void unmarshalStreamSourceInputStream() throws Exception {
void unmarshalStreamSourceInputStream() throws Exception {
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes(StandardCharsets.UTF_8)));
Object flights = unmarshaller.unmarshal(source);
testFlight(flights);
}
@Test
public void unmarshalStreamSourceReader() throws Exception {
void unmarshalStreamSourceReader() throws Exception {
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
Object flights = unmarshaller.unmarshal(source);
testFlight(flights);
@ -95,11 +95,10 @@ public class XStreamUnmarshallerTests { @@ -95,11 +95,10 @@ public class XStreamUnmarshallerTests {
private void testFlight(Object o) {
boolean condition = o instanceof Flight;
assertThat(condition).as("Unmarshalled object is not Flights").isTrue();
Flight flight = (Flight) o;
assertThat(flight).as("Flight is null").isNotNull();
assertThat(flight.getFlightNumber()).as("Number is invalid").isEqualTo(42L);
assertThat(o).isInstanceOfSatisfying(Flight.class, flight -> {
assertThat(flight).as("Flight is null").isNotNull();
assertThat(flight.getFlightNumber()).as("Number is invalid").isEqualTo(42L);
});
}
}

Loading…
Cancel
Save