Search in sources :

Example 11 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testErrorHandling1.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testErrorHandling1() throws Exception {
    // Broken .classpath file
    File projectDir = createProject("testError1", "test.pkg");
    File classPath = new File(projectDir, ".classpath");
    Files.write("" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<classpath>\n" + // <== XML error
    "        <classpathentry kind=\"src\" path=\"src\"/\n" + "        <classpathentry kind=\"src\" path=\"gen\"/>\n" + "        <classpathentry kind=\"con\" path=\"com.android.ide.eclipse.adt.ANDROID_FRAMEWORK\"/>\n" + "        <classpathentry exported=\"true\" kind=\"con\" path=\"com.android.ide.eclipse.adt.LIBRARIES\"/>\n" + "        <classpathentry exported=\"true\" kind=\"con\" path=\"com.android.ide.eclipse.adt.DEPENDENCIES\"/>\n" + "        <classpathentry kind=\"output\" path=\"bin/classes\"/>\n" + "</classpath>", classPath, UTF_8);
    final AtomicReference<GradleImport> importReference = new AtomicReference<>();
    File imported = checkProject(projectDir, "" + MSG_HEADER + "\n" + " * $ROOT/.classpath:\n" + "Invalid XML file: $ROOT/.classpath:\n" + "Element type \"classpathentry\" must be followed by either attribute specifications, \">\" or \"/>\".\n" + MSG_FOOTER, false, /* checkBuild */
    importReference::set);
    assertEquals("[$CLASSPATH_FILE:\n" + "Invalid XML file: $CLASSPATH_FILE:\n" + "Element type \"classpathentry\" must be followed by either attribute " + "specifications, \">\" or \"/>\".]", importReference.get().getErrors().toString().replace(classPath.getPath(), "$CLASSPATH_FILE").replace(classPath.getCanonicalPath(), "$CLASSPATH_FILE"));
    deleteDir(projectDir);
    deleteDir(imported);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 12 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testResolveExpressions.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testResolveExpressions() throws Exception {
    File root = Files.createTempDir();
    File projectDir = new File(root, "dir1" + separator + "dir2" + separator + "dir3" + separator + "dir4" + separator + "prj");
    projectDir.mkdirs();
    createProject(projectDir, "test1", "test.pkg");
    File var1 = new File(root, "sub1" + separator + "sub2" + separator + "sub3");
    var1.mkdirs();
    File parentFile = projectDir.getParentFile();
    File var4 = new File(parentFile, "var4");
    var4.mkdirs();
    File tpl = new File(parentFile.getParentFile().getParentFile(), "TARGET" + separator + "android" + separator + "third-party");
    tpl.mkdirs();
    File supportLib = new File(tpl, "android-support-v4.r19.jar");
    supportLib.createNewFile();
    Files.write("" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<projectDescription>\n" + "\t<name>UnitTest</name>\n" + "\t<comment></comment>\n" + "\t<projects>\n" + "\t</projects>\n" + "\t<buildSpec>\n" + "\t\t<buildCommand>\n" + "\t\t\t<name>org.eclipse.jdt.core.javabuilder</name>\n" + "\t\t\t<arguments>\n" + "\t\t\t</arguments>\n" + "\t\t</buildCommand>\n" + "\t</buildSpec>\n" + "\t<natures>\n" + "\t\t<nature>org.eclipse.jdt.core.javanature</nature>\n" + "\t</natures>\n" + "\t<linkedResources>\n" + "\t\t<link>\n" + "\t\t\t<name>MYLIBS</name>\n" + "\t\t\t<type>2</type>\n" + "\t\t\t<locationURI>MYLIBS</locationURI>\n" + "\t\t</link>\n" + "\t\t<link>\n" + "\t\t\t<name>3rd_java_libs</name>\n" + "\t\t\t<type>2</type>\n" + "\t\t\t<locationURI>PARENT-3-PROJECT_LOC/TARGET/android/third-party</locationURI>\n" + "\t\t</link>\n" + "\t\t<link>\n" + "\t\t\t<name>jnilibs</name>\n" + "\t\t\t<type>2</type>\n" + "\t\t\t<locationURI>virtual:/virtual</locationURI>\n" + "\t\t</link>\n" + "\t</linkedResources>\n" + "\t<variableList>\n" + "\t\t<variable>\n" + "\t\t\t<name>MY_VAR_1</name>\n" + "\t\t\t<value>" + SdkUtils.fileToUrl(var1) + "</value>\n" + "\t\t</variable>\n" + "\t\t<variable>\n" + "\t\t\t<name>MY_VAR_2</name>\n" + "\t\t\t<value>$%7BMY_VAR_1%7D</value>\n" + "\t\t</variable>\n" + "\t\t<variable>\n" + "\t\t\t<name>MY_VAR_3</name>\n" + "\t\t\t<value>$%7BPROJECT_LOC%7D/src</value>\n" + "\t\t</variable>\n" + "\t\t<variable>\n" + "\t\t\t<name>MY_VAR_4</name>\n" + "\t\t\t<value>$%7BPARENT-1-PROJECT_LOC%7D/var4</value>\n" + "\t\t</variable>\n" + "\t\t<variable>\n" + "\t\t\t<name>MY_VAR_5</name>\n" + "\t\t\t<value>$%7BPARENT_LOC%7D/var4</value>\n" + "\t\t</variable>\n" + "\t</variableList>\n" + "</projectDescription>", new File(projectDir, ".project"), UTF_8);
    GradleImport importer = new GradleImport();
    EclipseProject project = EclipseProject.getProject(importer, projectDir);
    importer.getPathMap();
    // Test absolute paths
    assertEquals(var1, project.resolveVariableExpression(var1.getPath()));
    assertEquals(var1, project.resolveVariableExpression(var1.getAbsolutePath()));
    assertEquals(var1.getCanonicalFile(), project.resolveVariableExpression(var1.getCanonicalPath()));
    // on Windows, make sure we handle workspace files with forwards
    assertEquals(var1, project.resolveVariableExpression(var1.getPath().replace('/', separatorChar)));
    // Test project relative paths
    String relative = "src" + separator + "test" + separator + "pkg" + separator + "MyActivity.java";
    assertEquals(new File(projectDir, relative), project.resolveVariableExpression(relative));
    assertEquals(new File(projectDir, relative), project.resolveVariableExpression(relative.replace('/', separatorChar)));
    // Test workspace paths
    // This is handled by testLibraries2
    // Test path variables
    assertEquals(var1, project.resolveVariableExpression("MY_VAR_1"));
    assertEquals(var1, project.resolveVariableExpression("MY_VAR_2"));
    assertEquals(new File(projectDir, "src"), project.resolveVariableExpression("MY_VAR_3"));
    assertEquals(var4, project.resolveVariableExpression("MY_VAR_4"));
    assertEquals(var4, project.resolveVariableExpression("MY_VAR_5"));
    // Test linked variables
    assertEquals(supportLib, project.resolveVariableExpression("3rd_java_libs/android-support-v4.r19.jar"));
    // Test user-supplied values
    assertEquals(var1, project.resolveVariableExpression("MY_VAR_1"));
    importer.getPathMap().put("MY_VAR_1", projectDir);
    assertEquals(projectDir, project.resolveVariableExpression("MY_VAR_1"));
    importer.getPathMap().put("/some/unresolved/path", var4);
    assertEquals(var4, project.resolveVariableExpression("/some/unresolved/path"));
    // Setup for workspace tests
    assertNull(project.resolveVariableExpression("MY_GLOBAL_VAR"));
    final File workspace = new File(root, "workspace");
    workspace.mkdirs();
    File prefs = new File(workspace, ".metadata" + separator + ".plugins" + separator + "org.eclipse.core.runtime" + separator + ".settings" + separator + "org.eclipse.jdt.core.prefs");
    prefs.getParentFile().mkdirs();
    File global1 = var1.getParentFile();
    Files.write("" + "eclipse.preferences.version=1\n" + "org.eclipse.jdt.core.classpathVariable.MY_GLOBAL_VAR=" + global1.getPath().replace(separatorChar, '/').replace(":", "\\:") + "\n" + "org.eclipse.jdt.core.codeComplete.visibilityCheck=enabled\n" + "org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled\n" + "org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6\n" + "org.eclipse.jdt.core.compiler.compliance=1.6\n" + "org.eclipse.jdt.core.compiler.problem.assertIdentifier=error\n" + "org.eclipse.jdt.core.compiler.problem.enumIdentifier=error\n" + "org.eclipse.jdt.core.compiler.source=1.6\n" + "org.eclipse.jdt.core.formatter.tabulation.char=space", prefs, UTF_8);
    File global2 = var4.getParentFile();
    prefs = new File(workspace, ".metadata" + separator + ".plugins" + separator + "org.eclipse.core.runtime" + separator + ".settings" + separator + "org.eclipse.core.resources.prefs");
    prefs.getParentFile().mkdirs();
    Files.write("" + "eclipse.preferences.version=1\n" + "pathvariable.MY_GLOBAL_VAR_2=" + global2.getPath().replace(separatorChar, '/').replace(":", "\\:") + "\n" + "version=1", prefs, UTF_8);
    importer.setEclipseWorkspace(workspace);
    // Test global path variables
    assertEquals(global1, project.resolveVariableExpression("MY_GLOBAL_VAR"));
    assertEquals(var1, project.resolveVariableExpression("MY_GLOBAL_VAR/sub3"));
    assertEquals(var1, project.resolveVariableExpression("MY_GLOBAL_VAR" + separator + "sub3"));
    // Test workspace linked resources
    assertEquals(global2, project.resolveVariableExpression("MY_GLOBAL_VAR_2"));
    deleteDir(projectDir);
}
Also used : GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 13 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testLibrariesWithWorkspaceMapping1.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testLibrariesWithWorkspaceMapping1() throws Exception {
    // Provide manually edited workspace mapping /Library1 = actual dir
    final String library1Path = "subdir1" + separator + "subdir2" + separator + "UnrelatedName";
    final File library1Dir = new File(library1Path);
    Pair<File, File> pair = createLibrary2(library1Dir);
    final File root = pair.getFirst();
    File app = pair.getSecond();
    final AtomicReference<GradleImport> importReference = new AtomicReference<>();
    File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "* .gitignore\n" + MSG_REPLACED_JARS + "guava-13.0.1.jar => com.google.guava:guava:13.0.1\n" + MSG_GUESSED_VERSIONS + "guava-13.0.1.jar => version 13.0.1 in com.google.guava:guava:13.0.1\n" + MSG_FOLDER_STRUCTURE + "In Library1:\n" + "* src/ => library1/src/main/java/\n" + "In Library2:\n" + "* src/ => library2/src/main/java/\n" + "In AndroidLibrary:\n" + "* AndroidManifest.xml => androidLibrary/src/main/AndroidManifest.xml\n" + "* src/ => androidLibrary/src/main/java/\n" + "In AndroidApp:\n" + "* AndroidManifest.xml => androidApp/src/main/AndroidManifest.xml\n" + "* res/ => androidApp/src/main/res/\n" + "* src/ => androidApp/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
    importer -> {
        importReference.set(importer);
        importer.getPathMap().put("/Library1", new File(root, library1Path));
    });
    assertEquals("{/Library1=" + new File(root, library1Path).getCanonicalPath() + ", /Library2=" + new File(root, "Library2").getCanonicalPath() + "}", describePathMap(importReference.get()));
    deleteDir(root);
    deleteDir(imported);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 14 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method testLibrariesWithWorkspacePathVars.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testLibrariesWithWorkspacePathVars() throws Exception {
    // Provide manually edited workspace location which contains workspace locations
    final String library1Path = "subdir1" + separator + "subdir2" + separator + "UnrelatedName";
    final File library1Dir = new File(library1Path);
    Pair<File, File> pair = createLibrary2(library1Dir);
    final File root = pair.getFirst();
    File app = pair.getSecond();
    final File library1AbsDir = new File(root, library1Path);
    final File workspace = new File(root, "workspace");
    workspace.mkdirs();
    File metadata = new File(workspace, ".metadata");
    metadata.mkdirs();
    new File(metadata, "version.ini").createNewFile();
    assertTrue(isEclipseWorkspaceDir(workspace));
    File projects = new File(metadata, ".plugins" + separator + "org.eclipse.core.resources" + separator + ".projects");
    projects.mkdirs();
    File library1 = new File(projects, "Library1");
    library1.mkdirs();
    File location = new File(library1, ".location");
    byte[] data = ("blahblahblahURI//" + SdkUtils.fileToUrl(library1AbsDir) + "\000blahblahblah").getBytes(UTF_8);
    Files.write(data, location);
    final AtomicReference<GradleImport> importReference = new AtomicReference<>();
    File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "* .gitignore\n" + MSG_REPLACED_JARS + "guava-13.0.1.jar => com.google.guava:guava:13.0.1\n" + MSG_GUESSED_VERSIONS + "guava-13.0.1.jar => version 13.0.1 in com.google.guava:guava:13.0.1\n" + MSG_FOLDER_STRUCTURE + "In Library1:\n" + "* src/ => library1/src/main/java/\n" + "In Library2:\n" + "* src/ => library2/src/main/java/\n" + "In AndroidLibrary:\n" + "* AndroidManifest.xml => androidLibrary/src/main/AndroidManifest.xml\n" + "* src/ => androidLibrary/src/main/java/\n" + "In AndroidApp:\n" + "* AndroidManifest.xml => androidApp/src/main/AndroidManifest.xml\n" + "* res/ => androidApp/src/main/res/\n" + "* src/ => androidApp/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
    importer -> {
        importReference.set(importer);
        importer.setEclipseWorkspace(workspace);
    });
    assertEquals("{/Library1=" + new File(root, library1Path).getCanonicalPath() + ", /Library2=" + new File(root, "Library2").getCanonicalPath() + "}", describePathMap(importReference.get()));
    deleteDir(root);
    deleteDir(imported);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Example 15 with GradleImport

use of com.android.tools.idea.gradle.eclipse.GradleImport in project android by JetBrains.

the class GradleImportTest method test65167.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void test65167() throws Exception {
    // Regression test for https://code.google.com/p/android/issues/detail?id=65167
    Pair<File, File> pair = createLibrary2(new File("Library1"));
    File root = pair.getFirst();
    File app = pair.getSecond();
    File libs = new File(app, "libs");
    libs.mkdirs();
    new File(libs, "unknown-lib.jar").createNewFile();
    final AtomicReference<GradleImport> importReference = new AtomicReference<>();
    File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "* .gitignore\n" + MSG_REPLACED_JARS + "guava-13.0.1.jar => com.google.guava:guava:13.0.1\n" + MSG_GUESSED_VERSIONS + "guava-13.0.1.jar => version 13.0.1 in com.google.guava:guava:13.0.1\n" + MSG_FOLDER_STRUCTURE + "In Library1:\n" + "* src/ => library1/src/main/java/\n" + "In Library2:\n" + "* src/ => library2/src/main/java/\n" + "In AndroidLibrary:\n" + "* AndroidManifest.xml => androidLibrary/src/main/AndroidManifest.xml\n" + "* src/ => androidLibrary/src/main/java/\n" + "In AndroidApp:\n" + "* AndroidManifest.xml => androidApp/src/main/AndroidManifest.xml\n" + "* libs/unknown-lib.jar => androidApp/libs/unknown-lib.jar\n" + "* res/ => androidApp/src/main/res/\n" + "* src/ => androidApp/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
    importReference::set);
    deleteDir(root);
    deleteDir(imported);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) GradleImport(com.android.tools.idea.gradle.eclipse.GradleImport) File(java.io.File)

Aggregations

GradleImport (com.android.tools.idea.gradle.eclipse.GradleImport)15 File (java.io.File)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 AdtImportBuilder (com.android.tools.idea.gradle.eclipse.AdtImportBuilder)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 VfsUtil.findFileByIoFile (com.intellij.openapi.vfs.VfsUtil.findFileByIoFile)2 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Map (java.util.Map)2 NotNull (org.jetbrains.annotations.NotNull)2 Supplier (com.google.common.base.Supplier)1 Project (com.intellij.openapi.project.Project)1