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.impl;
017:
018: import java.lang.reflect.Method;
019: import java.util.ArrayList;
020:
021: import org.directwebremoting.AjaxFilter;
022: import org.directwebremoting.AjaxFilterChain;
023: import org.directwebremoting.create.NewCreator;
024: import org.directwebremoting.extend.AccessControl;
025: import org.directwebremoting.extend.AjaxFilterManager;
026: import org.directwebremoting.extend.Calls;
027: import org.directwebremoting.extend.ConverterManager;
028: import org.directwebremoting.extend.CreatorManager;
029: import org.directwebremoting.extend.Marshaller;
030: import org.directwebremoting.extend.NonNestedOutboundVariable;
031: import org.directwebremoting.extend.OutboundContext;
032: import org.directwebremoting.extend.Replies;
033: import org.directwebremoting.impl.test.TestCreatedObject;
034: import org.directwebremoting.impl.test.TestWebContextFactory;
035: import org.junit.Before;
036: import org.junit.Ignore;
037: import org.junit.Test;
038: import org.springframework.mock.web.MockHttpServletRequest;
039: import org.springframework.mock.web.MockHttpServletResponse;
040:
041: import static org.easymock.EasyMock.createMock;
042: import static org.easymock.EasyMock.eq;
043: import static org.easymock.EasyMock.expect;
044: import static org.easymock.EasyMock.expectLastCall;
045: import static org.easymock.EasyMock.isA;
046: import static org.easymock.EasyMock.replay;
047: import static org.easymock.EasyMock.verify;
048: import static org.junit.Assert.assertFalse;
049: import static org.junit.Assert.assertNotNull;
050: import static org.junit.Assert.assertTrue;
051: import static org.junit.Assert.fail;
052:
053: /**
054: * @author Joe Walker [joe at getahead dot ltd dot uk]
055: */
056: public class DefaultRemoterTest {
057: private DefaultRemoter defaultRemoter = new DefaultRemoter();
058:
059: private CreatorManager creatorManager;
060:
061: private AccessControl accessControl;
062:
063: private ConverterManager converterManager;
064:
065: private AjaxFilterManager ajaxFilterManager;
066:
067: private MockHttpServletRequest request;
068:
069: private Marshaller marshaller;
070:
071: @Before
072: public void setUp() throws Exception {
073: creatorManager = createMock(CreatorManager.class);
074: defaultRemoter.setCreatorManager(creatorManager);
075:
076: accessControl = createMock(AccessControl.class);
077: defaultRemoter.setAccessControl(accessControl);
078:
079: marshaller = createMock(Marshaller.class);
080:
081: ajaxFilterManager = createMock(AjaxFilterManager.class);
082: defaultRemoter.setAjaxFilterManager(ajaxFilterManager);
083:
084: request = new MockHttpServletRequest();
085: }
086:
087: /**
088: * @throws Exception
089: */
090: public void testHandle() throws Exception {
091: request.setPathInfo("/exec/dataManager.doTest");
092: request.setMethod("POST");
093: request
094: .setContent(("callCount=2\n"
095: + "c0-id=1\nc0-scriptName=creatorName\nc0-methodName=toString\n"
096: + "c1-id=2\nc1-scriptName=creatorName\nc1-methodName=hashCode")
097: .getBytes());
098:
099: creatorManager.getCreator("creatorName");
100: NewCreator creator = new NewCreator();
101: creator.setClass(TestCreatedObject.class.getName());
102: expectLastCall().andReturn(creator).times(4);
103:
104: accessControl.assertExecutionIsPossible(eq(creator),
105: eq("creatorName"), isA(Method.class));
106: expectLastCall().andReturn(null).times(2);
107:
108: expect(converterManager.convertOutbound(isA(Object.class),
109: isA(OutboundContext.class)));
110: expectLastCall().andReturn(new NonNestedOutboundVariable(""))
111: .times(2);
112:
113: // TODO: this should not be necessary!
114: ArrayList<AjaxFilter> filters = new ArrayList<AjaxFilter>();
115: filters.add(new AjaxFilter() {
116: public Object doFilter(Object obj, Method method,
117: Object[] params, AjaxFilterChain chain)
118: throws Exception {
119: return new Object();
120: }
121: });
122: filters.add(new AjaxFilter() {
123: public Object doFilter(Object obj, Method method,
124: Object[] params, AjaxFilterChain chain)
125: throws Exception {
126: return new Object();
127: }
128: });
129: expect(ajaxFilterManager.getAjaxFilters("creatorName"))
130: .andReturn(filters.iterator()).atLeastOnce();
131:
132: replay(creatorManager);
133: replay(accessControl);
134: replay(converterManager);
135: replay(ajaxFilterManager);
136:
137: DefaultWebContextBuilder builder = new DefaultWebContextBuilder();
138: builder.set(request, null, null, null, null);
139: TestWebContextFactory.setWebContextBuilder(builder);
140:
141: MockHttpServletResponse response = new MockHttpServletResponse();
142: Calls calls = marshaller.marshallInbound(request, response);
143: Replies replies = defaultRemoter.execute(calls);
144: marshaller.marshallOutbound(replies, request, response);
145:
146: String result = response.getContentAsString();
147: verify(creatorManager);
148: verify(accessControl);
149: verify(converterManager);
150: verify(ajaxFilterManager);
151:
152: assertNotNull(result);
153: assertTrue(result.indexOf("<script type='text/javascript'>") != -1);
154: assertTrue(result
155: .indexOf("window.parent.DWREngine._handleResponse('1',") != -1);
156: assertTrue(result
157: .indexOf("window.parent.DWREngine._handleResponse('2',") != -1);
158: assertTrue(result.indexOf("</script>") != -1);
159: }
160:
161: @Ignore
162: @Test
163: public void handle2() throws Exception {
164: request.setPathInfo("/interface/creatorName.js");
165:
166: creatorManager.getCreator("creatorName");
167: NewCreator creator = new NewCreator();
168: creator.setClass(TestCreatedObject.class.getName());
169: expectLastCall().andReturn(creator);
170:
171: // expect 9 method calls to 'getReasonToNotDisplay' for each method on Object
172: accessControl.assertIsDisplayable(creator, "creatorName",
173: isA(Method.class));
174: expectLastCall().andReturn(null).times(11);
175:
176: replay(creatorManager);
177: replay(accessControl);
178:
179: String result = defaultRemoter.generateInterfaceScript(
180: "creatorName", "/path");
181:
182: verify(creatorManager);
183: verify(accessControl);
184:
185: // check the response
186: assertTrue(result.indexOf("function creatorName() { }") != -1);
187: assertTrue(result.indexOf("creatorName.hashCode = function(") != -1);
188: assertTrue(result.indexOf("creatorName.getClass = function(") != -1);
189: assertTrue(result.indexOf("creatorName.wait = function(") != -1);
190: assertTrue(result.indexOf("creatorName.wait = function(") != -1);
191: assertTrue(result.indexOf("creatorName.wait = function(") != -1);
192: assertTrue(result.indexOf("creatorName.equals = function(") != -1);
193: assertTrue(result.indexOf("creatorName.notify = function(") != -1);
194: assertTrue(result.indexOf("creatorName.notifyAll = function(") != -1);
195: assertTrue(result.indexOf("creatorName.toString = function(") != -1);
196: assertTrue(result
197: .indexOf("creatorName.testMethodWithServletParameters = function(") != -1);
198:
199: // make sure no entry is generated for the reserved javascript word 'namespace'
200: assertFalse(result.indexOf("creatorName.namespace = function(") != -1);
201: }
202:
203: @Ignore
204: @Test
205: public void handleWithoutInterface() throws Exception {
206: request.setMethod("GET");
207:
208: creatorManager.getCreator("");
209: expectLastCall().andThrow(new SecurityException());
210:
211: replay(creatorManager);
212: replay(accessControl);
213:
214: try {
215: Calls calls = marshaller.marshallInbound(request, null);
216: defaultRemoter.execute(calls);
217: fail("a security exception was expected");
218: } catch (SecurityException e) {
219: // do nothing, was expected
220: }
221:
222: verify(creatorManager);
223: verify(accessControl);
224: }
225:
226: @Ignore
227: @Test
228: public void handleWithReasonsNotToDisplay() throws Exception {
229: request.setMethod("GET");
230:
231: // make sure not to allow an impossible test
232: defaultRemoter.setAllowImpossibleTests(false);
233:
234: request.setPathInfo("/interface/creatorName.js");
235:
236: creatorManager.getCreator("creatorName");
237: NewCreator creator = new NewCreator();
238: creator.setClass(TestCreatedObject.class.getName());
239: expectLastCall().andReturn(creator);
240:
241: // expect 9 method calls to 'getReasonToNotDisplay' for each method on Object
242: accessControl.assertIsDisplayable(eq(creator),
243: eq("creatorName"), isA(Method.class));
244: expectLastCall().andReturn("myReason").times(11);
245:
246: replay(creatorManager);
247: replay(accessControl);
248:
249: MockHttpServletResponse response = new MockHttpServletResponse();
250: Calls calls = marshaller.marshallInbound(request, response);
251: Replies replies = defaultRemoter.execute(calls);
252: marshaller.marshallOutbound(replies, request, response);
253:
254: verify(creatorManager);
255: verify(accessControl);
256:
257: // check the response
258: String result = response.getContentAsString();
259: assertTrue(result.indexOf("function creatorName() { }") != -1);
260: assertFalse(result
261: .indexOf("creatorName.hashCode = function(callback)") != -1);
262: assertFalse(result
263: .indexOf("creatorName.getClass = function(callback)") != -1);
264: assertFalse(result
265: .indexOf("creatorName.wait = function(callback)") != -1);
266: assertFalse(result
267: .indexOf("creatorName.wait = function(p0, p1, callback)") != -1);
268: assertFalse(result
269: .indexOf("creatorName.wait = function(p0, callback)") != -1);
270: assertFalse(result
271: .indexOf("creatorName.equals = function(p0, callback)") != -1);
272: assertFalse(result
273: .indexOf("creatorName.notify = function(callback)") != -1);
274: assertFalse(result
275: .indexOf("creatorName.notifyAll = function(callback)") != -1);
276: assertFalse(result
277: .indexOf("creatorName.toString = function(callback)") != -1);
278: assertFalse(result
279: .indexOf("creatorName.testMethodWithServletParameters = function(callback)") != -1);
280: assertFalse(result
281: .indexOf("creatorName.namespace = function(callback)") != -1);
282: }
283: }
|