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.Iterator;
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  import org.apache.jetspeed.prefs.om.Node;
27  import org.apache.jetspeed.prefs.om.Property;
28  
29  /***
30   * <p>
31   * Unit testing for {@link Preferences}.
32   * </p>
33   * 
34   * @author <a href="dlestrat@yahoo.com">David Le Strat </a>
35   */
36  public class TestPreferencesProvider extends DatasourceEnabledSpringTestCase
37  {
38      private PreferencesProvider provider;
39  
40      /***
41       * @see junit.framework.TestCase#setUp()
42       */
43      public void setUp() throws Exception
44      {
45          super.setUp();
46          provider = (PreferencesProvider) ctx.getBean("prefsProvider");
47  
48          // Make sure we are starting with a clean slate
49          clearChildren(Preferences.userRoot());
50          clearChildren(Preferences.systemRoot());
51      }
52  
53      /***
54       * @see junit.framework.TestCase#tearDown()
55       */
56      public void tearDown() throws Exception
57      {
58          super.tearDown();
59      }
60  
61      /***
62       * @return The test suite.
63       */
64      public static Test suite()
65      {
66          // All methods starting with "test" will be executed in the test suite.
67          return new TestSuite(TestPreferencesProvider.class);
68      }
69  
70      public void testLookupProperty() throws Exception
71      {
72          Preferences info = Preferences.userRoot().node("/user/dynamite/userinfo");
73          info.put("user.name.family", "Dynamite");
74          info.put("user.name.given", "Napolean");
75          info.put("user.email", "napolean@dynamite.xxx");
76          info.flush();
77  
78          Iterator result = provider.lookupPreference("userinfo", "user.email", "napolean@dynamite.xxx").iterator();
79          int count = 0;
80          while (result.hasNext())
81          {
82              Node node = (Node) result.next();
83              System.out.println("node = " + node.getFullPath());
84              Iterator props = node.getNodeProperties().iterator();
85              while (props.hasNext())
86              {
87                  Property prop = (Property) props.next();
88                  String name = prop.getPropertyName();
89                  String value = prop.getPropertyValue();
90                  if ("user.name.family".equals(name))
91                  {
92                      assertTrue("family name wrong " + value, "Dynamite".equals(value));
93                  }
94                  else if ("user.name.given".equals(name))
95                  {
96                      assertTrue("given name wrong " + value, "Napolean".equals(value));
97                  }
98                  else if ("user.email".equals(name))
99                  {
100                     assertTrue("email is wrong " + value, "napolean@dynamite.xxx".equals(value));
101                 }       
102                 else
103                 {
104                     assertTrue("bad property name " + name, false);
105                 }
106             }
107             count++;
108         }
109         assertTrue("test-1: count is one " + count, count == 1);
110     }
111 
112     /***
113      * <p>
114      * Clears all test data.
115      * </p>
116      * 
117      * @param node
118      * @throws Exception
119      */
120     protected void clearChildren(Preferences node) throws Exception
121     {
122         String[] names = node.childrenNames();
123         for (int i = 0; i < names.length; i++)
124         {
125             node.node(names[i]).removeNode();
126         }
127     }
128 
129     /***
130      * @see org.apache.jetspeed.components.test.AbstractSpringTestCase#getConfigurations()
131      */
132     protected String[] getConfigurations()
133     {
134         return new String[] { "prefs.xml", "transaction.xml", "cache.xml" };
135     }
136 }