/*******************************************************************************
 * Copyright (c) 2012 Ricardo Gladwell
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package me.gladwell.eclipse.m2e.android.test;

import static com.android.ide.eclipse.adt.internal.sdk.Sdk.getProjectState;
import static org.eclipse.m2e.core.MavenPlugin.getProjectConfigurationManager;
import junit.framework.Assert;

import me.gladwell.eclipse.m2e.android.AndroidMavenPlugin;
import me.gladwell.eclipse.m2e.android.configuration.ProjectConfigurationException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

public class LibraryAndroidMavenPluginTest extends AndroidMavenPluginTestCase {

    private static final String ANDROID_LIB_PROJECT_NAME = "apklib-project";

    private IProject libraryProject;

	@Override
	protected void setUp() throws Exception {
		super.setUp();

		deleteProject(ANDROID_LIB_PROJECT_NAME);
		libraryProject = importAndroidProject(ANDROID_LIB_PROJECT_NAME);
	}

    @Override
    protected void tearDown() throws Exception {
        try {
            deleteProject(ANDROID_LIB_PROJECT_NAME);
        } catch(Throwable t) {
            
        } finally {
            try {
                super.tearDown();
            } catch(Throwable t) {
                t.printStackTrace();
            }
        }
    }

	public void testConfigure() throws Exception {
		assertNoErrors(libraryProject);
	}

	public void testConfigureAppliesLibraryState() throws Exception {
		assertTrue(getProjectState(libraryProject).isLibrary());
	}

	public void testConfigureAddsWorkspaceLibraryProjectToProjectProperties() throws Exception {
		IProject project = importAndroidProject("test-project-apklib-deps");

		assertTrue(getProjectState(project).getFullLibraryProjects().contains(libraryProject));
	}

	public void testConfigureAddsErrorForNonExistentLibraryProject() throws Exception {
		deleteProject(ANDROID_LIB_PROJECT_NAME);
		IProject project = importAndroidProject("test-project-apklib-deps");

		assertErrorMarker(project, AndroidMavenPlugin.APKLIB_ERROR_TYPE);
	}

	public void testConfigureWithAClosedProjectInTheWorkspace() throws Exception {
		
		IProject javaProject = importAndroidProject("closed-java-project");
		IJavaProject closedJavaProject = JavaCore.create(javaProject);
		closedJavaProject.close();
		javaProject.close(null);
		try{
			IProject project = importAndroidProject("test-project-apklib-deps");
			
		} catch (CoreException ex){
			if (ex.getCause() instanceof ProjectConfigurationException){
				Assert.fail("Access denied: M2E is trying to access a closed project");
			}
		}
	}

	public void testConfigureAddsWorkspaceLibraryProjectWithDifferentArtifactId() throws Exception {
		IProject project = importAndroidProject("test-project-apklib-deps-diff-artifact-id");

		assertTrue(getProjectState(project).getFullLibraryProjects().contains(libraryProject));
	}

	public void testConfigureClearsOldErrors() throws Exception {
		deleteProject(ANDROID_LIB_PROJECT_NAME);
		IProject project = importAndroidProject("test-project-apklib-deps");
		importAndroidProject(ANDROID_LIB_PROJECT_NAME);
		getProjectConfigurationManager().updateProjectConfiguration(project, monitor);

		assertNoErrors(project);
	}

}

@@ -1595,21 +1595,9 @@
 ) %7B%0A
-            
 %0A
+
     
@@ -2230,32 +2230,126 @@
 ibraryProject));
+%0A%0A%09%09try %7B%0A%09%09    deleteProject(%22test-project-apklib-deps%22);%0A%09%09%7D catch(Throwable t) %7B%0A%09%09    %0A%09%09%7D
 %0A%09%7D%0A%0A%09public voi
@@ -2604,16 +2604,140 @@
 R_TYPE);
+%0A%0A        try %7B%0A            deleteProject(%22test-project-apklib-deps%22);%0A        %7D catch(Throwable t) %7B%0A            %0A        %7D
 %0A%09%7D%0A%0A%09pu
@@ -3265,16 +3265,299 @@
 );%0A%09%09%09%7D%0A
+%09%09%7D finally %7B%0A%09%09    try %7B%0A%09%09        deleteProject(%22closed-java-project%22);%0A            %7D catch(Throwable t) %7B%0A                %0A            %7D%0A            try %7B%0A                deleteProject(%22test-project-apklib-deps%22);%0A            %7D catch(Throwable t) %7B%0A                %0A            %7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -3832,16 +3832,85 @@
 oject));
+%0A%0A        deleteProject(%22test-project-apklib-deps-diff-artifact-id%22);
 %0A%09%7D%0A%0A%09pu
@@ -4211,24 +4211,24 @@
  monitor);%0A%0A
-
 %09%09assertNoEr
@@ -4237,19 +4237,143 @@
 rs(project);
+%0A%0A        try %7B%0A            deleteProject(%22test-project-apklib-deps%22);%0A        %7D catch(Throwable t) %7B%0A            %0A        %7D
 %0A%09%7D%0A%0A%7D%0A

delete test projects after creation for more stable integration tests.