/* ***************************************************************************
 * 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 junit.framework.TestCase;


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"));
    }


}

@@ -915,37 +915,184 @@
 ort 
-junit.framework.TestCase;%0D%0A%0D%0A
+org.settings4j.Settings4j;%0D%0A%0D%0Aimport junit.framework.TestCase;%0D%0A%0D%0A/**%0D%0A * Test Suite for %7B@link PreferencesConnector%7D.%0D%0A * Checkstyle:OFF .*%0D%0A * %0D%0A * @author brabenetz%0D%0A *%0D%0A */
 %0D%0Apu
@@ -3314,13 +3314,322 @@
    %7D%0D%0A%0D%0A
+    public void testFormularExample() %7B%0D%0A        final PreferencesConnector connector = new PreferencesConnector();%0D%0A        connector.setString(%22com/mycompany/mycalculation/my-formula%22, %22a * a - 2%22);%0D%0A%0D%0A        assertEquals(%22a * a - 2%22, Settings4j.getString(%22com/mycompany/mycalculation/my-formula%22));%0D%0A    %7D
 %0D%0A%7D%0D%0A

add testCase