1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.prefs;
18  
19  
20  import java.util.prefs.BackingStoreException;
21  import java.util.prefs.Preferences;
22  
23  import org.apache.ldap.server.AbstractAdminTestCase;
24  
25  
26  /***
27   * Tests the ServerSystemPreferences class.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   * @version $Rev: 264732 $
31   */
32  public class ServerSystemPreferencesTest extends AbstractAdminTestCase
33  {
34      private ServerSystemPreferences prefs;
35      
36      public void setUp() throws Exception
37      {
38          super.setUp();
39          prefs = new ServerSystemPreferences();
40      }
41      
42      /***
43       * Tests to make sure the system preferences root has entry (test, abc123).
44       */
45      public void testRoot() throws Exception
46      {
47          assertEquals( "sysPrefRoot", prefs.get( "prefNodeName", "not the value" ) );
48      }
49  
50      /***
51       * Tests the creation and use of a new preferences node.
52       *
53       * @throws BackingStoreException if there are failures with the store
54       */
55      public void testCreate() throws BackingStoreException
56      {
57          Preferences testNode = prefs.node( "testNode" );
58  
59          testNode.put( "testNodeKey", "testNodeValue" );
60          testNode.sync();
61      }
62  
63  
64      /***
65       * Tests the creation and use of a new preferences node.
66       *
67       * @throws BackingStoreException if there are failures with the store
68       */
69      public void testCreateAndSet() throws BackingStoreException
70      {
71          Preferences testNode = prefs.node( "testNode" );
72  
73          testNode.put( "testNodeKey", "testNodeValue" );
74          testNode.sync();
75  
76          testNode.putBoolean( "boolKey", true );
77          testNode.putByteArray( "arrayKey", new byte[10] );
78          testNode.putDouble( "doubleKey", 3.14 );
79          testNode.putFloat( "floatKey", ( float ) 3.14 );
80          testNode.putInt( "intKey", 345 );
81          testNode.putLong( "longKey", 75449559185447L );
82          testNode.sync();
83  
84          testNode = prefs.node( "testNode" );
85  
86          assertEquals( true, testNode.getBoolean( "boolKey", false ) );
87          assertTrue( 3.14 == testNode.getDouble( "doubleKey", 9.20 ) );
88          assertTrue( (float) 3.14 == testNode.getFloat( "floatKey", (float) 3.90 ) );
89          assertEquals( 345, testNode.getInt( "intKey", 87 ) );
90          assertEquals( 75449559185447L, testNode.getLong( "longKey", 75449547L ) );
91      }
92  
93  
94      /***
95       * Tests the creation and use of a new preferences node.
96       *
97       * @throws BackingStoreException if there are failures with the store
98       */
99      public void testCreateAndRemove() throws BackingStoreException
100     {
101         Preferences testNode = prefs.node( "testNode" );
102 
103         testNode.put( "testNodeKey", "testNodeValue" );
104         testNode.sync();
105 
106         testNode.putBoolean( "boolKey", true );
107         testNode.putByteArray( "arrayKey", new byte[10] );
108         testNode.putDouble( "doubleKey", 3.14 );
109         testNode.putFloat( "floatKey", ( float ) 3.14 );
110         testNode.putInt( "intKey", 345 );
111         testNode.putLong( "longKey", 75449559185447L );
112         testNode.sync();
113 
114         testNode = prefs.node( "testNode" );
115 
116         assertEquals( true, testNode.getBoolean( "boolKey", false ) );
117         assertTrue( 3.14 == testNode.getDouble( "doubleKey", 9.20 ) );
118         assertTrue( (float) 3.14 == testNode.getFloat( "floatKey", (float) 3.90 ) );
119         assertEquals( 345, testNode.getInt( "intKey", 87 ) );
120         assertEquals( 75449559185447L, testNode.getLong( "longKey", 75449547L ) );
121 
122         testNode.remove( "doubleKey" );
123         testNode.remove( "arrayKey" );
124 
125         assertEquals( "no value", testNode.get( "doubleKey", "no value" ) );
126         assertEquals( "no value", testNode.get( "arrayKey", "no value" ) );
127 
128         testNode.sync();
129 
130         assertEquals( "no value", testNode.get( "doubleKey", "no value" ) );
131         assertEquals( "no value", testNode.get( "arrayKey", "no value" ) );
132     }
133 }