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.Collection;
20  import java.util.Iterator;
21  import java.util.Locale;
22  
23  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
24  import org.apache.jetspeed.om.common.DublinCore;
25  import org.apache.jetspeed.om.common.GenericMetadata;
26  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
27  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
28  import org.apache.jetspeed.om.impl.DublinCoreImpl;
29  import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
30  import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
31  
32  /***
33   * @author scott
34   */
35  public abstract class AbstractRegistryTest extends DatasourceEnabledSpringTestCase
36  {
37  
38      protected static final String PORTLET_0_CLASS = "com.portlet.MyClass0";
39  
40      protected static final String PORTLET_0_NAME = "Portlet 0";
41  
42      protected static final String PORTLET_1_CLASS = "com.portlet.MyClass";
43  
44      protected static final String PORTLET_1_NAME = "Portlet 1";
45  
46      protected static final String PORTLET_1_UID = "com.portlet.MyClass.Portlet 1";
47  
48      protected static final String PORTLET_0_UID = "com.portlet.MyClass0.Portlet 0";
49  
50      protected static final String MODE_HELP = "HELP";
51  
52      protected static final String MODE_VIEW = "VIEW";
53  
54      protected static final String MODE_EDIT = "EDIT";
55  
56      public static final String APP_1_NAME = "RegistryTestPortlet";
57  
58      protected PortletRegistry registry;
59  
60      private static int testPasses = 0;
61  
62      /***
63       * <p>
64       * setUp
65       * </p>
66       * 
67       * @see junit.framework.TestCase#setUp()
68       * @throws Exception
69       */
70      protected void setUp() throws Exception
71      {
72          super.setUp();
73          this.registry = (PortletRegistry) ctx.getBean("portletRegistry");
74  
75          testPasses++;
76      }
77  
78      /***
79       * <p>
80       * tearDown
81       * </p>
82       * 
83       * @see junit.framework.TestCase#tearDown()
84       * @throws Exception
85       */
86      protected void tearDown() throws Exception
87      {
88          //super.tearDown();
89      }
90  
91      protected void validateDublinCore(GenericMetadata metadata)
92      {
93          DublinCore dc = new DublinCoreImpl(metadata);
94          assertEquals(dc.getTitles().size(), 3);
95          assertEquals(dc.getContributors().size(), 1);
96          assertEquals(dc.getCoverages().size(), 2);
97          assertEquals(dc.getCreators().size(), 1);
98          assertEquals(dc.getDescriptions().size(), 1);
99          assertEquals(dc.getFormats().size(), 1);
100         assertEquals(dc.getIdentifiers().size(), 1);
101         assertEquals(dc.getLanguages().size(), 1);
102         assertEquals(dc.getPublishers().size(), 1);
103         assertEquals(dc.getRelations().size(), 1);
104         assertEquals(dc.getRights().size(), 1);
105         assertEquals(dc.getSources().size(), 1);
106         assertEquals(dc.getSubjects().size(), 1);
107         assertEquals(dc.getTypes().size(), 1);
108     }
109 
110     // protected void invalidate( Object[] objs ) throws LockFailedException
111     // {
112     // persistenceStore.getTransaction().begin();
113     // for (int i = 0; i < objs.length; i++)
114     // {
115     // persistenceStore.invalidate(objs[i]);
116     // }
117     // persistenceStore.getTransaction().commit();
118     // }
119 
120     protected void verifyData(boolean afterUpdates) throws Exception
121     {
122         MutablePortletApplication app;
123         WebApplicationDefinitionImpl webApp;
124         PortletDefinitionComposite portlet;
125 
126         app = null;
127 
128         app = registry.getPortletApplication("App_1");
129 
130         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
131         portlet = (PortletDefinitionImpl) app.getPortletDefinitionByName("Portlet 1");
132 
133         assertNotNull("Failed to reteive portlet application", app);
134 
135         validateDublinCore(app.getMetadata());
136 
137         Collection services = app.getJetspeedServices();
138         assertNotNull("jetspeed services is null", services);
139         System.out.println("services is " + services);
140 
141         assertNotNull("Failed to reteive portlet application via registry", registry.getPortletApplication("App_1"));
142         assertNotNull("Web app was not saved along with the portlet app.", webApp);
143         assertNotNull("Portlet was not saved along with the portlet app.", app.getPortletDefinitionByName("Portlet 1"));
144         if (!afterUpdates)
145         {
146             assertTrue("\"user.name.family\" user attribute was not found.", app.getUserAttributes().size() == 1);
147         }
148         else
149         {
150             assertTrue("\"user.name.family\" and user.pets user attributes were not found.", app.getUserAttributes()
151                     .size() == 2);
152 
153         }
154 
155         portlet = registry.getPortletDefinitionByUniqueName("App_1::Portlet 1");
156 
157         assertNotNull("Portlet could not be retreived by unique name.", portlet);
158 
159         validateDublinCore(portlet.getMetadata());
160 
161         assertNotNull("Portlet Application was not set in the portlet defintion.", portlet
162                 .getPortletApplicationDefinition());
163         assertNotNull("French description was not materialized for the web app.", webApp.getDescription(Locale.FRENCH));
164         assertNotNull("French display name was not materialized for the web app.", webApp.getDisplayName(Locale.FRENCH));
165         assertNotNull("description was not materialized for the portlet.", portlet.getDescription(Locale.getDefault()));
166         assertNotNull("display name was not materialized for the portlet.", portlet.getDisplayName(Locale.getDefault()));
167         assertNotNull("\"testparam\" portlet parameter was not saved", portlet.getInitParameterSet().get("testparam"));
168         assertNotNull("\"preference 1\" was not found.", portlet.getPreferenceSet().get("preference 1"));
169         assertNotNull("Language information not found for Portlet 1", portlet.getLanguageSet().get(Locale.getDefault()));
170         assertNotNull("Content Type html not found.", portlet.getContentTypeSet().get("html/text"));
171         assertNotNull("Content Type wml not found.", portlet.getContentTypeSet().get("wml"));
172         Iterator itr = portlet.getPreferenceSet().get("preference 1").getValues();
173         int valueCount = 0;
174 
175         while (itr.hasNext())
176         {
177             itr.next();
178             valueCount++;
179         }
180         assertEquals("\"preference 1\" did not have 2 values.", 2, valueCount);
181 
182         // Pull out our Web app and add a Description to it
183 
184         webApp = null;
185 
186         app = registry.getPortletApplication("App_1");
187 
188         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
189         assertNotNull("Web app was not located by query.", webApp);
190         webApp.addDescription(Locale.getDefault(), "Web app description");
191 
192         webApp = null;
193 
194         app = registry.getPortletApplication("App_1");
195         webApp = (WebApplicationDefinitionImpl) app.getWebApplicationDefinition();
196 
197         assertNotNull("Web app was not located by query.", webApp);
198         assertNotNull("Web app did NOT persist its description", webApp.getDescription(Locale.FRENCH));
199 
200     }
201 
202     protected String[] getConfigurations()
203     {
204         return new String[]
205         { "transaction.xml", "prefs.xml", "registry-test.xml", "cache.xml" };
206     }
207 
208 }