KickJava   Java API By Example, From Geeks To Geeks.

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


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.test.portlet.AbstractPortlet;
14 import org.jboss.portal.junit.result.AssertResult;
15 import org.jboss.portal.junit.result.InvokeURLResult;
16 import org.jboss.portal.junit.Result;
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.PortletPreferences;
23 import javax.portlet.RenderRequest;
24 import javax.portlet.RenderResponse;
25 import javax.portlet.PortletURL;
26 import javax.portlet.ReadOnlyException;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
31  * @version $Revision: 1.1 $
32  */

33 public class ReadOnlyPreferenceCannotBeModifiedPortlet extends AbstractPortlet
34 {
35    public void processAction(final ActionRequest request, ActionResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
36    {
37       AssertResult result = new AssertResult();
38       result.execute(new AssertResult.Test()
39       {
40          public void run() throws Exception JavaDoc
41          {
42             // Get prefs
43
PortletPreferences prefs = request.getPreferences();
44
45             // Test the initial values are ok
46
assertEquals("single_pref_value", prefs.getValue("single_pref", "other"));
47             assertEquals(new String JavaDoc[]{"multi_pref_value_1","multi_pref_value_2"}, prefs.getValues("multi_pref", new String JavaDoc[]{"other"}));
48
49             // Try to modify
50
try
51             {
52                prefs.setValue("single_pref", "");
53                fail();
54             }
55             catch (ReadOnlyException e)
56             {
57                // expected
58
}
59             try
60             {
61                prefs.setValues("single_pref", new String JavaDoc[]{""});
62                fail();
63             }
64             catch (ReadOnlyException e)
65             {
66                // expected
67
}
68             try
69             {
70                prefs.reset("single_pref");
71                fail();
72             }
73             catch (ReadOnlyException e)
74             {
75                // expected
76
}
77             try
78             {
79                prefs.setValue("multi_pref", "");
80                fail();
81             }
82             catch (ReadOnlyException e)
83             {
84                // expected
85
}
86             try
87             {
88                prefs.setValues("multi_pref", new String JavaDoc[]{""});
89                fail();
90             }
91             catch (ReadOnlyException e)
92             {
93                // expected
94
}
95             try
96             {
97                prefs.reset("multi_pref");
98                fail();
99             }
100             catch (ReadOnlyException e)
101             {
102                // expected
103
}
104
105             // Test values have not changed
106
assertEquals("single_pref_value", prefs.getValue("single_pref", "other"));
107             assertEquals(new String JavaDoc[]{"multi_pref_value_1","multi_pref_value_2"}, prefs.getValues("multi_pref", new String JavaDoc[]{"other"}));
108          }
109       });
110       setRequestLocal(request, result);
111    }
112
113    public void render(final RenderRequest request, RenderResponse response) throws PortletException, PortletSecurityException, IOException JavaDoc
114    {
115       Result result = null;
116       int invocationCount = getRequestCount(request);
117       if (invocationCount == 0)
118       {
119          PortletURL url = response.createActionURL();
120          result = new InvokeURLResult(url.toString());
121       }
122       else if (invocationCount == 1)
123       {
124          result = (Result)getRequestLocal(request);
125       }
126
127       //
128
sendResult(response, result);
129    }
130 }
131
Popular Tags