blob: 3a0f8eaee694ef74bcff7b3f0570966ffe86e7c1 [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));
Vladimir Marko7a7c1db2014-11-17 15:13:34 +000057 testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070058 }
59
60 public void runTests() {
61 for (TestCase tc : testCases) {
62 System.out.println(tc.testName);
63 try {
64 runTest(tc);
65 } catch (Exception exc) {
66 exc.printStackTrace(System.out);
67 }
68 }
69 }
70
71 private void runTest(TestCase tc) throws Exception {
Andreas Gampe8fda9f22014-10-03 16:15:37 -070072 Exception errorReturn = null;
73 try {
Ian Rogers8e1f4f82014-11-05 11:07:30 -080074 Class<?> c = Class.forName(tc.testClass);
75
76 Method[] methods = c.getDeclaredMethods();
77
78 // For simplicity we assume that test methods are not overloaded. So searching by name
79 // will give us the method we need to run.
80 Method method = null;
81 for (Method m : methods) {
82 if (m.getName().equals(tc.testMethodName)) {
83 method = m;
84 break;
85 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -070086 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -080087
88 if (method == null) {
89 errorReturn = new IllegalArgumentException("Could not find test method " +
90 tc.testMethodName + " in class " +
91 tc.testClass + " for test " +
92 tc.testName);
93 } else {
94 Object retValue;
95 if (Modifier.isStatic(method.getModifiers())) {
96 retValue = method.invoke(null, tc.values);
97 } else {
98 retValue = method.invoke(method.getDeclaringClass().newInstance(), tc.values);
99 }
100 if (tc.expectedException != null) {
101 errorReturn = new IllegalStateException("Expected an exception in test " +
102 tc.testName);
103 }
104 if (tc.expectedReturn == null && retValue != null) {
105 errorReturn = new IllegalStateException("Expected a null result in test " +
106 tc.testName);
107 } else if (tc.expectedReturn != null &&
108 (retValue == null || !tc.expectedReturn.equals(retValue))) {
109 errorReturn = new IllegalStateException("Expected return " +
110 tc.expectedReturn +
111 ", but got " + retValue);
112 } else {
113 // Expected result, do nothing.
114 }
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700115 }
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800116 } catch (Throwable exc) {
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700117 if (tc.expectedException == null) {
118 errorReturn = new IllegalStateException("Did not expect exception", exc);
119 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
120 errorReturn = new IllegalStateException("Expected " +
Ian Rogers8e1f4f82014-11-05 11:07:30 -0800121 tc.expectedException.getClass().getName() +
122 ", but got " + exc.getClass(), exc);
123 } else {
124 // Expected exception, do nothing.
Andreas Gampe8fda9f22014-10-03 16:15:37 -0700125 }
126 } finally {
127 if (errorReturn != null) {
128 throw errorReturn;
129 }
130 }
131 }
132
133 public static void main(String[] args) throws Exception {
134 Main main = new Main();
135
136 main.runTests();
137
138 System.out.println("Done!");
139 }
140}