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.portletentity;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Locale;
23  import java.util.prefs.Preferences;
24  
25  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
26  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
27  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
28  import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
29  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
30  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
31  import org.apache.jetspeed.om.common.preference.PreferenceSetComposite;
32  import org.apache.jetspeed.om.page.ContentFragment;
33  import org.apache.jetspeed.om.page.Fragment;
34  import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
35  import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
36  import org.apache.jetspeed.om.servlet.impl.WebApplicationDefinitionImpl;
37  import org.apache.jetspeed.util.JetspeedObjectID;
38  import org.apache.pluto.om.portlet.PortletApplicationDefinition;
39  import org.apache.pluto.om.portlet.PortletDefinitionList;
40  import org.jmock.Mock;
41  import org.jmock.core.matcher.InvokeAtLeastOnceMatcher;
42  import org.jmock.core.stub.ReturnStub;
43  
44  /***
45   * <p>
46   * TestPortletEntityDAO
47   * </p>
48   * 
49   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
50   * @version $Id: TestPortletEntityDAO.java,v 1.3 2005/05/24 14:43:19 ate Exp $
51   */
52  public class TestPortletEntityDAO extends DatasourceEnabledSpringTestCase
53  {
54      private static final String TEST_APP = "EntityTestApp";
55  
56      private static final String TEST_PORTLET = "EntityTestPortlet";
57  
58      private static final String TEST_ENTITY = "user5/entity-9";
59  
60      private PersistenceBrokerPortletEntityAccess entityAccess = null;
61  
62      private PortletRegistry registry;
63  
64      protected void setUp() throws Exception
65      {
66          super.setUp();
67          this.registry = (PortletRegistry) ctx.getBean("portletRegistry");
68          this.entityAccess = (PersistenceBrokerPortletEntityAccess) ctx.getBean("portletEntityAccessImpl");
69  
70          teardownTestData();
71          setupTestData();
72      }
73  
74      protected void tearDown() throws Exception
75      {
76          teardownTestData();
77      }
78  
79      public void test1() throws Exception
80      {
81          assertNotNull(this.entityAccess);
82          assertNotNull(this.registry);
83      }
84  
85      public void testEntities() throws Exception
86      {
87          PortletApplicationDefinition pa = registry.getPortletApplication(TEST_APP);
88          assertNotNull("Portlet Application", pa);
89          System.out.println("pa = " + pa.getId());
90          PortletDefinitionList portlets = pa.getPortletDefinitionList(); // .get(JetspeedObjectID.createFromString(TEST_PORTLET));
91          Iterator pi = portlets.iterator();
92          PortletDefinitionComposite pd = null;
93          while (pi.hasNext())
94          {
95              pd = (PortletDefinitionComposite) pi.next();
96              assertTrue("Portlet Def not found", pd.getName().equals("EntityTestPortlet"));
97          }
98          assertNotNull("Portlet Def is null", pd);
99  
100         Mock mockf1 = new Mock(Fragment.class);
101         mockf1.expects(new InvokeAtLeastOnceMatcher()).method("getName").will(new ReturnStub(pd.getUniqueName()));
102         mockf1.expects(new InvokeAtLeastOnceMatcher()).method("getId").will(new ReturnStub(TEST_ENTITY));
103         ContentFragment f1 = new ContentFragmentTestImpl((Fragment) mockf1.proxy(), new HashMap());
104 
105         MutablePortletEntity entity = entityAccess
106                 .generateEntityFromFragment(new ContentFragmentTestImpl(f1, new HashMap()));
107         PreferenceSetComposite prefs = (PreferenceSetComposite) entity.getPreferenceSet();
108         prefs.remove("pref1");
109         assertNotNull(prefs);
110         assertNull(prefs.get("pref1"));
111 
112         // test adding a pref
113         prefs.add("pref1", Arrays.asList(new String[]
114         { "1" }));
115         assertNotNull(prefs.get("pref1"));
116 
117         // Remove should return the deleted pref
118         assertNotNull(prefs.remove("pref1"));
119 
120         // Should be gone
121         assertNull(prefs.get("pref1"));
122 
123         // Add it back so we can test tole back
124         prefs.add("pref1", Arrays.asList(new String[]
125         { "1" }));
126 
127         entityAccess.storePortletEntity(entity);
128 
129         prefs = (PreferenceSetComposite) entity.getPreferenceSet();
130 
131         assertNotNull(prefs.get("pref1"));
132 
133         PreferenceComposite pref = (PreferenceComposite) prefs.get("pref1");
134 
135         assertEquals("1", pref.getValueAt(0));
136 
137         pref.setValueAt(0, "2");
138 
139         assertEquals("2", pref.getValueAt(0));
140 
141         entity.reset();
142 
143         pref = (PreferenceComposite) prefs.get("pref1");
144 
145         assertEquals("1", pref.getValueAt(0));
146 
147         prefs.remove(pref);
148 
149         assertNull(prefs.get("pref1"));
150 
151         entity.reset();
152 
153         assertNotNull(prefs.get("pref1"));
154 
155         prefs.add("pref2", Arrays.asList(new String[]
156         { "2", "3" }));
157 
158         entity.store();
159 
160         PreferenceComposite pref2 = (PreferenceComposite) prefs.get("pref2");
161 
162         assertNotNull(pref2);
163 
164         Iterator prefsValues = pref2.getValues();
165         int count = 0;
166         while (prefsValues.hasNext())
167         {
168             prefsValues.next();
169             count++;
170         }
171 
172         assertEquals(2, count);
173 
174         pref2.addValue("4");
175         prefsValues = pref2.getValues();
176         count = 0;
177         while (prefsValues.hasNext())
178         {
179             assertEquals(String.valueOf(count + 2), prefsValues.next());
180             count++;
181         }
182         assertEquals(3, count);
183 
184         entity.reset();
185 
186         prefsValues = pref2.getValues();
187         count = 0;
188         while (prefsValues.hasNext())
189         {
190             assertEquals(String.valueOf(count + 2), prefsValues.next());
191             count++;
192         }
193         assertEquals(2, count);
194 
195         // testing preferences null values assignments fix, issue JS2-607
196         pref2.setValueAt(0, null);        
197         assertNull("pref2.value[0] should be null", pref2.getValueAt(0));        
198         String[] values = pref2.getValueArray();
199         assertEquals(2, values.length);
200         assertNull("pref2.value[0] should be null", values[0]);
201         assertEquals("3", values[1]);
202         pref2.setValues(new String[]{"2",null,"3"});
203         assertNull("pref2.value[1] should be null", pref2.getValueAt(1));
204         values = pref2.getValueArray();
205         assertEquals(3, values.length);
206         assertEquals("2", values[0]);
207         assertNull("pref2.value[1] should be null", values[1]);
208         assertEquals("3", values[2]);
209         assertTrue(pref2.isValueSet());
210         pref2.setValues((String[])null);
211         assertFalse(pref2.isValueSet());
212         assertTrue(pref2.getValueArray().length == 0);
213         entity.reset();
214         assertTrue(pref2.getValueArray().length == 2);
215         pref2.setValues(new String[]{});
216         assertFalse(pref2.isValueSet());
217         assertTrue(pref2.getValueArray().length == 0);
218         entity.reset();
219         assertTrue(pref2.getValueArray().length == 2);
220 
221         MutablePortletEntity entity2 = entityAccess.getPortletEntityForFragment(f1);
222         assertTrue("entity id ", entity2.getId().toString().equals(TEST_ENTITY));
223         assertNotNull("entity's portlet ", entity2.getPortletDefinition());
224         mockf1.verify();
225 
226         Mock mockf2 = new Mock(Fragment.class);
227         mockf2.expects(new InvokeAtLeastOnceMatcher()).method("getName").will(new ReturnStub(pd.getUniqueName()));
228         ContentFragment f2 = new ContentFragmentTestImpl((Fragment) mockf2.proxy(), new HashMap());
229 
230         MutablePortletEntity entity5 = entityAccess.newPortletEntityInstance(pd);
231 
232         System.out.println("before storing entity: " + entity5.getId());
233 
234         entityAccess.storePortletEntity(entity5);
235         System.out.println("store done: " + entity5.getId());
236         mockf2.expects(new InvokeAtLeastOnceMatcher()).method("getId").will(new ReturnStub(entity5.getId().toString()));
237 
238         MutablePortletEntity entity6 = entityAccess.getPortletEntityForFragment(f2);
239         assertNotNull(entity6);
240         System.out.println("reget : " + entity6.getId());
241 
242         entityAccess.removePortletEntity(entity6);
243 
244     }
245 
246     private void teardownTestData() throws Exception
247     {
248 
249         JetspeedObjectID objId = JetspeedObjectID.createFromString(TEST_ENTITY);
250         MutablePortletEntity entity = entityAccess.getPortletEntity(objId);
251         System.out.println("entity == " + entity);
252 
253         if (entity != null)
254         {
255             entityAccess.removePortletEntity(entity);
256         }
257 
258         PortletApplicationDefinition pa = registry.getPortletApplication(TEST_APP);
259         System.out.println("pa == " + pa);
260         if (pa != null)
261         {
262             registry.removeApplication(pa);
263         }
264 
265         if (Preferences.systemRoot().nodeExists(MutablePortletApplication.PREFS_ROOT))
266         {
267             Preferences.systemRoot().node(MutablePortletApplication.PREFS_ROOT).removeNode();
268         }
269 
270         if (Preferences.userRoot().nodeExists(PortletDefinitionComposite.PORTLETS_PREFS_ROOT))
271         {
272             Preferences.userRoot().node(PortletDefinitionComposite.PORTLETS_PREFS_ROOT).removeNode();
273         }
274 
275         if (Preferences.userRoot().nodeExists(MutablePortletEntity.PORTLET_ENTITY_ROOT))
276         {
277             Preferences.userRoot().node(MutablePortletEntity.PORTLET_ENTITY_ROOT).removeNode();
278         }
279 
280     }
281 
282     private void setupTestData() throws Exception
283     {
284 
285         PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
286         app.setName(TEST_APP);
287         app.setApplicationIdentifier(TEST_APP);
288 
289         WebApplicationDefinitionImpl webApp = new WebApplicationDefinitionImpl();
290         webApp.setContextRoot("/app1");
291         webApp.addDescription(Locale.FRENCH, "Description: Le fromage est dans mon pantalon!");
292         webApp.addDisplayName(Locale.FRENCH, "Display Name: Le fromage est dans mon pantalon!");
293 
294         PortletDefinitionComposite portlet = new PortletDefinitionImpl();
295         portlet.setClassName("org.apache.Portlet");
296         portlet.setName(TEST_PORTLET);
297         portlet.addDescription(Locale.getDefault(), "Portlet description.");
298         portlet.addDisplayName(Locale.getDefault(), "Portlet display Name.");
299 
300         portlet.addInitParameter("testparam", "test value", "This is a test portlet parameter", Locale.getDefault());
301 
302         app.addPortletDefinition(portlet);
303 
304         app.setWebApplicationDefinition(webApp);
305 
306         PreferenceSetComposite prefSet = (PreferenceSetComposite) portlet.getPreferenceSet();
307         prefSet.add("pref1", Arrays.asList(new String[]
308         { "1" }));
309 
310         registry.registerPortletApplication(app);
311     }
312 
313     protected String[] getConfigurations()
314     {
315         return new String[]
316         { "transaction.xml", "registry-test.xml", "prefs.xml", "cache.xml" };
317     }
318 }