KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > portlet > preferences > spec > StoreMustPersistAllChangesPortlet


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Forums JBoss Portlet *
6  * *
7  * Distributable under LGPL license. *
8  * See terms of license at gnu.org. *
9  * *
10  *****************************************/

11 package org.jboss.portal.test.portlet.preferences.spec;
12
13 import org.jboss.portal.junit.result.InvokeURLResult;
14 import org.jboss.portal.junit.result.AssertResult;
15 import org.jboss.portal.junit.Result;
16 import org.jboss.portal.test.portlet.AbstractPortlet;
17
18 import javax.portlet.ActionRequest;
19 import javax.portlet.ActionResponse;
20 import javax.portlet.PortletException;
21 import javax.portlet.PortletSecurityException;
22 import javax.portlet.RenderRequest;
23 import javax.portlet.RenderResponse;
24 import javax.portlet.PortletURL;
25 import javax.portlet.PortletPreferences;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
30  * @version $Revision: 1.1 $
31  */

32 public class StoreMustPersistAllChangesPortlet extends AbstractPortlet
33 {
34
35    public void processAction(final ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
36    {
37       AssertResult result = new AssertResult();
38       int requestCount = getRequestCount(request);
39       if (requestCount == 1)
40       {
41          result = new AssertResult();
42          result.execute(new AssertResult.Test()
43          {
44             public void run() throws Exception JavaDoc
45             {
46                // Get prefs
47
PortletPreferences prefs = request.getPreferences();
48
49                // Check the initial value are good
50
assertEquals("static_single_pref_value", prefs.getValue("static_single_pref", "other"));
51                assertEquals(new String JavaDoc[]{"static_multi_pref_value_1","static_multi_pref_value_2"}, prefs.getValues("static_multi_pref", new String JavaDoc[]{"other"}));
52                assertEquals("other", prefs.getValue("dynamic_single_pref", "other"));
53                assertEquals(new String JavaDoc[]{"other"}, prefs.getValues("dynamic_multi_pref", new String JavaDoc[]{"other"}));
54
55                // Set values
56
prefs.setValue("static_single_pref", "new_static_single_pref_value");
57                prefs.setValues("static_multi_pref", new String JavaDoc[]{"new_static_multi_pref_value_1","new_static_multi_pref_value_2"});
58                prefs.setValue("dynamic_single_pref", "new_dynamic_single_pref_value");
59                prefs.setValues("dynamic_multi_pref", new String JavaDoc[]{"new_dynamic_multi_pref_value_1","new_dynamic_multi_pref_value_2"});
60
61                // Store
62
prefs.store();
63             }
64          });
65          setRequestLocal(request, result);
66       }
67       if (requestCount == 2)
68       {
69          result = (AssertResult)getRequestLocal(request);
70          result.execute(new AssertResult.Test()
71          {
72             public void run() throws Exception JavaDoc
73             {
74                // Get prefs
75
PortletPreferences prefs = request.getPreferences();
76
77                // Check the new value are good
78
assertEquals("new_static_single_pref_value", prefs.getValue("static_single_pref", "other"));
79                assertEquals(new String JavaDoc[]{"new_static_multi_pref_value_1","new_static_multi_pref_value_2"}, prefs.getValues("static_multi_pref", new String JavaDoc[]{"other"}));
80                assertEquals("new_dynamic_single_pref_value", prefs.getValue("dynamic_single_pref", "other"));
81                assertEquals(new String JavaDoc[]{"new_dynamic_multi_pref_value_1","new_dynamic_multi_pref_value_2"}, prefs.getValues("dynamic_multi_pref", new String JavaDoc[]{"other"}));
82             }
83          });
84       }
85    }
86
87    public void render(final RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
88    {
89       Result result = null;
90       int invocationCount = getRequestCount(request);
91       if (invocationCount == 0)
92       {
93          PortletURL url = response.createActionURL();
94          result = new InvokeURLResult(url.toString());
95       }
96       else if (invocationCount == 1)
97       {
98          PortletURL url = response.createActionURL();
99          result = new InvokeURLResult(url.toString());
100       }
101       else if (invocationCount == 2)
102       {
103          result = (Result)getRequestLocal(request);
104       }
105
106       //
107
sendResult(response, result);
108    }
109 }
110
Popular Tags