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;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.InOrder;
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 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 javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.stream.StreamResult;
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 static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.inOrder;
import static org.mockito.BDDMockito.isA;
import static org.mockito.BDDMockito.mock;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
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
@ -80,8 +74,9 @@ public class XStreamMarshallerTests {
private Flight flight;
@Before
public void createMarshaller() throws Exception {
public void createMarshaller() {
marshaller = new XStreamMarshaller();
Map<String, String> aliases = new HashMap<>();
aliases.put("flight", Flight.class.getName());
@ -90,6 +85,7 @@ public class XStreamMarshallerTests {
flight.setFlightNumber(42L);
}
@Test
public void marshalDOMResult() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
@ -190,7 +186,7 @@ public class XStreamMarshallerTests {
@Test
public void converters() throws Exception {
marshaller.setConverters(new Converter[]{new EncodedByteArrayConverter()});
marshaller.setConverters(new EncodedByteArrayConverter());
byte[] buf = new byte[]{0x1, 0x2};
Writer writer = new StringWriter();
marshaller.marshal(buf, new StreamResult(writer));
@ -229,8 +225,7 @@ public class XStreamMarshallerTests {
@Test
public void useAttributesForClassStringListMap() throws Exception {
marshaller
.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("flightNumber")));
marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("flightNumber")));
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
@ -349,6 +344,7 @@ public class XStreamMarshallerTests {
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected));
}
private static void assertXpathExists(String xPathExpression, String inXMLString){
Source source = Input.fromString(inXMLString).build();
Iterable<Node> nodes = new JAXPXPathEngine().selectNodes(xPathExpression, source);

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");
* you may not use this file except in compliance with the License.
@ -40,67 +40,57 @@ public class TransactionAttributeSourceTests {
@Test
public void matchAlwaysTransactionAttributeSource() throws Exception {
MatchAlwaysTransactionAttributeSource tas = new MatchAlwaysTransactionAttributeSource();
TransactionAttribute ta = tas.getTransactionAttribute(
Object.class.getMethod("hashCode"), null);
TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
assertNotNull(ta);
assertTrue(TransactionDefinition.PROPAGATION_REQUIRED == ta.getPropagationBehavior());
tas.setTransactionAttribute(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_SUPPORTS));
ta = tas.getTransactionAttribute(
IOException.class.getMethod("getMessage"), IOException.class);
ta = tas.getTransactionAttribute(IOException.class.getMethod("getMessage"), IOException.class);
assertNotNull(ta);
assertTrue(TransactionDefinition.PROPAGATION_SUPPORTS == ta.getPropagationBehavior());
}
@Test
public void nameMatchTransactionAttributeSourceWithStarAtStartOfMethodName()
throws NoSuchMethodException {
public void nameMatchTransactionAttributeSourceWithStarAtStartOfMethodName() throws Exception {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties();
attributes.put("*ashCode", "PROPAGATION_REQUIRED");
tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute(
Object.class.getMethod("hashCode"), null);
TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior());
}
@Test
public void nameMatchTransactionAttributeSourceWithStarAtEndOfMethodName()
throws NoSuchMethodException {
public void nameMatchTransactionAttributeSourceWithStarAtEndOfMethodName() throws Exception {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties();
attributes.put("hashCod*", "PROPAGATION_REQUIRED");
tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute(
Object.class.getMethod("hashCode"), null);
TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_REQUIRED, ta.getPropagationBehavior());
}
@Test
public void nameMatchTransactionAttributeSourceMostSpecificMethodNameIsDefinitelyMatched()
throws NoSuchMethodException {
public void nameMatchTransactionAttributeSourceMostSpecificMethodNameIsDefinitelyMatched() throws Exception {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties();
attributes.put("*", "PROPAGATION_REQUIRED");
attributes.put("hashCode", "PROPAGATION_MANDATORY");
tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute(
Object.class.getMethod("hashCode"), null);
TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
assertNotNull(ta);
assertEquals(TransactionDefinition.PROPAGATION_MANDATORY, ta.getPropagationBehavior());
}
@Test
public void nameMatchTransactionAttributeSourceWithEmptyMethodName()
throws NoSuchMethodException {
public void nameMatchTransactionAttributeSourceWithEmptyMethodName() throws Exception {
NameMatchTransactionAttributeSource tas = new NameMatchTransactionAttributeSource();
Properties attributes = new Properties();
attributes.put("", "PROPAGATION_MANDATORY");
tas.setProperties(attributes);
TransactionAttribute ta = tas.getTransactionAttribute(
Object.class.getMethod("hashCode"), null);
TransactionAttribute ta = tas.getTransactionAttribute(Object.class.getMethod("hashCode"), null);
assertNull(ta);
}