View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
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  package org.ini4j;
17  
18  import org.ini4j.test.Helper;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertSame;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.Test;
27  
28  import java.util.prefs.Preferences;
29  
30  public class IniPreferencesFactoryTest extends Ini4jCase
31  {
32      private static final String DUMMY = "dummy";
33  
34      @Test public void testGetIniLocation() throws Exception
35      {
36          IniPreferencesFactory factory = new IniPreferencesFactory();
37  
38          System.setProperty(DUMMY, DUMMY);
39          assertEquals(DUMMY, factory.getIniLocation(DUMMY));
40          System.getProperties().remove(DUMMY);
41          assertNull(factory.getIniLocation(DUMMY));
42      }
43  
44      @SuppressWarnings("empty-statement")
45      @Test public void testGetResourceAsStream() throws Exception
46      {
47          IniPreferencesFactory factory = new IniPreferencesFactory();
48  
49          // class path
50          assertNotNull(factory.getResourceAsStream(Helper.DWARFS_INI));
51  
52          // url
53          String location = Helper.getResourceURL(Helper.DWARFS_INI).toString();
54  
55          assertNotNull(factory.getResourceAsStream(location));
56  
57          // invalid url should throw IllegalArgumentException
58          try
59          {
60              factory.getResourceAsStream("http://");
61              fail();
62          }
63          catch (IllegalArgumentException x)
64          {
65              ;
66          }
67      }
68  
69      @Test public void testNewIniPreferences()
70      {
71          System.setProperty(DUMMY, DUMMY);
72          try
73          {
74              new IniPreferencesFactory().newIniPreferences(DUMMY);
75              missing(IllegalArgumentException.class);
76          }
77          catch (IllegalArgumentException x)
78          {
79              //
80          }
81          finally
82          {
83              System.getProperties().remove(DUMMY);
84          }
85      }
86  
87      @Test public void testSystemRoot() throws Exception
88      {
89          Preferences prefs = Preferences.systemRoot();
90  
91          assertNotNull(prefs);
92          assertEquals(IniPreferences.class, prefs.getClass());
93          assertSame(prefs, Preferences.systemRoot());
94      }
95  
96      @Test public void testUserRoot() throws Exception
97      {
98          Preferences prefs = Preferences.userRoot();
99  
100         assertNotNull(prefs);
101         assertEquals(IniPreferences.class, prefs.getClass());
102         assertSame(prefs, Preferences.userRoot());
103     }
104 }