/* ***************************************************************************
 * Copyright (c) 2011 Brabenetz Harald, Austria.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 *****************************************************************************/
package org.settings4j.connector;

import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.settings4j.Settings4j;

import junit.framework.TestCase;

/**
 * Test Suite for {@link PreferencesConnector}.
 * Checkstyle:OFF .*
 * 
 * @author brabenetz
 *
 */
public class PreferencesConnectorTest extends TestCase {


    private static final String PREF_UNITTEST_NODE = "org/settings4j/unittest";

    protected void setUp() throws Exception {
        removeUnitTestNode(Preferences.userRoot());
        removeUnitTestNode(Preferences.systemRoot());
    }

    private void removeUnitTestNode(final Preferences userRoot) throws BackingStoreException {
        if (userRoot.nodeExists(PREF_UNITTEST_NODE)) {
            userRoot.node(PREF_UNITTEST_NODE).removeNode();
        }
    }

    public void testSaveSuccessSlash() {
        final PreferencesConnector connector = new PreferencesConnector();
        connector.setString(PREF_UNITTEST_NODE + "/testSaveSuccessSlash", "test");

        assertEquals("test", Preferences.userRoot().node(PREF_UNITTEST_NODE).get("testSaveSuccessSlash", null));
    }
    
    public void testSaveSuccessBackslash() {
        final PreferencesConnector connector = new PreferencesConnector();
        connector.setString(PREF_UNITTEST_NODE + "\\testSaveSuccessBackslash", "test");

        assertEquals("test", Preferences.userRoot().node(PREF_UNITTEST_NODE).get("testSaveSuccessBackslash", null));
    }

    public void testReadSuccess() {
        Preferences.userRoot().node(PREF_UNITTEST_NODE).put("testReadSuccess", "test");
        
        final PreferencesConnector connector = new PreferencesConnector();

        assertEquals("test", connector.getString(PREF_UNITTEST_NODE + "/testReadSuccess"));
    }

    public void testReadFail() {
        final PreferencesConnector connector = new PreferencesConnector();

        assertNull(connector.getString(PREF_UNITTEST_NODE + "/testReadFailDoesntExist"));
        assertNull(connector.getString("testReadFailDoesntExist"));
    }
    
    public void testRoundup() {
        final PreferencesConnector connector = new PreferencesConnector();
        
        assertNull(connector.getString(PREF_UNITTEST_NODE + "/testRoundup"));
        // 1. save
        connector.setString(PREF_UNITTEST_NODE + "/testRoundup", "test");
        // 2. read
        assertEquals("test", connector.getString(PREF_UNITTEST_NODE + "/testRoundup"));
    }

    public void testFormularExample() {
        final PreferencesConnector connector = new PreferencesConnector();
        connector.setString("com/mycompany/mycalculation/my-formula", "a * a - 2");

        assertEquals("a * a - 2", Settings4j.getString("com/mycompany/mycalculation/my-formula"));
    }
}

@@ -915,69 +915,69 @@
 ort 
-org.settings4j.Settings4j;%0D%0A%0D%0Aimport junit.framework.TestCase
+junit.framework.TestCase;%0D%0A%0D%0Aimport org.settings4j.Settings4j
 ;%0D%0A%0D
@@ -1029,20 +1029,16 @@
 nector%7D.
-%0D%0A *
  Checkst
@@ -1076,20 +1076,16 @@
 benetz%0D%0A
- *%0D%0A
  */%0D%0Apub
@@ -1222,24 +1222,39 @@
 ittest%22;%0D%0A%0D%0A
+    @Override%0D%0A
     protecte
@@ -1401,24 +1401,899 @@
 ;%0D%0A    %7D%0D%0A%0D%0A
+    /**%0D%0A     * Smoke Test if the Preferences from JDK works as expected.%0D%0A     * %3Cp%3E%0D%0A     * This Test tests the PRE-REQUIREMENTS for the PreferencesConnector-tests and NOT the %7B@link PreferencesConnector%7D%0D%0A     * itself.%0D%0A     * %0D%0A     * @throws BackingStoreException in case of an error.%0D%0A     */%0D%0A    public void testSmokeTest() throws BackingStoreException %7B%0D%0A        %0D%0A        final Preferences myModulePrefs = Preferences.userRoot().node(%22com/myapp/mymodule%22);%0D%0A        // put test value%0D%0A        myModulePrefs.put(%22myKey%22, %22myNewValue%22);%0D%0A        // read TestValue%0D%0A        final String myValue = myModulePrefs.get(%22myKey%22, null);%0D%0A%0D%0A        // validate Result%0D%0A        assertEquals(%22myNewValue%22, myValue);%0D%0A%0D%0A        // remove node%0D%0A        myModulePrefs.removeNode();%0D%0A        assertEquals(false, Preferences.userRoot().nodeExists(%22com/myapp/mymodule%22));%0D%0A    %7D%0D%0A%0D%0A
     private 
@@ -2830,36 +2830,32 @@
 null));%0D%0A    %7D%0D%0A
-    
 %0D%0A    public voi
@@ -3302,32 +3302,24 @@
 , %22test%22);%0D%0A
-        
 %0D%0A        fi
@@ -3767,20 +3767,16 @@
 %0A    %7D%0D%0A
-    
 %0D%0A    pu
@@ -3878,24 +3878,16 @@
 tor();%0D%0A
-        
 %0D%0A      

add SmokeTest