blob: 014edc0fa44e725d10152b2bf31ff7a3f3be549b [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
17import java.lang.reflect.Method;
Ian Rogers8e1f4f82014-11-05 11:07:30 -080018import java.lang.reflect.Modifier;
Andreas Gampe8fda9f22014-10-03 16:15:37 -070019import java.util.LinkedList;
20import java.util.List;
21
22/**
23 * Smali excercise.
24 */
25public class Main {
26
27 private static class TestCase {
28 public TestCase(String testName, String testClass, String testMethodName, Object[] values,
29 Throwable expectedException, Object expectedReturn) {
30 this.testName = testName;
31 this.testClass = testClass;
32 this.testMethodName = testMethodName;
33 this.values = values;
34 this.expectedException = expectedException;
35 this.expectedReturn = expectedReturn;
36 }
37
38 String testName;
39 String testClass;
40 String testMethodName;
41 Object[] values;
42 Throwable expectedException;
43 Object expectedReturn;
44 }
45
46 private List<TestCase> testCases;
47
48 public Main() {
49 // Create the test cases.
50 testCases = new LinkedList<TestCase>();
51
52 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Ian Rogers8e1f4f82014-11-05 11:07:30 -080053 testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(), null));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010054 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
55 new Object[]{100}, null, 100));
nikolay serdjukd24c9342014-11-10 16:53:27 +070056 testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070057 }
58
59 public void runTests() {
60 for (TestCase tc : testCases) {
61 System.out.println(tc.testName);
62 try {
63 runTest(tc);
64 } catch (Exception exc) {
65 exc.printStackTrace(System.out);
66 }
67 }
68 }
69
70 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -070071 Exception errorReturn = null;
72 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080073 Class<?> c = Class.forName(tc.testClass);
74
75 Method[] methods = c.getDeclaredMethods();
76
77 // For simplicity we assume that test methods are not overloaded. So searching by name
78 // will give us the method we need to run.
79 Method method = null;
80 for (Method m : methods) {
81 if (m.getName().equals(tc.testMethodName)) {
82 method = m;
83 break;
84 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -070085 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -080086
87 if (method == null) {
88 errorReturn = new IllegalArgumentException("Could not find test method " +
89 tc.testMethodName + " in class " +
90 tc.testClass + " for test " +
91 tc.testName);
92 } else {
93 Object retValue;
94 if (Modifier.isStatic(method.getModifiers())) {
95 retValue = method.invoke(null, tc.values);
96 } else {
97 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
98 }
99 if (tc.expectedException != null) {
100 errorReturn = new IllegalStateException("Expected an exception in test " +
101 tc.testName);
102 }
103 if (tc.expectedReturn == null && retValue != null) {
104 errorReturn = new IllegalStateException("Expected a null result in test " +
105 tc.testName);
106 } else if (tc.expectedReturn != null &&
107 (retValue == null || !tc.expectedReturn.equals(retValue))) {
108 errorReturn = new IllegalStateException("Expected return " +
109 tc.expectedReturn +
110 ", but got " + retValue);
111 } else {
112 // Expected result, do nothing.
113 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700114 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800115 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700116 if (tc.expectedException == null) {
117 errorReturn = new IllegalStateException("Did not expect exception", exc);
118 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
119 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800120 tc.expectedException.getClass().getName() +
121 ", but got " + exc.getClass(), exc);
122 } else {
123 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700124 }
125 } finally {
126 if (errorReturn != null) {
127 throw errorReturn;
128 }
129 }
130 }
131
132 public static void main(String[] args) throws Exception {
133 Main main = new Main();
134
135 main.runTests();
136
137 System.out.println("Done!");
138 }
139}