1 /*
2  * Copyright (C) 2014 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 
17 package com.android.compatibility.common.scanner;
18 
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.InputStreamReader;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 
25 import junit.framework.TestCase;
26 
27 public class JavaScannerTest extends TestCase {
28 
29     private static final String JAR = "out/host/linux-x86/framework/compatibility-java-scanner_v2.jar";
30     private static final String VALID_RESULT =
31         "suite:com.android.test" +
32         "case:ValidTest" +
33         "test:testA";
34 
35     private static final String VALID_FILENAME = "ValidTest";
36     private static final String VALID =
37         "package com.android.test;" +
38         "import junit.framework.TestCase;" +
39         "public class ValidTest extends TestCase {" +
40         "  public void testA() throws Exception {" +
41         "    helper();" +
42         "  }" +
43         "  public void helper() {" +
44         "    fail();" +
45         "  }" +
46         "}";
47 
48     // TestCases must have TestCase in their hierarchy
49     private static final String INVALID_A_FILENAME = "NotTestCase";
50     private static final String INVALID_A =
51         "package com.android.test;" +
52         "public class NotTestCase {" +
53         "  public void testA() throws Exception {" +
54         "    helper();" +
55         "  }" +
56         "  public void helper() {" +
57         "    fail();" +
58         "  }" +
59         "}";
60 
61     // TestCases cant be abstract classes
62     private static final String INVALID_B_FILENAME = "AbstractClass";
63     private static final String INVALID_B =
64         "package com.android.test;" +
65         "import junit.framework.TestCase;" +
66         "public abstract class AbstractClass extends TestCase {" +
67         "  public void testA() throws Exception {" +
68         "    helper();" +
69         "  }" +
70         "  public void helper() {" +
71         "    fail();" +
72         "  }" +
73         "}";
74 
testValidFile()75     public void testValidFile() throws Exception {
76         String result = runScanner(VALID_FILENAME, VALID);
77         assertEquals(VALID_RESULT, result);
78     }
79 
testInvalidFileA()80     public void testInvalidFileA() throws Exception {
81         assertEquals("", runScanner(INVALID_A_FILENAME, INVALID_A));
82     }
83 
testInvalidFileB()84     public void testInvalidFileB() throws Exception {
85         assertEquals("", runScanner(INVALID_B_FILENAME, INVALID_B));
86     }
87 
runScanner(String filename, String content)88     private static String runScanner(String filename, String content) throws Exception {
89         final File parent0 = new File(System.getProperty("java.io.tmpdir"));
90         final File parent1 = new File(parent0, "tmp" + System.currentTimeMillis());
91         final File parent2 = new File(parent1, "com");
92         final File parent3 = new File(parent2, "android");
93         final File parent4 = new File(parent3, "test");
94         File f = null;
95         try {
96             parent4.mkdirs();
97             f = new File(parent4, filename + ".java");
98             final PrintWriter out = new PrintWriter(f);
99             out.print(content);
100             out.flush();
101             out.close();
102             ArrayList<String> args = new ArrayList<String>();
103             args.add("java");
104             args.add("-jar");
105             args.add(JAR);
106             args.add("-s");
107             args.add(parent1.toString());
108             args.add("-d");
109             args.add(JAR);
110 
111             final Process p = new ProcessBuilder(args).start();
112             final StringBuilder output = new StringBuilder();
113             final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
114             String line = null;
115             while ((line = in.readLine()) != null) {
116                 output.append(line);
117             }
118             int ret = p.waitFor();
119             if (ret == 0) {
120                 return output.toString();
121             }
122         } finally {
123             if (f != null) {
124                 f.delete();
125             }
126             parent4.delete();
127             parent3.delete();
128             parent2.delete();
129             parent1.delete();
130         }
131         return null;
132     }
133 }
134