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