001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j;
017    
018    import org.ini4j.test.Helper;
019    
020    import static org.junit.Assert.assertEquals;
021    import static org.junit.Assert.assertNotNull;
022    import static org.junit.Assert.assertNull;
023    import static org.junit.Assert.assertSame;
024    import static org.junit.Assert.fail;
025    
026    import org.junit.Test;
027    
028    import java.util.prefs.Preferences;
029    
030    public class IniPreferencesFactoryTest extends Ini4jCase
031    {
032        private static final String DUMMY = "dummy";
033    
034        @Test public void testGetIniLocation() throws Exception
035        {
036            IniPreferencesFactory factory = new IniPreferencesFactory();
037    
038            System.setProperty(DUMMY, DUMMY);
039            assertEquals(DUMMY, factory.getIniLocation(DUMMY));
040            System.getProperties().remove(DUMMY);
041            assertNull(factory.getIniLocation(DUMMY));
042        }
043    
044        @SuppressWarnings("empty-statement")
045        @Test public void testGetResourceAsStream() throws Exception
046        {
047            IniPreferencesFactory factory = new IniPreferencesFactory();
048    
049            // class path
050            assertNotNull(factory.getResourceAsStream(Helper.DWARFS_INI));
051    
052            // url
053            String location = Helper.getResourceURL(Helper.DWARFS_INI).toString();
054    
055            assertNotNull(factory.getResourceAsStream(location));
056    
057            // invalid url should throw IllegalArgumentException
058            try
059            {
060                factory.getResourceAsStream("http://");
061                fail();
062            }
063            catch (IllegalArgumentException x)
064            {
065                ;
066            }
067        }
068    
069        @Test public void testNewIniPreferences()
070        {
071            System.setProperty(DUMMY, DUMMY);
072            try
073            {
074                new IniPreferencesFactory().newIniPreferences(DUMMY);
075                missing(IllegalArgumentException.class);
076            }
077            catch (IllegalArgumentException x)
078            {
079                //
080            }
081            finally
082            {
083                System.getProperties().remove(DUMMY);
084            }
085        }
086    
087        @Test public void testSystemRoot() throws Exception
088        {
089            Preferences prefs = Preferences.systemRoot();
090    
091            assertNotNull(prefs);
092            assertEquals(IniPreferences.class, prefs.getClass());
093            assertSame(prefs, Preferences.systemRoot());
094        }
095    
096        @Test public void testUserRoot() throws Exception
097        {
098            Preferences prefs = Preferences.userRoot();
099    
100            assertNotNull(prefs);
101            assertEquals(IniPreferences.class, prefs.getClass());
102            assertSame(prefs, Preferences.userRoot());
103        }
104    }