KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > core > CorePortletPreferencesTestCase


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

9 package org.jboss.portal.test.core;
10
11 import java.util.Arrays JavaDoc;
12
13 import javax.portlet.ReadOnlyException;
14
15 import junit.framework.TestCase;
16
17 import org.jboss.portlet.JBossPortletPreferences;
18
19 /**
20  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
21  * @version $Revision: 1.2 $
22  */

23 public class CorePortletPreferencesTestCase extends TestCase
24 {
25
26    public CorePortletPreferencesTestCase(String JavaDoc key)
27    {
28       super(key);
29    }
30
31    private JBossPortletPreferences prefs;
32    private TestProxy proxy;
33
34    protected void setUp() throws Exception JavaDoc
35    {
36 // Class proxyClass = Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), new Class[]{TestProxy.class});
37
// ProxyInfo info = new ProxyInfo(proxyClass);
38
// PreferenceStore store = new AbstractPreferenceStore();
39
// PreferenceSet set = store.get(new FQN("ns"));
40
// prefs = new JBossPortletPreferences(
41
// new PreferenceSet[]{set},
42
// null,
43
// PortletPreferencesImpl.ACTION,
44
// info
45
// );
46
// proxy = (TestProxy)prefs.getProxy();
47
}
48
49    protected void tearDown() throws Exception JavaDoc
50    {
51       proxy = null;
52       prefs = null;
53    }
54
55    public void testSetNonNullString()
56    {
57       proxy.setString("value");
58       assertEquals("value", prefs.getValue("String", null));
59    }
60
61    public void testSetNullString() throws ReadOnlyException
62    {
63       prefs.setValue("String", "value1");
64       proxy.setString(null);
65       assertEquals("value2", prefs.getValue("String", "value2"));
66    }
67
68    public void testGetExistingString() throws ReadOnlyException
69    {
70       prefs.setValue("String", "value");
71       assertEquals("value", proxy.getString(null));
72    }
73
74    public void testGetNonExistingString() throws ReadOnlyException
75    {
76       assertEquals("value", proxy.getString("value"));
77       assertEquals(null, proxy.getString(null));
78    }
79
80    public void testGetExistingStringArray() throws ReadOnlyException
81    {
82       prefs.setValues("StringArray", new String JavaDoc[]{"value"});
83       assertTrue(Arrays.equals(new String JavaDoc[]{"value"}, proxy.getStringArray(null)));
84    }
85
86    public void testGetNonExistingStringArray() throws ReadOnlyException
87    {
88       assertTrue(Arrays.equals(new String JavaDoc[]{"value"}, proxy.getStringArray(new String JavaDoc[]{"value"})));
89    }
90
91    public void testSetNonNullStringArray()
92    {
93       proxy.setStringArray(new String JavaDoc[]{"value"});
94       assertTrue(Arrays.equals(new String JavaDoc[]{"value"}, prefs.getValues("StringArray", null)));
95    }
96
97    public void testSetNullStringArray() throws ReadOnlyException
98    {
99       prefs.setValues("StringArray", new String JavaDoc[]{"value1"});
100       proxy.setStringArray(null);
101       assertTrue(Arrays.equals(new String JavaDoc[]{"value2"}, prefs.getValues("StringArray", new String JavaDoc[]{"value2"})));
102    }
103
104    public void testSetInt()
105    {
106       proxy.setInt(1);
107       assertEquals("1", prefs.getValue("Int", null));
108    }
109
110    public void testGetExistingInt() throws ReadOnlyException
111    {
112       prefs.setValue("Int", "1");
113       assertEquals(1, proxy.getInt(0));
114
115       prefs.setValue("Int", "not a number");
116       assertEquals(2, proxy.getInt(2));
117    }
118
119    public void testGetNonExistingInt() throws ReadOnlyException
120    {
121       assertEquals(1, proxy.getInt(1));
122    }
123
124    public void testSetIntArray()
125    {
126       proxy.setIntArray(new int[]{0,1,2});
127       assertTrue(Arrays.equals(new String JavaDoc[]{"0","1","2"}, prefs.getValues("IntArray", null)));
128    }
129
130    public void testGetExistingIntArray() throws ReadOnlyException
131    {
132       prefs.setValues("IntArray", new String JavaDoc[]{"0","1","2"});
133       assertTrue(Arrays.equals(new int[]{0,1,2}, proxy.getIntArray(new int[]{-1})));
134
135       prefs.setValues("IntArray", new String JavaDoc[]{"not a number"});
136       assertTrue(Arrays.equals(new int[]{-1}, proxy.getIntArray(new int[]{-1})));
137
138       prefs.setValues("IntArray", new String JavaDoc[]{null});
139       assertTrue(Arrays.equals(new int[]{-1}, proxy.getIntArray(new int[]{-1})));
140    }
141
142    public void testGetNonExistingIntArray() throws ReadOnlyException
143    {
144       assertTrue(Arrays.equals(new int[]{-1}, proxy.getIntArray(new int[]{-1})));
145    }
146
147    public void testSetBoolean()
148    {
149       proxy.setBoolean(true);
150       assertEquals("true", prefs.getValue("Boolean", null));
151    }
152
153    public void testGetExistingBoolean() throws ReadOnlyException
154    {
155       prefs.setValue("Boolean", "true");
156       assertEquals(true, proxy.getBoolean(false));
157
158       prefs.setValue("Boolean", "not a boolean");
159       assertEquals(true, proxy.getBoolean(true));
160    }
161
162    public void testGetNonExistingBoolean() throws ReadOnlyException
163    {
164       assertEquals(true, proxy.getBoolean(true));
165    }
166
167    public void testSetBooleanArray()
168    {
169       proxy.setBooleanArray(new boolean[]{true,false});
170       assertTrue(Arrays.equals(new String JavaDoc[]{"true","false"}, prefs.getValues("BooleanArray", null)));
171    }
172
173    public void testGetExistingBooleanArray() throws ReadOnlyException
174    {
175       prefs.setValues("BooleanArray", new String JavaDoc[]{"true","false"});
176       assertTrue(Arrays.equals(new boolean[]{true,false}, proxy.getBooleanArray(new boolean[]{true})));
177
178       prefs.setValues("BooleanArray", new String JavaDoc[]{"not a boolean"});
179       assertTrue(Arrays.equals(new boolean[]{true}, proxy.getBooleanArray(new boolean[]{true})));
180
181       prefs.setValues("BooleanArray", new String JavaDoc[]{null});
182       assertTrue(Arrays.equals(new boolean[]{true}, proxy.getBooleanArray(new boolean[]{true})));
183    }
184
185    public void testGetNonExistingBooleanArray() throws ReadOnlyException
186    {
187       assertTrue(Arrays.equals(new boolean[]{true}, proxy.getBooleanArray(new boolean[]{true})));
188    }
189
190    public interface TestProxy
191    {
192       int getInt(int dflt);
193       String JavaDoc getString(String JavaDoc dflt);
194       boolean getBoolean(boolean dflt);
195       int[] getIntArray(int[] dflt);
196       String JavaDoc[] getStringArray(String JavaDoc[] dflt);
197       boolean[] getBooleanArray(boolean[] dflt);
198
199       void setString(String JavaDoc value);
200       void setStringArray(String JavaDoc[] value);
201       void setInt(int value);
202       void setIntArray(int[] value);
203       void setBoolean(boolean value);
204       void setBooleanArray(boolean[] value);
205    }
206 }
207
Popular Tags