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; |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 18 | import java.lang.reflect.Modifier; |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | /** |
| 23 | * Smali excercise. |
| 24 | */ |
| 25 | public 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 Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 53 | testCases.add(new TestCase("b/17978759", "B17978759", "test", null, new VerifyError(), null)); |
Stephen Kyle | 8fe0e35 | 2014-10-16 15:02:42 +0100 | [diff] [blame] | 54 | testCases.add(new TestCase("FloatBadArgReg", "FloatBadArgReg", "getInt", |
| 55 | new Object[]{100}, null, 100)); |
nikolay serdjuk | d24c934 | 2014-11-10 16:53:27 +0700 | [diff] [blame] | 56 | testCases.add(new TestCase("negLong", "negLong", "negLong", null, null, 122142L)); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame^] | 57 | testCases.add(new TestCase("sameFieldNames", "sameFieldNames", "getInt", null, null, 7)); |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 58 | } |
| 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 Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 72 | Exception errorReturn = null; |
| 73 | try { |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 74 | 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 Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 86 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 87 | |
| 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 Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 115 | } |
Ian Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 116 | } catch (Throwable exc) { |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 117 | 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 Rogers | 8e1f4f8 | 2014-11-05 11:07:30 -0800 | [diff] [blame] | 121 | tc.expectedException.getClass().getName() + |
| 122 | ", but got " + exc.getClass(), exc); |
| 123 | } else { |
| 124 | // Expected exception, do nothing. |
Andreas Gampe | 8fda9f2 | 2014-10-03 16:15:37 -0700 | [diff] [blame] | 125 | } |
| 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 | } |