1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.support;
18  
19  import javax.xml.transform.Result;
20  import javax.xml.transform.Source;
21  
22  import org.springframework.oxm.Marshaller;
23  import org.springframework.oxm.Unmarshaller;
24  import org.springframework.oxm.mime.MimeContainer;
25  import org.springframework.oxm.mime.MimeMarshaller;
26  import org.springframework.oxm.mime.MimeUnmarshaller;
27  import org.springframework.ws.WebServiceMessage;
28  import org.springframework.ws.mime.MimeMessage;
29  import org.springframework.xml.transform.StringResult;
30  import org.springframework.xml.transform.StringSource;
31  
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import static org.easymock.EasyMock.*;
36  
37  public class MarshallingUtilsTest {
38  
39      @Test
40      public void testUnmarshal() throws Exception {
41          Unmarshaller unmarshallerMock = createMock(Unmarshaller.class);
42          WebServiceMessage messageMock = createMock(WebServiceMessage.class);
43  
44          Source source = new StringSource("");
45          Object unmarshalled = new Object();
46          expect(messageMock.getPayloadSource()).andReturn(source);
47          expect(unmarshallerMock.unmarshal(source)).andReturn(unmarshalled);
48  
49          replay(unmarshallerMock, messageMock);
50  
51          Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);
52          Assert.assertEquals("Invalid unmarshalled object", unmarshalled, result);
53  
54          verify(unmarshallerMock, messageMock);
55      }
56  
57      @Test
58      public void testUnmarshalMime() throws Exception {
59          MimeUnmarshaller unmarshallerMock = createMock(MimeUnmarshaller.class);
60          MimeMessage messageMock = createMock(MimeMessage.class);
61  
62          Source source = new StringSource("");
63          Object unmarshalled = new Object();
64          expect(messageMock.getPayloadSource()).andReturn(source);
65          expect(unmarshallerMock.unmarshal(eq(source), isA(MimeContainer.class))).andReturn(unmarshalled);
66  
67          replay(unmarshallerMock, messageMock);
68  
69          Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);
70          Assert.assertEquals("Invalid unmarshalled object", unmarshalled, result);
71  
72          verify(unmarshallerMock, messageMock);
73      }
74  
75      @Test
76      public void testUnmarshalNoPayload() throws Exception {
77          Unmarshaller unmarshallerMock = createMock(Unmarshaller.class);
78          MimeMessage messageMock = createMock(MimeMessage.class);
79  
80          expect(messageMock.getPayloadSource()).andReturn(null);
81  
82          replay(unmarshallerMock, messageMock);
83  
84          Object result = MarshallingUtils.unmarshal(unmarshallerMock, messageMock);
85          Assert.assertNull("Invalid unmarshalled object", result);
86  
87          verify(unmarshallerMock, messageMock);
88      }
89  
90      @Test
91      public void testMarshal() throws Exception {
92          Marshaller marshallerMock = createMock(Marshaller.class);
93          WebServiceMessage messageMock = createMock(WebServiceMessage.class);
94  
95          Result result = new StringResult();
96          Object marshalled = new Object();
97          expect(messageMock.getPayloadResult()).andReturn(result);
98          marshallerMock.marshal(marshalled, result);
99  
100         replay(marshallerMock, messageMock);
101 
102         MarshallingUtils.marshal(marshallerMock, marshalled, messageMock);
103 
104         verify(marshallerMock, messageMock);
105     }
106 
107     @Test
108     public void testMarshalMime() throws Exception {
109         MimeMarshaller marshallerMock = createMock(MimeMarshaller.class);
110         MimeMessage messageMock = createMock(MimeMessage.class);
111 
112         Result result = new StringResult();
113         Object marshalled = new Object();
114         expect(messageMock.getPayloadResult()).andReturn(result);
115         marshallerMock.marshal(eq(marshalled), eq(result), isA(MimeContainer.class));
116 
117         replay(marshallerMock, messageMock);
118 
119         MarshallingUtils.marshal(marshallerMock, marshalled, messageMock);
120 
121         verify(marshallerMock, messageMock);
122     }
123 
124 
125 }