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, null);
     29         } else if (expectedErrorClass == null && thrown != null) {
     30             fail("Unexpected error " + thrown, 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, thrown);
     35                 }
     36                 thrown = thrown.getCause();
     37             }
     38             if (!expectedErrorClass.equals(thrown.getClass())) {
     39                 fail("Expected error of type " + expectedErrorClass + ", but got " +
     40                          thrown.getClass(), thrown);
     41             }
     42         }
     43     }
     44 
     45     /**
     46      * Try to load the class with the given name, and check for the expected error.
     47      */
     48     public static Class<?> load(String className, Class<?> expectedErrorClass) {
     49         Class<?> c;
     50         try {
     51             c = Class.forName(className);
     52         } catch (Throwable t) {
     53             if (expectedErrorClass != null) {
     54                 checkError(expectedErrorClass, t, false);
     55             } else {
     56                 fail("Could not load class " + className, t);
     57             }
     58             return null;
     59         }
     60         checkError(expectedErrorClass, null, false);
     61         return c;
     62     }
     63 
     64     /**
     65      * Try to load the class with the given name, find the "run" method and run it.
     66      * If expectedErrorClass is not null, check for an exception of that class.
     67      */
     68     public static void loadAndRun(String className, boolean isStatic, boolean wrapped,
     69                                   Class<?> expectedErrorClass, Object... args) {
     70         Class<?> c = load(className, null);
     71 
     72         java.lang.reflect.Method method = null;
     73         // We expect only ever one declared method named run, but don't know the arguments. So
     74         // search for one.
     75         for (java.lang.reflect.Method m : c.getDeclaredMethods()) {
     76             if (m.getName().equals("run")) {
     77                 method = m;
     78                 break;
     79             }
     80         }
     81         if (method == null) {
     82             fail("Could not find method 'run'", null);
     83         }
     84 
     85         Object receiver = null;
     86         if (!isStatic) {
     87             try {
     88                 receiver = c.newInstance();
     89             } catch (Exception exc) {
     90                 fail("Could not instantiate " + className, exc);
     91             }
     92         }
     93 
     94         try {
     95             method.invoke(receiver, args);
     96         } catch (Throwable t) {
     97             checkError(expectedErrorClass, t, wrapped);
     98             return;
     99         }
    100         checkError(expectedErrorClass, null, false);
    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)
    113             throw new AssertionFailedException(
    114                     "not equals. Expected " + expected + " actual " + actual);
    115     }
    116 
    117     static public void assertEquals(String message, int expected, int actual) {
    118         if (expected != actual)
    119             throw new AssertionFailedException(
    120                     "not equals: " + message + " Expected " + expected + " actual " + actual);
    121     }
    122 
    123     static public void assertEquals(long expected, long actual) {
    124         if (expected != actual)
    125             throw new AssertionFailedException(
    126                     "not equals. Expected " + expected + " actual " + actual);
    127     }
    128 
    129     static public void assertEquals(double expected, double actual, double delta) {
    130         if (!(Math.abs(expected - actual) <= delta))
    131             throw new AssertionFailedException("not within delta");
    132     }
    133 
    134     static public void assertEquals(Object expected, Object actual) {
    135         if (expected == null && actual == null)
    136             return;
    137         if (expected != null && expected.equals(actual))
    138             return;
    139         throw new AssertionFailedException("not the same: " + expected + " vs " + actual);
    140     }
    141 
    142     static public void assertTrue(boolean condition) {
    143         if (!condition)
    144             throw new AssertionFailedException("condition was false");
    145     }
    146 
    147     static public void assertFalse(boolean condition) {
    148         if (condition)
    149             throw new AssertionFailedException("condition was true");
    150     }
    151 
    152     static public void assertNotNull(Object object) {
    153         if (object == null)
    154             throw new AssertionFailedException("object was null");
    155     }
    156 
    157     static public void assertNull(Object object) {
    158         if (object != null)
    159             throw new AssertionFailedException("object was not null");
    160     }
    161 
    162     static public void fail(String message) {
    163         fail(message, null);
    164     }
    165 
    166     static public void fail(String message, Throwable cause) {
    167         throw new AssertionFailedException(message, cause);
    168     }
    169 }
    170