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.Iterator;
21  import java.util.Locale;
22  
23  import javax.portlet.PortletMode;
24  
25  import org.apache.jetspeed.components.persistence.store.LockFailedException;
26  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
27  import org.apache.jetspeed.om.common.JetspeedServiceReference;
28  import org.apache.jetspeed.om.common.UserAttribute;
29  import org.apache.jetspeed.om.common.UserAttributeRef;
30  import org.apache.jetspeed.om.common.portlet.ContentTypeComposite;
31  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
32  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
33  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
34  import org.apache.jetspeed.om.impl.JetspeedServiceReferenceImpl;
35  import org.apache.jetspeed.om.impl.UserAttributeImpl;
36  import org.apache.jetspeed.om.impl.UserAttributeRefImpl;
37  import org.apache.jetspeed.om.portlet.impl.ContentTypeImpl;
38  import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
39  import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
40  import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
41  import org.apache.jetspeed.util.JetspeedLocale;
42  import org.apache.pluto.om.common.PreferenceSetCtrl;
43  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
44  
45  /***
46   * <p>
47   * TestPortletRegistryDAO
48   * </p>
49   * 
50   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
51   * @version $Id: TestPortletRegistryDAO.java 506825 2007-02-13 02:47:07Z taylor $
52   */
53  public class TestRegistryCache extends DatasourceEnabledSpringTestCase
54  {
55      protected PortletRegistry portletRegistry;
56  
57      /*
58       * @see TestCase#setUp()
59       */
60      protected void setUp() throws Exception
61      {
62          super.setUp();
63          this.portletRegistry = (PortletRegistry) ctx.getBean("portletRegistry");
64  
65          buildTestData();
66  
67      }
68  
69      // impl not complete
70      public void xtestProxy() throws Exception
71      {
72          MutablePortletApplication app = portletRegistry.getPortletApplication("PA-001");
73          MutablePortletApplication cached = MutablePortletApplicationProxy.createProxy(app);
74          if (cached instanceof MutablePortletApplication)
75              System.out.println("ISA Mutable");
76          if (cached instanceof PortletApplicationProxy)
77              System.out.println("ISA Mutable Proxy");
78          PortletApplicationProxy proxy = (PortletApplicationProxy)cached;
79          MutablePortletApplication two = proxy.getRealApplication();
80          proxy.setRealApplication(two);
81          System.out.println("Two is " + two);
82          assertEquals(app, two);
83      }
84      
85      public void testCache() throws Exception
86      {
87          assertNotNull(portletRegistry);
88          MutablePortletApplication one = portletRegistry.getPortletApplication("PA-001");
89          MutablePortletApplication two = portletRegistry.getPortletApplication("PA-001");
90          assertEquals(one, two);
91          PortletDefinitionComposite def = portletRegistry.getPortletDefinitionByUniqueName("PA-001::Portlet-1");
92          assertNotNull(def);
93          assertEquals(def.getPortletApplicationDefinition(), one);
94          assertEquals(def, two.getPortletDefinitions().iterator().next());
95          MutablePortletApplication o = (MutablePortletApplication)portletRegistry.getPortletApplications().iterator().next();
96          assertEquals(one, o);
97          assertEquals(portletRegistry.getAllPortletDefinitions().iterator().next(), def);
98      }
99      
100     private void buildTestData() throws RegistryException, LockFailedException
101     {
102         // start clean
103         Iterator itr = portletRegistry.getPortletApplications().iterator();
104         while (itr.hasNext())
105         {
106             portletRegistry.removeApplication((PortletApplicationDefinition) itr.next());
107         }
108 
109         // Create an Application and a Web app
110 
111         PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
112         app.setName("PA-001");
113         UserAttributeRef uaRef = new UserAttributeRefImpl("user-name-family", "user.name.family");
114         app.addUserAttributeRef(uaRef);
115 
116         UserAttribute ua = new UserAttributeImpl("user.name.family", "User Last Name");
117         app.addUserAttribute(ua);
118 
119         JetspeedServiceReference service1 = new JetspeedServiceReferenceImpl("PortletEntityAccessComponent");
120         app.addJetspeedService(service1);
121         JetspeedServiceReference service2 = new JetspeedServiceReferenceImpl("PortletRegistryComponent");
122         app.addJetspeedService(service2);
123 
124         WebApplicationDefinitionImpl webApp = new WebApplicationDefinitionImpl();
125         webApp.setContextRoot("/pa-001");
126         webApp.addDescription(Locale.FRENCH, "Description: Le fromage est dans mon pantalon!");
127         webApp.addDisplayName(Locale.FRENCH, "Display Name: Le fromage est dans mon pantalon!");
128 
129         PortletDefinitionComposite portlet = new PortletDefinitionImpl();
130         portlet.setClassName("org.apache.Portlet");
131         portlet.setName("Portlet-1");
132         portlet.addDescription(Locale.getDefault(), "POrtlet description.");
133         portlet.addDisplayName(Locale.getDefault(), "Portlet display Name.");
134 
135         portlet.addInitParameter("testparam", "test value", "This is a test portlet parameter", Locale.getDefault());
136 
137         // PreferenceComposite pc = new PrefsPreference();
138         app.addPortletDefinition(portlet);
139 
140         PreferenceSetCtrl prefSetCtrl = (PreferenceSetCtrl) portlet.getPreferenceSet();
141         PreferenceComposite pc = (PreferenceComposite) prefSetCtrl.add("preference 1", Arrays.asList(new String[]
142         { "value 1", "value 2" }));
143         pc.addDescription(JetspeedLocale.getDefaultLocale(), "Preference Description");
144 
145         assertNotNull(pc.getValueAt(0));
146 
147         portlet.addLanguage(portletRegistry.createLanguage(Locale.getDefault(), "Portlet 1", "Portlet 1",
148                 "This is Portlet 1", null));
149 
150         ContentTypeComposite html = new ContentTypeImpl();
151         html.setContentType("html/text");
152         ContentTypeComposite wml = new ContentTypeImpl();
153         html.addPortletMode(new PortletMode("EDIT"));
154         html.addPortletMode(new PortletMode("VIEW"));
155         html.addPortletMode(new PortletMode("HELP"));
156         wml.setContentType("wml");
157         wml.addPortletMode(new PortletMode("HELP"));
158         wml.addPortletMode(new PortletMode("VIEW"));
159         portlet.addContentType(html);
160         portlet.addContentType(wml);
161 
162         app.setWebApplicationDefinition(webApp);
163         portletRegistry.updatePortletApplication(app);
164     }    
165     /*
166      * @see TestCase#tearDown()
167      */
168     protected void tearDown() throws Exception
169     {
170         Iterator itr = portletRegistry.getPortletApplications().iterator();
171         while (itr.hasNext())
172         {
173             portletRegistry.removeApplication((PortletApplicationDefinition) itr.next());
174         }
175     }
176     
177     protected String[] getConfigurations()
178     {
179         return new String[]
180         { "transaction.xml", "registry-test.xml", "prefs.xml", "cache.xml" };
181     }
182     
183 }