001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout;
018:
019: import java.io.File;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.jetspeed.components.ComponentManager;
024: import org.apache.jetspeed.components.SpringComponentManager;
025: import org.apache.jetspeed.components.factorybeans.ServletConfigFactoryBean;
026: import org.apache.jetspeed.layout.impl.LayoutValve;
027: import org.apache.jetspeed.mocks.ResourceLocatingServletContext;
028: import org.apache.jetspeed.pipeline.PipelineException;
029: import org.apache.jetspeed.request.RequestContext;
030: import com.mockrunner.mock.web.MockServletConfig;
031:
032: /**
033: * Test for Fragment placement
034: *
035: * @author <a>David Gurney </a>
036: * @version $Id: $
037: */
038: public class TestLayout extends TestCase {
039:
040: private ComponentManager cm;
041:
042: private LayoutValve valve;
043:
044: public static void main(String[] args) {
045: junit.swingui.TestRunner.run(TestLayout.class);
046: }
047:
048: /**
049: * Setup the request context
050: */
051: protected void setUp() throws Exception {
052: super .setUp();
053:
054: String appRoot = "./"; //PortalTestConstants.JETSPEED_APPLICATION_ROOT;
055:
056: MockServletConfig servletConfig = new MockServletConfig();
057: ResourceLocatingServletContext servletContent = new ResourceLocatingServletContext(
058: new File(appRoot));
059: servletConfig.setServletContext(servletContent);
060: ServletConfigFactoryBean.setServletConfig(servletConfig);
061:
062: // Load the Spring configs
063: String[] bootConfigs = null;
064: String[] appConfigs = { //"src/webapp/WEB-INF/assembly/layout-api.xml",
065: "src/test/resources/assembly/test-layout-api.xml" };
066:
067: cm = new SpringComponentManager(bootConfigs, appConfigs,
068: servletContent, ".");
069: cm.start();
070: valve = (LayoutValve) cm.getComponent("layoutValve");
071: }
072:
073: protected void tearDown() throws Exception {
074: cm.stop();
075: }
076:
077: public void testNullRequestContext() {
078: // Get the layout that has a null request context
079: try {
080: valve.invoke(null, null);
081: TestLayout.fail("should have thrown an exception");
082: } catch (PipelineException e) {
083: TestLayout
084: .assertTrue("detected null request context", true);
085: }
086: }
087:
088: public void testNullParameters() {
089: try {
090: // Test the success case
091: RequestContext rc = FragmentUtil.setupRequestContext(null,
092: "1234", "0", "0");
093: valve.invoke(rc, null);
094: TestLayout.assertTrue("couldn't find value", FragmentUtil
095: .findValue(rc, "failure"));
096: } catch (PipelineException e) {
097: TestLayout.fail("unexpected exception");
098: }
099:
100: try {
101: // Test the success case
102: RequestContext rc = FragmentUtil.setupRequestContext(
103: "moveabs", "33", "0", "0");
104: valve.invoke(rc, null);
105:
106: // Take a look at the response to verify a failiure
107: TestLayout.assertTrue("couldn't find value", FragmentUtil
108: .findValue(rc, "failure"));
109: } catch (PipelineException e) {
110: TestLayout.fail("unexpected exception");
111: }
112:
113: try {
114: // Test the success case
115: RequestContext rc = FragmentUtil.setupRequestContext(
116: "moveabs", "1234", null, "0");
117: valve.invoke(rc, null);
118:
119: // Take a look at the response to verify a failiure
120: TestLayout.assertTrue("couldn't find value", FragmentUtil
121: .findValue(rc, "failure"));
122: } catch (PipelineException e) {
123: TestLayout.fail("unexpected exception");
124: }
125:
126: try {
127: // Test the success case
128: RequestContext rc = FragmentUtil.setupRequestContext(
129: "moveabs", "1234", "0", null);
130: valve.invoke(rc, null);
131:
132: // Take a look at the response to verify a failiure
133: TestLayout.assertTrue("couldn't find value", FragmentUtil
134: .findValue(rc, "failure"));
135: } catch (PipelineException e) {
136: TestLayout.fail("unexpected exception");
137: }
138: }
139:
140: public void testEasy() {
141: moveSuccess("moveabs", "1", "0", "0", "0", "1", "0", "1"); // Move down
142: moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0"); // Straight across
143: }
144:
145: public void testMoveSuccess() {
146: moveSuccess("moveabs", "1", "0", "0", "0", "0", "-1", "-1"); // Doesn't
147: // really
148: // move
149: moveSuccess("moveabs", "1", "0", "0", "0", "1", "0", "1"); // Move down
150:
151: moveSuccess("moveabs", "2", "0", "1", "0", "0", "0", "0"); // Move up
152: moveSuccess("moveabs", "1", "0", "0", "1", "0", "1", "0"); // Move
153: // right
154: moveSuccess("moveabs", "3", "1", "0", "0", "0", "0", "0"); // Move left
155: moveSuccess("moveabs", "2", "0", "1", "1", "2", "1", "2"); // Move
156: // right &
157: // move down
158: moveSuccess("moveabs", "3", "1", "0", "0", "1", "0", "1"); // Move left
159: // & move
160: // down
161: moveSuccess("moveabs", "4", "1", "1", "0", "0", "0", "0"); // Move left
162: // & move up
163:
164: moveSuccess("moveabs", "1", "0", "0", "0", "2", "0", "1"); // Move too
165: // far down,
166: // should be
167: // at end of
168: // row
169: moveSuccess("moveabs", "2", "0", "1", "0", "2", "-1", "-1"); // Move too
170: // far down,
171: // shouldn't
172: // move
173: moveSuccess("moveabs", "3", "1", "0", "1", "3", "1", "2"); // Move too
174: // far down,
175: // should be
176: // at end of
177: // row
178: moveSuccess("moveabs", "4", "1", "1", "1", "3", "1", "2"); // Move too
179: // far down,
180: // should be
181: // at end of
182: // row
183: moveSuccess("moveabs", "5", "1", "2", "1", "3", "-1", "-1"); // Move too
184: // far down,
185: // shouldn't
186: // move
187: moveSuccess("moveabs", "1", "0", "0", "1", "4", "1", "3"); // Move too
188: // far down,
189: // should be
190: // at end of
191: // row
192: moveSuccess("moveabs", "2", "0", "1", "1", "4", "1", "3"); // Move too
193: // far down,
194: // should be
195: // at end of
196: // row
197: moveSuccess("moveleft", "1", "0", "0", "0", "0", "-1", "-1"); // Shouldn't
198: // move
199: // Root layout ("6") shouldn't/cannot be moved, so the following test doesn't make sense
200: // moveSuccess("moveleft", "6", "0", "0", "0", "0", "-1", "-1"); // Shouldn't
201: // move
202: moveSuccess("moveleft", "3", "1", "0", "0", "0", "0", "0"); // Straight
203: // across
204: moveSuccess("moveleft", "4", "1", "1", "0", "1", "0", "1"); // Straight
205: // across
206: moveSuccess("moveleft", "5", "1", "2", "0", "2", "0", "2"); // Straight
207: // across
208:
209: moveSuccess("moveright", "1", "0", "0", "1", "0", "1", "0"); // Straight
210: // across
211: moveSuccess("moveright", "2", "0", "1", "1", "1", "1", "1"); // Straight
212: // across
213: moveSuccess("moveright", "3", "1", "0", "2", "0", "-1", "-1"); // Shouldn't
214: // move
215: moveSuccess("moveright", "4", "1", "1", "2", "0", "-1", "-1"); // Shouldn't
216: // move
217: moveSuccess("moveright", "5", "1", "2", "2", "0", "-1", "-1"); // Shouldn't
218: // move
219:
220: moveSuccess("moveup", "2", "0", "1", "0", "0", "0", "0"); // Straight
221: // across
222: moveSuccess("moveup", "4", "1", "1", "1", "0", "1", "0"); // Straight
223: // across
224: moveSuccess("moveup", "5", "1", "2", "1", "1", "1", "1"); // Straight
225: // across
226:
227: moveSuccess("movedown", "1", "0", "0", "0", "1", "0", "1"); // Straight
228: // across
229: moveSuccess("movedown", "2", "0", "1", "0", "1", "-1", "-1"); // Shouldn't
230: // move
231: moveSuccess("movedown", "3", "1", "0", "1", "1", "1", "1"); // Straight
232: // across
233: moveSuccess("movedown", "4", "1", "1", "1", "2", "1", "2"); // Straight
234: // across
235: moveSuccess("movedown", "5", "1", "2", "1", "2", "-1", "-1"); // Shouldn't
236: // move
237: }
238:
239: public void testMoveFailure() {
240: moveFailure("moveabs", "bogus", "0", "0", "0", "0"); // non integer
241: // portlet id
242: moveFailure("moveleft", "0", "0", "0", "0", "0"); // portlet not found
243: // moveFailure("moveabs", "1", "0", "0", "3", "0"); // non existent
244: // column
245: moveFailure("bogus", "0", "0", "0", "0", "0"); // bogus action
246: moveFailure("moveabs", "1", "0", "0", "a", "0"); // non integer value
247: moveFailure("moveabs", "1", "0", "0", "0", "b"); // non integer value
248: }
249:
250: public void moveSuccess(String a_sMoveType, String p_sPortletId,
251: String p_sOldCol, String p_sOldRow, String p_sNewCol,
252: String p_sNewRow, String p_sExpectedNewCol,
253: String p_sExpectedNewRow) {
254: try {
255: // Test the success case
256: RequestContext rc = null;
257:
258: if (a_sMoveType.equalsIgnoreCase("moveabs")) {
259: rc = FragmentUtil.setupRequestContext(a_sMoveType,
260: p_sPortletId, p_sNewCol, p_sNewRow);
261: } else {
262: rc = FragmentUtil.setupRequestContext(a_sMoveType,
263: p_sPortletId, null, null);
264: }
265:
266: valve.invoke(rc, null);
267:
268: // Take a look at the response to verify a failiure
269: TestLayout.assertTrue("couldn't find value", FragmentUtil
270: .findValue(rc, "success"));
271: TestLayout.assertTrue("couldn't find value", FragmentUtil
272: .findValue(rc, "<js>"));
273: TestLayout.assertTrue("couldn't find value", FragmentUtil
274: .findValue(rc, "<status>success</status>"));
275: TestLayout.assertTrue("couldn't find value", FragmentUtil
276: .findValue(rc, "<action>" + a_sMoveType
277: + "</action>"));
278: TestLayout.assertTrue("couldn't find value", FragmentUtil
279: .findValue(rc, "<id>" + p_sPortletId + "</id>"));
280: TestLayout.assertTrue("couldn't find value", FragmentUtil
281: .findValue(rc, "<old_position><col>" + p_sOldCol
282: + "</col><row>" + p_sOldRow
283: + "</row></old_position>"));
284: TestLayout.assertTrue("couldn't find value", FragmentUtil
285: .findValue(rc, "<new_position><col>"
286: + p_sExpectedNewCol + "</col><row>"
287: + p_sExpectedNewRow
288: + "</row></new_position>"));
289: TestLayout.assertTrue("couldn't find value", FragmentUtil
290: .findValue(rc, "</js>"));
291: } catch (PipelineException e) {
292: e.printStackTrace();
293: TestLayout.fail("layout valve failed");
294: }
295: }
296:
297: public void moveFailure(String a_sMoveType, String p_sPortletId,
298: String p_sOldCol, String p_sOldRow, String p_sNewCol,
299: String p_sNewRow) {
300: try {
301: // Test failure case
302: RequestContext rc = null;
303:
304: if (a_sMoveType.equalsIgnoreCase("moveabs")) {
305: rc = FragmentUtil.setupRequestContext(a_sMoveType,
306: p_sPortletId, p_sNewCol, p_sNewRow);
307: } else {
308: rc = FragmentUtil.setupRequestContext(a_sMoveType,
309: p_sPortletId, null, null);
310: }
311: valve.invoke(rc, null);
312:
313: //FragmentUtil.debugContentOutput(rc);
314:
315: // Take a look at the response to verify a failure
316: TestLayout.assertTrue("couldn't find value", FragmentUtil
317: .findValue(rc, "<status>failure</status>"));
318: } catch (PipelineException p) {
319: TestLayout.fail("unexpected exception");
320: }
321:
322: try {
323: // Test failure case
324: RequestContext rc = null;
325:
326: if (a_sMoveType.equalsIgnoreCase("moveabs")) {
327: rc = FragmentUtil.setupRequestContext(a_sMoveType,
328: "1234", "0", "foo");
329: } else {
330: rc = FragmentUtil.setupRequestContext(a_sMoveType,
331: "1234", null, null);
332: }
333:
334: valve.invoke(rc, null);
335:
336: // Take a look at the response to verify a failiure
337: TestLayout.assertTrue("couldn't find value", FragmentUtil
338: .findValue(rc, "<status>failure</status>"));
339: } catch (PipelineException p) {
340: TestLayout.fail("unexpected exception");
341: }
342: }
343:
344: public void testRemove() {
345: remove("1");
346: remove("2");
347: remove("3");
348: remove("4");
349: }
350:
351: private void remove(String p_sPortletId) {
352: try {
353: // Test the success case
354: RequestContext rc = null;
355:
356: rc = FragmentUtil.setupRequestContext("remove",
357: p_sPortletId, null, null);
358:
359: valve.invoke(rc, null);
360:
361: // Take a look at the response to verify a failiure
362: TestLayout.assertTrue("couldn't find value", FragmentUtil
363: .findValue(rc, "success"));
364: TestLayout.assertTrue("couldn't find value", FragmentUtil
365: .findValue(rc, "<js>"));
366: TestLayout.assertTrue("couldn't find value", FragmentUtil
367: .findValue(rc, "<status>success</status>"));
368: TestLayout
369: .assertTrue("couldn't find value", FragmentUtil
370: .findValue(rc, "<action>" + "remove"
371: + "</action>"));
372: TestLayout.assertTrue("couldn't find value", FragmentUtil
373: .findValue(rc, "<id>" + p_sPortletId + "</id>"));
374: TestLayout.assertTrue("couldn't find value", FragmentUtil
375: .findValue(rc, "<old_position>"));
376: TestLayout.assertTrue("couldn't find value", FragmentUtil
377: .findValue(rc, "<col>"));
378: TestLayout.assertTrue("couldn't find value", FragmentUtil
379: .findValue(rc, "<row>"));
380: TestLayout.assertTrue("couldn't find value", FragmentUtil
381: .findValue(rc, "</old_position>"));
382: TestLayout.assertTrue("couldn't find value", FragmentUtil
383: .findValue(rc, "</js>"));
384: } catch (PipelineException e) {
385: e.printStackTrace();
386: TestLayout.fail("layout valve failed");
387: }
388:
389: }
390: }
|