1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.prefs;
18  
19  import java.util.prefs.BackingStoreException;
20  import java.util.prefs.Preferences;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
26  
27  /***
28   * <p>
29   * Unit testing for {@link Preferences}.
30   * </p>
31   * 
32   * @author <a href="dlestrat@yahoo.com">David Le Strat </a>
33   */
34  public class TestPreferences extends DatasourceEnabledSpringTestCase
35  {
36  
37      /***
38       * @see junit.framework.TestCase#setUp()
39       */
40      public void setUp() throws Exception
41      {
42          super.setUp();
43        
44          // Make sure we are starting with a clean slate
45          clearChildren(Preferences.userRoot());
46          clearChildren(Preferences.systemRoot());
47      }
48  
49      /***
50       * @see junit.framework.TestCase#tearDown()
51       */
52      public void tearDown() throws Exception
53      {
54          super.tearDown();
55      }
56  
57      /***
58       * @return The test suite.
59       */
60      public static Test suite()
61      {
62          // All methods starting with "test" will be executed in the test suite.
63          return new TestSuite(TestPreferences.class);
64      }
65      
66      /***
67       * <p>
68       * Test user root.
69       * </p>
70       */
71      public void testUserRoot()
72      {
73  
74          Preferences prefs = Preferences.userRoot();
75          if (null != prefs)
76          {
77              assertTrue("expected user root == '/', " + prefs.absolutePath(), prefs.absolutePath().equals("/"));
78          }
79          else
80          {
81              assertTrue("expected user root == '/', " + prefs, false);
82          }
83      }
84  
85      /***
86       * <p>
87       * Test system root.
88       * </p>
89       */
90      public void testSystemRoot()
91      {
92          Preferences prefs = Preferences.systemRoot();
93          if (null != prefs)
94          {
95              assertTrue("expected system root == '/', " + prefs.absolutePath(), prefs.absolutePath().equals("/"));
96          }
97          else
98          {
99              assertTrue("expected system root == '/', " + prefs, false);
100         }
101     }
102 
103     /***
104      * <p>
105      * Test node and whether children exist under a given node.
106      * </p>
107      */
108     public void testNodeAndChildrenNames()
109     {
110         Preferences prefs = Preferences.userRoot();
111         // Test without children.
112         try
113         {
114             String[] childrenNames = prefs.childrenNames();
115             if (childrenNames.length > 0)
116             {
117                 assertTrue("expected no children, " + childrenNames.length + ", " + childrenNames[0],
118                         childrenNames.length == 0);
119             }
120         }
121         catch (BackingStoreException bse)
122         {
123             assertTrue("backing store exception: " + bse, false);
124         }
125 
126         // Absolute path.
127         // 1. The node does not exist. Create it.
128         Preferences prefs0 = Preferences.userRoot().node("/an1/san1");
129         assertNotNull("should not be null", prefs0);
130         assertTrue("expected node == /an1/san1, " + prefs0.absolutePath(), prefs0.absolutePath().equals("/an1/san1"));
131 
132         // 2. If node exists. Get it.
133         Preferences prefs1 = Preferences.userRoot().node("/an1/san1");
134         assertNotNull("should not be null", prefs1);
135         assertTrue("expected node == /an1/san1, " + prefs1.absolutePath(), prefs1.absolutePath().equals("/an1/san1"));
136 
137         // Relative path.
138         Preferences prefs3 = Preferences.userRoot().node("/an1");
139         Preferences prefs4 = prefs3.node("rn1/srn1");
140         assertNotNull("should not be null", prefs4);
141         assertTrue("expected node == /an1/rn1/srn1, " + prefs4.absolutePath(), prefs4.absolutePath().equals(
142                 "/an1/rn1/srn1"));
143 
144         try
145         {
146             String[] childrenNames = prefs3.childrenNames();
147             assertEquals("should have 2 children", 2, childrenNames.length);
148         }
149         catch (BackingStoreException bse)
150         {
151             assertTrue("backing store exception: " + bse, false);
152         }
153 
154         // Remove all nodes.
155         try
156         {
157             prefs3.removeNode();
158         }
159         catch (BackingStoreException bse)
160         {
161             assertTrue("backing store exception: " + bse, false);
162         }
163 
164     }
165 
166     /***
167      * <p>
168      * Test adding properties to a property set node and get property keys for a given node.
169      * </p>
170      * 
171      * @throws Exception
172      */
173     public void testPropertyAndPropertyKeys() throws Exception
174     {
175         // 1. Current node does not have any property associated to it. We are adding
176         // a property at the user root level.
177         // No property has been defined nor added to the node. This should return
178         // the property value
179         Preferences pref0 = Preferences.userRoot();
180         try
181         {
182             String[] propertyKeys = pref0.keys();
183             if (propertyKeys.length > 0)
184             {
185                 assertTrue("expected no children, " + propertyKeys.length + ", " + propertyKeys[0],
186                         propertyKeys.length == 0);
187             }
188         }
189         catch (BackingStoreException bse)
190         {
191             assertTrue("backing store exception: " + bse, false);
192         }
193 
194         pref0.put("propertyName0", "true");
195         String prop = pref0.get("propertyName0", null);
196         assertTrue("should be prop == true.", prop.equals("true"));
197 
198         // 2. Current node has properties associated to it.
199         Preferences pref1 = Preferences.userRoot().node("/user/principal1/propertyset1");
200         pref1.put("propertyName0", "true");
201         String prop1 = pref1.get("propertyName0", null);
202         assertTrue("expected prop1 == true, " + prop1, prop1.equals("true"));
203 
204         // There should be 1 property under pref1.
205         try
206         {
207             String[] propertyKeys = pref1.keys();
208             assertEquals("expected 1 child, ", 1, propertyKeys.length);
209         }
210         catch (BackingStoreException bse)
211         {
212             assertTrue("backing store exception: " + bse, false);
213         }
214 
215         // Test remove property.
216         pref1.remove("propertyName0");
217         prop1 = pref1.get("propertyName0", null);
218         assertNull("should be null.", prop1);
219 
220         // Remove all nodes with properties assigned to property sets.
221         pref1.put("propertyName0", "true");
222         prop1 = pref1.get("propertyName0", null);
223         assertTrue("expected prop1 == true, " + prop1, prop1.equals("true"));
224 
225         try
226         {
227             Preferences pref2 = Preferences.userRoot().node("/user");
228             pref2.removeNode();
229         }
230         catch (BackingStoreException bse)
231         {
232             assertTrue("backing store exception: " + bse, false);
233         }
234 
235     }
236 
237     public void testNodeRemoval() throws Exception
238     {
239         Preferences prefs = Preferences.userRoot();
240 
241         final String test_node = "removeTest";
242 
243         assertFalse(prefs.nodeExists(test_node));
244 
245         Preferences removeNode = prefs.node(test_node);
246 
247         assertNotNull(removeNode);
248 
249         // now remove then re-add and see if a IllegalStateException is thrown
250 
251         removeNode.removeNode();
252         assertFalse(prefs.nodeExists(test_node));
253 
254         try
255         {
256             removeNode.childrenNames();
257             assertFalse("An IllegalStateException should have been thrown by the AbtractPreferences class", true);
258         }
259         catch (IllegalStateException e)
260         {
261 
262         }
263     }
264 
265     /***
266      * <p>
267      * Clear all the children.
268      * </p>
269      * 
270      * @param node
271      * @throws Exception
272      */
273     protected void clearChildren(Preferences node) throws Exception
274     {
275         String[] names = node.childrenNames();
276         for (int i = 0; i < names.length; i++)
277         {
278             node.node(names[i]).removeNode();
279         }
280         // Remove the properties of the current node.
281         String[] keys = node.keys();
282         for (int j = 0; j < keys.length; j++)
283         {
284             node.remove(keys[j]);
285         }
286     }
287 
288     /***
289      * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
290      */
291     protected String[] getConfigurations()
292     {
293         return new String[]
294         { "prefs.xml", "transaction.xml", "cache.xml" };
295     }
296 }