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

import org.osgi.framework.Bundle;
import org.osgi.framework.launch.Framework;
import org.osgi.service.cm.ConfigurationAdmin;

/**
 * Checks chameleon configurations.
 * @author <a href="mailto:chameleon-dev@ow2.org">Chameleon Project Team</a>
 */
public class ChameleonStartWithconfigurationTest extends TestCase {

    /**
     * Deletes the load folder if it exists.
     * @see junit.framework.TestCase#tearDown()
     */
    public void tearDown() {
        File load = new File("load");
        if (load.exists()) {
            load.delete();
        }
    }

    /**
     * Checks configuration files.
     * @throws Exception if something bad happened
     */
    public void testCoreAndBundleRuntimeStartAndConfApp() throws Exception {
        Chameleon chameleon = new Chameleon(null, false, null, null, "load",
                Constants.ANOTHER_CONF_FILE, null);
        assertNotNull(chameleon);
        Framework fmwk = chameleon.start();
        // Check bundles
        assertEquals(fmwk.getBundleContext().getBundles().length,
                CoreActivator.COUNT + 1 + 2); // Core + SytemBundle + 2 bundles
        // Check that all bundles are resolved
        Bundle[] bundles = fmwk.getBundleContext().getBundles();
        for (int i = 0; i < bundles.length; i++) {
            assertEquals("Check state of " + bundles[i].getSymbolicName(),
                    Bundle.ACTIVE, bundles[i].getState());
        }

        // Check configuration admin
        assertNotNull(fmwk.getBundleContext().getServiceReference(
                ConfigurationAdmin.class.getName()));

        chameleon.stop();
    }

    /**
     * Checks file install configuration.
     * @throws Exception if something bad happened
     */
    public void testFileInstallConfiguration() throws Exception {
        Chameleon chameleon = new Chameleon(Constants.FULL_CORE, false, null,
                null, null,
                Constants.ANOTHER_CONF_FILE, null);
        assertNotNull(chameleon);
        Framework fmwk = chameleon.start();
        // Check bundles
        assertEquals(fmwk.getBundleContext().getBundles().length,
                CoreActivator.COUNT + 1 + 2); // Core + SytemBundle + 2 bundles
        // Check that all bundles are resolved
        Bundle[] bundles = fmwk.getBundleContext().getBundles();
        for (int i = 0; i < bundles.length; i++) {
            assertEquals("Check state of " + bundles[i].getSymbolicName(),
                    Bundle.ACTIVE, bundles[i].getState());
        }

        // Check configuration admin
        assertNotNull(fmwk.getBundleContext().getServiceReference(
                ConfigurationAdmin.class.getName()));

        // Check that the load folder was created
        chameleon.stop();
    }

    /**
     * Checks debug mode.
     * @throws Exception if something bad happened
     */
    public void testDebugMode() throws Exception {
        Chameleon chameleon = new Chameleon(null, true, null, null, null,
                Constants.ANOTHER_CONF_FILE, null);
        assertNotNull(chameleon);
        Framework fmwk = chameleon.start();
        // Check bundles
        Bundle[] bundles = fmwk.getBundleContext().getBundles();
        for (int i = 0; i < bundles.length; i++) {
            assertEquals("Check state of " + bundles[i].getSymbolicName(),
                    Bundle.ACTIVE, bundles[i].getState());
        }
        assertEquals(fmwk.getBundleContext().getBundles().length,
                CoreActivator.COUNT + 1 + 2 + 4); // Core + SytemBundle + 2
                                                  // bundles + Gogo x 3 + Arch

        // Check configuration admin
        assertNotNull(fmwk.getBundleContext().getServiceReference(
                ConfigurationAdmin.class.getName()));

        chameleon.stop();
    }

    /**
     * Checks debug mode.
     * @throws Exception if something bad happened
     */
    public void testDebugModeWithShellTui() throws Exception {
        Chameleon chameleon = new Chameleon(null, true, null, null, null,
                Constants.ANOTHER_CONF_FILE_SHELL, null);
        assertNotNull(chameleon);
        Framework fmwk = chameleon.start();
        // Check bundles
        Bundle[] bundles = fmwk.getBundleContext().getBundles();
        for (int i = 0; i < bundles.length; i++) {
            assertEquals("Check state of " + bundles[i].getSymbolicName(),
                    Bundle.ACTIVE, bundles[i].getState());
        }
        assertEquals(fmwk.getBundleContext().getBundles().length,
                CoreActivator.COUNT + 1 + 2 + 3); // Core + SytemBundle + 2
                                                  // bundles + Shell (Shell, TUI, Arch)

        // Check configuration admin
        assertNotNull(fmwk.getBundleContext().getServiceReference(
                ConfigurationAdmin.class.getName()));

        // Check that the load folder was created
        File load = new File("myfolder");
        assertTrue(load.exists());
        chameleon.stop();
        load.delete();
    }



}
