1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.cache;
18
19 import java.io.Serializable;
20 import java.util.Arrays;
21 import java.util.List;
22 import java.util.Set;
23
24 import net.sf.ehcache.Ehcache;
25 import net.sf.ehcache.Element;
26
27 import org.apache.jetspeed.cache.impl.EhCacheImpl;
28 import org.apache.jetspeed.cache.impl.EhPortletWindowCache;
29 import org.apache.pluto.om.common.ObjectID;
30 import org.apache.pluto.om.entity.PortletEntity;
31 import org.apache.pluto.om.window.PortletWindow;
32 import org.jmock.Mock;
33 import org.jmock.cglib.MockObjectTestCase;
34 import org.jmock.core.stub.VoidStub;
35
36
37
38 /***
39 *
40 * Tests for {@link EhPortletWindowCache}.
41 *
42 * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
43 *
44 */
45 public class TestPortletWindowCache extends MockObjectTestCase
46 {
47 private static final String WINDOW_ID = "window1";
48 private static final String ENTITY_ID = "entity1";
49
50
51 private Mock cacheMock;
52 private Mock windowMock;
53 private Mock entityMock;
54 private Mock oidMock;
55 private Mock entityOidMock;
56
57 protected void setUp() throws Exception
58 {
59 super.setUp();
60 cacheMock = mock(Ehcache.class);
61 windowMock = mock(SerializablePortletWindow.class);
62 entityMock = mock(PortletEntity.class);
63 oidMock = mock(ObjectID.class);
64 entityOidMock = (Mock) mock(ObjectID.class);
65 }
66
67 public void testSimplePutAndGet()
68 {
69
70 PortletWindow window = (PortletWindow) windowMock.proxy();
71 Element element = new Element(WINDOW_ID, window);
72 ObjectID oid = (ObjectID) oidMock.proxy();
73 ObjectID entityOid = (ObjectID) entityOidMock.proxy();
74 entityOidMock.expects(atLeastOnce()).method("toString").will(returnValue(ENTITY_ID));
75 oidMock.expects(atLeastOnce()).method("toString").will(returnValue(WINDOW_ID));
76 windowMock.expects(once()).method("getId").withNoArguments().will(returnValue(oid));
77 windowMock.expects(once()).method("getPortletEntity").withNoArguments().will(returnValue(entityMock.proxy()));
78 entityMock.expects(once()).method("getId").withNoArguments().will(returnValue(entityOid));
79 cacheMock.expects(once()).method("put").with(eq(element));
80 cacheMock.expects(atLeastOnce()).method("get").with(eq(WINDOW_ID)).will(returnValue(element));
81
82
83 Ehcache cache = (Ehcache) cacheMock.proxy();
84 PortletWindowCache windowCache = new EhPortletWindowCache(cache);
85 windowCache.putPortletWindow(window);
86
87 assertNotNull(windowCache.getPortletWindow(WINDOW_ID));
88 assertEquals(windowCache.getPortletWindow(WINDOW_ID), window);
89
90 verify();
91 }
92
93 public void testGetByPortletEntity()
94 {
95
96 SerializablePortletWindow window = (SerializablePortletWindow) windowMock.proxy();
97 Element element = new Element(WINDOW_ID, window);
98
99 ObjectID oid = (ObjectID) oidMock.proxy();
100 oidMock.expects(atLeastOnce()).method("toString").will(returnValue(WINDOW_ID));
101 ObjectID entityOid = (ObjectID) entityOidMock.proxy();
102 entityOidMock.expects(atLeastOnce()).method("toString").will(returnValue(ENTITY_ID));
103 cacheMock.expects(once()).method("put").with(eq(element));
104 cacheMock.expects(once()).method("get").with(eq(WINDOW_ID)).will(returnValue(element));
105 windowMock.expects(once()).method("getId").withNoArguments().will(returnValue(oid));
106 windowMock.expects(once()).method("getPortletEntity").withNoArguments().will(returnValue(entityMock.proxy()));
107 entityMock.expects(once()).method("getId").withNoArguments().will(returnValue(entityOid));
108
109 Ehcache cache = (Ehcache) cacheMock.proxy();
110 PortletWindowCache windowCache = new EhPortletWindowCache(cache);
111 windowCache.putPortletWindow(window);
112
113 PortletWindow fromCache = windowCache.getPortletWindowByEntityId(ENTITY_ID);
114 assertNotNull(fromCache);
115
116 verify();
117 }
118
119 public void testRemove()
120 {
121 SerializablePortletWindow window = (SerializablePortletWindow) windowMock.proxy();
122 Element element = new Element(WINDOW_ID, window);
123
124 ObjectID oid = (ObjectID) oidMock.proxy();
125 oidMock.expects(atLeastOnce()).method("toString").will(returnValue(WINDOW_ID));
126
127 ObjectID entityOid = (ObjectID)entityOidMock.proxy();
128 entityOidMock.expects(atLeastOnce()).method("toString").will(returnValue(ENTITY_ID));
129
130 cacheMock.expects(once()).method("put").with(eq(element));
131 cacheMock.expects(exactly(2)).method("get").with(eq(WINDOW_ID)).will(returnValue(element));
132 windowMock.expects(once()).method("getId").withNoArguments().will(returnValue(oid));
133 windowMock.expects(exactly(2)).method("getPortletEntity").withNoArguments().will(returnValue(entityMock.proxy()));
134 entityMock.expects(exactly(2)).method("getId").withNoArguments().will(returnValue(entityOid));
135
136
137 cacheMock.expects(once()).method("removeQuiet").with(eq(WINDOW_ID)).will(returnValue(true));
138
139
140 Ehcache cache = (Ehcache) cacheMock.proxy();
141 PortletWindowCache windowCache = new EhPortletWindowCache(cache);
142 windowCache.putPortletWindow(window);
143
144 windowCache.removePortletWindow(WINDOW_ID);
145 assertNull(windowCache.getPortletWindowByEntityId(ENTITY_ID));
146
147 verify();
148 }
149
150 public void testRemoveByEntityId()
151 {
152 SerializablePortletWindow window = (SerializablePortletWindow) windowMock.proxy();
153 Element element = new Element(WINDOW_ID, window);
154
155 ObjectID oid = (ObjectID) oidMock.proxy();
156 oidMock.expects(atLeastOnce()).method("toString").will(returnValue(WINDOW_ID));
157
158 ObjectID entityOid = (ObjectID) entityOidMock.proxy();
159 entityOidMock.expects(atLeastOnce()).method("toString").will(returnValue(ENTITY_ID));
160
161 cacheMock.expects(once()).method("put").with(eq(element));
162 cacheMock.expects(exactly(3)).method("get").with(eq(WINDOW_ID)).will(onConsecutiveCalls(returnValue(element), returnValue(element), new VoidStub()));
163 windowMock.expects(exactly(2)).method("getId").withNoArguments().will(returnValue(oid));
164 windowMock.expects(once()).method("getPortletEntity").withNoArguments().will(returnValue(entityMock.proxy()));
165 entityMock.expects(once()).method("getId").withNoArguments().will(returnValue(entityOid));
166
167
168 cacheMock.expects(atLeastOnce()).method("removeQuiet").with(eq(WINDOW_ID)).will(returnValue(true));
169
170
171 Ehcache cache = (Ehcache) cacheMock.proxy();
172 PortletWindowCache windowCache = new EhPortletWindowCache(cache);
173 windowCache.putPortletWindow(window);
174
175 windowCache.removePortletWindowByPortletEntityId(ENTITY_ID);
176 assertNull(windowCache.getPortletWindow(WINDOW_ID));
177
178 verify();
179 }
180
181 public void testGetAllPortletWindows()
182 {
183 PortletWindow window = (PortletWindow) windowMock.proxy();
184 PortletWindow window2 = (PortletWindow) mock(SerializablePortletWindow.class).proxy();
185 PortletWindow window3 = (PortletWindow) mock(SerializablePortletWindow.class).proxy();
186
187 List keys = Arrays.asList(new String[] {WINDOW_ID, "window2", "window3"});
188
189 cacheMock.expects(once()).method("getKeys").withNoArguments().will(returnValue(keys));
190 cacheMock.expects(once()).method("get").with(eq(WINDOW_ID)).will(returnValue(new Element(WINDOW_ID, window)));
191 cacheMock.expects(once()).method("get").with(eq("window2")).will(returnValue(new Element("window2", window2)));
192 cacheMock.expects(once()).method("get").with(eq("window3")).will(returnValue(new Element("window3", window3)));
193
194 PortletWindowCache windowCache = new EhPortletWindowCache((Ehcache) cacheMock.proxy());
195
196 Set allPortletWindows = windowCache.getAllPortletWindows();
197 assertNotNull(allPortletWindows);
198 assertEquals(3, allPortletWindows.size());
199 }
200
201 public void testUnexpected()
202 {
203
204
205
206
207 }
208
209 /***
210 * We need this class to test the cache as the {@link EhCacheImpl} object only
211 * allows {@link Serializable} objects to be cached.
212 *
213 * @author <a href="mailto:scott.t.weaver@gmail.com">Scott T. Weaver</a>
214 *
215 */
216 private interface SerializablePortletWindow extends PortletWindow, Serializable
217 {
218
219 }
220
221
222 }