blob: ea25da6fd0435dd248284e8984374e5b8d60f227 [file] [log] [blame]
Andreas Gampe8fda9f22014-10-03 16:15:37 -07001/*
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
Vladimir Marko920506d2014-11-18 14:47:31 +000017import java.lang.reflect.InvocationTargetException;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070018import java.lang.reflect.Method;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080019import java.lang.reflect.Modifier;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070020import java.util.LinkedList;
21import java.util.List;
22
23/**
24 * Smali excercise.
25 */
26public class Main {
27
28 private static class TestCase {
29 public TestCase(String testName, String testClass, String testMethodName, Object[] values,
30 Throwable expectedException, Object expectedReturn) {
31 this.testName = testName;
32 this.testClass = testClass;
33 this.testMethodName = testMethodName;
34 this.values = values;
35 this.expectedException = expectedException;
36 this.expectedReturn = expectedReturn;
37 }
38
39 String testName;
40 String testClass;
41 String testMethodName;
42 Object[] values;
43 Throwable expectedException;
44 Object expectedReturn;
45 }
46
47 private List<TestCase> testCases;
48
49 public Main() {
50 // Create the test cases.
51 testCases = new LinkedList<TestCase>();
buzbee6489d222014-11-25 10:52:19 -080052 testCases.add(new TestCase("PackedSwitch", "PackedSwitch", "packedSwitch",
53 new Object[]{123}, null, 123));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070054
55 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Ian Rogers8e1f4f82014-11-05 11:07:30 -080056 testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(), null));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010057 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
58 new Object[]{100}, null, 100));
nikolay serdjukd24c9342014-11-10 16:53:27 +070059 testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L));
Vladimir Marko7a7c1db2014-11-17 15:13:34 +000060 testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7));
Vladimir Marko920506d2014-11-18 14:47:31 +000061 testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo",
62 new Object[]{42}, null, 42));
63 testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo",
64 new Object[]{0}, new AbstractMethodError(), null));
Pavel Vyssotski4ee71b22014-11-18 11:51:24 +060065 testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null, null, 2));
Nicolas Geoffray32b2a522014-11-27 14:54:18 +000066 testCases.add(new TestCase("CmpLong", "CmpLong", "run", null, null, 0));
Serguei Katkov717a3e42014-11-13 17:19:42 +060067 testCases.add(new TestCase("FloatIntConstPassing", "FloatIntConstPassing", "run", null, null, 2));
Vladimir Marko341e4252014-12-19 10:29:51 +000068 testCases.add(new TestCase("b/18718277", "B18718277", "getInt", null, null, 0));
Stephen Kyle7e541c92014-12-17 17:10:02 +000069 testCases.add(new TestCase("b/18800943 (1)", "B18800943_1", "n_a", null, new VerifyError(), 0));
70 testCases.add(new TestCase("b/18800943 (2)", "B18800943_2", "n_a", null, new VerifyError(), 0));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070071 }
72
73 public void runTests() {
74 for (TestCase tc : testCases) {
75 System.out.println(tc.testName);
76 try {
77 runTest(tc);
78 } catch (Exception exc) {
79 exc.printStackTrace(System.out);
80 }
81 }
82 }
83
84 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -070085 Exception errorReturn = null;
86 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080087 Class<?> c = Class.forName(tc.testClass);
88
89 Method[] methods = c.getDeclaredMethods();
90
91 // For simplicity we assume that test methods are not overloaded. So searching by name
92 // will give us the method we need to run.
93 Method method = null;
94 for (Method m : methods) {
95 if (m.getName().equals(tc.testMethodName)) {
96 method = m;
97 break;
98 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -070099 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800100
101 if (method == null) {
102 errorReturn = new IllegalArgumentException("Could not find test method " +
103 tc.testMethodName + " in class " +
104 tc.testClass + " for test " +
105 tc.testName);
106 } else {
107 Object retValue;
108 if (Modifier.isStatic(method.getModifiers())) {
109 retValue = method.invoke(null, tc.values);
110 } else {
111 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
112 }
113 if (tc.expectedException != null) {
114 errorReturn = new IllegalStateException("Expected an exception in test " +
115 tc.testName);
116 }
117 if (tc.expectedReturn == null && retValue != null) {
118 errorReturn = new IllegalStateException("Expected a null result in test " +
119 tc.testName);
120 } else if (tc.expectedReturn != null &&
121 (retValue == null || !tc.expectedReturn.equals(retValue))) {
122 errorReturn = new IllegalStateException("Expected return " +
123 tc.expectedReturn +
124 ", but got " + retValue);
125 } else {
126 // Expected result, do nothing.
127 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700128 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800129 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700130 if (tc.expectedException == null) {
131 errorReturn = new IllegalStateException("Did not expect exception", exc);
Vladimir Marko920506d2014-11-18 14:47:31 +0000132 } else if (exc instanceof InvocationTargetException && exc.getCause() != null &&
133 exc.getCause().getClass().equals(tc.expectedException.getClass())) {
134 // Expected exception is wrapped in InvocationTargetException.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700135 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
136 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800137 tc.expectedException.getClass().getName() +
138 ", but got " + exc.getClass(), exc);
139 } else {
140 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700141 }
142 } finally {
143 if (errorReturn != null) {
144 throw errorReturn;
145 }
146 }
147 }
148
149 public static void main(String[] args) throws Exception {
150 Main main = new Main();
151
152 main.runTests();
153
154 System.out.println("Done!");
155 }
156}