1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.core.prefs;
21  
22  
23  import org.apache.directory.server.core.DirectoryService;
24  import org.apache.directory.server.core.integ.CiRunner;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.assertNotNull;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  
31  import java.util.prefs.BackingStoreException;
32  import java.util.prefs.Preferences;
33  
34  
35  /**
36   * Tests the ServerSystemPreferences class.
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   * @version $Rev: 613608 $
40   */
41  @RunWith ( CiRunner.class )
42  public class PreferencesIT
43  {
44      public static DirectoryService service;
45  
46  
47      @Test
48      public void testSystemRoot()
49      {
50          ServerPreferencesFactory factory = new ServerPreferencesFactory( service );
51          Preferences prefs = factory.systemRoot();
52  
53          assertNotNull( prefs );
54          assertEquals( "sysPrefRoot", prefs.get( "prefNodeName", "default value" ) );
55      }
56  
57  
58      /**
59       * Tests to make sure the system preferences root has entry (test, abc123).
60       *
61       * @throws Exception if there are failures with the store
62       */
63      @Test
64      public void testRoot() throws Exception
65      {
66          ServerSystemPreferences prefs = new ServerSystemPreferences( service );
67          assertEquals( "sysPrefRoot", prefs.get( "prefNodeName", "not the value" ) );
68      }
69  
70  
71      /**
72       * Tests the creation and use of a new preferences node.
73       *
74       * @throws BackingStoreException if there are failures with the store
75       */
76      @Test
77      public void testCreate() throws BackingStoreException
78      {
79          ServerSystemPreferences prefs = new ServerSystemPreferences( service );
80          Preferences testNode = prefs.node( "testNode" );
81  
82          testNode.put( "cn", "testNodeValue" );
83          testNode.sync();
84      }
85  
86  
87      /**
88       * Tests the creation and use of a new preferences node.
89       *
90       * @throws BackingStoreException if there are failures with the store
91       */
92      @Test
93      public void testCreateAndSetBoolean() throws BackingStoreException
94      {
95          ServerSystemPreferences prefs = new ServerSystemPreferences( service );
96          Preferences testNode = prefs.node( "testNode" );
97          testNode.putBoolean( "cn", false );
98          testNode.sync();
99          testNode = prefs.node( "testNode" );
100         assertEquals( false, testNode.getBoolean( "cn", false ) );
101     }
102 
103 
104     /**
105      * Tests the creation and use of a new preferences node.
106      *
107      * @throws BackingStoreException if there are failures with the store
108      */
109     /* TODO: Temporarily commented until we get a clear status about this package
110     public void testCreateAndSetByteArray() throws BackingStoreException
111     {
112         byte[] jpegValue = new byte[]
113                                    { (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0, 0x01, 0x02, 'J', 'F', 'I', 'F', 0x00, 0x45, 0x23, 0x7d, 0x7f };
114         Preferences testNode = prefs.node( "testNode" );
115         testNode.putByteArray( "jpegPhoto", jpegValue );
116         testNode.sync();
117         testNode = prefs.node( "testNode" );
118     }
119     */
120 
121 
122     /**
123      * Tests the creation and use of a new preferences node.
124      *
125      * @throws BackingStoreException if there are failures with the store
126      */
127     @Test
128     public void testCreateAndSetDouble() throws BackingStoreException
129     {
130         ServerSystemPreferences prefs = new ServerSystemPreferences( service );
131         Preferences testNode = prefs.node( "testNode" );
132         testNode.putDouble( "cn", 3.14 );
133         testNode.sync();
134         testNode = prefs.node( "testNode" );
135         assertTrue( 3.14 == testNode.getDouble( "cn", 9.20 ) );
136     }
137 
138 
139     /**
140      * Tests the creation and use of a new preferences node.
141      *
142      * @throws BackingStoreException if there are failures with the store
143      */
144     @Test
145     public void testCreateAndSetFloat() throws BackingStoreException
146     {
147         ServerSystemPreferences prefs = new ServerSystemPreferences( service );
148         Preferences testNode = prefs.node( "testNode" );
149         testNode.putFloat( "cn", ( float ) 9.20 );
150         testNode.sync();
151         testNode = prefs.node( "testNode" );
152         assertTrue( ( float ) 9.20 == testNode.getFloat( "cn", ( float ) 9.20 ) );
153     }
154 
155 
156     /**
157      * Tests the creation and use of a new preferences node.
158      *
159      * @throws BackingStoreException if there are failures with the store
160      */
161     @Test
162     public void testCreateAndSetInt() throws BackingStoreException
163     {
164         ServerSystemPreferences prefs = new ServerSystemPreferences( service );
165         Preferences testNode = prefs.node( "testNode" );
166         testNode.putInt( "cn", 345 );
167         testNode.sync();
168         testNode = prefs.node( "testNode" );
169         assertTrue( 345 == testNode.getInt( "cn", 345 ) );
170     }
171 
172 
173     /**
174      * Tests the creation and use of a new preferences node.
175      *
176      * @throws BackingStoreException if there are failures with the store
177      */
178     @Test
179     public void testCreateAndSetLong() throws BackingStoreException
180     {
181         ServerSystemPreferences prefs = new ServerSystemPreferences( service );
182         Preferences testNode = prefs.node( "testNode" );
183         testNode.putLong( "cn", 75449559185447L );
184         testNode.sync();
185         testNode = prefs.node( "testNode" );
186         assertTrue( 75449559185447L == testNode.getLong( "cn", 75449559185447L ) );
187     }
188 
189 
190     /**
191      * Tests the creation and use of a new preferences node.
192      *
193      * @throws BackingStoreException if there are failures with the store
194      */
195     @Test
196     public void testCreateAndRemove() throws BackingStoreException
197     {
198         ServerSystemPreferences prefs = new ServerSystemPreferences( service );
199         Preferences testNode = prefs.node( "testNode" );
200 
201         testNode.put( "cn", "testNodeValue" );
202         testNode.putInt( "roomNumber", 345 );
203         testNode.sync();
204 
205         testNode = prefs.node( "testNode" );
206         assertEquals( 345, testNode.getInt( "roomNumber", 87 ) );
207         testNode.remove( "cn" );
208         testNode.remove( "roomNumber" );
209         testNode.sync();
210 
211         assertEquals( "no value", testNode.get( "cn", "no value" ) );
212         assertEquals( "no value", testNode.get( "roomNumber", "no value" ) );
213     }
214 }