From 7f2adceb198db28f55717e9e78064931e77e0510 Mon Sep 17 00:00:00 2001 From: robokaso Date: Fri, 5 Sep 2008 09:30:47 +0000 Subject: [PATCH] RESOLVED - BATCH-821: remove EventSerializer/Deserializer abstractions and use Marshaller/Unmarshaller directly cleanup of now redundant classes --- .../batch/item/xml/StaxEventItemWriter.java | 2 +- .../oxm/MarshallingEventWriterSerializer.java | 37 ----- .../UnmarshallingEventReaderDeserializer.java | 39 ------ ...MarshallingObjectToXmlSerializerTests.java | 132 ------------------ ...nmarshallingFragmentDeserializerTests.java | 89 ------------ 5 files changed, 1 insertion(+), 298 deletions(-) delete mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/MarshallingEventWriterSerializer.java delete mode 100644 spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/UnmarshallingEventReaderDeserializer.java delete mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/MarshallingObjectToXmlSerializerTests.java delete mode 100644 spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/UnmarshallingFragmentDeserializerTests.java diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index a3b74c042..8852c7db6 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -40,7 +40,7 @@ import org.springframework.xml.transform.StaxResult; /** * An implementation of {@link ItemWriter} which uses StAX and - * {@link EventWriterSerializer} for serializing object to XML. + * {@link Marshaller} for serializing object to XML. * * This item writer also provides restart, statistics and transaction features * by implementing corresponding interfaces. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/MarshallingEventWriterSerializer.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/MarshallingEventWriterSerializer.java deleted file mode 100644 index a0276e7c5..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/MarshallingEventWriterSerializer.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.springframework.batch.item.xml.oxm; - -import java.io.IOException; - -import javax.xml.stream.XMLEventWriter; -import javax.xml.transform.Result; - -import org.springframework.batch.item.xml.EventWriterSerializer; -import org.springframework.dao.DataAccessResourceFailureException; -import org.springframework.oxm.Marshaller; -import org.springframework.xml.transform.StaxResult; - -/** - * Object to xml serializer that wraps a Spring OXM {@link Marshaller} object. - * - * @author Lucas Ward - * - */ -public class MarshallingEventWriterSerializer implements EventWriterSerializer { - - private Marshaller marshaller; - - public MarshallingEventWriterSerializer(Marshaller marshaller) { - this.marshaller = marshaller; - } - - public void serializeObject(XMLEventWriter writer, T output) { - Result result = new StaxResult(writer); - try { - marshaller.marshal(output, result); - } - catch (IOException xse) { - throw new DataAccessResourceFailureException("Unable to write to file resource: [" + result.getSystemId() - + "]", xse); - } - } -} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/UnmarshallingEventReaderDeserializer.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/UnmarshallingEventReaderDeserializer.java deleted file mode 100644 index 338610cee..000000000 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/oxm/UnmarshallingEventReaderDeserializer.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.springframework.batch.item.xml.oxm; - -import java.io.IOException; - -import javax.xml.stream.XMLEventReader; - -import org.springframework.batch.item.xml.EventReaderDeserializer; -import org.springframework.dao.DataAccessResourceFailureException; -import org.springframework.oxm.Unmarshaller; -import org.springframework.util.Assert; -import org.springframework.xml.transform.StaxSource; - -/** - * Delegates deserializing to Spring OXM {@link Unmarshaller}. - * - * @author Robert Kasanicky - * @author Lucas Ward - */ -public class UnmarshallingEventReaderDeserializer implements EventReaderDeserializer { - - private Unmarshaller unmarshaller; - - public UnmarshallingEventReaderDeserializer(Unmarshaller unmarshaller){ - Assert.notNull(unmarshaller); - this.unmarshaller = unmarshaller; - } - - @SuppressWarnings("unchecked") - public T deserializeFragment(XMLEventReader eventReader) { - T item = null; - try { - item = (T) unmarshaller.unmarshal(new StaxSource(eventReader)); - } - catch (IOException e) { - throw new DataAccessResourceFailureException("IO error during unmarshalling", e); - } - return item; - } -} diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/MarshallingObjectToXmlSerializerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/MarshallingObjectToXmlSerializerTests.java deleted file mode 100644 index 90b72a9fb..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/MarshallingObjectToXmlSerializerTests.java +++ /dev/null @@ -1,132 +0,0 @@ -/* - * Copyright 2006-2007 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.item.xml.oxm; - -import java.io.IOException; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLEventWriter; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.XMLEvent; -import javax.xml.transform.Result; - -import junit.framework.TestCase; - -import org.springframework.dao.DataAccessResourceFailureException; -import org.springframework.oxm.Marshaller; -import org.springframework.oxm.XmlMappingException; - -/** - * - * - * @author Lucas Ward - * - */ -public class MarshallingObjectToXmlSerializerTests extends TestCase { - - MarshallingEventWriterSerializer xmlSerializer; - - MockMarshaller mockMarshaller = new MockMarshaller(); - - private StubXmlEventWriter writer; - - protected void setUp() throws Exception { - super.setUp(); - - xmlSerializer = new MarshallingEventWriterSerializer(mockMarshaller); - writer = new StubXmlEventWriter(); - } - - public void testSuccessfulWrite(){ - - Object objectToOutput = new Object(); - xmlSerializer.serializeObject(writer, objectToOutput); - assertEquals(objectToOutput, mockMarshaller.getMarshalledObject()); - } - - public void testUnsucessfulWrite(){ - - mockMarshaller.setThrowException(true); - try{ - xmlSerializer.serializeObject(writer, new Object()); - fail("Exception expected"); - }catch(DataAccessResourceFailureException ex){ - //expected - } - } - - private static class MockMarshaller implements Marshaller{ - - private Object marshalledObject; - private boolean throwException = false; - - public void marshal(Object arg0, Result arg1) - throws XmlMappingException, IOException { - if(throwException){ - throw new IOException(); - } - marshalledObject = arg0; - } - - @SuppressWarnings("unchecked") - public boolean supports(Class arg0) { - return false; - } - - public Object getMarshalledObject() { - return marshalledObject; - } - - public void setThrowException(boolean throwException) { - this.throwException = throwException; - } - } - - private static class StubXmlEventWriter implements XMLEventWriter{ - - public void add(XMLEvent arg0) throws XMLStreamException { } - - public void add(XMLEventReader arg0) throws XMLStreamException { } - - public void close() throws XMLStreamException { - } - - public void flush() throws XMLStreamException { - } - - public NamespaceContext getNamespaceContext() { - return null; - } - - public String getPrefix(String arg0) throws XMLStreamException { - return null; - } - - public void setDefaultNamespace(String arg0) throws XMLStreamException { - } - - public void setNamespaceContext(NamespaceContext arg0) - throws XMLStreamException { - } - - public void setPrefix(String arg0, String arg1) - throws XMLStreamException { - } - - } -} - diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/UnmarshallingFragmentDeserializerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/UnmarshallingFragmentDeserializerTests.java deleted file mode 100644 index 994c6deee..000000000 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/oxm/UnmarshallingFragmentDeserializerTests.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.springframework.batch.item.xml.oxm; - -import java.io.IOException; - -import javax.xml.stream.XMLEventReader; -import javax.xml.stream.XMLInputFactory; -import javax.xml.transform.Source; - -import junit.framework.TestCase; - -import static org.easymock.EasyMock.*; -import org.springframework.core.io.ByteArrayResource; -import org.springframework.core.io.Resource; -import org.springframework.dao.DataAccessException; -import org.springframework.oxm.Unmarshaller; - -/** - * Tests for {@link UnmarshallingEventReaderDeserializer} - * - * @author Robert Kasanicky - */ -public class UnmarshallingFragmentDeserializerTests extends TestCase { - - // object under test - private UnmarshallingEventReaderDeserializer deserializer; - - private XMLEventReader eventReader; - private String xml = " "; - - private Unmarshaller unmarshaller; - - - - protected void setUp() throws Exception { - Resource input = new ByteArrayResource(xml.getBytes()); - eventReader = XMLInputFactory.newInstance().createXMLEventReader(input.getInputStream()); - unmarshaller = createMock(Unmarshaller.class); - //unmarshallerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER); - deserializer = new UnmarshallingEventReaderDeserializer(unmarshaller); - } - - /** - * Regular scenario when deserializer returns the object provided by Unmarshaller - */ - public void testSuccessfulDeserialization() throws Exception { - Object expectedResult = new Object(); - expect(unmarshaller.unmarshal(isA(Source.class))).andReturn(expectedResult); - replay(unmarshaller); - - Object result = deserializer.deserializeFragment(eventReader); - - assertEquals(expectedResult, result); - - verify(unmarshaller); - } - - /** - * Appropriate exception rethrown in case of failure. - */ - public void testFailedDeserialization() throws Exception { - expect(unmarshaller.unmarshal(isA(Source.class))).andThrow(new IOException()); - replay(unmarshaller); - - try { - deserializer.deserializeFragment(eventReader); - fail("Exception expected"); - } - catch (DataAccessException e) { - // expected - } - - verify(unmarshaller); - } - - /** - * It makes no sense to create UnmarshallingFragmentDeserializer with null Unmarshaller, - * therefore it should cause exception. - */ - public void testExceptionOnNullUnmarshaller() { - try { - deserializer = new UnmarshallingEventReaderDeserializer(null); - fail("Exception expected"); - } - catch (IllegalArgumentException e) { - // expected - } - - } -}