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.portletregistry;
018:
019: import java.util.Arrays;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Locale;
023:
024: import javax.portlet.PortletMode;
025:
026: import org.apache.jetspeed.components.persistence.store.LockFailedException;
027: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
028: import org.apache.jetspeed.om.common.DublinCore;
029: import org.apache.jetspeed.om.common.GenericMetadata;
030: import org.apache.jetspeed.om.common.JetspeedServiceReference;
031: import org.apache.jetspeed.om.common.UserAttribute;
032: import org.apache.jetspeed.om.common.UserAttributeRef;
033: import org.apache.jetspeed.om.common.portlet.ContentTypeComposite;
034: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
035: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
036: import org.apache.jetspeed.om.common.preference.PreferenceComposite;
037: import org.apache.jetspeed.om.impl.DublinCoreImpl;
038: import org.apache.jetspeed.om.impl.JetspeedServiceReferenceImpl;
039: import org.apache.jetspeed.om.impl.UserAttributeImpl;
040: import org.apache.jetspeed.om.impl.UserAttributeRefImpl;
041: import org.apache.jetspeed.om.portlet.impl.ContentTypeImpl;
042: import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
043: import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
044: import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
045: import org.apache.jetspeed.util.JetspeedLocale;
046: import org.apache.pluto.om.common.PreferenceSetCtrl;
047: import org.apache.pluto.om.portlet.PortletApplicationDefinition;
048:
049: /**
050: * <p>
051: * TestPortletRegistryDAO
052: * </p>
053: *
054: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
055: * @version $Id: TestPortletRegistryDAO.java 517121 2007-03-12 07:45:49Z ate $
056: */
057: public class TestPortletRegistryDAO extends
058: DatasourceEnabledSpringTestCase {
059: public static final String APP_1_NAME = "RegistryTestPortlet";
060:
061: protected static final String MODE_EDIT = "EDIT";
062:
063: protected static final String MODE_HELP = "HELP";
064:
065: protected static final String MODE_VIEW = "VIEW";
066:
067: protected static final String PORTLET_0_CLASS = "com.portlet.MyClass0";
068:
069: protected static final String PORTLET_0_NAME = "Portlet 0";
070:
071: protected static final String PORTLET_0_UID = "com.portlet.MyClass0.Portlet 0";
072:
073: protected static final String PORTLET_1_CLASS = "com.portlet.MyClass";
074:
075: protected static final String PORTLET_1_NAME = "Portlet 1";
076:
077: protected static final String PORTLET_1_UID = "com.portlet.MyClass.Portlet 1";
078:
079: protected PortletRegistry portletRegistry;
080:
081: /*
082: * @see TestCase#setUp()
083: */
084: protected void setUp() throws Exception {
085: super .setUp();
086: this .portletRegistry = (PortletRegistry) ctx
087: .getBean("portletRegistry");
088:
089: buildTestData();
090:
091: }
092:
093: /*
094: * @see TestCase#tearDown()
095: */
096: protected void tearDown() throws Exception {
097: Iterator itr = portletRegistry.getPortletApplications()
098: .iterator();
099: while (itr.hasNext()) {
100: portletRegistry
101: .removeApplication((PortletApplicationDefinition) itr
102: .next());
103: }
104: }
105:
106: public void test1() throws Exception {
107: assertNotNull(portletRegistry);
108: }
109:
110: public void testData() throws Exception {
111: verifyData(false);
112: }
113:
114: private void addDublinCore(GenericMetadata metadata) {
115: DublinCore dc = new DublinCoreImpl(metadata);
116: dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 1");
117: dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 2");
118: dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 3");
119: dc.addContributor(JetspeedLocale.getDefaultLocale(),
120: "Contrib 1");
121: dc.addCoverage(JetspeedLocale.getDefaultLocale(), "Coverage 1");
122: dc.addCoverage(JetspeedLocale.getDefaultLocale(), "Coverage 2");
123: dc.addCreator(JetspeedLocale.getDefaultLocale(), "Creator 1");
124: dc.addDescription(JetspeedLocale.getDefaultLocale(),
125: "Description 1");
126: dc.addFormat(JetspeedLocale.getDefaultLocale(), "Format 1");
127: dc.addIdentifier(JetspeedLocale.getDefaultLocale(),
128: "Identifier 1");
129: dc.addLanguage(JetspeedLocale.getDefaultLocale(), "Language 1");
130: dc.addPublisher(JetspeedLocale.getDefaultLocale(),
131: "Publisher 1");
132: dc.addRelation(JetspeedLocale.getDefaultLocale(), "Relation 1");
133: dc.addRight(JetspeedLocale.getDefaultLocale(), "Right 1");
134: dc.addSource(JetspeedLocale.getDefaultLocale(), "Source 1");
135: dc.addSubject(JetspeedLocale.getDefaultLocale(), "Subject 1");
136: dc.addType(JetspeedLocale.getDefaultLocale(), "Type 1");
137: }
138:
139: /**
140: * <p>
141: * buildTestData
142: * </p>
143: *
144: * @throws RegistryException
145: * @throws LockFailedException
146: */
147: private void buildTestData() throws RegistryException,
148: LockFailedException {
149: // start clean
150: Iterator itr = portletRegistry.getPortletApplications()
151: .iterator();
152: while (itr.hasNext()) {
153: portletRegistry
154: .removeApplication((PortletApplicationDefinition) itr
155: .next());
156: }
157:
158: // Create an Application and a Web app
159:
160: PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
161: app.setName("App_1");
162: app.setApplicationIdentifier("App_1");
163:
164: UserAttributeRef uaRef = new UserAttributeRefImpl(
165: "user-name-family", "user.name.family");
166: app.addUserAttributeRef(uaRef);
167:
168: UserAttribute ua = new UserAttributeImpl("user.name.family",
169: "User Last Name");
170: app.addUserAttribute(ua);
171:
172: JetspeedServiceReference service1 = new JetspeedServiceReferenceImpl(
173: "PortletEntityAccessComponent");
174: app.addJetspeedService(service1);
175: JetspeedServiceReference service2 = new JetspeedServiceReferenceImpl(
176: "PortletRegistryComponent");
177: app.addJetspeedService(service2);
178:
179: addDublinCore(app.getMetadata());
180:
181: WebApplicationDefinitionImpl webApp = new WebApplicationDefinitionImpl();
182: webApp.setContextRoot("/app1");
183: webApp.addDescription(Locale.FRENCH,
184: "Description: Le fromage est dans mon pantalon!");
185: webApp.addDisplayName(Locale.FRENCH,
186: "Display Name: Le fromage est dans mon pantalon!");
187:
188: PortletDefinitionComposite portlet = new PortletDefinitionImpl();
189: portlet.setClassName("org.apache.Portlet");
190: portlet.setName("Portlet 1");
191: portlet.addDescription(Locale.getDefault(),
192: "POrtlet description.");
193: portlet.addDisplayName(Locale.getDefault(),
194: "Portlet display Name.");
195:
196: portlet
197: .addInitParameter("testparam", "test value",
198: "This is a test portlet parameter", Locale
199: .getDefault());
200:
201: addDublinCore(portlet.getMetadata());
202:
203: // PreferenceComposite pc = new PrefsPreference();
204: app.addPortletDefinition(portlet);
205: PreferenceSetCtrl prefSetCtrl = (PreferenceSetCtrl) portlet
206: .getPreferenceSet();
207: PreferenceComposite pc = (PreferenceComposite) prefSetCtrl.add(
208: "preference 1", Arrays.asList(new String[] { "value 1",
209: "value 2" }));
210: pc.addDescription(JetspeedLocale.getDefaultLocale(),
211: "Preference Description");
212:
213: assertNotNull(pc.getValueAt(0));
214:
215: portlet.addLanguage(portletRegistry.createLanguage(Locale
216: .getDefault(), "Portlet 1", "Portlet 1",
217: "This is Portlet 1", null));
218:
219: ContentTypeComposite html = new ContentTypeImpl();
220: html.setContentType("html/text");
221: ContentTypeComposite wml = new ContentTypeImpl();
222: html.addPortletMode(new PortletMode(MODE_EDIT));
223: html.addPortletMode(new PortletMode(MODE_VIEW));
224: html.addPortletMode(new PortletMode(MODE_HELP));
225: wml.setContentType("wml");
226: wml.addPortletMode(new PortletMode(MODE_HELP));
227: wml.addPortletMode(new PortletMode(MODE_VIEW));
228: portlet.addContentType(html);
229: portlet.addContentType(wml);
230:
231: app.setWebApplicationDefinition(webApp);
232: portletRegistry.updatePortletApplication(app);
233: }
234:
235: protected void verifyData(boolean afterUpdates) throws Exception {
236: MutablePortletApplication app;
237: WebApplicationDefinitionImpl webApp;
238: PortletDefinitionComposite portlet;
239:
240: // Now makes sure everthing got persisted
241:
242: app = null;
243:
244: app = portletRegistry.getPortletApplication("App_1");
245:
246: assertNotNull(app);
247:
248: webApp = (WebApplicationDefinitionImpl) app
249: .getWebApplicationDefinition();
250: portlet = (PortletDefinitionImpl) app
251: .getPortletDefinitionByName("Portlet 1");
252:
253: assertNotNull("Failed to reteive portlet application", app);
254:
255: validateDublinCore(app.getMetadata());
256:
257: Collection services = app.getJetspeedServices();
258: assertNotNull("jetspeed services is null", services);
259: System.out.println("services is " + services);
260:
261: assertNotNull(
262: "Failed to reteive portlet application via registry",
263: portletRegistry.getPortletApplication("App_1"));
264: assertNotNull(
265: "Web app was not saved along with the portlet app.",
266: webApp);
267: assertNotNull(
268: "Portlet was not saved along with the portlet app.",
269: app.getPortletDefinitionByName("Portlet 1"));
270: if (!afterUpdates) {
271: assertTrue(
272: "\"user.name.family\" user attribute was not found.",
273: app.getUserAttributes().size() == 1);
274: } else {
275: assertTrue(
276: "\"user.name.family\" and user.pets user attributes were not found.",
277: app.getUserAttributes().size() == 2);
278:
279: }
280:
281: portlet = portletRegistry
282: .getPortletDefinitionByUniqueName("App_1::Portlet 1");
283:
284: assertNotNull("Portlet could not be retreived by unique name.",
285: portlet);
286:
287: validateDublinCore(portlet.getMetadata());
288:
289: assertNotNull(
290: "Portlet Application was not set in the portlet defintion.",
291: portlet.getPortletApplicationDefinition());
292: assertNotNull(
293: "French description was not materialized for the web app.",
294: webApp.getDescription(Locale.FRENCH));
295: assertNotNull(
296: "French display name was not materialized for the web app.",
297: webApp.getDisplayName(Locale.FRENCH));
298: assertNotNull(
299: "description was not materialized for the portlet.",
300: portlet.getDescription(Locale.getDefault()));
301: assertNotNull(
302: "display name was not materialized for the portlet.",
303: portlet.getDisplayName(Locale.getDefault()));
304: assertNotNull("\"testparam\" portlet parameter was not saved",
305: portlet.getInitParameterSet().get("testparam"));
306: assertNotNull("\"preference 1\" was not found.", portlet
307: .getPreferenceSet().get("preference 1"));
308: assertNotNull("Language information not found for Portlet 1",
309: portlet.getLanguageSet().get(Locale.getDefault()));
310: assertNotNull("Content Type html not found.", portlet
311: .getContentTypeSet().get("html/text"));
312: assertNotNull("Content Type wml not found.", portlet
313: .getContentTypeSet().get("wml"));
314: Iterator itr = portlet.getPreferenceSet().get("preference 1")
315: .getValues();
316: int valueCount = 0;
317:
318: while (itr.hasNext()) {
319: itr.next();
320: valueCount++;
321: }
322: assertEquals("\"preference 1\" did not have 2 values.", 2,
323: valueCount);
324:
325: // Pull out our Web app and add a Description to it
326: webApp = null;
327:
328: app = portletRegistry.getPortletApplication("App_1");
329:
330: webApp = (WebApplicationDefinitionImpl) app
331: .getWebApplicationDefinition();
332: assertNotNull("Web app was not located by query.", webApp);
333: webApp.addDescription(Locale.getDefault(),
334: "Web app description");
335:
336: webApp = null;
337:
338: app = portletRegistry.getPortletApplication("App_1");
339: webApp = (WebApplicationDefinitionImpl) app
340: .getWebApplicationDefinition();
341:
342: assertNotNull("Web app was not located by query.", webApp);
343: assertNotNull("Web app did NOT persist its description", webApp
344: .getDescription(Locale.FRENCH));
345:
346: }
347:
348: protected void validateDublinCore(GenericMetadata metadata) {
349: DublinCore dc = new DublinCoreImpl(metadata);
350: assertEquals(dc.getTitles().size(), 3);
351: assertEquals(dc.getContributors().size(), 1);
352: assertEquals(dc.getCoverages().size(), 2);
353: assertEquals(dc.getCreators().size(), 1);
354: assertEquals(dc.getDescriptions().size(), 1);
355: assertEquals(dc.getFormats().size(), 1);
356: assertEquals(dc.getIdentifiers().size(), 1);
357: assertEquals(dc.getLanguages().size(), 1);
358: assertEquals(dc.getPublishers().size(), 1);
359: assertEquals(dc.getRelations().size(), 1);
360: assertEquals(dc.getRights().size(), 1);
361: assertEquals(dc.getSources().size(), 1);
362: assertEquals(dc.getSubjects().size(), 1);
363: assertEquals(dc.getTypes().size(), 1);
364: }
365:
366: protected String[] getConfigurations() {
367: return new String[] { "transaction.xml", "registry-test.xml",
368: "prefs.xml", "cache.xml" };
369: }
370: }
|