/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.karaf.features;

import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.karaf.features.internal.service.FeatureConfigInstaller;
import org.apache.karaf.features.internal.service.RepositoryImpl;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

public class AppendTest {

    private IMocksControl c;
    private Feature feature;
    private ConfigurationAdmin admin;
    private FeatureConfigInstaller installer;
    
    @Before
    public void before() throws Exception {
        System.setProperty("karaf.data", "data");
        System.setProperty("karaf.etc", "etc");
        RepositoryImpl r = new RepositoryImpl(getClass().getResource("internal/service/f08.xml").toURI());
        Feature[] features = r.getFeatures();
        feature = features[0];
        checkFeature(feature);
        c = EasyMock.createControl();
        admin = c.createMock(ConfigurationAdmin.class);
        installer = new FeatureConfigInstaller(admin);
    }

    @Test
    public void testNoChange() throws Exception {
        Hashtable<String, Object> original = new Hashtable<>();
        original.put("javax.servlet.context.tempdir", "bar");
        expectConfig(admin, original);

        c.replay();
        installer.installFeatureConfigs(feature);
        c.verify();
    }

    @Test
    public void testAppend() throws Exception {
        Hashtable<String, Object> original = new Hashtable<>();
        original.put("foo", "bar");
        Configuration config = expectConfig(admin, original);
        Capture<Dictionary<String, ?>> captured = EasyMock.newCapture();
        config.update(EasyMock.capture(captured));
        expectLastCall();
        c.replay();
        installer.installFeatureConfigs(feature);
        c.verify();
        assertEquals("data/pax-web-jsp", captured.getValue().get("javax.servlet.context.tempdir"));
    }

    private Configuration expectConfig(ConfigurationAdmin admin, Hashtable<String, Object> original)
        throws IOException, InvalidSyntaxException {
        Configuration config = c.createMock(Configuration.class);
        expect(admin.listConfigurations(eq("(service.pid=org.ops4j.pax.web)")))
            .andReturn(new Configuration[] {
                                            config
        }).atLeastOnce();
        expect(config.getProperties()).andReturn(original).atLeastOnce();
        return config;
    }

    private void checkFeature(Feature feature) {
        ConfigInfo configInfo = feature.getConfigurations().get(0);
        assertTrue(configInfo.isAppend());

        Properties properties = configInfo.getProperties();
        String tempDir = properties.getProperty("javax.servlet.context.tempdir");
        assertEquals("data/pax-web-jsp", tempDir);
    }
}

@@ -1057,16 +1057,69 @@
 tTrue;%0A%0A
+import java.io.File;%0Aimport java.io.FileInputStream;%0A
 import j
@@ -1134,24 +1134,24 @@
 OException;%0A
-
 import java.
@@ -1968,19 +1968,22 @@
 .etc%22, %22
+targ
 et
-c
 %22);%0A    
@@ -2722,32 +2722,123 @@
 ows Exception %7B%0A
+        File cfgFile = new File(%22target/org.ops4j.pax.web.cfg%22);%0A        cfgFile.delete();%0A
         Hashtabl
@@ -3219,24 +3219,24 @@
 c.verify();%0A
-
         asse
@@ -3319,24 +3319,213 @@
 tempdir%22));%0A
+        Properties props = new Properties();%0A        props.load(new FileInputStream(cfgFile));%0A        assertEquals(%22data/pax-web-jsp%22, props.getProperty(%22javax.servlet.context.tempdir%22));%0A
     %7D%0A%0A    p

Test config write back