KickJava   Java API By Example, From Geeks To Geeks.

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


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 ChangesMadeButNotStoredAreDiscardedPortlet 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                // Check wit new values
62
assertEquals("new_static_single_pref_value", prefs.getValue("static_single_pref", "other"));
63                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"}));
64                assertEquals("new_dynamic_single_pref_value", prefs.getValue("dynamic_single_pref", "other"));
65                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"}));
66             }
67          });
68          setRequestLocal(request, result);
69       }
70       if (requestCount == 2)
71       {
72          result = (AssertResult)getRequestLocal(request);
73          result.execute(new AssertResult.Test()
74          {
75             public void run() throws Exception JavaDoc
76             {
77                // Get prefs
78
PortletPreferences prefs = request.getPreferences();
79
80                // Check we have the original values back
81
assertEquals("static_single_pref_value", prefs.getValue("static_single_pref", "other"));
82                assertEquals(new String JavaDoc[]{"static_multi_pref_value_1","static_multi_pref_value_2"}, prefs.getValues("static_multi_pref", new String JavaDoc[]{"other"}));
83                assertEquals("other", prefs.getValue("dynamic_single_pref", "other"));
84                assertEquals(new String JavaDoc[]{"other"}, prefs.getValues("dynamic_multi_pref", new String JavaDoc[]{"other"}));
85             }
86          });
87       }
88    }
89
90    public void render(final RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
91    {
92       Result result = null;
93       int invocationCount = getRequestCount(request);
94       if (invocationCount == 0)
95       {
96          PortletURL url = response.createActionURL();
97          result = new InvokeURLResult(url.toString());
98       }
99       else if (invocationCount == 1)
100       {
101          PortletURL url = response.createActionURL();
102          result = new InvokeURLResult(url.toString());
103       }
104       else if (invocationCount == 2)
105       {
106          result = (Result)getRequestLocal(request);
107       }
108
109       //
110
sendResult(response, result);
111    }
112 }
113
Popular Tags