RESOLVED - BATCH-821: remove EventSerializer/Deserializer abstractions and use Marshaller/Unmarshaller directly

cleanup of now redundant classes
This commit is contained in:
robokaso
2008-09-05 09:30:47 +00:00
parent 5994782b37
commit 7f2adceb19
5 changed files with 1 additions and 298 deletions

View File

@@ -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<Object> xmlSerializer;
MockMarshaller mockMarshaller = new MockMarshaller();
private StubXmlEventWriter writer;
protected void setUp() throws Exception {
super.setUp();
xmlSerializer = new MarshallingEventWriterSerializer<Object>(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 {
}
}
}

View File

@@ -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 = "<root> </root>";
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<Object>(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<Object>(null);
fail("Exception expected");
}
catch (IllegalArgumentException e) {
// expected
}
}
}