Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.lang.reflect.Method; |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * Smali excercise. |
| 23 | */ |
| 24 | public 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 Kyle | 8fe0e35 | 2014-10-16 15:02:42 +0100 | [diff] [blame] | 52 | testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt", |
| 53 | new Object[]{100}, null, 100)); |
nikolay serdjuk | d24c934 | 2014-11-10 16:53:27 +0700 | [diff] [blame] | 54 | testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | public void runTests() { |
| 58 | for (TestCase tc : testCases) { |
| 59 | System.out.println(tc.testName); |
| 60 | try { |
| 61 | runTest(tc); |
| 62 | } catch (Exception exc) { |
| 63 | exc.printStackTrace(System.out); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private void runTest(TestCase tc) throws Exception { |
| 69 | Class<?> c = Class.forName(tc.testClass); |
| 70 | |
| 71 | Method[] methods = c.getDeclaredMethods(); |
| 72 | |
| 73 | // For simplicity we assume that test methods are not overloaded. So searching by name |
| 74 | // will give us the method we need to run. |
| 75 | Method method = null; |
| 76 | for (Method m : methods) { |
| 77 | if (m.getName().equals(tc.testMethodName)) { |
| 78 | method = m; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (method == null) { |
| 84 | throw new IllegalArgumentException("Could not find test method " + tc.testMethodName + |
| 85 | " in class " + tc.testClass + " for test " + tc.testName); |
| 86 | } |
| 87 | |
| 88 | Exception errorReturn = null; |
| 89 | try { |
| 90 | Object retValue = method.invoke(null, tc.values); |
| 91 | if (tc.expectedException != null) { |
| 92 | errorReturn = new IllegalStateException("Expected an exception in test " + |
| 93 | tc.testName); |
| 94 | } |
| 95 | if (tc.expectedReturn == null && retValue != null) { |
| 96 | errorReturn = new IllegalStateException("Expected a null result in test " + |
| 97 | tc.testName); |
| 98 | } else if (tc.expectedReturn != null && |
| 99 | (retValue == null || !tc.expectedReturn.equals(retValue))) { |
| 100 | errorReturn = new IllegalStateException("Expected return " + tc.expectedReturn + |
| 101 | ", but got " + retValue); |
| 102 | } |
| 103 | } catch (Exception exc) { |
| 104 | if (tc.expectedException == null) { |
| 105 | errorReturn = new IllegalStateException("Did not expect exception", exc); |
| 106 | } else if (!tc.expectedException.getClass().equals(exc.getClass())) { |
| 107 | errorReturn = new IllegalStateException("Expected " + |
| 108 | tc.expectedException.getClass().getName() + |
| 109 | ", but got " + exc.getClass(), exc); |
| 110 | } |
| 111 | } finally { |
| 112 | if (errorReturn != null) { |
| 113 | throw errorReturn; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public static void main(String[] args) throws Exception { |
| 119 | Main main = new Main(); |
| 120 | |
| 121 | main.runTests(); |
| 122 | |
| 123 | System.out.println("Done!"); |
| 124 | } |
| 125 | } |