1 2 package dot.junit; 3 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.util.Scanner; 9 10 public class DeviceUtil { 11 12 private static boolean DEBUG = System.getProperty("cts.vm-tests.debug") != null; 13 14 /** 15 * Executes the command and its arguments in a native process. 16 * 17 * @param commandAndArgs a string array to be passed containing the 18 * executable and its arguments 19 * @param okIndicator if not null, this String must occur in the stdout of 20 * the executable (since only checking for the return code is not 21 * sufficient e.g. for adb shell cmd) 22 * @throws Exception thrown by the underlying command in case of an error. 23 */ 24 public static void digestCommand(String[] commandAndArgs, String okIndicator) { 25 RuntimeException re = null; 26 try { 27 String c = ""; 28 for (int i = 0; i < commandAndArgs.length; i++) { 29 c += commandAndArgs[i] + " "; 30 } 31 if (DEBUG) System.out.print("com: " + c); 32 StringBuilder sb = new StringBuilder(); 33 ProcessBuilder pb = new ProcessBuilder(commandAndArgs).redirectErrorStream(true); 34 Process p = pb.start(); 35 36 InputStream is = p.getInputStream(); 37 Scanner scanner = new Scanner(is); 38 int retCode = p.waitFor(); 39 while (scanner.hasNextLine()) { 40 sb.append(scanner.nextLine()); 41 } 42 scanner.close(); 43 if (retCode != 0 || (okIndicator != null && !sb.toString().contains(okIndicator))) { 44 String msg = sb.toString() + "\nreturn code: " + retCode; 45 re = new RuntimeException(msg); 46 if (DEBUG) System.out.println("-> error! msg:"+msg); 47 } else { 48 if (DEBUG) System.out.println(" -> " + retCode); 49 } 50 } catch (Exception e) { 51 throw new RuntimeException("Exception occurred: " + e.getClass().getName() + ", msg:" 52 + e.getMessage()); 53 } finally { 54 if (re != null) { 55 throw re; 56 } 57 } 58 } 59 60 public static String createFilePath(String testName) throws IOException { 61 // e.g. /dot/junit/opcodes/add_double/d/T_add_double_1.jar 62 FileOutputStream fos = null; 63 InputStream is = null; 64 File f; 65 try { 66 is = DeviceUtil.class.getResourceAsStream("/tests/" + testName); 67 if (is == null) { 68 throw new RuntimeException("could not find resource /tests" + testName 69 + " in classpath"); 70 } 71 f = File.createTempFile("cts-adbpush-", ".jar"); 72 int len = 4096; 73 byte[] bytes = new byte[len]; 74 fos = new FileOutputStream(f); 75 int b; 76 while ((b = is.read(bytes)) > 0) { 77 fos.write(bytes, 0, b); 78 } 79 } finally { 80 if (is != null) { 81 is.close(); 82 } 83 if (fos != null) { 84 fos.close(); 85 } 86 } 87 return f.getAbsolutePath(); 88 } 89 90 public static void adbPush(String source, String target) 91 throws IOException { 92 DeviceUtil.digestCommand(new String[] {"adb", "push", 93 DeviceUtil.createFilePath(source), target}, null); 94 } 95 96 public static void adbExec(String classpath, String mainclass) { 97 DeviceUtil.digestCommand(new String[] {"adb", "shell", 98 "dalvikvm", "-Xint:portable", "-Xmx512M", 99 "-Xss32K", "-Djava.io.tmpdir=/data/local/tmp", 100 "-classpath", classpath, mainclass, 101 "&&", "echo", "mk_dalvikvmok" }, "mk_dalvikvmok"); 102 } 103 104 105 } 106