Polishing

This commit is contained in:
Juergen Hoeller 2018-02-14 17:41:08 +01:00
parent 24adc7d3c6
commit de7ff556d9
3 changed files with 65 additions and 79 deletions

View File

@ -16,26 +16,16 @@
package org.springframework.oxm.xstream; package org.springframework.oxm.xstream;
import com.thoughtworks.xstream.converters.Converter; import java.io.ByteArrayOutputStream;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter; import java.io.Reader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import java.io.StringReader;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver; import java.io.StringWriter;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; import java.io.Writer;
import com.thoughtworks.xstream.io.json.JsonWriter; import java.util.Arrays;
import org.junit.Before; import java.util.Collections;
import org.junit.Ignore; import java.util.HashMap;
import org.junit.Test; import java.util.Map;
import org.mockito.InOrder; import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.util.xml.StaxUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventWriter; import javax.xml.stream.XMLEventWriter;
@ -47,26 +37,30 @@ import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXResult; import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayOutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals; import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import static org.junit.Assert.assertNotNull; import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import static org.junit.Assert.assertThat; import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import static org.junit.Assert.assertTrue; import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import static org.mockito.BDDMockito.eq; import com.thoughtworks.xstream.io.json.JsonWriter;
import static org.mockito.BDDMockito.inOrder; import org.junit.Before;
import static org.mockito.BDDMockito.isA; import org.junit.Ignore;
import static org.mockito.BDDMockito.mock; import org.junit.Test;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo; import org.mockito.InOrder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import org.springframework.util.xml.StaxUtils;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.xmlunit.matchers.CompareMatcher.*;
/** /**
* @author Arjen Poutsma * @author Arjen Poutsma
@ -80,8 +74,9 @@ public class XStreamMarshallerTests {
private Flight flight; private Flight flight;
@Before @Before
public void createMarshaller() throws Exception { public void createMarshaller() {
marshaller = new XStreamMarshaller(); marshaller = new XStreamMarshaller();
Map<String, String> aliases = new HashMap<>(); Map<String, String> aliases = new HashMap<>();
aliases.put("flight", Flight.class.getName()); aliases.put("flight", Flight.class.getName());
@ -90,6 +85,7 @@ public class XStreamMarshallerTests {
flight.setFlightNumber(42L); flight.setFlightNumber(42L);
} }
@Test @Test
public void marshalDOMResult() throws Exception { public void marshalDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
@ -190,7 +186,7 @@ public class XStreamMarshallerTests {
@Test @Test
public void converters() throws Exception { public void converters() throws Exception {
marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()}); marshaller.setConverters(new EncodedByteArrayConverter());
byte[] buf = new byte[]{0x1, 0x2}; byte[] buf = new byte[]{0x1, 0x2};
Writer writer = new StringWriter(); Writer writer = new StringWriter();
marshaller.marshal(buf, new StreamResult(writer)); marshaller.marshal(buf, new StreamResult(writer));
@ -229,8 +225,7 @@ public class XStreamMarshallerTests {
@Test @Test
public void useAttributesForClassStringListMap() throws Exception { public void useAttributesForClassStringListMap() throws Exception {
marshaller marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("flightNumber")));
.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("flightNumber")));
Writer writer = new StringWriter(); Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer)); marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />"; String expected = "<flight flightNumber=\"42\" />";
@ -349,6 +344,7 @@ public class XStreamMarshallerTests {
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected)); assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected));
} }
private static void assertXpathExists(String xPathExpression, String inXMLString){ private static void assertXpathExists(String xPathExpression, String inXMLString){
Source source = Input.fromString(inXMLString).build(); Source source = Input.fromString(inXMLString).build();
Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source); Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);

View File

@ -63,19 +63,19 @@ public class TransactionAttributeSourceEditorTests {
TransactionAttributeSource tas = (TransactionAttributeSource) editor.getValue(); TransactionAttributeSource tas = (TransactionAttributeSource) editor.getValue();
checkTransactionProperties(tas, Object.class.getMethod("hashCode"), checkTransactionProperties(tas, Object.class.getMethod("hashCode"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("equals", Object.class), checkTransactionProperties(tas, Object.class.getMethod("equals", Object.class),
TransactionDefinition.PROPAGATION_MANDATORY); TransactionDefinition.PROPAGATION_MANDATORY);
checkTransactionProperties(tas, Object.class.getMethod("wait"), checkTransactionProperties(tas, Object.class.getMethod("wait"),
TransactionDefinition.PROPAGATION_SUPPORTS); TransactionDefinition.PROPAGATION_SUPPORTS);
checkTransactionProperties(tas, Object.class.getMethod("wait", long.class), checkTransactionProperties(tas, Object.class.getMethod("wait", long.class),
TransactionDefinition.PROPAGATION_SUPPORTS); TransactionDefinition.PROPAGATION_SUPPORTS);
checkTransactionProperties(tas, Object.class.getMethod("wait", long.class, int.class), checkTransactionProperties(tas, Object.class.getMethod("wait", long.class, int.class),
TransactionDefinition.PROPAGATION_SUPPORTS); TransactionDefinition.PROPAGATION_SUPPORTS);
checkTransactionProperties(tas, Object.class.getMethod("notify"), checkTransactionProperties(tas, Object.class.getMethod("notify"),
TransactionDefinition.PROPAGATION_SUPPORTS); TransactionDefinition.PROPAGATION_SUPPORTS);
checkTransactionProperties(tas, Object.class.getMethod("notifyAll"), checkTransactionProperties(tas, Object.class.getMethod("notifyAll"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("toString"), -1); checkTransactionProperties(tas, Object.class.getMethod("toString"), -1);
} }
@ -85,21 +85,21 @@ public class TransactionAttributeSourceEditorTests {
TransactionAttributeSource tas = (TransactionAttributeSource) editor.getValue(); TransactionAttributeSource tas = (TransactionAttributeSource) editor.getValue();
checkTransactionProperties(tas, Object.class.getMethod("hashCode"), checkTransactionProperties(tas, Object.class.getMethod("hashCode"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("equals", Object.class), checkTransactionProperties(tas, Object.class.getMethod("equals", Object.class),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("wait"), checkTransactionProperties(tas, Object.class.getMethod("wait"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("wait", long.class), checkTransactionProperties(tas, Object.class.getMethod("wait", long.class),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("wait", long.class, int.class), checkTransactionProperties(tas, Object.class.getMethod("wait", long.class, int.class),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("notify"), checkTransactionProperties(tas, Object.class.getMethod("notify"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("notifyAll"), checkTransactionProperties(tas, Object.class.getMethod("notifyAll"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
checkTransactionProperties(tas, Object.class.getMethod("toString"), checkTransactionProperties(tas, Object.class.getMethod("toString"),
TransactionDefinition.PROPAGATION_REQUIRED); TransactionDefinition.PROPAGATION_REQUIRED);
} }
private void checkTransactionProperties(TransactionAttributeSource tas, Method method, int propagationBehavior) { private void checkTransactionProperties(TransactionAttributeSource tas, Method method, int propagationBehavior) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2018 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.
@ -40,67 +40,57 @@ public class TransactionAttributeSourceTests {
@Test @Test
public void matchAlwaysTransactionAttributeSource() throws Exception { public void matchAlwaysTransactionAttributeSource() throws Exception {
MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource(); MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource();
TransactionAttribute ta = tas.getTransactionAttribute( TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
Object.class.getMethod("hashCode"), null);
assertNotNull(ta); assertNotNull(ta);
assertTrue(TransactionDefinition.PROPAGATION_REQUIRED == ta.getPropagationBehavior()); assertTrue(TransactionDefinition.PROPAGATION_REQUIRED == ta.getPropagationBehavior());
tas.setTransactionAttribute(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_SUPPORTS)); tas.setTransactionAttribute(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_SUPPORTS));
ta = tas.getTransactionAttribute( ta = tas.getTransactionAttribute(IOException.class.getMethod("getMessage"), IOException.class);
IOException.class.getMethod("getMessage"), IOException.class);
assertNotNull(ta); assertNotNull(ta);
assertTrue(TransactionDefinition.PROPAGATION_SUPPORTS == ta.getPropagationBehavior()); assertTrue(TransactionDefinition.PROPAGATION_SUPPORTS == ta.getPropagationBehavior());
} }
@Test @Test
public void nameMatchTransactionAttributeSourceWithStarAtStartOfMethodName() public void nameMatchTransactionAttributeSourceWithStarAtStartOfMethodName() throws Exception {
throws NoSuchMethodException {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource(); NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties(); Properties attributes = new Properties();
attributes.put("*ashCode", "PROPAGATION_REQUIRED"); attributes.put("*ashCode", "PROPAGATION_REQUIRED");
tas.setProperties(attributes); tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute( TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
Object.class.getMethod("hashCode"), null);
assertNotNull(ta); assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior());
} }
@Test @Test
public void nameMatchTransactionAttributeSourceWithStarAtEndOfMethodName() public void nameMatchTransactionAttributeSourceWithStarAtEndOfMethodName() throws Exception {
throws NoSuchMethodException {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource(); NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties(); Properties attributes = new Properties();
attributes.put("hashCod*", "PROPAGATION_REQUIRED"); attributes.put("hashCod*", "PROPAGATION_REQUIRED");
tas.setProperties(attributes); tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute( TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
Object.class.getMethod("hashCode"), null);
assertNotNull(ta); assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior());
} }
@Test @Test
public void nameMatchTransactionAttributeSourceMostSpecificMethodNameIsDefinitelyMatched() public void nameMatchTransactionAttributeSourceMostSpecificMethodNameIsDefinitelyMatched() throws Exception {
throws NoSuchMethodException {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource(); NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties(); Properties attributes = new Properties();
attributes.put("*", "PROPAGATION_REQUIRED"); attributes.put("*", "PROPAGATION_REQUIRED");
attributes.put("hashCode", "PROPAGATION_MANDATORY"); attributes.put("hashCode", "PROPAGATION_MANDATORY");
tas.setProperties(attributes); tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute( TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
Object.class.getMethod("hashCode"), null);
assertNotNull(ta); assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_MANDATORY, ta.getPropagationBehavior()); assertEquals(TransactionDefinition.PROPAGATION_MANDATORY, ta.getPropagationBehavior());
} }
@Test @Test
public void nameMatchTransactionAttributeSourceWithEmptyMethodName() public void nameMatchTransactionAttributeSourceWithEmptyMethodName() throws Exception {
throws NoSuchMethodException {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource(); NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties(); Properties attributes = new Properties();
attributes.put("", "PROPAGATION_MANDATORY"); attributes.put("", "PROPAGATION_MANDATORY");
tas.setProperties(attributes); tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute( TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
Object.class.getMethod("hashCode"), null);
assertNull(ta); assertNull(ta);
} }