001: /*
002: * Copyright 2005 Joe Walker
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: package org.directwebremoting.convert;
017:
018: import org.directwebremoting.convert.test.MyBeanImpl;
019: import org.directwebremoting.extend.ConverterManager;
020: import org.directwebremoting.extend.InboundContext;
021: import org.directwebremoting.extend.InboundVariable;
022: import org.directwebremoting.extend.NonNestedOutboundVariable;
023: import org.directwebremoting.extend.OutboundContext;
024: import org.directwebremoting.extend.OutboundVariable;
025: import org.directwebremoting.extend.TypeHintContext;
026: import org.easymock.EasyMock;
027: import org.junit.Before;
028: import org.junit.Test;
029:
030: import static org.junit.Assert.assertEquals;
031: import static org.junit.Assert.assertNotNull;
032: import static org.junit.Assert.assertNull;
033: import static org.junit.Assert.assertTrue;
034:
035: /**
036: * @author Bram Smeets
037: * @author Joe Walker [joe at getahead dot ltd dot uk]
038: */
039: public class BeanConverterTest {
040: private BeanConverter converter = new BeanConverter();
041:
042: private ConverterManager manager;
043:
044: @Before
045: public void setUp() throws Exception {
046: manager = EasyMock.createMock(ConverterManager.class);
047: converter.setConverterManager(manager);
048: }
049:
050: @Test
051: public void setImplementation() throws Exception {
052: converter.setImplementation(MyBeanImpl.class.getName());
053: assertEquals(MyBeanImpl.class, converter.getInstanceType());
054: }
055:
056: @Test(expected=Exception.class)
057: public void setWrongImplementation() throws Exception {
058: converter.setImplementation("UnknownClass");
059: }
060:
061: @Test
062: public void convertInboundWithNull() throws Exception {
063: InboundVariable var = new InboundVariable(null, null, null,
064: "null");
065: Object result = converter.convertInbound(null, var, null);
066: assertNull(result);
067: }
068:
069: @Test(expected=Exception.class)
070: public void convertInboundWithInvalidArguments1() throws Exception {
071: // test with missing map start in the variable value
072: InboundVariable var = new InboundVariable(null, null, null,
073: "value");
074: converter.convertInbound(null, var, null);
075: }
076:
077: @Test(expected=Exception.class)
078: public void convertInboundWithInvalidArguments2() throws Exception {
079: // test with missing map end in the variable value
080: InboundVariable var = new InboundVariable(null, null, null,
081: "{ value");
082: converter.convertInbound(null, var, null);
083: }
084:
085: @Test
086: public void convertInbound() throws Exception {
087: // also test with an instance type
088: InboundContext ctx = new InboundContext();
089: InboundVariable var = new InboundVariable(null, null, "type",
090: "{ property: bla }");
091: converter.setInstanceType(MyBeanImpl.class);
092:
093: EasyMock.expect(
094: manager.convertInbound(EasyMock.eq(String.class),
095: EasyMock.isA(InboundVariable.class), EasyMock
096: .eq(ctx), EasyMock
097: .isA(TypeHintContext.class)))
098: .andReturn("bla");
099: EasyMock.replay(manager);
100:
101: Object result = converter
102: .convertInbound(Object.class, var, ctx);
103: assertNotNull(result);
104: assertTrue(result instanceof MyBeanImpl);
105: MyBeanImpl bean = (MyBeanImpl) result;
106: assertEquals("bla", bean.getProperty());
107:
108: EasyMock.verify(manager);
109: }
110:
111: @Test(expected=Exception.class)
112: public void convertInboundNullPointerException() throws Exception {
113: converter.convertInbound(null, null, null);
114: }
115:
116: @Test(expected=Exception.class)
117: public void convertInboundNullPointerException2() throws Exception {
118: InboundVariable var = new InboundVariable(null, null, null,
119: (String) null);
120: converter.convertInbound(null, var, null);
121: }
122:
123: @Test(expected=Exception.class)
124: public void convertInboundIllegalArgumentException()
125: throws Exception {
126: InboundVariable var = new InboundVariable(null, null, null,
127: "value");
128: converter.convertInbound(null, var, null);
129: }
130:
131: @Test(expected=Exception.class)
132: public void convertInboundMarshallException() throws Exception {
133: InboundVariable var = new InboundVariable(null, null, null,
134: "{ value }");
135: converter.convertInbound(null, var, null);
136: }
137:
138: @Test(expected=Exception.class)
139: public void convertInboundMarshallException2() throws Exception {
140: InboundVariable var = new InboundVariable(null, null, null,
141: "{ value }");
142: converter.convertInbound(Object.class, var, null);
143: }
144:
145: @Test(expected=Exception.class)
146: public void convertInboundMarshallException3() throws Exception {
147: InboundVariable var = new InboundVariable(null, null, null,
148: "{ value }");
149: // TODO: this is an error due to a null pointer exception in hashcode of InboundVariable. This should be fixed!
150: converter.convertInbound(Object.class, var,
151: new InboundContext());
152: }
153:
154: @Test(expected=Exception.class)
155: public void convertInboundMarshallException4() throws Exception {
156: InboundVariable var = new InboundVariable(null, null, "type",
157: "{ value }");
158: converter.convertInbound(Object.class, var,
159: new InboundContext());
160: }
161:
162: @Test
163: public void convertInboundExceptions() throws Exception {
164: InboundContext ctx = new InboundContext();
165:
166: InboundVariable var = new InboundVariable(null, null, "type",
167: "{ value: , }");
168: Object result = converter
169: .convertInbound(Object.class, var, ctx);
170: assertNotNull(result);
171:
172: var = new InboundVariable(null, null, "type", "{ value: , }");
173: result = converter.convertInbound(Object.class, var, ctx);
174: assertNotNull(result);
175: }
176:
177: @Test
178: public void convertOutbound() throws Exception {
179: OutboundContext ctx = new OutboundContext(false);
180:
181: EasyMock.expect(manager.convertOutbound("bla", ctx)).andReturn(
182: new NonNestedOutboundVariable(""));
183: EasyMock.replay(manager);
184:
185: MyBeanImpl bean = new MyBeanImpl();
186: bean.setProperty("bla");
187: OutboundVariable result = converter.convertOutbound(bean, ctx);
188: assertNotNull(result);
189: assertEquals("s0", result.getAssignCode());
190: assertTrue(result.getBuildCode().length() > 10);
191:
192: EasyMock.verify(manager);
193: }
194:
195: @Test(expected=Exception.class)
196: public void convertOutboundWithInclusionsAndExclusions()
197: throws Exception {
198: converter.setInclude("bla, getSomething");
199: converter.setExclude("bla");
200: }
201:
202: @Test(expected=Exception.class)
203: public void convertOutboundWithExclusionsAndInclusions()
204: throws Exception {
205: converter.setExclude("bla, getSomething");
206: converter.setInclude("bla");
207: }
208:
209: @Test
210: public void convertOutboundWithInclusions() throws Exception {
211: OutboundContext ctx = new OutboundContext(false);
212:
213: converter.setInclude("property, getSomething");
214:
215: EasyMock.expect(manager.convertOutbound("bla", ctx)).andReturn(
216: new NonNestedOutboundVariable(""));
217: EasyMock.replay(manager);
218:
219: MyBeanImpl bean = new MyBeanImpl();
220: bean.setProperty("bla");
221: OutboundVariable result = converter.convertOutbound(bean, ctx);
222: assertNotNull(result);
223: assertEquals("s0", result.getAssignCode());
224: //assertTrue(result.getBuildCode().length() > 10);
225:
226: EasyMock.verify(manager);
227: }
228:
229: @Test
230: public void convertOutboundWithExclusions() throws Exception {
231: OutboundContext ctx = new OutboundContext(false);
232:
233: converter.setExclude("property, getSomething");
234:
235: EasyMock.replay(manager);
236:
237: MyBeanImpl bean = new MyBeanImpl();
238: bean.setProperty("bla");
239: OutboundVariable result = converter.convertOutbound(bean, ctx);
240: assertNotNull(result);
241: assertEquals("s0", result.getAssignCode());
242: //assertEquals(result.getBuildCode(), "");
243:
244: EasyMock.verify(manager);
245: }
246:
247: @Test(expected=Exception.class)
248: public void convertOutboundNullPointerException() throws Exception {
249: converter.convertOutbound(null, null);
250: }
251:
252: @Test(expected=Exception.class)
253: public void convertOutboundNullPointerException2() throws Exception {
254: converter.convertOutbound(null, new OutboundContext(false));
255: }
256:
257: @Test
258: public void convertOutboundExceptions() throws Exception {
259: OutboundVariable result = converter.convertOutbound(
260: new Object(), new OutboundContext(false));
261: assertNotNull(result);
262: assertEquals("s0", result.getAssignCode());
263: //assertEquals(result.getBuildCode(), "");
264: }
265: }
|