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.window;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022:
023: import junit.framework.Assert;
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027:
028: import org.apache.jetspeed.HashMapWindowCache;
029: import org.apache.jetspeed.PortletFactoryMock;
030: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
031: import org.apache.jetspeed.container.window.PortletWindowAccessor;
032: import org.apache.jetspeed.container.window.impl.PortletWindowAccessorImpl;
033: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
034: import org.apache.jetspeed.om.page.ContentFragment;
035: import org.apache.jetspeed.om.page.Fragment;
036: import org.apache.jetspeed.om.page.ContentFragmentImpl;
037: import org.apache.jetspeed.util.JetspeedObjectID;
038: import org.apache.pluto.om.window.PortletWindow;
039: import org.apache.pluto.om.window.PortletWindowList;
040: import org.apache.pluto.om.window.PortletWindowListCtrl;
041: import org.jmock.Mock;
042: import org.jmock.core.Invocation;
043: import org.jmock.core.InvocationMatcher;
044: import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;
045: import org.jmock.core.matcher.InvokeOnceMatcher;
046: import org.jmock.core.stub.CustomStub;
047: import org.jmock.core.stub.ReturnStub;
048:
049: /**
050: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
051: *
052: */
053: public class TestWindows extends TestCase {
054: protected PortletWindowAccessor windowAccess;
055: protected Mock fragMock;
056: protected Mock entityAccessMock;
057: protected Mock entityMock;
058: protected Mock windowListMock;
059:
060: public static Test suite() {
061: // All methods starting with "test" will be executed in the test suite.
062: return new TestSuite(TestWindows.class);
063: }
064:
065: /**
066: * <p>
067: * setUp
068: * </p>
069: *
070: * @see junit.framework.TestCase#setUp()
071: * @throws java.lang.Exception
072: */
073: protected void setUp() throws Exception {
074: super .setUp();
075: entityAccessMock = new Mock(PortletEntityAccessComponent.class);
076: fragMock = new Mock(Fragment.class);
077: entityMock = new Mock(MutablePortletEntity.class);
078: windowListMock = new Mock(CompositeWindowList.class);
079: windowAccess = new PortletWindowAccessorImpl(
080: (PortletEntityAccessComponent) entityAccessMock.proxy(),
081: PortletFactoryMock.instance, new HashMapWindowCache(),
082: true);
083: }
084:
085: public void testWindowAccess() throws Exception {
086: List windows = new ArrayList();
087: ContentFragment f1 = new ContentFragmentImpl(
088: (Fragment) fragMock.proxy(), new HashMap());
089: MutablePortletEntity entity = (MutablePortletEntity) entityMock
090: .proxy();
091: CompositeWindowList windowList = (CompositeWindowList) windowListMock
092: .proxy();
093: entityAccessMock.expects(new InvokeAtLeastOnceMatcher())
094: .method("getPortletEntityForFragment")
095: .withAnyArguments().will(new ReturnStub(entity));
096: fragMock.expects(new InvokeAtLeastOnceMatcher())
097: .method("getId").withNoArguments().will(
098: new ReturnStub("frag1"));
099: entityMock.expects(new InvokeAtLeastOnceMatcher()).method(
100: "getPortletWindowList").withNoArguments().will(
101: new ReturnStub(windowList));
102: entityMock.expects(new InvokeAtLeastOnceMatcher()).method(
103: "getId").withNoArguments().will(
104: new ReturnStub(new JetspeedObjectID("entity1")));
105:
106: windowListMock.expects(new InvokeCountMatcher(4)).method("add")
107: .withAnyArguments().will(new ListAppendStub(windows));
108:
109: PortletWindow window = windowAccess.getPortletWindow(f1);
110: assertNotNull(window);
111: assertEquals("frag1", window.getId().toString());
112:
113: // Make sure the portlet entity's window list got updated
114: assertEquals(1, windows.size());
115:
116: PortletWindow windowInList = (PortletWindow) windows.get(0);
117:
118: // The window in the entities list should be the same as the one
119: // returned by getPortletWindow(f1)
120: assertEquals(windowInList, window);
121:
122: // remove the window
123: windowAccess.removeWindow(window);
124:
125: // Calling this after a remove go through th procedure of adding a newly
126: // created window
127: // back the portlet entity's list. We check this through vefirying calls
128: // to our mocks
129: windowAccess.getPortletWindow(f1);
130:
131: // Test same remove but via entity
132: windowAccess.removeWindow(window);
133:
134: assertNotNull(windowAccess.getPortletWindow(f1));
135:
136: windowListMock.expects(new InvokeOnceMatcher()).method(
137: "iterator").withNoArguments().will(
138: new ReturnStub(windows.iterator()));
139:
140: /*
141: windowAccess.removeWindows(entity);
142:
143: windowAccess.getPortletWindow(f1);
144: // Double that second call bypasses creating a new window
145: //windowAccess.getPortletWindow(f1);
146:
147: windowListMock.verify();
148: */
149: }
150:
151: interface CompositeWindowList extends PortletWindowList,
152: PortletWindowListCtrl {
153:
154: }
155:
156: class ListAppendStub extends CustomStub {
157:
158: List list;
159:
160: /**
161: * @param arg0
162: */
163: public ListAppendStub(List list) {
164: super ("Appends object to a list");
165: this .list = list;
166: }
167:
168: /**
169: * <p>
170: * invoke
171: * </p>
172: *
173: * @see org.jmock.core.Stub#invoke(org.jmock.core.Invocation)
174: * @param arg0
175: * @return @throws
176: * java.lang.Throwable
177: */
178: public Object invoke(Invocation invocation) throws Throwable {
179: list.add(invocation.parameterValues.get(0));
180: return null;
181: }
182: }
183:
184: /**
185: * Inline copy of InvokeCountMatcher from latest jMock Development Snapshot: 20050628-175146
186: * so we don't need to depend on their SNAPSHOT release anymore but can fallback on their 1.0.1 version.
187: * (doesn't seem they are going to release a new real version soon as it has been ages since 1.0.1 came out)
188: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
189: *
190: */
191: private static class InvokeCountMatcher implements
192: InvocationMatcher {
193: private int invocationCount = 0;
194:
195: private int expectedCount;
196:
197: public InvokeCountMatcher(int expectedCount) {
198: this .expectedCount = expectedCount;
199: }
200:
201: public boolean matches(Invocation invocation) {
202: return getInvocationCount() < expectedCount;
203: }
204:
205: public void verify() {
206: verifyHasBeenInvokedExactly(expectedCount);
207: }
208:
209: public boolean hasDescription() {
210: return true;
211: }
212:
213: public StringBuffer describeTo(StringBuffer buffer) {
214: return buffer.append("expected ").append(expectedCount)
215: .append(" times, invoked ").append(
216: getInvocationCount()).append(" times");
217: }
218:
219: public int getInvocationCount() {
220: return invocationCount;
221: }
222:
223: public boolean hasBeenInvoked() {
224: return invocationCount > 0;
225: }
226:
227: public void invoked(Invocation invocation) {
228: invocationCount++;
229: }
230:
231: public void verifyHasBeenInvoked() {
232: Assert.assertTrue("expected method was not invoked",
233: hasBeenInvoked());
234: }
235:
236: public void verifyHasBeenInvokedExactly(int expectedCount) {
237: Assert.assertTrue(
238: "expected method was not invoked the expected number of times: expected "
239: + expectedCount + " times, was invoked "
240: + invocationCount + " times",
241: invocationCount == expectedCount);
242: }
243:
244: }
245: }
|