1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.components.portletregistry;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Locale;
23  
24  import javax.portlet.PortletMode;
25  
26  import org.apache.jetspeed.components.persistence.store.LockFailedException;
27  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
28  import org.apache.jetspeed.om.common.DublinCore;
29  import org.apache.jetspeed.om.common.GenericMetadata;
30  import org.apache.jetspeed.om.common.JetspeedServiceReference;
31  import org.apache.jetspeed.om.common.UserAttribute;
32  import org.apache.jetspeed.om.common.UserAttributeRef;
33  import org.apache.jetspeed.om.common.portlet.ContentTypeComposite;
34  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
35  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
36  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
37  import org.apache.jetspeed.om.impl.DublinCoreImpl;
38  import org.apache.jetspeed.om.impl.JetspeedServiceReferenceImpl;
39  import org.apache.jetspeed.om.impl.UserAttributeImpl;
40  import org.apache.jetspeed.om.impl.UserAttributeRefImpl;
41  import org.apache.jetspeed.om.portlet.impl.ContentTypeImpl;
42  import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
43  import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
44  import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
45  import org.apache.jetspeed.util.JetspeedLocale;
46  import org.apache.pluto.om.common.PreferenceSetCtrl;
47  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
48  
49  /***
50   * <p>
51   * TestPortletRegistryDAO
52   * </p>
53   * 
54   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
55   * @version $Id: TestPortletRegistryDAO.java 517121 2007-03-12 07:45:49Z ate $
56   */
57  public class TestPortletRegistryDAO extends DatasourceEnabledSpringTestCase
58  {
59      public static final String APP_1_NAME = "RegistryTestPortlet";
60  
61      protected static final String MODE_EDIT = "EDIT";
62  
63      protected static final String MODE_HELP = "HELP";
64  
65      protected static final String MODE_VIEW = "VIEW";
66  
67      protected static final String PORTLET_0_CLASS = "com.portlet.MyClass0";
68  
69      protected static final String PORTLET_0_NAME = "Portlet 0";
70  
71      protected static final String PORTLET_0_UID = "com.portlet.MyClass0.Portlet 0";
72  
73      protected static final String PORTLET_1_CLASS = "com.portlet.MyClass";
74  
75      protected static final String PORTLET_1_NAME = "Portlet 1";
76  
77      protected static final String PORTLET_1_UID = "com.portlet.MyClass.Portlet 1";
78  
79      protected PortletRegistry portletRegistry;
80  
81      /*
82       * @see TestCase#setUp()
83       */
84      protected void setUp() throws Exception
85      {
86          super.setUp();
87          this.portletRegistry = (PortletRegistry) ctx.getBean("portletRegistry");
88  
89          buildTestData();
90  
91      }
92  
93      /*
94       * @see TestCase#tearDown()
95       */
96      protected void tearDown() throws Exception
97      {
98          Iterator itr = portletRegistry.getPortletApplications().iterator();
99          while (itr.hasNext())
100         {
101             portletRegistry.removeApplication((PortletApplicationDefinition) itr.next());
102         }
103     }
104 
105     public void test1() throws Exception
106     {
107         assertNotNull(portletRegistry);
108     }
109 
110     public void testData() throws Exception
111     {
112         verifyData(false);
113     }
114 
115     private void addDublinCore(GenericMetadata metadata)
116     {
117         DublinCore dc = new DublinCoreImpl(metadata);
118         dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 1");
119         dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 2");
120         dc.addTitle(JetspeedLocale.getDefaultLocale(), "Test title 3");
121         dc.addContributor(JetspeedLocale.getDefaultLocale(), "Contrib 1");
122         dc.addCoverage(JetspeedLocale.getDefaultLocale(), "Coverage 1");
123         dc.addCoverage(JetspeedLocale.getDefaultLocale(), "Coverage 2");
124         dc.addCreator(JetspeedLocale.getDefaultLocale(), "Creator 1");
125         dc.addDescription(JetspeedLocale.getDefaultLocale(), "Description 1");
126         dc.addFormat(JetspeedLocale.getDefaultLocale(), "Format 1");
127         dc.addIdentifier(JetspeedLocale.getDefaultLocale(), "Identifier 1");
128         dc.addLanguage(JetspeedLocale.getDefaultLocale(), "Language 1");
129         dc.addPublisher(JetspeedLocale.getDefaultLocale(), "Publisher 1");
130         dc.addRelation(JetspeedLocale.getDefaultLocale(), "Relation 1");
131         dc.addRight(JetspeedLocale.getDefaultLocale(), "Right 1");
132         dc.addSource(JetspeedLocale.getDefaultLocale(), "Source 1");
133         dc.addSubject(JetspeedLocale.getDefaultLocale(), "Subject 1");
134         dc.addType(JetspeedLocale.getDefaultLocale(), "Type 1");
135     }
136 
137     /***
138      * <p>
139      * buildTestData
140      * </p>
141      * 
142      * @throws RegistryException
143      * @throws LockFailedException
144      */
145     private void buildTestData() throws RegistryException, LockFailedException
146     {
147         // start clean
148         Iterator itr = portletRegistry.getPortletApplications().iterator();
149         while (itr.hasNext())
150         {
151             portletRegistry.removeApplication((PortletApplicationDefinition) itr.next());
152         }
153 
154         // Create an Application and a Web app
155 
156         PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
157         app.setName("App_1");
158         app.setApplicationIdentifier("App_1");
159 
160         UserAttributeRef uaRef = new UserAttributeRefImpl("user-name-family", "user.name.family");
161         app.addUserAttributeRef(uaRef);
162 
163         UserAttribute ua = new UserAttributeImpl("user.name.family", "User Last Name");
164         app.addUserAttribute(ua);
165 
166         JetspeedServiceReference service1 = new JetspeedServiceReferenceImpl("PortletEntityAccessComponent");
167         app.addJetspeedService(service1);
168         JetspeedServiceReference service2 = new JetspeedServiceReferenceImpl("PortletRegistryComponent");
169         app.addJetspeedService(service2);
170 
171         addDublinCore(app.getMetadata());
172 
173         WebApplicationDefinitionImpl webApp = new WebApplicationDefinitionImpl();
174         webApp.setContextRoot("/app1");
175         webApp.addDescription(Locale.FRENCH, "Description: Le fromage est dans mon pantalon!");
176         webApp.addDisplayName(Locale.FRENCH, "Display Name: Le fromage est dans mon pantalon!");
177 
178         PortletDefinitionComposite portlet = new PortletDefinitionImpl();
179         portlet.setClassName("org.apache.Portlet");
180         portlet.setName("Portlet 1");
181         portlet.addDescription(Locale.getDefault(), "POrtlet description.");
182         portlet.addDisplayName(Locale.getDefault(), "Portlet display Name.");
183 
184         portlet.addInitParameter("testparam", "test value", "This is a test portlet parameter", Locale.getDefault());
185 
186         addDublinCore(portlet.getMetadata());
187 
188         // PreferenceComposite pc = new PrefsPreference();
189         app.addPortletDefinition(portlet);
190         PreferenceSetCtrl prefSetCtrl = (PreferenceSetCtrl) portlet.getPreferenceSet();
191         PreferenceComposite pc = (PreferenceComposite) prefSetCtrl.add("preference 1", Arrays.asList(new String[]
192         { "value 1", "value 2" }));
193         pc.addDescription(JetspeedLocale.getDefaultLocale(), "Preference Description");
194 
195         assertNotNull(pc.getValueAt(0));
196 
197         portlet.addLanguage(portletRegistry.createLanguage(Locale.getDefault(), "Portlet 1", "Portlet 1",
198                 "This is Portlet 1", null));
199 
200         ContentTypeComposite html = new ContentTypeImpl();
201         html.setContentType("html/text");
202         ContentTypeComposite wml = new ContentTypeImpl();
203         html.addPortletMode(new PortletMode(MODE_EDIT));
204         html.addPortletMode(new PortletMode(MODE_VIEW));
205         html.addPortletMode(new PortletMode(MODE_HELP));
206         wml.setContentType("wml");
207         wml.addPortletMode(new PortletMode(MODE_HELP));
208         wml.addPortletMode(new PortletMode(MODE_VIEW));
209         portlet.addContentType(html);
210         portlet.addContentType(wml);
211 
212         app.setWebApplicationDefinition(webApp);
213         portletRegistry.updatePortletApplication(app);
214     }
215 
216     protected void verifyData(boolean afterUpdates) throws Exception
217     {
218         MutablePortletApplication app;
219         WebApplicationDefinitionImpl webApp;
220         PortletDefinitionComposite portlet;
221 
222         // Now makes sure everthing got persisted
223 
224         app = null;
225 
226         app = portletRegistry.getPortletApplication("App_1");
227 
228         assertNotNull(app);
229 
230         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
231         portlet = (PortletDefinitionImpl) app.getPortletDefinitionByName("Portlet 1");
232 
233         assertNotNull("Failed to reteive portlet application", app);
234 
235         validateDublinCore(app.getMetadata());
236 
237         Collection services = app.getJetspeedServices();
238         assertNotNull("jetspeed services is null", services);
239         System.out.println("services is " + services);
240 
241         assertNotNull("Failed to reteive portlet application via registry", portletRegistry
242                 .getPortletApplication("App_1"));
243         assertNotNull("Web app was not saved along with the portlet app.", webApp);
244         assertNotNull("Portlet was not saved along with the portlet app.", app.getPortletDefinitionByName("Portlet 1"));
245         if (!afterUpdates)
246         {
247             assertTrue("\"user.name.family\" user attribute was not found.", app.getUserAttributes().size() == 1);
248         }
249         else
250         {
251             assertTrue("\"user.name.family\" and user.pets user attributes were not found.", app.getUserAttributes()
252                     .size() == 2);
253 
254         }
255 
256         portlet = portletRegistry.getPortletDefinitionByUniqueName("App_1::Portlet 1");
257 
258         assertNotNull("Portlet could not be retreived by unique name.", portlet);
259 
260         validateDublinCore(portlet.getMetadata());
261 
262         assertNotNull("Portlet Application was not set in the portlet defintion.", portlet
263                 .getPortletApplicationDefinition());
264         assertNotNull("French description was not materialized for the web app.", webApp.getDescription(Locale.FRENCH));
265         assertNotNull("French display name was not materialized for the web app.", webApp.getDisplayName(Locale.FRENCH));
266         assertNotNull("description was not materialized for the portlet.", portlet.getDescription(Locale.getDefault()));
267         assertNotNull("display name was not materialized for the portlet.", portlet.getDisplayName(Locale.getDefault()));
268         assertNotNull("\"testparam\" portlet parameter was not saved", portlet.getInitParameterSet().get("testparam"));
269         assertNotNull("\"preference 1\" was not found.", portlet.getPreferenceSet().get("preference 1"));
270         assertNotNull("Language information not found for Portlet 1", portlet.getLanguageSet().get(Locale.getDefault()));
271         assertNotNull("Content Type html not found.", portlet.getContentTypeSet().get("html/text"));
272         assertNotNull("Content Type wml not found.", portlet.getContentTypeSet().get("wml"));
273         Iterator itr = portlet.getPreferenceSet().get("preference 1").getValues();
274         int valueCount = 0;
275 
276         while (itr.hasNext())
277         {
278             itr.next();
279             valueCount++;
280         }
281         assertEquals("\"preference 1\" did not have 2 values.", 2, valueCount);
282 
283         // Pull out our Web app and add a Description to it
284         webApp = null;
285 
286         app = portletRegistry.getPortletApplication("App_1");
287 
288         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
289         assertNotNull("Web app was not located by query.", webApp);
290         webApp.addDescription(Locale.getDefault(), "Web app description");
291 
292         webApp = null;
293 
294         app = portletRegistry.getPortletApplication("App_1");
295         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
296 
297         assertNotNull("Web app was not located by query.", webApp);
298         assertNotNull("Web app did NOT persist its description", webApp.getDescription(Locale.FRENCH));
299 
300     }
301 
302     protected void validateDublinCore(GenericMetadata metadata)
303     {
304         DublinCore dc = new DublinCoreImpl(metadata);
305         assertEquals(dc.getTitles().size(), 3);
306         assertEquals(dc.getContributors().size(), 1);
307         assertEquals(dc.getCoverages().size(), 2);
308         assertEquals(dc.getCreators().size(), 1);
309         assertEquals(dc.getDescriptions().size(), 1);
310         assertEquals(dc.getFormats().size(), 1);
311         assertEquals(dc.getIdentifiers().size(), 1);
312         assertEquals(dc.getLanguages().size(), 1);
313         assertEquals(dc.getPublishers().size(), 1);
314         assertEquals(dc.getRelations().size(), 1);
315         assertEquals(dc.getRights().size(), 1);
316         assertEquals(dc.getSources().size(), 1);
317         assertEquals(dc.getSubjects().size(), 1);
318         assertEquals(dc.getTypes().size(), 1);
319     }
320 
321     protected String[] getConfigurations()
322     {
323         return new String[]
324         { "transaction.xml", "registry-test.xml", "prefs.xml", "cache.xml" };
325     }
326 }