MarshallingUtils { public static Object unmarshal(Unmarshaller unmarshaller, WebServiceMessage message) throws IOException { Source payload = message.getPayloadSource(); if (payload == null) { return null; } else if (unmarshaller instanceof MimeUnmarshaller && message instanceof MimeMessage) { MimeUnmarshaller mimeUnmarshaller = (MimeUnmarshaller) unmarshaller; MimeMessageContainer container = new MimeMessageContainer((MimeMessage) message); return mimeUnmarshaller.unmarshal(payload, container); } else { return unmarshaller.unmarshal(payload); } } private  MarshallingUtils(); static Object unmarshal(Unmarshaller unmarshaller, WebServiceMessage message); static void marshal(Marshaller marshaller, Object graph, WebServiceMessage message);  }
@Test public void testUnmarshal() throws Exception { Unmarshaller unmarshallerMock = createMock(Unmarshaller.class); WebServiceMessage messageMock = createMock(WebServiceMessage.class); Source source = new StringSource(""); Object unmarshalled = new Object(); expect(messageMock.getPayloadSource()).andReturn(source); expect(unmarshallerMock.unmarshal(source)).andReturn(unmarshalled); replay(unmarshallerMock, messageMock); Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock); Assert.assertEquals("Invalid unmarshalled object", unmarshalled, result); verify(unmarshallerMock, messageMock); }
@Test public void testUnmarshalMime() throws Exception { MimeUnmarshaller unmarshallerMock = createMock(MimeUnmarshaller.class); MimeMessage messageMock = createMock(MimeMessage.class); Source source = new StringSource(""); Object unmarshalled = new Object(); expect(messageMock.getPayloadSource()).andReturn(source); expect(unmarshallerMock.unmarshal(eq(source), isA(MimeContainer.class))).andReturn(unmarshalled); replay(unmarshallerMock, messageMock); Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock); Assert.assertEquals("Invalid unmarshalled object", unmarshalled, result); verify(unmarshallerMock, messageMock); }
@Test public void testUnmarshalNoPayload() throws Exception { Unmarshaller unmarshallerMock = createMock(Unmarshaller.class); MimeMessage messageMock = createMock(MimeMessage.class); expect(messageMock.getPayloadSource()).andReturn(null); replay(unmarshallerMock, messageMock); Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock); Assert.assertNull("Invalid unmarshalled object", result); verify(unmarshallerMock, messageMock); }