BaseCompilationTest.java revision 9064a1dd60eb8c2f9d2ed705b36bb5f0b1629771
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.compilationTest;
18
19import org.apache.commons.io.FileUtils;
20import org.apache.commons.io.IOUtils;
21import org.apache.commons.io.filefilter.IOFileFilter;
22import org.apache.commons.io.filefilter.TrueFileFilter;
23import org.apache.commons.lang3.StringUtils;
24import org.junit.Test;
25
26import java.io.File;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.net.URI;
31import java.net.URISyntaxException;
32import java.net.URL;
33import java.nio.file.Files;
34import java.nio.file.Paths;
35import java.nio.file.attribute.PosixFilePermission;
36import java.util.HashSet;
37import java.util.Map;
38import java.util.Set;
39
40import static org.junit.Assert.assertEquals;
41import static org.junit.Assert.assertNotNull;
42import static org.junit.Assert.assertNull;
43import static org.junit.Assert.assertTrue;
44
45
46public class BaseCompilationTest {
47    File testFolder = new File("./build/build-test");
48
49    protected void copyResourceTo(String name, String path) throws IOException {
50        copyResourceTo(name, new File(testFolder, path));
51    }
52
53    protected void copyResourceDirectory(String name, String targetPath)
54            throws URISyntaxException, IOException {
55        URL dir = getClass().getResource(name);
56        assertNotNull(dir);
57        assertEquals("file", dir.getProtocol());
58        File folder = new File(dir.toURI());
59        assertTrue(folder.isDirectory());
60        File target = new File(testFolder, targetPath);
61        int len = folder.getAbsolutePath().length() + 1;
62        for (File item : FileUtils.listFiles(folder, null, true)) {
63            if (item.getAbsolutePath().equals(folder.getAbsolutePath())) {
64                continue;
65            }
66            String resourcePath = item.getAbsolutePath().substring(len);
67
68            copyResourceTo(name + "/" + resourcePath, new File(target, resourcePath));
69        }
70    }
71
72    protected void copyResourceTo(String name, File targetFile) throws IOException {
73        File directory = targetFile.getParentFile();
74        FileUtils.forceMkdir(directory);
75        InputStream contents = getClass().getResourceAsStream(name);
76        FileOutputStream fos = new FileOutputStream(targetFile);
77        IOUtils.copy(contents, fos);
78        IOUtils.closeQuietly(fos);
79        IOUtils.closeQuietly(contents);
80    }
81
82    protected void prepareProject() throws IOException, URISyntaxException {
83        // how to get build folder, pass from gradle somehow ?
84
85        if (testFolder.exists()) {
86            FileUtils.forceDelete(testFolder);
87        }
88        FileUtils.forceMkdir(testFolder);
89        copyResourceTo("/AndroidManifest.xml", new File(testFolder, "app/src/main/AndroidManifest.xml"));
90        copyResourceTo("/project_build.gradle", new File(testFolder, "build.gradle"));
91        copyResourceTo("/app_build.gradle", new File(testFolder, "app/build.gradle"));
92        copyResourceTo("/settings.gradle", new File(testFolder, "settings.gradle"));
93        FileUtils.copyFile(new File("../local.properties"), new File(testFolder, "local.properties"));
94        FileUtils.copyFile(new File("../gradlew"), new File(testFolder, "gradlew"));
95        FileUtils.copyDirectory(new File("../gradle"), new File(testFolder, "gradle"));
96    }
97
98    protected CompilationResult runGradle(String params) throws IOException, InterruptedException {
99        setExecutable();
100        File pathToExecutable = new File(testFolder, "gradlew");
101        ProcessBuilder builder = new ProcessBuilder(pathToExecutable.getAbsolutePath(), params);
102        builder.environment().putAll(System.getenv());
103        builder.directory(testFolder); // this is where you set the root folder for the executable to run with
104        //builder.redirectErrorStream(true); // merges error and input streams
105        Process process =  builder.start();
106        String output = IOUtils.toString(process.getInputStream());
107        String error = IOUtils.toString(process.getErrorStream());
108        int result = process.waitFor();
109        return new CompilationResult(result, output, error);
110    }
111
112    private void setExecutable() throws IOException {
113        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
114        //add owners permission
115        perms.add(PosixFilePermission.OWNER_READ);
116        perms.add(PosixFilePermission.OWNER_WRITE);
117        perms.add(PosixFilePermission.OWNER_EXECUTE);
118        //add group permissions
119        perms.add(PosixFilePermission.GROUP_READ);
120        //add others permissions
121        perms.add(PosixFilePermission.OTHERS_READ);
122        Files.setPosixFilePermissions(Paths.get(new File(testFolder, "gradlew").getAbsolutePath()), perms);
123    }
124
125
126}
127