001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with this
004: * work for additional information regarding copyright ownership. The ASF
005: * licenses this file to You under the Apache License, Version 2.0 (the
006: * "License"); you may not use this file except in compliance with the License.
007: * 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, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package org.apache.jetspeed.cache;
018:
019: import java.io.Serializable;
020: import java.util.Arrays;
021: import java.util.List;
022: import java.util.Set;
023:
024: import net.sf.ehcache.Ehcache;
025: import net.sf.ehcache.Element;
026:
027: import org.apache.jetspeed.cache.impl.EhCacheImpl;
028: import org.apache.jetspeed.cache.impl.EhPortletWindowCache;
029: import org.apache.pluto.om.common.ObjectID;
030: import org.apache.pluto.om.entity.PortletEntity;
031: import org.apache.pluto.om.window.PortletWindow;
032: import org.jmock.Mock;
033: import org.jmock.cglib.MockObjectTestCase;
034: import org.jmock.core.stub.VoidStub;
035:
036: /**
037: *
038: * Tests for {@link EhPortletWindowCache}.
039: *
040: * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
041: *
042: */
043: public class TestPortletWindowCache extends MockObjectTestCase {
044: private static final String WINDOW_ID = "window1";
045: private static final String ENTITY_ID = "entity1";
046:
047: private Mock cacheMock;
048: private Mock windowMock;
049: private Mock entityMock;
050: private Mock oidMock;
051: private Mock entityOidMock;
052:
053: protected void setUp() throws Exception {
054: super .setUp();
055: cacheMock = mock(Ehcache.class);
056: windowMock = mock(SerializablePortletWindow.class);
057: entityMock = mock(PortletEntity.class);
058: oidMock = mock(ObjectID.class);
059: entityOidMock = (Mock) mock(ObjectID.class);
060: }
061:
062: public void testSimplePutAndGet() {
063:
064: PortletWindow window = (PortletWindow) windowMock.proxy();
065: Element element = new Element(WINDOW_ID, window);
066: ObjectID oid = (ObjectID) oidMock.proxy();
067: ObjectID entityOid = (ObjectID) entityOidMock.proxy();
068: entityOidMock.expects(atLeastOnce()).method("toString").will(
069: returnValue(ENTITY_ID));
070: oidMock.expects(atLeastOnce()).method("toString").will(
071: returnValue(WINDOW_ID));
072: windowMock.expects(once()).method("getId").withNoArguments()
073: .will(returnValue(oid));
074: windowMock.expects(once()).method("getPortletEntity")
075: .withNoArguments()
076: .will(returnValue(entityMock.proxy()));
077: entityMock.expects(once()).method("getId").withNoArguments()
078: .will(returnValue(entityOid));
079: cacheMock.expects(once()).method("put").with(eq(element));
080: cacheMock.expects(atLeastOnce()).method("get").with(
081: eq(WINDOW_ID)).will(returnValue(element));
082:
083: Ehcache cache = (Ehcache) cacheMock.proxy();
084: PortletWindowCache windowCache = new EhPortletWindowCache(cache);
085: windowCache.putPortletWindow(window);
086:
087: assertNotNull(windowCache.getPortletWindow(WINDOW_ID));
088: assertEquals(windowCache.getPortletWindow(WINDOW_ID), window);
089:
090: verify();
091: }
092:
093: public void testGetByPortletEntity() {
094:
095: SerializablePortletWindow window = (SerializablePortletWindow) windowMock
096: .proxy();
097: Element element = new Element(WINDOW_ID, window);
098:
099: ObjectID oid = (ObjectID) oidMock.proxy();
100: oidMock.expects(atLeastOnce()).method("toString").will(
101: returnValue(WINDOW_ID));
102: ObjectID entityOid = (ObjectID) entityOidMock.proxy();
103: entityOidMock.expects(atLeastOnce()).method("toString").will(
104: returnValue(ENTITY_ID));
105: cacheMock.expects(once()).method("put").with(eq(element));
106: cacheMock.expects(once()).method("get").with(eq(WINDOW_ID))
107: .will(returnValue(element));
108: windowMock.expects(once()).method("getId").withNoArguments()
109: .will(returnValue(oid));
110: windowMock.expects(once()).method("getPortletEntity")
111: .withNoArguments()
112: .will(returnValue(entityMock.proxy()));
113: entityMock.expects(once()).method("getId").withNoArguments()
114: .will(returnValue(entityOid));
115:
116: Ehcache cache = (Ehcache) cacheMock.proxy();
117: PortletWindowCache windowCache = new EhPortletWindowCache(cache);
118: windowCache.putPortletWindow(window);
119:
120: PortletWindow fromCache = windowCache
121: .getPortletWindowByEntityId(ENTITY_ID);
122: assertNotNull(fromCache);
123:
124: verify();
125: }
126:
127: public void testRemove() {
128: SerializablePortletWindow window = (SerializablePortletWindow) windowMock
129: .proxy();
130: Element element = new Element(WINDOW_ID, window);
131:
132: ObjectID oid = (ObjectID) oidMock.proxy();
133: oidMock.expects(atLeastOnce()).method("toString").will(
134: returnValue(WINDOW_ID));
135:
136: ObjectID entityOid = (ObjectID) entityOidMock.proxy();
137: entityOidMock.expects(atLeastOnce()).method("toString").will(
138: returnValue(ENTITY_ID));
139:
140: cacheMock.expects(once()).method("put").with(eq(element));
141: cacheMock.expects(exactly(2)).method("get").with(eq(WINDOW_ID))
142: .will(returnValue(element));
143: windowMock.expects(once()).method("getId").withNoArguments()
144: .will(returnValue(oid));
145: windowMock.expects(exactly(2)).method("getPortletEntity")
146: .withNoArguments()
147: .will(returnValue(entityMock.proxy()));
148: entityMock.expects(exactly(2)).method("getId")
149: .withNoArguments().will(returnValue(entityOid));
150:
151: cacheMock.expects(once()).method("removeQuiet").with(
152: eq(WINDOW_ID)).will(returnValue(true));
153:
154: Ehcache cache = (Ehcache) cacheMock.proxy();
155: PortletWindowCache windowCache = new EhPortletWindowCache(cache);
156: windowCache.putPortletWindow(window);
157:
158: windowCache.removePortletWindow(WINDOW_ID);
159: assertNull(windowCache.getPortletWindowByEntityId(ENTITY_ID));
160:
161: verify();
162: }
163:
164: public void testRemoveByEntityId() {
165: SerializablePortletWindow window = (SerializablePortletWindow) windowMock
166: .proxy();
167: Element element = new Element(WINDOW_ID, window);
168:
169: ObjectID oid = (ObjectID) oidMock.proxy();
170: oidMock.expects(atLeastOnce()).method("toString").will(
171: returnValue(WINDOW_ID));
172:
173: ObjectID entityOid = (ObjectID) entityOidMock.proxy();
174: entityOidMock.expects(atLeastOnce()).method("toString").will(
175: returnValue(ENTITY_ID));
176:
177: cacheMock.expects(once()).method("put").with(eq(element));
178: cacheMock.expects(exactly(3)).method("get").with(eq(WINDOW_ID))
179: .will(
180: onConsecutiveCalls(returnValue(element),
181: returnValue(element), new VoidStub()));
182: windowMock.expects(exactly(2)).method("getId")
183: .withNoArguments().will(returnValue(oid));
184: windowMock.expects(once()).method("getPortletEntity")
185: .withNoArguments()
186: .will(returnValue(entityMock.proxy()));
187: entityMock.expects(once()).method("getId").withNoArguments()
188: .will(returnValue(entityOid));
189:
190: cacheMock.expects(atLeastOnce()).method("removeQuiet").with(
191: eq(WINDOW_ID)).will(returnValue(true));
192:
193: Ehcache cache = (Ehcache) cacheMock.proxy();
194: PortletWindowCache windowCache = new EhPortletWindowCache(cache);
195: windowCache.putPortletWindow(window);
196:
197: windowCache.removePortletWindowByPortletEntityId(ENTITY_ID);
198: assertNull(windowCache.getPortletWindow(WINDOW_ID));
199:
200: verify();
201: }
202:
203: public void testGetAllPortletWindows() {
204: PortletWindow window = (PortletWindow) windowMock.proxy();
205: PortletWindow window2 = (PortletWindow) mock(
206: SerializablePortletWindow.class).proxy();
207: PortletWindow window3 = (PortletWindow) mock(
208: SerializablePortletWindow.class).proxy();
209:
210: List keys = Arrays.asList(new String[] { WINDOW_ID, "window2",
211: "window3" });
212:
213: cacheMock.expects(once()).method("getKeys").withNoArguments()
214: .will(returnValue(keys));
215: cacheMock.expects(once()).method("get").with(eq(WINDOW_ID))
216: .will(returnValue(new Element(WINDOW_ID, window)));
217: cacheMock.expects(once()).method("get").with(eq("window2"))
218: .will(returnValue(new Element("window2", window2)));
219: cacheMock.expects(once()).method("get").with(eq("window3"))
220: .will(returnValue(new Element("window3", window3)));
221:
222: PortletWindowCache windowCache = new EhPortletWindowCache(
223: (Ehcache) cacheMock.proxy());
224:
225: Set allPortletWindows = windowCache.getAllPortletWindows();
226: assertNotNull(allPortletWindows);
227: assertEquals(3, allPortletWindows.size());
228: }
229:
230: public void testUnexpected() {
231: // PortletWindowCache windowCache = new EhPortletWindowCache((Ehcache) cacheMock.proxy());
232: // cacheMock.proxy();
233: // windowCache.getPortletWindow(null);
234: // verify();
235: }
236:
237: /**
238: * We need this class to test the cache as the {@link EhCacheImpl} object only
239: * allows {@link Serializable} objects to be cached.
240: *
241: * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
242: *
243: */
244: private interface SerializablePortletWindow extends PortletWindow,
245: Serializable {
246:
247: }
248:
249: }
|