/*
 * 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.net.MalformedURLException;

import junit.framework.TestCase;

import org.mockito.Mockito;
import org.osgi.framework.BundleContext;

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

    /**
     * Checks that the activator correctly gets bundles.
     * @throws MalformedURLException if something bad happened.
     */
    public void testFullCreation() throws MalformedURLException {
        File mockedDir = MockUtils.getCompleteCoreDirectory();
        assertTrue(mockedDir.exists());
        assertTrue(mockedDir.isDirectory());
        CoreActivator activator = new CoreActivator(mockedDir);
        assertNotNull(activator);
    }

    /**
     * Checks that the activator correctly gets bundles and start
     * them.
     * @throws Exception if something bad happened.
     */
    public void testFullStart() throws Exception {
        File mockedDir = MockUtils.getCompleteCoreDirectory();

        BundleContext mockedContext = MockUtils.getContext();
        CoreActivator activator = new CoreActivator(mockedDir);
        activator.start(mockedContext);

        // Verify
        // Three install
        ((BundleContext) Mockito.verify(mockedContext, Mockito
                .times(CoreActivator.COUNT)))
                .installBundle(Mockito.anyString());
    }

    /**
     * Checks that the activator detects incomplete core.
     * @throws Exception if something bad happened.
     */
    public void testIncompleteStart() throws Exception {
        File mockedDir = MockUtils.getIncompleteCoreDirectory();

        BundleContext mockedContext = MockUtils.getContext();
        CoreActivator activator = new CoreActivator(mockedDir);

        try {
            activator.start(mockedContext);
            fail("Incomplete directory undetected");
        } catch (Exception e) {
            // OK
        }

        // Verify
        // No install
        ((BundleContext) Mockito.verify(mockedContext, Mockito.times(1)))
                .installBundle(Mockito.anyString());

    }

    /**
     * Checks the stop method.
     * @throws Exception if something bad happened.
     */
    public void testStop() throws Exception {
        File mockedDir = MockUtils.getCompleteCoreDirectory();

        BundleContext mockedContext = MockUtils.getContext();
        CoreActivator activator = new CoreActivator(mockedDir);
        activator.stop(mockedContext);
    }

    /**
     * Checks with the real complete core.
     * @throws Exception if something bad happened.
     */
    public void testOnFullCoreDirectory() throws Exception {
        File core = new File(Constants.FULL_CORE);
        CoreActivator activator = new CoreActivator(core);
        BundleContext mockedContext = MockUtils.getContext();
        activator.start(mockedContext);

        // Verify
        // Three install
        ((BundleContext) Mockito.verify(mockedContext, Mockito
                .times(CoreActivator.COUNT)))
                .installBundle(Mockito.anyString());
    }

    /**
     * Checks that the activator detects incomplete core.
     * @throws Exception if something bad happened.
     */
    public void testOnIncompleteCoreDirectory() throws Exception {
        File core = new File(
                Constants.INCOMPLETE_CORE);

        BundleContext mockedContext = MockUtils.getContext();
        CoreActivator activator = new CoreActivator(core);

        try {
            activator.start(mockedContext);
            fail("Incomplete directory undetected");
        } catch (Exception e) {
            // OK
        }

        // Verify
        // No install
        ((BundleContext) Mockito.verify(mockedContext,
                Mockito.times(2)))
                .installBundle(Mockito.anyString());

    }

}
