/*
 * 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;

/**
 * Debug Activator unit tests.
 * @author <a href="mailto:chameleon-dev@ow2.org">Chameleon Project Team</a>
 */
public class DebugActivatorTest extends TestCase {

    /**
     * Simulates the starting with the debug activator.
     * @throws MalformedURLException if something bad happened
     */
    public void testFullCreation() throws MalformedURLException {
        File mockedDir = MockUtils.getCompleteCoreDirectory();
        assertTrue(mockedDir.exists());
        assertTrue(mockedDir.isDirectory());
        DebugActivator activator = new DebugActivator(mockedDir);
        assertNotNull(activator);
    }

    /**
     * Simulates the starting with the debug activator.
     * Checks the installation.
     * @throws Exception if something bad happened
     */
    public void testFullStart() throws Exception {
        File mockedDir = MockUtils.getCompleteCoreDirectory();

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

        // Verify
        ((BundleContext) Mockito.verify(mockedContext, Mockito.times(4)))
                .installBundle(Mockito.anyString());
    }

    /**
     * Simulates the starting with the debug activator and old Felix shell.
     * Checks the installation.
     * @throws Exception if something bad happened
     */
    public void testFullStartWithFelixShell() throws Exception {
        File mockedDir = MockUtils.getCompleteCoreDirectoryWithShell();

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

        // Verify
        ((BundleContext) Mockito.verify(mockedContext, Mockito.times(3)))
                .installBundle(Mockito.anyString());
    }

    /**
     * Check stopping.
     * @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 on real core.
     * @throws Exception if something bad happened
     */
    public void testOnFullCoreDirectory() throws Exception {
        File core = new File(Constants.FULL_CORE);

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

        // Verify
        ((BundleContext) Mockito.verify(mockedContext, Mockito.times(4)))
                .installBundle(Mockito.anyString());
    }

}
