blob: c86470ce674075bec31b158534127d59bad6b02b [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;
18import java.util.LinkedList;
19import java.util.List;
20
21/**
22 * Smali excercise.
23 */
24public class Main {
25
26 private static class TestCase {
27 public TestCase(String testName, String testClass, String testMethodName, Object[] values,
28 Throwable expectedException, Object expectedReturn) {
29 this.testName = testName;
30 this.testClass = testClass;
31 this.testMethodName = testMethodName;
32 this.values = values;
33 this.expectedException = expectedException;
34 this.expectedReturn = expectedReturn;
35 }
36
37 String testName;
38 String testClass;
39 String testMethodName;
40 Object[] values;
41 Throwable expectedException;
42 Object expectedReturn;
43 }
44
45 private List<TestCase> testCases;
46
47 public Main() {
48 // Create the test cases.
49 testCases = new LinkedList<TestCase>();
50
51 testCases.add(new TestCase("b/17790197", "B17790197", "getInt", null, null, 100));
Stephen Kyle8fe0e352014-10-16 15:02:42 +010052 testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt",
53 new Object[]{100}, null, 100));
Andreas Gampe8fda9f22014-10-03 16:15:37 -070054 }
55
56 public void runTests() {
57 for (TestCase tc : testCases) {
58 System.out.println(tc.testName);
59 try {
60 runTest(tc);
61 } catch (Exception exc) {
62 exc.printStackTrace(System.out);
63 }
64 }
65 }
66
67 private void runTest(TestCase tc) throws Exception {
68 Class<?> c = Class.forName(tc.testClass);
69
70 Method[] methods = c.getDeclaredMethods();
71
72 // For simplicity we assume that test methods are not overloaded. So searching by name
73 // will give us the method we need to run.
74 Method method = null;
75 for (Method m : methods) {
76 if (m.getName().equals(tc.testMethodName)) {
77 method = m;
78 break;
79 }
80 }
81
82 if (method == null) {
83 throw new IllegalArgumentException("Could not find test method " + tc.testMethodName +
84 " in class " + tc.testClass + " for test " + tc.testName);
85 }
86
87 Exception errorReturn = null;
88 try {
89 Object retValue = method.invoke(null, tc.values);
90 if (tc.expectedException != null) {
91 errorReturn = new IllegalStateException("Expected an exception in test " +
92 tc.testName);
93 }
94 if (tc.expectedReturn == null && retValue != null) {
95 errorReturn = new IllegalStateException("Expected a null result in test " +
96 tc.testName);
97 } else if (tc.expectedReturn != null &&
98 (retValue == null || !tc.expectedReturn.equals(retValue))) {
99 errorReturn = new IllegalStateException("Expected return " + tc.expectedReturn +
100 ", but got " + retValue);
101 }
102 } catch (Exception exc) {
103 if (tc.expectedException == null) {
104 errorReturn = new IllegalStateException("Did not expect exception", exc);
105 } else if (!tc.expectedException.getClass().equals(exc.getClass())) {
106 errorReturn = new IllegalStateException("Expected " +
107 tc.expectedException.getClass().getName() +
108 ", but got " + exc.getClass(), exc);
109 }
110 } finally {
111 if (errorReturn != null) {
112 throw errorReturn;
113 }
114 }
115 }
116
117 public static void main(String[] args) throws Exception {
118 Main main = new Main();
119
120 main.runTests();
121
122 System.out.println("Done!");
123 }
124}