/*
 * 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 java.util.ArrayList;
import java.util.List;

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

/**
 * Creates mock objects.
 * @author <a href="mailto:chameleon-dev@ow2.org">Chameleon Project Team</a>
 */
public final class MockUtils {

    /**
     * Number of bundle from the core folder.
     */
    private static final int COMPLETE_CORE_COUNT = 7;

    /**
     * Avoid instantiation.
     */
    private MockUtils() { }

    /**
     * Gets a fake bundle file.
     * @param bundlename the bundle name
     * @return a fake file pointed on a bundle
     * @throws MalformedURLException if something wrong happened.
     */
    public static File getBundleFile(String bundlename)
            throws MalformedURLException {
        File file = new File("/Users/foo/xxx/" + bundlename);
        File mockedFile = (File) Mockito.mock(File.class);
        Mockito.when(mockedFile.getAbsolutePath()).thenReturn(
                file.getAbsolutePath());
        Mockito.when(mockedFile.getName()).thenReturn(bundlename);

        Mockito.when(mockedFile.toURI()).thenReturn(file.toURI());

        return mockedFile;
    }

    /**
     * Gets a fake complete core directory.
     * @return the file pointing on a fake complete core directory
     * @throws MalformedURLException if something wrong happened.
     */
    public static File getCompleteCoreDirectory() throws MalformedURLException {
        File mockedDir = (File) Mockito.mock(File.class);
        Mockito.when(new Boolean(mockedDir.exists())).thenReturn(
                new Boolean(true));
        Mockito.when(new Boolean(mockedDir.isDirectory())).thenReturn(
                new Boolean(true));


        File mockedGogoCommand =
            getBundleFile("org.apache.felix.gogo.command-0.6.1.jar");
        File mockedGogoRuntime =
            getBundleFile("org.apache.felix.gogo.runtime-0.6.1.jar");
        File mockedGogoShell =
            getBundleFile("org.apache.felix.gogo.shell-0.6.1.jar");
        File mockedArch =
            getBundleFile("org.apache.felix.ipojo.arch.gogo-1.0.1.jar");

        File mockedIPOJO =
            getBundleFile("org.apache.felix.ipojo-1.3.0-SNAPSHOT.jar");

        File mockedShellTUI =
            getBundleFile("org.apache.felix.shell.tui-1.3.0-SNAPSHOT.jar");

        File mockedCA =
            getBundleFile("org.apache.felix.configadmin-1.0.11-SNAPSHOT.jar");

        File mockedFI =
            getBundleFile("org.apache.felix.fileinstall-0.9.3-SNAPSHOT.jar");

        File mockedCompendium =
            getBundleFile("org.osgi.compendium-1.3.0-SNAPSHOT.jar");

        List<File> files = new ArrayList<File>();
        files.add(mockedGogoCommand);
        files.add(mockedGogoRuntime);
        files.add(mockedGogoShell);
        files.add(mockedArch);
        files.add(mockedIPOJO);
        files.add(mockedShellTUI);
        files.add(mockedCA);
        files.add(mockedFI);
        files.add(mockedCompendium);

        Mockito.when(mockedDir.listFiles()).thenReturn(
                files.toArray(new File[COMPLETE_CORE_COUNT]));

        return mockedDir;

    }

    /**
     * Gets a fake complete core directory using the old Felix Shell.
     * @return the file pointing on a fake complete core directory
     * @throws MalformedURLException if something wrong happened.
     */
    public static File getCompleteCoreDirectoryWithShell() throws MalformedURLException {
        File mockedDir = (File) Mockito.mock(File.class);
        Mockito.when(new Boolean(mockedDir.exists())).thenReturn(
                new Boolean(true));
        Mockito.when(new Boolean(mockedDir.isDirectory())).thenReturn(
                new Boolean(true));

        File mockedShell =
            getBundleFile("org.apache.felix.shell-1.3.0-SNAPSHOT.jar");

        File mockedArch =
            getBundleFile("org.apache.felix.ipojo.arch-1.3.0-SNAPSHOT.jar");

        File mockedIPOJO =
            getBundleFile("org.apache.felix.ipojo-1.3.0-SNAPSHOT.jar");

        File mockedShellTUI =
            getBundleFile("org.apache.felix.shell.tui-1.3.0-SNAPSHOT.jar");

        File mockedCA =
            getBundleFile("org.apache.felix.configadmin-1.0.11-SNAPSHOT.jar");

        File mockedFI =
            getBundleFile("org.apache.felix.fileinstall-0.9.3-SNAPSHOT.jar");

        File mockedCompendium =
            getBundleFile("org.osgi.compendium-1.3.0-SNAPSHOT.jar");

        List<File> files = new ArrayList<File>();
        files.add(mockedShell);
        files.add(mockedArch);
        files.add(mockedIPOJO);
        files.add(mockedShellTUI);
        files.add(mockedCA);
        files.add(mockedFI);
        files.add(mockedCompendium);

        Mockito.when(mockedDir.listFiles()).thenReturn(
                files.toArray(new File[COMPLETE_CORE_COUNT]));

        return mockedDir;

    }

    /**
     * Gets a fake bundle context.
     * @return a fake bundle context.
     * @throws BundleException if something bad hapenned,
     */
    public static BundleContext getContext() throws BundleException {
        BundleContext mockedContext = (BundleContext) Mockito
                .mock(BundleContext.class);
        Bundle mockedBundle = (Bundle) Mockito.mock(Bundle.class);

        Mockito.when(mockedContext.installBundle(Mockito.anyString()))
                .thenReturn(mockedBundle);

        return mockedContext;

    }

    /**
     * Gets a fake incomplete core directory.
     * @return the file pointing on a fake incomplete core directory
     * @throws MalformedURLException if something wrong happened.
     */
    public static File getIncompleteCoreDirectory()
            throws MalformedURLException {
        File mockedDir = (File) Mockito.mock(File.class);
        Mockito.when(new Boolean(mockedDir.exists())).thenReturn(
                new Boolean(true));
        Mockito.when(new Boolean(mockedDir.isDirectory())).thenReturn(
                new Boolean(true));

        // Add only two files
        File mockedArch =
            getBundleFile("/Users/foo/xxx/"
                    + "org.apache.felix.ipojo.arch-1.3.0-SNAPSHOT.jar");

        File mockedIPOJO =
            getBundleFile("/Users/foo/xxx/"
                    + "org.apache.felix.ipojo-1.3.0-SNAPSHOT.jar");

        List<File> files = new ArrayList<File>();
        files.add(mockedArch);
        files.add(mockedIPOJO);

        Mockito.when(mockedDir.listFiles()).thenReturn(
                files.toArray(new File[2]));

        return mockedDir;

    }

}
