/*
 * Copyright 2009 OW2 Chameleon
 * 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.ow2.chameleon.core;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

/**
 * Unit tests about the {@link Chameleon} class.
 * @author <a href="mailto:chameleon-dev@ow2.org">Chameleon Project Team</a>
 */
public class ChameleonTest extends TestCase {

    /**
     * Checks default core.
     * @throws Exception if something bad happened.
     */
    public void testGetDirectoryDefault() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        new Chameleon(false, null, null, null);

        core.delete();
    }

    /**
     * Checks absent core detection.
     * @throws Exception if something bad happened.
     */
    public void testDetectNoCore() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        assertFalse(core.exists());

        try {
            new Chameleon(false, null, null, null);
            fail("The missing core directory is not detected");
        } catch (Exception e) {
            // Ok
        }

    }

    /**
     * Checks activator list.
     * @throws Exception if something bad happened.
     */
    public void testActivatorList() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(false, null, null, null);
        List act = chameleon.getActivators(new HashMap());

        assertEquals(act.size(), 2); // core + log

        core.delete();
    }

    /**
     * Checks activator list when the log is disabled.
     * @throws Exception if something bad happened.
     */
    public void testActivatorListLogDisable() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(false, null, null, null);
        Map map = new HashMap();
        map.put(Chameleon.CHAMELEON_LOG_PROPERTY, "FaLsE");
        List act = chameleon.getActivators(map);

        assertEquals(act.size(), 1); // core only

        core.delete();
    }

    /**
     * Checks activator list when debug mode enabled.
     * @throws Exception if something bad happened.
     */
    public void testActivatorListInDebugMode() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(true, null, null, null);
        List act = chameleon.getActivators(new HashMap());

        assertEquals(act.size(), 1 + 1 + 1); // Core, log, debug

        core.delete();
    }

    /**
     * Checks activator list when debug mode is enabled
     * and log disabled.
     * @throws Exception if something bad happened.
     */
    public void testActivatorListInDebugModeNoLog() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(true, null, null, null);
        Map map = new HashMap();
        map.put(Chameleon.CHAMELEON_LOG_PROPERTY, "FaLsE");
        List act = chameleon.getActivators(map);

        assertEquals(act.size(), 2); // Core, debug

        core.delete();
    }

    /**
     * Checks default properties.
     * @throws Exception if something bad happened.
     */
    public void testProperties() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(true, null, null, null);
        Map props = chameleon.getProperties(new HashMap());

        assertTrue(props.containsKey("org.osgi.framework.storage.clean"));
        assertTrue(props.containsKey("org.osgi.framework.storage"));
        assertTrue(props
                .containsKey("org.osgi.framework.system.packages.extra"));

        core.delete();

    }

    /**
     * Checks file install properties.
     * @throws Exception if something bad happened.
     */
    public void testFileInstallProperties() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(false, null, null, null);
        Map props = chameleon.getProperties(new HashMap());

        assertEquals(props.get("felix.fileinstall.poll"), "2000");
        assertEquals(props.get("felix.fileinstall.debug"), "-1");
        assertEquals(props.get("felix.fileinstall.bundles.new.start"), "true");
        assertFalse(props.containsKey("felix.fileinstall.dir"));

        core.delete();

    }

    /**
     * Checks file install configuration when debug mode enabled.
     * @throws Exception if something bad happened.
     */
    public void testFileInstallPropertiesInDebugMode() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(true, null, null, null);
        Map props = chameleon.getProperties(new HashMap());

        assertEquals(props.get("felix.fileinstall.poll"), "2000");
        assertEquals(props.get("felix.fileinstall.debug"), "1");
        assertEquals(props.get("felix.fileinstall.bundles.new.start"), "true");
        assertFalse(props.containsKey("felix.fileinstall.dir"));

        core.delete();

    }

    /**
     * Checks file install configuration when deploy folder
     * set.
     * @throws Exception if something bad happened.
     */
    public void testFileInstallPropertiesWithFISet() throws Exception {
        // Create the core directory
        File core = new File(Chameleon.CORE_DIR);
        core.mkdir();
        assertTrue(core.exists());

        Chameleon chameleon = new Chameleon(false, null, null, "load");
        Map props = chameleon.getProperties(new HashMap());

        assertEquals(props.get("felix.fileinstall.poll"), "2000");
        assertEquals(props.get("felix.fileinstall.debug"), "-1");
        assertEquals(props.get("felix.fileinstall.bundles.new.start"), "true");
        assertTrue(props.containsKey("felix.fileinstall.dir"));
        assertEquals(props.get("felix.fileinstall.dir"), "load");

        core.delete();
    }

}
