• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 package dot.junit;
18 
19 import junit.framework.TestCase;
20 
21 public class DxTestCase extends TestCase {
22 
checkError(Class<?> expectedErrorClass, Throwable thrown, boolean in_invocation_exc)23     private static void checkError(Class<?> expectedErrorClass, Throwable thrown,
24                                    boolean in_invocation_exc) {
25         if (expectedErrorClass != null && thrown == null) {
26             fail("Expected error of type " + expectedErrorClass);
27         } else if (expectedErrorClass == null && thrown != null) {
28             fail("Unexpected error " + thrown);
29         } else if (expectedErrorClass != null && thrown != null) {
30             if (in_invocation_exc) {
31                 if (!(thrown instanceof java.lang.reflect.InvocationTargetException)) {
32                     fail("Expected invocation target exception, but got " + thrown);
33                 }
34                 thrown = thrown.getCause();
35             }
36             if (!expectedErrorClass.equals(thrown.getClass())) {
37                 fail("Expected error of type " + expectedErrorClass + ", but got " +
38                      thrown.getClass());
39             }
40         }
41     }
42 
43     /**
44      * Try to load the class with the given name, and check for the expected error.
45      */
load(String className, Class<?> expectedErrorClass)46     public static Class<?> load(String className, Class<?> expectedErrorClass) {
47         try {
48             Class<?> c = Class.forName(className);
49             checkError(expectedErrorClass, null, false);
50             return c;
51         } catch (Throwable t) {
52             if (expectedErrorClass != null) {
53                 checkError(expectedErrorClass, t, false);
54             } else {
55                 fail("Could not load class " + className + ": " + t);
56             }
57             return null;
58         }
59     }
60 
61     /**
62      * Try to load the class with the given name, find the "run" method and run it.
63      * If expectedErrorClass is not null, check for an exception of that class.
64      */
loadAndRun(String className, boolean isStatic, boolean wrapped, Class<?> expectedErrorClass, Object... args)65     public static void loadAndRun(String className, boolean isStatic, boolean wrapped,
66                                   Class<?> expectedErrorClass, Object... args) {
67         Class<?> c = load(className, null);
68 
69         java.lang.reflect.Method method = null;
70         // We expect only ever one declared method named run, but don't know the arguments. So
71         // search for one.
72         for (java.lang.reflect.Method m : c.getDeclaredMethods()) {
73             if (m.getName().equals("run")) {
74                 method = m;
75                 break;
76             }
77         }
78         if (method == null) {
79             fail("Could not find method 'run'");
80         }
81 
82         Object receiver = null;
83         if (!isStatic) {
84             try {
85                 receiver = c.newInstance();
86             } catch (Exception exc) {
87                 fail("Could not instantiate " + className + ": " + exc.getMessage());
88             }
89         }
90 
91         try {
92             method.invoke(receiver, args);
93             checkError(expectedErrorClass, null, false);
94         } catch (Throwable t) {
95             checkError(expectedErrorClass, t, wrapped);
96         }
97     }
98 
loadAndRun(String className, Class<?> expectedErrorClass)99     public static void loadAndRun(String className, Class<?> expectedErrorClass) {
100         loadAndRun(className, false, true, expectedErrorClass);
101     }
102 
loadAndRun(String className, Class<?> expectedErrorClass, Object... args)103     public static void loadAndRun(String className, Class<?> expectedErrorClass, Object... args) {
104         loadAndRun(className, false, true, expectedErrorClass, args);
105     }
106 
107     // omit the "extends TestCase" and uncomment the following methods if you would like to run the tests as rolled-out, separate tests.
108 
109 /*
110     static public void assertEquals(int expected, int actual) {
111         if (expected != actual) throw new RuntimeException("AssertionFailedError: not equals");
112     }
113 
114     static public void assertEquals(long expected, long actual) {
115         if (expected != actual) throw new RuntimeException("AssertionFailedError: not equals");
116     }
117 
118     static public void assertEquals(double expected, double actual, double delta) {
119         if(!(Math.abs(expected-actual) <= delta)) throw new RuntimeException("AssertionFailedError: not within delta");
120     }
121 
122     static public void assertEquals(Object expected, Object actual) {
123         if (expected == null && actual == null)
124             return;
125         if (expected != null && expected.equals(actual))
126             return;
127         throw new RuntimeException("AssertionFailedError: not the same");
128     }
129 
130     static public void assertTrue(boolean condition) {
131         if (!condition) throw new RuntimeException("AssertionFailedError: condition was false");
132     }
133 
134     static public void assertFalse(boolean condition) {
135         if (condition) throw new RuntimeException("AssertionFailedError: condition was true");
136     }
137 
138     static public void assertNotNull(Object object) {
139         if (object == null) throw new RuntimeException("AssertionFailedError: object was null");
140     }
141 
142     static public void assertNull(Object object) {
143         if (object != null) throw new RuntimeException("AssertionFailedError: object was not null");
144     }
145 
146     static public void fail(String message) {
147         throw new RuntimeException("AssertionFailedError msg:"+message);
148     }
149 */
150 
151 
152 }
153