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 junit.framework.TestCase;
020:
021: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
022: import org.apache.jetspeed.layout.impl.CoordinateImpl;
023: import org.apache.jetspeed.layout.impl.PortletPlacementContextImpl;
024: import org.apache.jetspeed.om.page.Fragment;
025: import org.apache.jetspeed.request.RequestContext;
026:
027: /**
028: * Test for Fragment placement
029: *
030: * @author <a>David Gurney </a>
031: * @version $Id: $
032: */
033: public class TestPortletPlacement extends TestCase {
034: private PortletRegistry portletRegistry;
035:
036: public void setUp() {
037: portletRegistry = MockPortletRegistryFactory
038: .createMockPortletRegistry();
039: }
040:
041: public void testGetFragmentAt() {
042: // Build a request object and populate it with fragments
043: RequestContext requestContext = FragmentUtil
044: .buildFullRequestContext();
045:
046: try {
047: PortletPlacementContext ppc = new PortletPlacementContextImpl(
048: requestContext.getPage(), portletRegistry);
049: int a_iNumCols = ppc.getNumberColumns();
050: assertEquals(a_iNumCols, 2);
051:
052: int a_iNumRows = ppc.getNumberRows(0);
053: assertEquals(a_iNumRows, 2);
054:
055: a_iNumRows = ppc.getNumberRows(1);
056: assertEquals(a_iNumRows, 3);
057:
058: // Check the fragments
059: Fragment a_oFrag = ppc
060: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
061: 0, 0, 0));
062: assertNotNull("null fragment found at 0,0", a_oFrag);
063: assertEquals(a_oFrag.getId(), "1");
064: assertEquals(a_oFrag.getName(), "frag1");
065:
066: a_oFrag = ppc
067: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
068: 1, 0, 1));
069: assertNotNull("null fragment found at 0,1", a_oFrag);
070: assertEquals(a_oFrag.getId(), "2");
071: assertEquals(a_oFrag.getName(), "frag2");
072:
073: a_oFrag = ppc
074: .getFragmentAtNewCoordinate(new CoordinateImpl(1,
075: 0, 1, 0));
076: assertNotNull("null fragment found at 1,0", a_oFrag);
077: assertEquals(a_oFrag.getId(), "3");
078: assertEquals(a_oFrag.getName(), "frag3");
079:
080: a_oFrag = ppc
081: .getFragmentAtNewCoordinate(new CoordinateImpl(1,
082: 1, 1, 1));
083: assertNotNull("null fragment found at 1,1", a_oFrag);
084: assertEquals(a_oFrag.getId(), "4");
085: assertEquals(a_oFrag.getName(), "frag4");
086:
087: a_oFrag = ppc
088: .getFragmentAtNewCoordinate(new CoordinateImpl(1,
089: 2, 1, 2));
090: assertNotNull("null fragment found at 1,2", a_oFrag);
091: assertEquals(a_oFrag.getId(), "5");
092: assertEquals(a_oFrag.getName(), "frag5");
093:
094: } catch (PortletPlacementException e) {
095: fail("creating the PortletPlacementManager failed");
096: }
097: }
098:
099: public void testGetFragmentById() {
100: // Build a request object and populate it with fragments
101: RequestContext requestContext = FragmentUtil
102: .buildFullRequestContext();
103:
104: try {
105: PortletPlacementContext ppc = new PortletPlacementContextImpl(
106: requestContext.getPage(), portletRegistry);
107:
108: // Check the fragments
109: Fragment a_oFrag = ppc.getFragmentById("1");
110: assertNotNull("null fragment with id 1", a_oFrag);
111: assertEquals(a_oFrag.getId(), "1");
112: assertEquals(a_oFrag.getName(), "frag1");
113:
114: a_oFrag = ppc.getFragmentById("2");
115: assertNotNull("null fragment with id 2", a_oFrag);
116: assertEquals(a_oFrag.getId(), "2");
117: assertEquals(a_oFrag.getName(), "frag2");
118:
119: a_oFrag = ppc.getFragmentById("3");
120: assertNotNull("null fragment with id 3", a_oFrag);
121: assertEquals(a_oFrag.getId(), "3");
122: assertEquals(a_oFrag.getName(), "frag3");
123:
124: a_oFrag = ppc.getFragmentById("4");
125: assertNotNull("null fragment with id 4", a_oFrag);
126: assertEquals(a_oFrag.getId(), "4");
127: assertEquals(a_oFrag.getName(), "frag4");
128:
129: a_oFrag = ppc.getFragmentById("5");
130: assertNotNull("null fragment with id 5", a_oFrag);
131: assertEquals(a_oFrag.getId(), "5");
132: assertEquals(a_oFrag.getName(), "frag5");
133:
134: } catch (PortletPlacementException e) {
135: fail("creating the PortletPlacementManager failed");
136: }
137: }
138:
139: public void testRemoveFragment() {
140: RequestContext requestContext = FragmentUtil
141: .buildFullRequestContext();
142:
143: try {
144: PortletPlacementContext ppc = new PortletPlacementContextImpl(
145: requestContext.getPage(), portletRegistry);
146:
147: Fragment a_oFrag = ppc
148: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
149: 0, 0, 0));
150:
151: Coordinate a_oCoordinate = ppc.remove(a_oFrag);
152:
153: assertEquals(a_oCoordinate.getOldCol(), 0);
154: assertEquals(a_oCoordinate.getOldRow(), 0);
155:
156: // Should be the second fragment now that the first has been deleted
157: a_oFrag = ppc
158: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
159: 0, 0, 0));
160: assertEquals(a_oFrag.getId(), "2");
161: assertEquals(a_oFrag.getName(), "frag2");
162: } catch (PortletPlacementException e) {
163: fail("creating the PortletPlacementManager failed");
164: }
165: }
166:
167: public void testFragmentMoveabs() {
168: RequestContext requestContext = FragmentUtil
169: .buildFullRequestContext();
170:
171: try {
172: PortletPlacementContext ppc = new PortletPlacementContextImpl(
173: requestContext.getPage(), portletRegistry);
174:
175: Fragment a_oFrag = ppc
176: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
177: 0, 0, 0));
178:
179: Coordinate a_oCoordinate = ppc.moveAbsolute(a_oFrag,
180: new CoordinateImpl(0, 0, 0, 1));
181:
182: assertEquals(a_oCoordinate.getOldCol(), 0);
183: assertEquals(a_oCoordinate.getOldRow(), 0);
184: assertEquals(a_oCoordinate.getNewCol(), 0);
185: assertEquals(a_oCoordinate.getNewRow(), 1);
186:
187: // Should be the second fragment now that the first has been moved
188: a_oFrag = ppc
189: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
190: 0, 0, 0));
191: assertEquals(a_oFrag.getId(), "2");
192: assertEquals(a_oFrag.getName(), "frag2");
193:
194: a_oFrag = ppc
195: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
196: 0, 0, 1));
197: assertEquals(a_oFrag.getId(), "1");
198: assertEquals(a_oFrag.getName(), "frag1");
199: } catch (PortletPlacementException e) {
200: fail("creating the PortletPlacementManager failed");
201: }
202: }
203:
204: public void testFragmentMoveUp() {
205: RequestContext requestContext = FragmentUtil
206: .buildFullRequestContext();
207:
208: try {
209: PortletPlacementContext ppc = new PortletPlacementContextImpl(
210: requestContext.getPage(), portletRegistry);
211:
212: Fragment a_oFrag = ppc
213: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
214: 0, 0, 1));
215:
216: Coordinate a_oCoordinate = ppc.moveUp(a_oFrag);
217:
218: assertEquals(a_oCoordinate.getOldCol(), 0);
219: assertEquals(a_oCoordinate.getOldRow(), 1);
220: assertEquals(a_oCoordinate.getNewCol(), 0);
221: assertEquals(a_oCoordinate.getNewRow(), 0);
222:
223: // Should be the second fragment since it was moved up
224: a_oFrag = ppc
225: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
226: 0, 0, 0));
227: assertEquals(a_oFrag.getId(), "2");
228: assertEquals(a_oFrag.getName(), "frag2");
229:
230: a_oFrag = ppc
231: .getFragmentAtNewCoordinate(new CoordinateImpl(0,
232: 0, 0, 1));
233: assertEquals(a_oFrag.getId(), "1");
234: assertEquals(a_oFrag.getName(), "frag1");
235: } catch (PortletPlacementException e) {
236: fail("creating the PortletPlacementManager failed");
237: }
238: }
239:
240: }
|