<commit_before>/*******************************************************************************
 * Copyright (c) 2009-2015 <NAME>, <NAME>, <NAME>
 * 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 java.io.File.separator;
import static org.codehaus.plexus.util.FileUtils.cleanDirectory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.maven.model.Model;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.project.ProjectConfigurationManager;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectImportResult;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.MavenUpdateRequest;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.project.ResolverConfiguration;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.eclipse.m2e.tests.common.WorkspaceHelpers;

@SuppressWarnings("restriction")
public abstract class AndroidMavenPluginTestCase extends AbstractMavenProjectTestCase {

    public static final String CONTAINER_NONRUNTIME_DEPENDENCIES = "me.gladwell.eclipse.m2e.android.classpath.NONRUNTIME_DEPENDENCIES";

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

    private void cleanClasspathCache() throws IOException {
        File cache = new File(workspace.getRoot().getLocation().toFile(), ".metadata/.plugins/me.gladwell.eclipse.m2e.android");
        if(cache.exists()) {
            cleanDirectory(cache);
        }
    }

    /**
     * @deprecated
     * Method replaced by the fluent {@link ProjectImporter#importAndroidTestProject(String)}
     * project builder.
     */
    @Deprecated
    protected IProject importAndroidProject(String name) throws Exception {
        IProject project = importProject("projects" + separator + name + separator + "pom.xml");
        waitForJobsToComplete();
        return project;
    }

    protected IProject importAndroidProject(String name, File folder) throws Exception {
        return importProject(name, folder, new ResolverConfiguration());
    }

    protected IProject importAndroidProject(String name, ResolverConfiguration configuration) throws Exception {
        IProject project = importProject("projects" + separator + name + separator + "pom.xml", configuration);
        waitForJobsToComplete();
        return project;
    }

    private IProject importProject(String name, File parent, ResolverConfiguration configuration) throws Exception {
        MavenModelManager mavenModelManager = MavenPlugin.getMavenModelManager();
        String pomLocation = "projects" + separator + name + separator + "pom.xml";

        File pomFile = new File(pomLocation);
        File src = new File(pomFile.getParentFile().getCanonicalPath());
        File dst = new File(parent, pomFile.getParentFile().getName());

        copyDir(src, dst);

        String pomName = pomFile.getName();
        pomFile = new File(dst, pomName);
        Model model = mavenModelManager.readMavenModel(pomFile);
        MavenProjectInfo projectInfo = new MavenProjectInfo(pomName, pomFile, model, null);
        File workspaceRoot = workspace.getRoot().getLocation().toFile();
        File basedir = projectInfo.getPomFile().getParentFile().getCanonicalFile();

        projectInfo.setBasedirRename(basedir.getParentFile().equals(workspaceRoot) ? MavenProjectInfo.RENAME_REQUIRED
                : MavenProjectInfo.RENAME_NO);

        final ArrayList<MavenProjectInfo> projectInfos = new ArrayList<MavenProjectInfo>();
        projectInfos.add(projectInfo);

        final ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(configuration);

        final ArrayList<IMavenProjectImportResult> importResults = new ArrayList<IMavenProjectImportResult>();

        workspace.run(new IWorkspaceRunnable() {
            public void run(IProgressMonitor monitor) throws CoreException {
                importResults.addAll(MavenPlugin.getProjectConfigurationManager().importProjects(projectInfos,
                        importConfiguration, monitor));
            }
        }, MavenPlugin.getProjectConfigurationManager().getRule(), IWorkspace.AVOID_UPDATE, monitor);

        IProject[] projects = new IProject[projectInfos.size()];
        for (int i = 0; i < projectInfos.size(); i++) {
            IMavenProjectImportResult importResult = importResults.get(i);
            assertSame(projectInfos.get(i), importResult.getMavenProjectInfo());
            projects[i] = importResult.getProject();
            assertNotNull("Failed to import project " + projectInfos, projects[i]);

            Model model1 = projectInfos.get(0).getModel();
            IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(projects[i], monitor);
            if (facade == null) {
                fail("Project " + model1.getGroupId() + "-" + model1.getArtifactId() + "-" + model1.getVersion()
                        + " was not imported. Errors: "
                        + WorkspaceHelpers.toString(WorkspaceHelpers.findErrorMarkers(projects[i])));
            }
        }

        return projects[0];
    }

    public IProject[] importAndroidProjects(String basedir, String[] pomNames) throws Exception {
        IProject[] projects = importProjects(basedir, pomNames, new ResolverConfiguration());
        waitForJobsToComplete();
        return projects;
    }

    protected void buildAndroidProject(IProject project, int kind) throws CoreException, InterruptedException {
        ResourcesPlugin.getWorkspace().build(project.getBuildConfigs(), kind, true, monitor);
        project.build(kind, "com.android.ide.eclipse.adt.ResourceManagerBuilder", new HashMap<String, String>(), monitor);
        project.build(kind, "com.android.ide.eclipse.adt.PreCompilerBuilder", new HashMap<String, String>(), monitor);
        project.build(kind, "org.eclipse.jdt.core.javabuilder", new HashMap<String, String>(), monitor);
    
        waitForJobsToComplete();
    }

    protected void assertClasspathContains(IJavaProject javaProject, String path) throws JavaModelException {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            if (entry.getPath().toOSString().contains(path)) {
                return;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
                for (IClasspathEntry e : container.getClasspathEntries()) {
                    if (e.getPath().toOSString().contains(path)) {
                        return;
                    }
                }
            }
        }
        fail(path + " should be in classpath");
    }

    protected void assertClasspathDoesNotContain(IJavaProject javaProject, String path) throws JavaModelException {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            assertFalse(entry.getPath().toOSString().contains(path));
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
                for (IClasspathEntry e : container.getClasspathEntries()) {
                    assertFalse(path + " should not be in classpath", e.getPath().toOSString().contains(path));
                }
            }
        }
    }<commit_msg>Invoke Maven Builder in buildAndroidProject<commit_after>/*******************************************************************************
 * Copyright (c) 2009-2015 <NAME>, <NAME>, <NAME>
 * 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 java.io.File.separator;
import static org.codehaus.plexus.util.FileUtils.cleanDirectory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.maven.model.Model;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathAttribute;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.internal.project.ProjectConfigurationManager;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectImportResult;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.MavenUpdateRequest;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.project.ResolverConfiguration;
import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase;
import org.eclipse.m2e.tests.common.WorkspaceHelpers;

@SuppressWarnings("restriction")
public abstract class AndroidMavenPluginTestCase extends AbstractMavenProjectTestCase {

    public static final String CONTAINER_NONRUNTIME_DEPENDENCIES = "me.gladwell.eclipse.m2e.android.classpath.NONRUNTIME_DEPENDENCIES";

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

    private void cleanClasspathCache() throws IOException {
        File cache = new File(workspace.getRoot().getLocation().toFile(), ".metadata/.plugins/me.gladwell.eclipse.m2e.android");
        if(cache.exists()) {
            cleanDirectory(cache);
        }
    }

    /**
     * @deprecated
     * Method replaced by the fluent {@link ProjectImporter#importAndroidTestProject(String)}
     * project builder.
     */
    @Deprecated
    protected IProject importAndroidProject(String name) throws Exception {
        IProject project = importProject("projects" + separator + name + separator + "pom.xml");
        waitForJobsToComplete();
        return project;
    }

    protected IProject importAndroidProject(String name, File folder) throws Exception {
        return importProject(name, folder, new ResolverConfiguration());
    }

    protected IProject importAndroidProject(String name, ResolverConfiguration configuration) throws Exception {
        IProject project = importProject("projects" + separator + name + separator + "pom.xml", configuration);
        waitForJobsToComplete();
        return project;
    }

    private IProject importProject(String name, File parent, ResolverConfiguration configuration) throws Exception {
        MavenModelManager mavenModelManager = MavenPlugin.getMavenModelManager();
        String pomLocation = "projects" + separator + name + separator + "pom.xml";

        File pomFile = new File(pomLocation);
        File src = new File(pomFile.getParentFile().getCanonicalPath());
        File dst = new File(parent, pomFile.getParentFile().getName());

        copyDir(src, dst);

        String pomName = pomFile.getName();
        pomFile = new File(dst, pomName);
        Model model = mavenModelManager.readMavenModel(pomFile);
        MavenProjectInfo projectInfo = new MavenProjectInfo(pomName, pomFile, model, null);
        File workspaceRoot = workspace.getRoot().getLocation().toFile();
        File basedir = projectInfo.getPomFile().getParentFile().getCanonicalFile();

        projectInfo.setBasedirRename(basedir.getParentFile().equals(workspaceRoot) ? MavenProjectInfo.RENAME_REQUIRED
                : MavenProjectInfo.RENAME_NO);

        final ArrayList<MavenProjectInfo> projectInfos = new ArrayList<MavenProjectInfo>();
        projectInfos.add(projectInfo);

        final ProjectImportConfiguration importConfiguration = new ProjectImportConfiguration(configuration);

        final ArrayList<IMavenProjectImportResult> importResults = new ArrayList<IMavenProjectImportResult>();

        workspace.run(new IWorkspaceRunnable() {
            public void run(IProgressMonitor monitor) throws CoreException {
                importResults.addAll(MavenPlugin.getProjectConfigurationManager().importProjects(projectInfos,
                        importConfiguration, monitor));
            }
        }, MavenPlugin.getProjectConfigurationManager().getRule(), IWorkspace.AVOID_UPDATE, monitor);

        IProject[] projects = new IProject[projectInfos.size()];
        for (int i = 0; i < projectInfos.size(); i++) {
            IMavenProjectImportResult importResult = importResults.get(i);
            assertSame(projectInfos.get(i), importResult.getMavenProjectInfo());
            projects[i] = importResult.getProject();
            assertNotNull("Failed to import project " + projectInfos, projects[i]);

            Model model1 = projectInfos.get(0).getModel();
            IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(projects[i], monitor);
            if (facade == null) {
                fail("Project " + model1.getGroupId() + "-" + model1.getArtifactId() + "-" + model1.getVersion()
                        + " was not imported. Errors: "
                        + WorkspaceHelpers.toString(WorkspaceHelpers.findErrorMarkers(projects[i])));
            }
        }

        return projects[0];
    }

    public IProject[] importAndroidProjects(String basedir, String[] pomNames) throws Exception {
        IProject[] projects = importProjects(basedir, pomNames, new ResolverConfiguration());
        waitForJobsToComplete();
        return projects;
    }

    protected void buildAndroidProject(IProject project, int kind) throws CoreException, InterruptedException {
        ResourcesPlugin.getWorkspace().build(project.getBuildConfigs(), kind, true, monitor);
        project.build(kind, "com.android.ide.eclipse.adt.ResourceManagerBuilder", new HashMap<String, String>(), monitor);
        project.build(kind, "com.android.ide.eclipse.adt.PreCompilerBuilder", new HashMap<String, String>(), monitor);
        project.build(kind, "org.eclipse.jdt.core.javabuilder", new HashMap<String, String>(), monitor);
        project.build(kind, IMavenConstants.BUILDER_ID, new HashMap<String, String>(), monitor);
    
        waitForJobsToComplete();
    }

    protected void assertClasspathContains(IJavaProject javaProject, String path) throws JavaModelException {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            if (entry.getPath().toOSString().contains(path)) {
                return;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
                for (IClasspathEntry e : container.getClasspathEntries()) {
                    if (e.getPath().toOSString().contains(path)) {
                        return;
                    }
                }
            }
        }
        fail(path + " should be in classpath");
    }

    protected void assertClasspathDoesNotContain(IJavaProject javaProject, String path) throws JavaModelException {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            assertFalse(entry.getPath().toOSString().contains(path));
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), javaProject);
                for (IClasspathEntry e : container.getClasspathEntries()) {
                    assertFalse(path + " should not be in classpath", e.getPath().toOSString().contains(path));
                }
            }
        }
    }
{'fraction_non_alphanumeric': 0.11887554771524497, 'fraction_numerical': 0.003869572639845217, 'mean_word_length': 3.5921087013326365, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '836609', 'n_tokens_mistral': 4691, 'n_tokens_neox': 4590, 'n_words': 914}