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.components.portletentity;
018:
019: import java.util.Arrays;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Locale;
023: import java.util.prefs.Preferences;
024:
025: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
026: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
027: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
028: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
029: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
030: import org.apache.jetspeed.om.common.preference.PreferenceComposite;
031: import org.apache.jetspeed.om.common.preference.PreferenceSetComposite;
032: import org.apache.jetspeed.om.page.ContentFragment;
033: import org.apache.jetspeed.om.page.Fragment;
034: import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
035: import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
036: import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
037: import org.apache.jetspeed.util.JetspeedObjectID;
038: import org.apache.pluto.om.portlet.PortletApplicationDefinition;
039: import org.apache.pluto.om.portlet.PortletDefinitionList;
040: import org.jmock.Mock;
041: import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;
042: import org.jmock.core.stub.ReturnStub;
043:
044: /**
045: * <p>
046: * TestPortletEntityDAO
047: * </p>
048: *
049: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
050: * @version $Id: TestPortletEntityDAO.java,v 1.3 2005/05/24 14:43:19 ate Exp $
051: */
052: public class TestPortletEntityDAO extends
053: DatasourceEnabledSpringTestCase {
054: private static final String TEST_APP = "EntityTestApp";
055:
056: private static final String TEST_PORTLET = "EntityTestPortlet";
057:
058: private static final String TEST_ENTITY = "user5/entity-9";
059:
060: private PersistenceBrokerPortletEntityAccess entityAccess = null;
061:
062: private PortletRegistry registry;
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066: this .registry = (PortletRegistry) ctx
067: .getBean("portletRegistry");
068: this .entityAccess = (PersistenceBrokerPortletEntityAccess) ctx
069: .getBean("portletEntityAccessImpl");
070:
071: teardownTestData();
072: setupTestData();
073: }
074:
075: protected void tearDown() throws Exception {
076: teardownTestData();
077: }
078:
079: public void test1() throws Exception {
080: assertNotNull(this .entityAccess);
081: assertNotNull(this .registry);
082: }
083:
084: public void testEntities() throws Exception {
085: PortletApplicationDefinition pa = registry
086: .getPortletApplication(TEST_APP);
087: assertNotNull("Portlet Application", pa);
088: System.out.println("pa = " + pa.getId());
089: PortletDefinitionList portlets = pa.getPortletDefinitionList(); // .get(JetspeedObjectID.createFromString(TEST_PORTLET));
090: Iterator pi = portlets.iterator();
091: PortletDefinitionComposite pd = null;
092: while (pi.hasNext()) {
093: pd = (PortletDefinitionComposite) pi.next();
094: assertTrue("Portlet Def not found", pd.getName().equals(
095: "EntityTestPortlet"));
096: }
097: assertNotNull("Portlet Def is null", pd);
098:
099: Mock mockf1 = new Mock(Fragment.class);
100: mockf1.expects(new InvokeAtLeastOnceMatcher())
101: .method("getName").will(
102: new ReturnStub(pd.getUniqueName()));
103: mockf1.expects(new InvokeAtLeastOnceMatcher()).method("getId")
104: .will(new ReturnStub(TEST_ENTITY));
105: ContentFragment f1 = new ContentFragmentTestImpl(
106: (Fragment) mockf1.proxy(), new HashMap());
107:
108: MutablePortletEntity entity = entityAccess
109: .generateEntityFromFragment(new ContentFragmentTestImpl(
110: f1, new HashMap()));
111: PreferenceSetComposite prefs = (PreferenceSetComposite) entity
112: .getPreferenceSet();
113: prefs.remove("pref1");
114: assertNotNull(prefs);
115: assertNull(prefs.get("pref1"));
116:
117: // test adding a pref
118: prefs.add("pref1", Arrays.asList(new String[] { "1" }));
119: assertNotNull(prefs.get("pref1"));
120:
121: // Remove should return the deleted pref
122: assertNotNull(prefs.remove("pref1"));
123:
124: // Should be gone
125: assertNull(prefs.get("pref1"));
126:
127: // Add it back so we can test tole back
128: prefs.add("pref1", Arrays.asList(new String[] { "1" }));
129:
130: entityAccess.storePortletEntity(entity);
131:
132: prefs = (PreferenceSetComposite) entity.getPreferenceSet();
133:
134: assertNotNull(prefs.get("pref1"));
135:
136: PreferenceComposite pref = (PreferenceComposite) prefs
137: .get("pref1");
138:
139: assertEquals("1", pref.getValueAt(0));
140:
141: pref.setValueAt(0, "2");
142:
143: assertEquals("2", pref.getValueAt(0));
144:
145: entity.reset();
146:
147: pref = (PreferenceComposite) prefs.get("pref1");
148:
149: assertEquals("1", pref.getValueAt(0));
150:
151: prefs.remove(pref);
152:
153: assertNull(prefs.get("pref1"));
154:
155: entity.reset();
156:
157: assertNotNull(prefs.get("pref1"));
158:
159: prefs.add("pref2", Arrays.asList(new String[] { "2", "3" }));
160:
161: entity.store();
162:
163: PreferenceComposite pref2 = (PreferenceComposite) prefs
164: .get("pref2");
165:
166: assertNotNull(pref2);
167:
168: Iterator prefsValues = pref2.getValues();
169: int count = 0;
170: while (prefsValues.hasNext()) {
171: prefsValues.next();
172: count++;
173: }
174:
175: assertEquals(2, count);
176:
177: pref2.addValue("4");
178: prefsValues = pref2.getValues();
179: count = 0;
180: while (prefsValues.hasNext()) {
181: assertEquals(String.valueOf(count + 2), prefsValues.next());
182: count++;
183: }
184: assertEquals(3, count);
185:
186: entity.reset();
187:
188: prefsValues = pref2.getValues();
189: count = 0;
190: while (prefsValues.hasNext()) {
191: assertEquals(String.valueOf(count + 2), prefsValues.next());
192: count++;
193: }
194: assertEquals(2, count);
195:
196: // testing preferences null values assignments fix, issue JS2-607
197: pref2.setValueAt(0, null);
198: assertNull("pref2.value[0] should be null", pref2.getValueAt(0));
199: String[] values = pref2.getValueArray();
200: assertEquals(2, values.length);
201: assertNull("pref2.value[0] should be null", values[0]);
202: assertEquals("3", values[1]);
203: pref2.setValues(new String[] { "2", null, "3" });
204: assertNull("pref2.value[1] should be null", pref2.getValueAt(1));
205: values = pref2.getValueArray();
206: assertEquals(3, values.length);
207: assertEquals("2", values[0]);
208: assertNull("pref2.value[1] should be null", values[1]);
209: assertEquals("3", values[2]);
210: assertTrue(pref2.isValueSet());
211: pref2.setValues((String[]) null);
212: assertFalse(pref2.isValueSet());
213: assertTrue(pref2.getValueArray().length == 0);
214: entity.reset();
215: assertTrue(pref2.getValueArray().length == 2);
216: pref2.setValues(new String[] {});
217: assertFalse(pref2.isValueSet());
218: assertTrue(pref2.getValueArray().length == 0);
219: entity.reset();
220: assertTrue(pref2.getValueArray().length == 2);
221:
222: MutablePortletEntity entity2 = entityAccess
223: .getPortletEntityForFragment(f1);
224: assertTrue("entity id ", entity2.getId().toString().equals(
225: TEST_ENTITY));
226: assertNotNull("entity's portlet ", entity2
227: .getPortletDefinition());
228: mockf1.verify();
229:
230: Mock mockf2 = new Mock(Fragment.class);
231: mockf2.expects(new InvokeAtLeastOnceMatcher())
232: .method("getName").will(
233: new ReturnStub(pd.getUniqueName()));
234: ContentFragment f2 = new ContentFragmentTestImpl(
235: (Fragment) mockf2.proxy(), new HashMap());
236:
237: MutablePortletEntity entity5 = entityAccess
238: .newPortletEntityInstance(pd);
239:
240: System.out.println("before storing entity: " + entity5.getId());
241:
242: entityAccess.storePortletEntity(entity5);
243: System.out.println("store done: " + entity5.getId());
244: mockf2.expects(new InvokeAtLeastOnceMatcher()).method("getId")
245: .will(new ReturnStub(entity5.getId().toString()));
246:
247: MutablePortletEntity entity6 = entityAccess
248: .getPortletEntityForFragment(f2);
249: assertNotNull(entity6);
250: System.out.println("reget : " + entity6.getId());
251:
252: entityAccess.removePortletEntity(entity6);
253:
254: }
255:
256: private void teardownTestData() throws Exception {
257:
258: JetspeedObjectID objId = JetspeedObjectID
259: .createFromString(TEST_ENTITY);
260: MutablePortletEntity entity = entityAccess
261: .getPortletEntity(objId);
262: System.out.println("entity == " + entity);
263:
264: if (entity != null) {
265: entityAccess.removePortletEntity(entity);
266: }
267:
268: PortletApplicationDefinition pa = registry
269: .getPortletApplication(TEST_APP);
270: System.out.println("pa == " + pa);
271: if (pa != null) {
272: registry.removeApplication(pa);
273: }
274:
275: if (Preferences.systemRoot().nodeExists(
276: MutablePortletApplication.PREFS_ROOT)) {
277: Preferences.systemRoot().node(
278: MutablePortletApplication.PREFS_ROOT).removeNode();
279: }
280:
281: if (Preferences.userRoot().nodeExists(
282: PortletDefinitionComposite.PORTLETS_PREFS_ROOT)) {
283: Preferences.userRoot().node(
284: PortletDefinitionComposite.PORTLETS_PREFS_ROOT)
285: .removeNode();
286: }
287:
288: if (Preferences.userRoot().nodeExists(
289: MutablePortletEntity.PORTLET_ENTITY_ROOT)) {
290: Preferences.userRoot().node(
291: MutablePortletEntity.PORTLET_ENTITY_ROOT)
292: .removeNode();
293: }
294:
295: }
296:
297: private void setupTestData() throws Exception {
298:
299: PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
300: app.setName(TEST_APP);
301: app.setApplicationIdentifier(TEST_APP);
302:
303: WebApplicationDefinitionImpl webApp = new WebApplicationDefinitionImpl();
304: webApp.setContextRoot("/app1");
305: webApp.addDescription(Locale.FRENCH,
306: "Description: Le fromage est dans mon pantalon!");
307: webApp.addDisplayName(Locale.FRENCH,
308: "Display Name: Le fromage est dans mon pantalon!");
309:
310: PortletDefinitionComposite portlet = new PortletDefinitionImpl();
311: portlet.setClassName("org.apache.Portlet");
312: portlet.setName(TEST_PORTLET);
313: portlet.addDescription(Locale.getDefault(),
314: "Portlet description.");
315: portlet.addDisplayName(Locale.getDefault(),
316: "Portlet display Name.");
317:
318: portlet
319: .addInitParameter("testparam", "test value",
320: "This is a test portlet parameter", Locale
321: .getDefault());
322:
323: app.addPortletDefinition(portlet);
324:
325: app.setWebApplicationDefinition(webApp);
326:
327: PreferenceSetComposite prefSet = (PreferenceSetComposite) portlet
328: .getPreferenceSet();
329: prefSet.add("pref1", Arrays.asList(new String[] { "1" }));
330:
331: registry.registerPortletApplication(app);
332: }
333:
334: protected String[] getConfigurations() {
335: return new String[] { "transaction.xml", "registry-test.xml",
336: "prefs.xml", "cache.xml" };
337: }
338: }
|