KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > imp > TestPortletPreferences


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.portletcontainer.imp;
6
7
8 import javax.portlet.PreferencesValidator;
9 import javax.portlet.ReadOnlyException;
10 import javax.portlet.ValidatorException;
11 import org.exoplatform.container.PortalContainer;
12 import org.exoplatform.services.portletcontainer.helper.PortletWindowInternal;
13 import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy;
14 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.PortletPreferencesImp;
15 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.persistenceImp.DefaultPersistenceManager;
16 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.persistenceImp.PersistenceManager;
17 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
18 import org.exoplatform.services.portletcontainer.pci.Input;
19 import org.exoplatform.services.portletcontainer.pci.model.ExoPortletPreferences;
20 import org.exoplatform.services.portletcontainer.pci.model.Portlet;
21 import java.io.IOException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Map JavaDoc;
24 /**
25  * Created by The eXo Platform SARL .
26  * Author : Mestrallet Benjamin
27  * benjmestrallet@users.sourceforge.net
28  * Date: Oct 6, 2003
29  * Time: 1:03:37 PM
30  */

31 public class TestPortletPreferences extends BaseTest {
32
33   javax.portlet.PortletPreferences pP;
34
35   PortletApplicationProxy proxy;
36   private ExoWindowID windowID;
37
38
39   public TestPortletPreferences(String JavaDoc s) {
40     super(s);
41   }
42
43     public void setUp() throws Exception JavaDoc {
44         super.setUp();
45     proxy = (PortletApplicationProxy) PortalContainer.getInstance().
46                                       getComponentInstance("hello");
47     ExoPortletPreferences prefs = ((Portlet) portletApp_.getPortlet().get(0)).getPortletPreferences();
48     PreferencesValidator validator = proxy.getValidator(prefs.getPreferencesValidator());
49     
50     windowID = new ExoWindowID("exotest:/hello/HelloWorld/banner");
51     pP = new PortletPreferencesImp(validator, prefs, windowID, persister);
52     }
53
54   public void testMap() {
55     Map JavaDoc map = pP.getMap();
56     assertTrue("Map non empty is false", !map.isEmpty());
57   }
58
59   public void testNames() {
60     Enumeration JavaDoc e = pP.getNames();
61     while (e.hasMoreElements()) {
62       e.nextElement();
63     }
64   }
65
66   public void testGetValue() {
67     assertEquals("DEFAULT", pP.getValue("not_exist", "DEFAULT"));
68     assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops"));
69     assertEquals("HH", pP.getValue("time-format", "ops"));
70   }
71
72   public void testGetValues() {
73     assertEquals("DEFAULT", pP.getValues("not_exist", new String JavaDoc[]{"DEFAULT"})[0]);
74     String JavaDoc[] arr = pP.getValues("time-format", new String JavaDoc[]{});
75     assertTrue("HH".equals(arr[0]));
76   }
77
78   public void testSetValue() throws ReadOnlyException {
79     //test a simple set value on a non read only preference (no tag)
80
pP.setValue("time-server", "MyValue");
81     assertEquals("MyValue", pP.getValue("time-server", "ops"));
82
83     //test a simple set value on an other non read only preference (with tag)
84
pP.setValue("time-format", "MyFormat");
85     assertTrue(pP.getValues("time-format", new String JavaDoc[]{}).length == 1);
86
87     //test a simple set value on a read only preference
88
try {
89       pP.setValue("port", "NotPossible");
90     } catch (ReadOnlyException e) {
91       assertEquals("the value port can not be changed", e.getMessage());
92     }
93
94     //add a new preference (not defined in xml)
95
pP.setValue("NewValue", "MyNewValue");
96     assertEquals("MyNewValue", pP.getValue("NewValue", "ops"));
97
98     //test xcix : manage the previously added preference as a non read only pref
99
pP.setValue("NewValue", "MyNewValue2");
100     assertEquals("MyNewValue2", pP.getValue("NewValue", "ops"));
101   }
102
103   public void testSetValues() throws ReadOnlyException {
104     //test a simple set values on a non read only preference (no tag)
105
pP.setValues("time-server", new String JavaDoc[] {"val1", "val2"});
106     assertTrue(pP.getValues("time-server", new String JavaDoc[]{}).length == 2);
107
108     //test a simple set values on an other non read only preference (with tag)
109
pP.setValues("time-format", new String JavaDoc[] {"val1", "val2"});
110     assertTrue(pP.getValues("time-format", new String JavaDoc[]{}).length == 2);
111
112     //test a simple set values on a read only preference
113
try {
114       pP.setValues("port", new String JavaDoc[]{"NotPossible"});
115     } catch (ReadOnlyException e) {
116       assertEquals("the value port can not be changed", e.getMessage());
117     }
118
119     //add a new preference (not defined in xml)
120
pP.setValues("NewValue", new String JavaDoc[]{"MyNewValue1","MyNewValue2"});
121     assertEquals("MyNewValue1", pP.getValues("NewValue", new String JavaDoc[]{"ops"})[0]);
122
123     //test xcix : manage the previously added preference as a non read only pref
124
pP.setValues("NewValue", new String JavaDoc[]{"MyNewValue1bis","MyNewValue2bis"});
125     assertEquals("MyNewValue2bis", pP.getValues("NewValue", new String JavaDoc[]{"ops"})[1]);
126   }
127
128   public void testIsReadOnly(){
129     assertTrue(!pP.isReadOnly("not_exist"));
130     assertTrue(pP.isReadOnly("port"));
131     assertTrue(!pP.isReadOnly("time-server"));
132     assertTrue(!pP.isReadOnly("time-format"));
133   }
134
135   public void testReset() throws ReadOnlyException {
136     //test a reset of a value with default (means present in portlet.xml)
137
pP.setValue("time-server", "MyValueToBeReseted");
138     assertEquals("MyValueToBeReseted", pP.getValue("time-server", "ops"));
139     pP.reset("time-server");
140     assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops"));
141
142     //test a reset of a value with no default (means not present in portlet.xml)
143
//test xci
144
pP.setValue("NewValue", "MyValueToBeDeleted");
145     assertEquals("MyValueToBeDeleted", pP.getValue("NewValue", "ops"));
146     pP.reset("NewValue");
147     assertEquals("ops", pP.getValue("NewValue", "ops"));
148     
149     //test a reset of a null value with no default
150
pP.setValue("NewValue", null);
151     assertNull(pP.getValue("NewValue", "ops"));
152     pP.reset("NewValue");
153     assertEquals("ops", pP.getValue("NewValue", "ops"));
154     
155     //test a reset of a null value with a default
156
pP.setValue("time-server", null);
157     assertNull(pP.getValue("time-server", "ops"));
158     pP.reset("time-server");
159     assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops"));
160   }
161
162   public void testStore() throws ReadOnlyException, IOException JavaDoc, ValidatorException {
163     pP.setValue("param-1", "value-1") ;
164     ((PortletPreferencesImp)pP).setMethodCalledIsAction(true);
165     pP.store();
166
167     PersistenceManager manager =
168         (PersistenceManager) PortalContainer.getInstance().
169         getComponentInstance(PersistenceManager.class);
170     Input input = new Input();
171     input.setWindowID(windowID);
172     PortletWindowInternal pwi = manager.getWindow(input, null);
173     assertEquals("value-1", pwi.getPreferences().getValue("param-1", "opts"));
174   }
175
176   public void testStateChangeFlag(){
177     ((PortletPreferencesImp)pP).setMethodCalledIsAction(true);
178     ((PortletPreferencesImp)pP).setStateChangeAuthorized(false);
179     try {
180       pP.store();
181       fail("Sate change should NOT be authorized");
182     } catch (Throwable JavaDoc t) {
183     }
184   }
185 }
Popular Tags