001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.support;
018:
019: import javax.xml.transform.Result;
020: import javax.xml.transform.Source;
021:
022: import junit.framework.TestCase;
023: import org.easymock.MockControl;
024: import org.springframework.oxm.Marshaller;
025: import org.springframework.oxm.Unmarshaller;
026: import org.springframework.oxm.mime.MimeMarshaller;
027: import org.springframework.oxm.mime.MimeUnmarshaller;
028: import org.springframework.ws.WebServiceMessage;
029: import org.springframework.ws.mime.MimeMessage;
030: import org.springframework.xml.transform.StringResult;
031: import org.springframework.xml.transform.StringSource;
032:
033: public class MarshallingUtilsTest extends TestCase {
034:
035: public void testUnmarshal() throws Exception {
036: MockControl unmarshallerControl = MockControl
037: .createControl(Unmarshaller.class);
038: Unmarshaller unmarshallerMock = (Unmarshaller) unmarshallerControl
039: .getMock();
040: MockControl messageControl = MockControl
041: .createControl(WebServiceMessage.class);
042: WebServiceMessage messageMock = (WebServiceMessage) messageControl
043: .getMock();
044:
045: Source source = new StringSource("");
046: Object unmarshalled = new Object();
047: messageControl.expectAndReturn(messageMock.getPayloadSource(),
048: source);
049: unmarshallerControl.expectAndReturn(unmarshallerMock
050: .unmarshal(source), unmarshalled);
051:
052: unmarshallerControl.replay();
053: messageControl.replay();
054:
055: Object result = MarshallingUtils.unmarshal(unmarshallerMock,
056: messageMock);
057: assertEquals("Invalid unmarshalled object", unmarshalled,
058: result);
059:
060: unmarshallerControl.verify();
061: messageControl.verify();
062: }
063:
064: public void testUnmarshalMime() throws Exception {
065: MockControl unmarshallerControl = MockControl
066: .createControl(MimeUnmarshaller.class);
067: MimeUnmarshaller unmarshallerMock = (MimeUnmarshaller) unmarshallerControl
068: .getMock();
069: MockControl messageControl = MockControl
070: .createControl(MimeMessage.class);
071: MimeMessage messageMock = (MimeMessage) messageControl
072: .getMock();
073:
074: Source source = new StringSource("");
075: Object unmarshalled = new Object();
076: messageControl.expectAndReturn(messageMock.getPayloadSource(),
077: source);
078: unmarshallerMock.unmarshal(source, null);
079: unmarshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
080: unmarshallerControl.setReturnValue(unmarshalled);
081:
082: unmarshallerControl.replay();
083: messageControl.replay();
084:
085: Object result = MarshallingUtils.unmarshal(unmarshallerMock,
086: messageMock);
087: assertEquals("Invalid unmarshalled object", unmarshalled,
088: result);
089:
090: unmarshallerControl.verify();
091: messageControl.verify();
092: }
093:
094: public void testMarshal() throws Exception {
095: MockControl marshallerControl = MockControl
096: .createControl(Marshaller.class);
097: Marshaller marshallerMock = (Marshaller) marshallerControl
098: .getMock();
099: MockControl messageControl = MockControl
100: .createControl(WebServiceMessage.class);
101: WebServiceMessage messageMock = (WebServiceMessage) messageControl
102: .getMock();
103:
104: Result result = new StringResult();
105: Object marshalled = new Object();
106: messageControl.expectAndReturn(messageMock.getPayloadResult(),
107: result);
108: marshallerMock.marshal(marshalled, result);
109:
110: marshallerControl.replay();
111: messageControl.replay();
112:
113: MarshallingUtils.marshal(marshallerMock, marshalled,
114: messageMock);
115:
116: marshallerControl.verify();
117: messageControl.verify();
118: }
119:
120: public void testMarshalMime() throws Exception {
121: MockControl marshallerControl = MockControl
122: .createControl(MimeMarshaller.class);
123: MimeMarshaller marshallerMock = (MimeMarshaller) marshallerControl
124: .getMock();
125: MockControl messageControl = MockControl
126: .createControl(MimeMessage.class);
127: MimeMessage messageMock = (MimeMessage) messageControl
128: .getMock();
129:
130: Result result = new StringResult();
131: Object marshalled = new Object();
132: messageControl.expectAndReturn(messageMock.getPayloadResult(),
133: result);
134: marshallerMock.marshal(marshalled, result, null);
135: marshallerControl.setMatcher(MockControl.ALWAYS_MATCHER);
136:
137: marshallerControl.replay();
138: messageControl.replay();
139:
140: MarshallingUtils.marshal(marshallerMock, marshalled,
141: messageMock);
142:
143: marshallerControl.verify();
144: messageControl.verify();
145: }
146:
147: }
|