summaryrefslogtreecommitdiff
path: root/compilationTests
diff options
context:
space:
mode:
authorYigit Boyar <yboyar@google.com>2015-06-09 00:32:59 -0700
committerYigit Boyar <yboyar@google.com>2015-06-11 16:53:27 -0700
commit9064a1dd60eb8c2f9d2ed705b36bb5f0b1629771 (patch)
treedf60795419743994b8927032ca3fe76d15a0d9bf /compilationTests
parent5199129ec90e0afbd6c3ee81e1e18f0e2bfca9c3 (diff)
downloaddata-binding-9064a1dd60eb8c2f9d2ed705b36bb5f0b1629771.tar.gz
Compilation tests module
This CL adds a new module which is a java project that can be used to create one-off project setups. This module will be used to test error cases and to make sure we detect errors and dispatch proper error messages. I've also updated antlr to 4.5 which gets rid of the annoying annotation processor source compatibility error: https://github.com/antlr/antlr4/issues/487 Bug: 21735564 Change-Id: I16c04923c7d69ca40fe13139acaf87c08166ad95
Diffstat (limited to 'compilationTests')
-rw-r--r--compilationTests/build.gradle15
-rw-r--r--compilationTests/src/test/java/android/databinding/compilationTest/BaseCompilationTest.java126
-rw-r--r--compilationTests/src/test/java/android/databinding/compilationTest/CompilationResult.java37
-rw-r--r--compilationTests/src/test/java/android/databinding/compilationTest/SimpleCompilationTest.java62
-rw-r--r--compilationTests/src/test/resources/AndroidManifest.xml20
-rw-r--r--compilationTests/src/test/resources/app_build.gradle33
-rw-r--r--compilationTests/src/test/resources/layout/undefined_variable_binding.xml28
-rw-r--r--compilationTests/src/test/resources/project_build.gradle29
-rw-r--r--compilationTests/src/test/resources/settings.gradle1
9 files changed, 351 insertions, 0 deletions
diff --git a/compilationTests/build.gradle b/compilationTests/build.gradle
new file mode 100644
index 00000000..abe046ac
--- /dev/null
+++ b/compilationTests/build.gradle
@@ -0,0 +1,15 @@
+apply plugin: 'java'
+
+sourceCompatibility = 1.7
+version = '1.0'
+
+repositories {
+ mavenCentral()
+}
+
+dependencies {
+ testCompile group: 'junit', name: 'junit', version: '4.11'
+ testCompile 'org.apache.commons:commons-lang3:3.3.2'
+ testCompile 'commons-io:commons-io:2.4'
+ testCompile 'commons-codec:commons-codec:1.10'
+} \ No newline at end of file
diff --git a/compilationTests/src/test/java/android/databinding/compilationTest/BaseCompilationTest.java b/compilationTests/src/test/java/android/databinding/compilationTest/BaseCompilationTest.java
new file mode 100644
index 00000000..24cc4aee
--- /dev/null
+++ b/compilationTests/src/test/java/android/databinding/compilationTest/BaseCompilationTest.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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 android.databinding.compilationTest;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.filefilter.IOFileFilter;
+import org.apache.commons.io.filefilter.TrueFileFilter;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFilePermission;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+
+public class BaseCompilationTest {
+ File testFolder = new File("./build/build-test");
+
+ protected void copyResourceTo(String name, String path) throws IOException {
+ copyResourceTo(name, new File(testFolder, path));
+ }
+
+ protected void copyResourceDirectory(String name, String targetPath)
+ throws URISyntaxException, IOException {
+ URL dir = getClass().getResource(name);
+ assertNotNull(dir);
+ assertEquals("file", dir.getProtocol());
+ File folder = new File(dir.toURI());
+ assertTrue(folder.isDirectory());
+ File target = new File(testFolder, targetPath);
+ int len = folder.getAbsolutePath().length() + 1;
+ for (File item : FileUtils.listFiles(folder, null, true)) {
+ if (item.getAbsolutePath().equals(folder.getAbsolutePath())) {
+ continue;
+ }
+ String resourcePath = item.getAbsolutePath().substring(len);
+
+ copyResourceTo(name + "/" + resourcePath, new File(target, resourcePath));
+ }
+ }
+
+ protected void copyResourceTo(String name, File targetFile) throws IOException {
+ File directory = targetFile.getParentFile();
+ FileUtils.forceMkdir(directory);
+ InputStream contents = getClass().getResourceAsStream(name);
+ FileOutputStream fos = new FileOutputStream(targetFile);
+ IOUtils.copy(contents, fos);
+ IOUtils.closeQuietly(fos);
+ IOUtils.closeQuietly(contents);
+ }
+
+ protected void prepareProject() throws IOException, URISyntaxException {
+ // how to get build folder, pass from gradle somehow ?
+
+ if (testFolder.exists()) {
+ FileUtils.forceDelete(testFolder);
+ }
+ FileUtils.forceMkdir(testFolder);
+ copyResourceTo("/AndroidManifest.xml", new File(testFolder, "app/src/main/AndroidManifest.xml"));
+ copyResourceTo("/project_build.gradle", new File(testFolder, "build.gradle"));
+ copyResourceTo("/app_build.gradle", new File(testFolder, "app/build.gradle"));
+ copyResourceTo("/settings.gradle", new File(testFolder, "settings.gradle"));
+ FileUtils.copyFile(new File("../local.properties"), new File(testFolder, "local.properties"));
+ FileUtils.copyFile(new File("../gradlew"), new File(testFolder, "gradlew"));
+ FileUtils.copyDirectory(new File("../gradle"), new File(testFolder, "gradle"));
+ }
+
+ protected CompilationResult runGradle(String params) throws IOException, InterruptedException {
+ setExecutable();
+ File pathToExecutable = new File(testFolder, "gradlew");
+ ProcessBuilder builder = new ProcessBuilder(pathToExecutable.getAbsolutePath(), params);
+ builder.environment().putAll(System.getenv());
+ builder.directory(testFolder); // this is where you set the root folder for the executable to run with
+ //builder.redirectErrorStream(true); // merges error and input streams
+ Process process = builder.start();
+ String output = IOUtils.toString(process.getInputStream());
+ String error = IOUtils.toString(process.getErrorStream());
+ int result = process.waitFor();
+ return new CompilationResult(result, output, error);
+ }
+
+ private void setExecutable() throws IOException {
+ Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
+ //add owners permission
+ perms.add(PosixFilePermission.OWNER_READ);
+ perms.add(PosixFilePermission.OWNER_WRITE);
+ perms.add(PosixFilePermission.OWNER_EXECUTE);
+ //add group permissions
+ perms.add(PosixFilePermission.GROUP_READ);
+ //add others permissions
+ perms.add(PosixFilePermission.OTHERS_READ);
+ Files.setPosixFilePermissions(Paths.get(new File(testFolder, "gradlew").getAbsolutePath()), perms);
+ }
+
+
+}
diff --git a/compilationTests/src/test/java/android/databinding/compilationTest/CompilationResult.java b/compilationTests/src/test/java/android/databinding/compilationTest/CompilationResult.java
new file mode 100644
index 00000000..496550ba
--- /dev/null
+++ b/compilationTests/src/test/java/android/databinding/compilationTest/CompilationResult.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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 android.databinding.compilationTest;
+
+public class CompilationResult {
+ public final int resultCode;
+ public final String output;
+ public final String error;
+
+ public CompilationResult(int resultCode, String output, String error) {
+ this.resultCode = resultCode;
+ this.output = output;
+ this.error = error;
+ }
+
+ public boolean resultContainsText(String text) {
+ return resultCode == 0 && output.indexOf(text) > 0;
+ }
+
+ public boolean errorContainsText(String text) {
+ return resultCode != 0 && error.indexOf(text) > 0;
+ }
+}
diff --git a/compilationTests/src/test/java/android/databinding/compilationTest/SimpleCompilationTest.java b/compilationTests/src/test/java/android/databinding/compilationTest/SimpleCompilationTest.java
new file mode 100644
index 00000000..2c2279f8
--- /dev/null
+++ b/compilationTests/src/test/java/android/databinding/compilationTest/SimpleCompilationTest.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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 android.databinding.compilationTest;
+
+
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.net.URISyntaxException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+public class SimpleCompilationTest extends BaseCompilationTest {
+
+ @Test
+ public void listTasks() throws IOException, URISyntaxException, InterruptedException {
+ prepareProject();
+ CompilationResult result = runGradle("tasks");
+ assertEquals(0, result.resultCode);
+ assertTrue("there should not be any errors", StringUtils.isEmpty(result.error));
+ assertTrue("Test sanity, empty project tasks",
+ result.resultContainsText("All tasks runnable from root project"));
+ }
+
+ @Test
+ public void testEmptyCompilation() throws IOException, URISyntaxException, InterruptedException {
+ prepareProject();
+ CompilationResult result = runGradle("assembleDebug");
+ assertEquals(0, result.resultCode);
+ assertTrue("there should not be any errors " + result.error, StringUtils.isEmpty(result.error));
+ assertTrue("Test sanity, should compile fine",
+ result.resultContainsText("BUILD SUCCESSFUL"));
+ }
+
+ @Test
+ public void testUndefinedVariable() throws IOException, URISyntaxException,
+ InterruptedException {
+ prepareProject();
+ copyResourceTo("/layout/undefined_variable_binding.xml", "/app/src/main/res/layout/broken.xml");
+ CompilationResult result = runGradle("assembleDebug");
+ assertNotEquals(0, result.resultCode);
+ assertTrue("Undefined variable",
+ result.errorContainsText("Identifiers must have user defined types from the XML file. myVariable is missing it"));
+ }
+}
diff --git a/compilationTests/src/test/resources/AndroidManifest.xml b/compilationTests/src/test/resources/AndroidManifest.xml
new file mode 100644
index 00000000..12d582ee
--- /dev/null
+++ b/compilationTests/src/test/resources/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<!--
+ Copyright (C) 2015 The Android Open Source Project
+
+ 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.
+ -->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.databinding.compilationTest.test">
+
+</manifest>
diff --git a/compilationTests/src/test/resources/app_build.gradle b/compilationTests/src/test/resources/app_build.gradle
new file mode 100644
index 00000000..e86f500b
--- /dev/null
+++ b/compilationTests/src/test/resources/app_build.gradle
@@ -0,0 +1,33 @@
+apply plugin: 'com.android.application'
+apply plugin: 'com.android.databinding'
+
+android {
+ compileSdkVersion 21
+ buildToolsVersion "22"
+
+ defaultConfig {
+ minSdkVersion 7
+ targetSdkVersion 21
+ versionCode 1
+ versionName "1.0"
+ }
+ buildTypes {
+ release {
+ minifyEnabled false
+ proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+ }
+ }
+
+ packagingOptions {
+ exclude 'META-INF/services/javax.annotation.processing.Processor'
+ }
+ compileOptions {
+ sourceCompatibility JavaVersion.VERSION_1_7
+ targetCompatibility JavaVersion.VERSION_1_7
+ }
+}
+
+dependencies {
+ compile fileTree(dir: 'libs', include: ['*.jar'])
+ compile "com.android.support:support-v4:+"
+}
diff --git a/compilationTests/src/test/resources/layout/undefined_variable_binding.xml b/compilationTests/src/test/resources/layout/undefined_variable_binding.xml
new file mode 100644
index 00000000..ab3431e5
--- /dev/null
+++ b/compilationTests/src/test/resources/layout/undefined_variable_binding.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2015 The Android Open Source Project
+ ~ 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.
+ -->
+
+<layout xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:bind="http://schemas.android.com/apk/res-auto">
+ <data>
+ </data>
+ <LinearLayout
+ android:orientation="vertical"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent">
+ <!-- undefined variable -->
+ <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:id="@+id/outerTextView"
+ android:text="@{myVariable.stringValue}"/>
+ </LinearLayout>
+</layout> \ No newline at end of file
diff --git a/compilationTests/src/test/resources/project_build.gradle b/compilationTests/src/test/resources/project_build.gradle
new file mode 100644
index 00000000..b8597551
--- /dev/null
+++ b/compilationTests/src/test/resources/project_build.gradle
@@ -0,0 +1,29 @@
+buildscript {
+ def Properties dataBindingProperties = new Properties()
+ dataBindingProperties.load(new FileInputStream("${projectDir}/../../../databinding.properties"))
+ dataBindingProperties.mavenRepoDir = "${projectDir}/../../../${dataBindingProperties.mavenRepoName}"
+ ext.config = dataBindingProperties
+ println "loaded config"
+
+ repositories {
+ jcenter()
+ maven {
+ url config.mavenRepoDir
+ }
+ }
+ dependencies {
+ classpath "com.android.tools.build:gradle:${config.androidPluginVersion}"
+ classpath "com.android.databinding:dataBinder:${config.version}"
+ // NOTE: Do not place your application dependencies here; they belong
+ // in the individual module build.gradle files
+ }
+}
+
+allprojects {
+ repositories {
+ jcenter()
+ maven {
+ url config.mavenRepoDir
+ }
+ }
+}
diff --git a/compilationTests/src/test/resources/settings.gradle b/compilationTests/src/test/resources/settings.gradle
new file mode 100644
index 00000000..e7b4def4
--- /dev/null
+++ b/compilationTests/src/test/resources/settings.gradle
@@ -0,0 +1 @@
+include ':app'