blob: 8d318c354b693dd69499ff5b6254d2679156c44d [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>();
52
53 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Ian Rogers8e1f4f82014-11-05 11:07:30 -080054 testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(), null));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010055 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
56 new Object[]{100}, null, 100));
nikolay serdjukd24c9342014-11-10 16:53:27 +070057 testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L));
Vladimir Marko7a7c1db2014-11-17 15:13:34 +000058 testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7));
Vladimir Marko920506d2014-11-18 14:47:31 +000059 testCases.add(new TestCase("b/18380491", "B18380491ConcreteClass", "foo",
60 new Object[]{42}, null, 42));
61 testCases.add(new TestCase("invoke-super abstract", "B18380491ConcreteClass", "foo",
62 new Object[]{0}, new AbstractMethodError(), null));
Pavel Vyssotski4ee71b22014-11-18 11:51:24 +060063 testCases.add(new TestCase("BadCaseInOpRegRegReg", "BadCaseInOpRegRegReg", "getInt", null, null, 2));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070064 }
65
66 public void runTests() {
67 for (TestCase tc : testCases) {
68 System.out.println(tc.testName);
69 try {
70 runTest(tc);
71 } catch (Exception exc) {
72 exc.printStackTrace(System.out);
73 }
74 }
75 }
76
77 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -070078 Exception errorReturn = null;
79 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080080 Class<?> c = Class.forName(tc.testClass);
81
82 Method[] methods = c.getDeclaredMethods();
83
84 // For simplicity we assume that test methods are not overloaded. So searching by name
85 // will give us the method we need to run.
86 Method method = null;
87 for (Method m : methods) {
88 if (m.getName().equals(tc.testMethodName)) {
89 method = m;
90 break;
91 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -070092 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -080093
94 if (method == null) {
95 errorReturn = new IllegalArgumentException("Could not find test method " +
96 tc.testMethodName + " in class " +
97 tc.testClass + " for test " +
98 tc.testName);
99 } else {
100 Object retValue;
101 if (Modifier.isStatic(method.getModifiers())) {
102 retValue = method.invoke(null, tc.values);
103 } else {
104 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
105 }
106 if (tc.expectedException != null) {
107 errorReturn = new IllegalStateException("Expected an exception in test " +
108 tc.testName);
109 }
110 if (tc.expectedReturn == null && retValue != null) {
111 errorReturn = new IllegalStateException("Expected a null result in test " +
112 tc.testName);
113 } else if (tc.expectedReturn != null &&
114 (retValue == null || !tc.expectedReturn.equals(retValue))) {
115 errorReturn = new IllegalStateException("Expected return " +
116 tc.expectedReturn +
117 ", but got " + retValue);
118 } else {
119 // Expected result, do nothing.
120 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700121 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800122 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700123 if (tc.expectedException == null) {
124 errorReturn = new IllegalStateException("Did not expect exception", exc);
Vladimir Marko920506d2014-11-18 14:47:31 +0000125 } else if (exc instanceof InvocationTargetException && exc.getCause() != null &&
126 exc.getCause().getClass().equals(tc.expectedException.getClass())) {
127 // Expected exception is wrapped in InvocationTargetException.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700128 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
129 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800130 tc.expectedException.getClass().getName() +
131 ", but got " + exc.getClass(), exc);
132 } else {
133 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700134 }
135 } finally {
136 if (errorReturn != null) {
137 throw errorReturn;
138 }
139 }
140 }
141
142 public static void main(String[] args) throws Exception {
143 Main main = new Main();
144
145 main.runTests();
146
147 System.out.println("Done!");
148 }
149}