From 7eb6db5c837db22055b51988031920599a7e7270 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Fri, 28 Jun 2019 09:20:02 +0200 Subject: Refactor: Deduplicate Code by Simplifying MarshallerConfig - Move JaxbM initialization into Marshaller such that it can be used in tests without Spring's Application Context. - Remove SpringRunner from Mzs2MsgConverter Test, which makes the test run faster. --- .../at/gv/egiz/moazs/config/MarshallerConfig.java | 47 +--------------------- .../java/at/gv/egiz/moazs/scheme/Marshaller.java | 15 ++++++- .../at/gv/egiz/moazs/Mzs2MsgConverterTest.java | 18 ++++----- 3 files changed, 22 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java b/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java index bde33c1..38f4c1a 100644 --- a/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java +++ b/src/main/java/at/gv/egiz/moazs/config/MarshallerConfig.java @@ -12,57 +12,14 @@ import java.util.HashMap; @Configuration public class MarshallerConfig { - @Bean - public NamespacePrefixMapper msgMapper() { - return new MoaZSPrefixMapper(false); - } - - @Bean - public NamespacePrefixMapper mzsMapper() { - return new MoaZSPrefixMapper(true); - } - - @Bean - public Jaxb2Marshaller mzsJaxbMarshaller() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - - marshaller.setClassesToBeBound( - at.gv.zustellung.app2mzs.xsd.ObjectFactory.class, - at.gv.zustellung.app2mzs.xsd.persondata.ObjectFactory.class, - at.gv.zustellung.msg.xsd.ObjectFactory.class); - - var map = new HashMap(); - map.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); - map.put("com.sun.xml.bind.namespacePrefixMapper", mzsMapper()); - marshaller.setMarshallerProperties(map); - - return marshaller; - } - - @Bean - public Jaxb2Marshaller msgJaxbMarshaller() { - Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); - - marshaller.setClassesToBeBound( - at.gv.zustellung.msg.xsd.ObjectFactory.class, - at.gv.zustellung.msg.xsd.persondata.ObjectFactory.class); - - var map = new HashMap(); - map.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); - map.put("com.sun.xml.bind.namespacePrefixMapper", msgMapper()); - marshaller.setMarshallerProperties(map); - - return marshaller; - } - @Bean public Marshaller mzsMarshaller() { - return new Marshaller(mzsJaxbMarshaller()); + return new Marshaller(true); } @Bean public Marshaller msgMarshaller() { - return new Marshaller(msgJaxbMarshaller()); + return new Marshaller(false); } diff --git a/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java index 3c82419..83ace5c 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/Marshaller.java @@ -7,6 +7,7 @@ import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.InputStream; import java.io.StringWriter; +import java.util.HashMap; /** * @author xerx593 @@ -16,8 +17,18 @@ public class Marshaller { private final Jaxb2Marshaller jaxbMarshaller; - public Marshaller(Jaxb2Marshaller marshaller) { - this.jaxbMarshaller = marshaller; + public Marshaller(boolean isMzs) { + this.jaxbMarshaller = new Jaxb2Marshaller(); + + jaxbMarshaller.setClassesToBeBound( + at.gv.zustellung.app2mzs.xsd.ObjectFactory.class, + at.gv.zustellung.app2mzs.xsd.persondata.ObjectFactory.class, + at.gv.zustellung.msg.xsd.ObjectFactory.class); + + var map = new HashMap(); + map.put(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, true); + map.put("com.sun.xml.bind.namespacePrefixMapper", new MoaZSPrefixMapper(isMzs)); + jaxbMarshaller.setMarshallerProperties(map); } public String marshallXml(final T obj) { diff --git a/src/test/java/at/gv/egiz/moazs/Mzs2MsgConverterTest.java b/src/test/java/at/gv/egiz/moazs/Mzs2MsgConverterTest.java index de72148..f18ddbe 100644 --- a/src/test/java/at/gv/egiz/moazs/Mzs2MsgConverterTest.java +++ b/src/test/java/at/gv/egiz/moazs/Mzs2MsgConverterTest.java @@ -4,13 +4,10 @@ import at.gv.egiz.moazs.scheme.Marshaller; import at.gv.egiz.moazs.scheme.Mzs2MsgConverter; import at.gv.zustellung.app2mzs.xsd.DeliveryRequestType; import at.gv.zustellung.msg.xsd.ObjectFactory; +import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import javax.xml.bind.JAXBElement; import java.io.BufferedInputStream; @@ -19,23 +16,22 @@ import java.io.IOException; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(SpringRunner.class) -@SpringBootTest public class Mzs2MsgConverterTest { private final String basePath = "src/test/resources/at/gv/egiz/moazs/Mzs2MsgConverterTest/"; private final static Logger logger = LoggerFactory.getLogger(Mzs2MsgConverterTest.class); - @Autowired private Mzs2MsgConverter converter; - - @Autowired private Marshaller mzsMarshaller; - - @Autowired private Marshaller msgMarshaller; + @Before + public void setUp() { + converter = new Mzs2MsgConverter(); + mzsMarshaller = new Marshaller(true); + msgMarshaller = new Marshaller(false); + } @Test public void testCanConvertValidMzsRequestToMsgRequest() throws IOException { -- cgit v1.2.3