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 static org.junit.Assert.*;
019:
020: import java.lang.reflect.Method;
021: import java.util.ArrayList;
022:
023: import org.directwebremoting.create.NewCreator;
024: import org.directwebremoting.extend.AccessControl;
025: import org.directwebremoting.extend.ConverterManager;
026: import org.directwebremoting.extend.CreatorManager;
027: import org.directwebremoting.impl.test.TestCreatedObject;
028: import org.easymock.EasyMock;
029: import org.junit.Before;
030: import org.junit.Ignore;
031: import org.junit.Test;
032:
033: /**
034: * @author Joe Walker [joe at getahead dot ltd dot uk]
035: */
036: public class DebugPageGeneratorTest {
037: private DefaultDebugPageGenerator debugPageGenerator = new DefaultDebugPageGenerator();
038:
039: private CreatorManager creatorManager;
040:
041: private AccessControl accessControl;
042:
043: private ConverterManager converterManager;
044:
045: @Before
046: public void setUp() throws Exception {
047: creatorManager = EasyMock.createMock(CreatorManager.class);
048: debugPageGenerator.setCreatorManager(creatorManager);
049:
050: accessControl = EasyMock.createMock(AccessControl.class);
051: debugPageGenerator.setAccessControl(accessControl);
052:
053: converterManager = EasyMock.createMock(ConverterManager.class);
054: debugPageGenerator.setConverterManager(converterManager);
055: }
056:
057: @Test
058: public void handleInNonDebug() throws Exception {
059: creatorManager.isDebug();
060: EasyMock.expectLastCall().andReturn(Boolean.FALSE);
061:
062: EasyMock.replay(creatorManager);
063: EasyMock.replay(accessControl);
064: EasyMock.replay(converterManager);
065:
066: String response = null;
067: try {
068: response = debugPageGenerator.generateIndexPage("/");
069: fail("a security exception was expected");
070: } catch (SecurityException e) {
071: // do nothing, was expected
072: }
073:
074: EasyMock.verify(creatorManager);
075: EasyMock.verify(accessControl);
076: EasyMock.verify(converterManager);
077:
078: assertNull(response);
079: // assertTrue(new String(response.getBody()).indexOf("Test Pages") != -1);
080: }
081:
082: @Ignore
083: @Test
084: public void handle() throws Exception {
085: creatorManager.isDebug();
086: EasyMock.expectLastCall().andReturn(Boolean.TRUE);
087:
088: creatorManager.getCreator("creatorName");
089: NewCreator creator = new NewCreator();
090: creator.setClass(TestCreatedObject.class.getName());
091: EasyMock.expectLastCall().andReturn(creator);
092:
093: accessControl.assertIsDisplayable(EasyMock.eq(creator),
094: EasyMock.eq("creatorName"), EasyMock.isA(Method.class));
095: EasyMock.expectLastCall().andReturn(null).times(11);
096:
097: converterManager.isConvertable((Class<?>) EasyMock.anyObject());
098: EasyMock.expectLastCall().andReturn(Boolean.TRUE).times(19);
099:
100: accessControl.assertExecutionIsPossible(EasyMock.eq(creator),
101: EasyMock.eq("creatorName"), EasyMock.isA(Method.class));
102: EasyMock.expectLastCall().andReturn(null).times(10);
103:
104: EasyMock.replay(creatorManager);
105: EasyMock.replay(accessControl);
106: EasyMock.replay(converterManager);
107:
108: String result = debugPageGenerator.generateTestPage("",
109: "creatorName");
110:
111: EasyMock.verify(creatorManager);
112: EasyMock.verify(accessControl);
113: EasyMock.verify(converterManager);
114:
115: assertNotNull(result);
116: assertTrue(result.indexOf("testMethodWithServletParameters(") != -1);
117: assertTrue(result.indexOf("hashCode(") != -1);
118: assertTrue(result.indexOf("getClass(") != -1);
119: assertTrue(result.indexOf("wait(") != -1);
120: assertTrue(result.indexOf("equals(") != -1);
121: assertTrue(result.indexOf("notify(") != -1);
122: assertTrue(result.indexOf("notifyAll(") != -1);
123: assertTrue(result.indexOf("toString(") != -1);
124: }
125:
126: @Test
127: public void handleWithoutDebug() throws Exception {
128: creatorManager.isDebug();
129: EasyMock.expectLastCall().andReturn(Boolean.FALSE);
130:
131: EasyMock.replay(creatorManager);
132:
133: try {
134: debugPageGenerator.generateIndexPage("root");
135: fail("Missing SecurityException");
136: } catch (SecurityException ex) {
137: }
138:
139: EasyMock.verify(creatorManager);
140: }
141:
142: @Test
143: public void generateIndexPage() throws Exception {
144: creatorManager.isDebug();
145: EasyMock.expectLastCall().andReturn(Boolean.TRUE);
146:
147: creatorManager.getCreatorNames();
148: ArrayList<String> names = new ArrayList<String>();
149: names.add("creatorName");
150: EasyMock.expectLastCall().andReturn(names);
151:
152: creatorManager.getCreator("creatorName");
153: NewCreator creator = new NewCreator();
154: creator.setClass(TestCreatedObject.class.getName());
155: EasyMock.expectLastCall().andReturn(creator);
156:
157: EasyMock.replay(creatorManager);
158:
159: String result = debugPageGenerator.generateIndexPage("root");
160:
161: EasyMock.verify(creatorManager);
162:
163: assertNotNull(result);
164: assertTrue(result.indexOf("creatorName") != -1);
165: assertTrue(result.indexOf(TestCreatedObject.class.getName()) != -1);
166: }
167: }
|