1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package android.jvmti.cts; 15 16 import java.io.BufferedReader; 17 import java.io.ByteArrayInputStream; 18 import java.io.ByteArrayOutputStream; 19 import java.io.InputStream; 20 import java.io.InputStreamReader; 21 import java.io.PrintStream; 22 import java.lang.reflect.Method; 23 24 import android.content.pm.PackageManager; 25 26 import org.junit.After; 27 import org.junit.Before; 28 import org.junit.Test; 29 30 /** 31 * Check redefineClasses-related functionality. 32 */ 33 public class JvmtiRunTestBasedTest extends JvmtiTestBase { 34 35 private PrintStream oldOut, oldErr; 36 private ByteArrayOutputStream bufferedOut, bufferedErr; 37 38 @Before setUp()39 public void setUp() throws Exception { 40 oldOut = System.out; 41 oldErr = System.err; 42 43 System.setOut(new PrintStream(bufferedOut = new ByteArrayOutputStream(), true)); 44 System.setErr(new PrintStream(bufferedErr = new ByteArrayOutputStream(), true)); 45 } 46 47 @After tearDown()48 public void tearDown() { 49 System.setOut(oldOut); 50 System.setErr(oldErr); 51 } 52 getTestNumber()53 protected int getTestNumber() throws Exception { 54 return mActivity.getPackageManager().getApplicationInfo(mActivity.getPackageName(), 55 PackageManager.GET_META_DATA).metaData.getInt("android.jvmti.cts.run_test_nr"); 56 } 57 58 @Test testRunTest()59 public void testRunTest() throws Exception { 60 final int nr = getTestNumber(); 61 62 // Load the test class. 63 Class<?> testClass = Class.forName("art.Test" + nr); 64 Method runMethod = testClass.getDeclaredMethod("run"); 65 runMethod.invoke(null); 66 67 // Load the expected txt file. 68 InputStream expectedStream = getClass().getClassLoader() 69 .getResourceAsStream("results." + nr + ".expected.txt"); 70 compare(expectedStream, bufferedOut); 71 72 if (bufferedErr.size() > 0) { 73 throw new IllegalStateException( 74 "Did not expect System.err output: " + bufferedErr.toString()); 75 } 76 } 77 78 // Very primitive diff. Doesn't do any smart things... compare(InputStream expectedStream, ByteArrayOutputStream resultStream)79 private void compare(InputStream expectedStream, ByteArrayOutputStream resultStream) 80 throws Exception { 81 // This isn't really optimal in any way. 82 BufferedReader expectedReader = new BufferedReader(new InputStreamReader(expectedStream)); 83 BufferedReader resultReader = new BufferedReader( 84 new InputStreamReader(new ByteArrayInputStream(resultStream.toByteArray()))); 85 StringBuilder resultBuilder = new StringBuilder(); 86 boolean failed = false; 87 for (;;) { 88 String expString = expectedReader.readLine(); 89 String resString = resultReader.readLine(); 90 91 if (expString == null && resString == null) { 92 // Done. 93 break; 94 } 95 96 if (expString != null && resString != null && expString.equals(resString)) { 97 resultBuilder.append(" "); 98 resultBuilder.append(expString); 99 resultBuilder.append('\n'); 100 continue; 101 } 102 103 failed = true; 104 if (expString != null) { 105 resultBuilder.append("- "); 106 resultBuilder.append(expString); 107 resultBuilder.append('\n'); 108 } 109 if (resString != null) { 110 resultBuilder.append("+ "); 111 resultBuilder.append(resString); 112 resultBuilder.append('\n'); 113 } 114 } 115 if (failed) { 116 throw new IllegalStateException(resultBuilder.toString()); 117 } 118 } 119 } 120