1 /* 2 * Copyright (C) 2011 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 util.build; 18 19 import java.io.File; 20 import java.io.IOException; 21 import java.util.Collection; 22 import java.util.Collections; 23 import java.util.Comparator; 24 import java.util.List; 25 26 /** 27 * Main class to generate data from the test suite to later run from a shell 28 * script. the project's home folder.<br> 29 * <project-home>/src must contain the java sources<br> 30 * <project-home>/src/<for-each-package>/Main_testN1.java will be generated<br> 31 * (one Main class for each test method in the Test_... class 32 */ 33 public class BuildCTSHostSources extends BuildUtilBase { 34 35 public static final String TARGET_MAIN_FILE = "mains.jar"; 36 37 // the folder for the generated junit-files for the cts host (which in turn 38 // execute the real vm tests using adb push/shell etc) 39 private static String HOSTJUNIT_SRC_OUTPUT_FOLDER = ""; 40 41 private static final String TARGET_JAR_ROOT_PATH = "/data/local/tmp/vm-tests"; 42 43 /** 44 * @param args 45 * args 0 must be the project root folder (where src, lib etc. 46 * resides) 47 * @throws IOException 48 */ main(String[] args)49 public static void main(String[] args) throws IOException { 50 BuildCTSHostSources cat = new BuildCTSHostSources(); 51 52 if (!cat.parseArgs(args)) { 53 printUsage(); 54 System.exit(-1); 55 } 56 57 long start = System.currentTimeMillis(); 58 cat.run(cat::handleTest); 59 long end = System.currentTimeMillis(); 60 61 System.out.println("elapsed seconds: " + (end - start) / 1000); 62 } 63 parseArgs(String[] args)64 private boolean parseArgs(String[] args) { 65 if (args.length == 1) { 66 HOSTJUNIT_SRC_OUTPUT_FOLDER = args[0]; 67 return true; 68 } else { 69 return false; 70 } 71 } 72 printUsage()73 private static void printUsage() { 74 System.out.println("usage: java-src-folder output-folder classpath " + 75 "generated-main-files compiled_output generated-main-files " + 76 "[restrict-to-opcode]"); 77 } 78 79 private static class HostState { 80 private String fileName; 81 private StringBuilder fileData; 82 HostState(String fileName)83 public HostState(String fileName) { 84 this.fileName = fileName; 85 fileData = new StringBuilder(); 86 } 87 append(String s)88 public void append(String s) { 89 fileData.append(s); 90 } 91 addCTSHostMethod(String pName, String method, Collection<String> dependentTestClassNames)92 private void addCTSHostMethod(String pName, String method, 93 Collection<String> dependentTestClassNames) { 94 fileData.append("public void " + method + "() throws Exception {\n"); 95 final String targetCoreJarPath = String.format("%s/dot/junit/dexcore.jar", 96 TARGET_JAR_ROOT_PATH); 97 98 String mainsJar = String.format("%s/%s", TARGET_JAR_ROOT_PATH, TARGET_MAIN_FILE); 99 100 String cp = String.format("%s:%s", targetCoreJarPath, mainsJar); 101 for (String depFqcn : dependentTestClassNames) { 102 String sourceName = depFqcn.replaceAll("\\.", "/") + ".jar"; 103 String targetName= String.format("%s/%s", TARGET_JAR_ROOT_PATH, 104 sourceName); 105 cp += ":" + targetName; 106 // dot.junit.opcodes.invoke_interface_range.ITest 107 // -> dot/junit/opcodes/invoke_interface_range/ITest.jar 108 } 109 110 //"dot.junit.opcodes.add_double_2addr.Main_testN2"; 111 String mainclass = pName + ".Main_" + method; 112 fileData.append(getShellExecJavaLine(cp, mainclass)); 113 fileData.append("\n}\n\n"); 114 } 115 end()116 public void end() { 117 fileData.append("\n}\n"); 118 } 119 getFileToWrite()120 public File getFileToWrite() { 121 return new File(fileName); 122 } getData()123 public String getData() { 124 return fileData.toString(); 125 } 126 } 127 flushHostState(HostState state)128 private void flushHostState(HostState state) { 129 state.end(); 130 131 File toWrite = state.getFileToWrite(); 132 writeToFileMkdir(toWrite, state.getData()); 133 } 134 openCTSHostFileFor(String pName, String classOnlyName)135 private HostState openCTSHostFileFor(String pName, String classOnlyName) { 136 String sourceName = classOnlyName; 137 138 String modPackage = pName; 139 { 140 // Given a class name of "Test_zzz" and a package of "xxx.yyy.zzz," strip 141 // "zzz" from the package to reduce duplication (and dashboard clutter). 142 int lastDot = modPackage.lastIndexOf('.'); 143 if (lastDot > 0) { 144 String lastPackageComponent = modPackage.substring(lastDot + 1); 145 if (classOnlyName.equals("Test_" + lastPackageComponent)) { 146 // Drop the duplication. 147 modPackage = modPackage.substring(0, lastDot); 148 } 149 } 150 } 151 152 String fileName = HOSTJUNIT_SRC_OUTPUT_FOLDER + "/" + modPackage.replaceAll("\\.", "/") 153 + "/" + sourceName + ".java"; 154 155 HostState newState = new HostState(fileName); 156 157 newState.append(getWarningMessage()); 158 newState.append("package " + modPackage + ";\n"); 159 newState.append("import java.io.IOException;\n" + 160 "import java.util.concurrent.TimeUnit;\n\n" + 161 "import com.android.tradefed.device.CollectingOutputReceiver;\n" + 162 "import com.android.tradefed.testtype.IAbi;\n" + 163 "import com.android.tradefed.testtype.IAbiReceiver;\n" + 164 "import com.android.tradefed.testtype.DeviceTestCase;\n" + 165 "import com.android.tradefed.util.AbiFormatter;\n" + 166 "\n"); 167 newState.append("public class " + sourceName + " extends DeviceTestCase implements " + 168 "IAbiReceiver {\n"); 169 170 newState.append("\n" + 171 "protected IAbi mAbi;\n" + 172 "@Override\n" + 173 "public void setAbi(IAbi abi) {\n" + 174 " mAbi = abi;\n" + 175 "}\n\n"); 176 177 return newState; 178 } 179 getShellExecJavaLine(String classpath, String mainclass)180 private static String getShellExecJavaLine(String classpath, String mainclass) { 181 String cmd = String.format("ANDROID_DATA=%s dalvikvm|#ABI#| -Xmx512M -Xss32K " + 182 "-Djava.io.tmpdir=%s -classpath %s %s", TARGET_JAR_ROOT_PATH, TARGET_JAR_ROOT_PATH, 183 classpath, mainclass); 184 StringBuilder code = new StringBuilder(); 185 code.append(" String cmd = AbiFormatter.formatCmdForAbi(\"") 186 .append(cmd) 187 .append("\", mAbi.getBitness());\n") 188 .append(" CollectingOutputReceiver receiver = new CollectingOutputReceiver();\n") 189 .append(" getDevice().executeShellCommand(cmd, receiver, 6, TimeUnit.MINUTES, 1);\n") 190 .append(" // A sucessful adb shell command returns an empty string.\n") 191 .append(" assertEquals(cmd, \"\", receiver.getOutput());"); 192 return code.toString(); 193 } 194 getWarningMessage()195 private String getWarningMessage() { 196 return "//Autogenerated code by " + this.getClass().getName() + "; do not edit.\n"; 197 } 198 handleTest(String fqcn, List<String> methods)199 private void handleTest(String fqcn, List<String> methods) { 200 int lastDotPos = fqcn.lastIndexOf('.'); 201 String pName = fqcn.substring(0, lastDotPos); 202 String classOnlyName = fqcn.substring(lastDotPos + 1); 203 204 HostState hostState = openCTSHostFileFor(pName, classOnlyName); 205 206 Collections.sort(methods, new Comparator<String>() { 207 @Override 208 public int compare(String s1, String s2) { 209 // TODO sort according: test ... N, B, E, VFE 210 return s1.compareTo(s2); 211 } 212 }); 213 for (String method : methods) { 214 // e.g. testN1 215 if (!method.startsWith("test")) { 216 throw new RuntimeException("no test method: " + method); 217 } 218 219 // generate the Main_xx java class 220 221 // a Main_testXXX.java contains: 222 // package <packagenamehere>; 223 // public class Main_testxxx { 224 // public static void main(String[] args) { 225 // new dxc.junit.opcodes.aaload.Test_aaload().testN1(); 226 // } 227 // } 228 MethodData md = parseTestMethod(pName, classOnlyName, method); 229 String methodContent = md.methodBody; 230 231 List<String> dependentTestClassNames = parseTestClassName(pName, 232 classOnlyName, methodContent); 233 234 hostState.addCTSHostMethod(pName, method, dependentTestClassNames); 235 } 236 237 flushHostState(hostState); 238 } 239 } 240