/*
 * 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.felix.ipojo.runtime.core;

import org.apache.felix.ipojo.runtime.core.services.FooService;
import org.junit.Test;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;

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

import static junit.framework.Assert.assertEquals;

/**
 * Test for FELIX-4172 Updated method called twice at the bundle start
 */
public class TestUpdated extends Common {

    private String factoryName = "org.apache.felix.ipojo.runtime.core.components.ImmediateConfigurableFooProvider";

    @Test
    public void testNumberOfUpdatedCalls() throws IOException {
        Properties props = new Properties();
        props.put("message", "message");
        props.put("propagated", "propagated");
        props.put(".private", "wow");

        Configuration configuration = admin.createFactoryConfiguration(factoryName, "?");
        configuration.update(props);

        ServiceReference ref = osgiHelper.waitForService(FooService.class.getName(),
                "(instance.name=" + configuration.getPid() + ")",
                1000);

        FooService fs = (FooService) osgiHelper.getServiceObject(ref);
        assertEquals(fs.getInt(), 1);

        // Update the property
        props.put("message", "message2");
        props.put("propagated", "propagated2");
        props.put(".private", "wow2");
        configuration.update(props);

        grace();

        assertEquals(fs.getInt(), 2);

        // Remove a property
        props.remove("propagated");
        configuration.update(props);

        grace();

        assertEquals(fs.getInt(), 3);


        configuration.delete();
    }

    @Test
    public void testNumberOfUpdatedCallsWithManagedService() throws IOException {
        Properties props = new Properties();
        props.put("message", "message");
        props.put("propagated", "propagated");
        props.put(".private", "wow");
        props.put("managed.service.pid", "config");

        Configuration configuration = admin.createFactoryConfiguration(factoryName, "?");
        configuration.update(props);

        ServiceReference ref = osgiHelper.waitForService(FooService.class.getName(),
                "(instance.name=" + configuration.getPid() + ")",
                1000);

        FooService fs = (FooService) osgiHelper.getServiceObject(ref);
        assertEquals(fs.getInt(), 1);

        // Update the property using the managed service.

        Configuration managedConfiguration = admin.getConfiguration("config", "?");
        props.put("message", "message2");
        props.put("propagated", "propagated2");
        props.put(".private", "wow2");
        managedConfiguration.update(props);

        grace();

        assertEquals(fs.getInt(), 2);

        // Remove a property
        props.remove("propagated");
        managedConfiguration.update(props);

        grace();

        assertEquals(fs.getInt(), 3);

        managedConfiguration.delete();
        configuration.delete();
    }


}

@@ -1048,16 +1048,73 @@
 eption;%0A
+import java.util.Dictionary;%0Aimport java.util.Hashtable;%0A
 import j
@@ -1512,41 +1512,72 @@
     
-Properties props = new Properties
+Dictionary%3CString, Object%3E props = new Hashtable%3CString, Object%3E
 ();%0A
@@ -2675,41 +2675,72 @@
     
-Properties props = new Properties
+Dictionary%3CString, Object%3E props = new Hashtable%3CString, Object%3E
 ();%0A

Use dictionaries instead of properties.