1 9 package org.jboss.portal.test.core; 10 11 import java.util.Arrays ; 12 13 import javax.portlet.ReadOnlyException; 14 15 import junit.framework.TestCase; 16 17 import org.jboss.portlet.JBossPortletPreferences; 18 19 23 public class CorePortletPreferencesTestCase extends TestCase 24 { 25 26 public CorePortletPreferencesTestCase(String key) 27 { 28 super(key); 29 } 30 31 private JBossPortletPreferences prefs; 32 private TestProxy proxy; 33 34 protected void setUp() throws Exception 35 { 36 } 48 49 protected void tearDown() throws Exception 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 []{"value"}); 83 assertTrue(Arrays.equals(new String []{"value"}, proxy.getStringArray(null))); 84 } 85 86 public void testGetNonExistingStringArray() throws ReadOnlyException 87 { 88 assertTrue(Arrays.equals(new String []{"value"}, proxy.getStringArray(new String []{"value"}))); 89 } 90 91 public void testSetNonNullStringArray() 92 { 93 proxy.setStringArray(new String []{"value"}); 94 assertTrue(Arrays.equals(new String []{"value"}, prefs.getValues("StringArray", null))); 95 } 96 97 public void testSetNullStringArray() throws ReadOnlyException 98 { 99 prefs.setValues("StringArray", new String []{"value1"}); 100 proxy.setStringArray(null); 101 assertTrue(Arrays.equals(new String []{"value2"}, prefs.getValues("StringArray", new String []{"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 []{"0","1","2"}, prefs.getValues("IntArray", null))); 128 } 129 130 public void testGetExistingIntArray() throws ReadOnlyException 131 { 132 prefs.setValues("IntArray", new String []{"0","1","2"}); 133 assertTrue(Arrays.equals(new int[]{0,1,2}, proxy.getIntArray(new int[]{-1}))); 134 135 prefs.setValues("IntArray", new String []{"not a number"}); 136 assertTrue(Arrays.equals(new int[]{-1}, proxy.getIntArray(new int[]{-1}))); 137 138 prefs.setValues("IntArray", new String []{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 []{"true","false"}, prefs.getValues("BooleanArray", null))); 171 } 172 173 public void testGetExistingBooleanArray() throws ReadOnlyException 174 { 175 prefs.setValues("BooleanArray", new String []{"true","false"}); 176 assertTrue(Arrays.equals(new boolean[]{true,false}, proxy.getBooleanArray(new boolean[]{true}))); 177 178 prefs.setValues("BooleanArray", new String []{"not a boolean"}); 179 assertTrue(Arrays.equals(new boolean[]{true}, proxy.getBooleanArray(new boolean[]{true}))); 180 181 prefs.setValues("BooleanArray", new String []{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 getString(String dflt); 194 boolean getBoolean(boolean dflt); 195 int[] getIntArray(int[] dflt); 196 String [] getStringArray(String [] dflt); 197 boolean[] getBooleanArray(boolean[] dflt); 198 199 void setString(String value); 200 void setStringArray(String [] 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 |