X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Ftest%2FAssertionUtils.java;h=966a52e4f4318a9f00d7ad9977f131e5f6c5a84c;hb=89c06d4d52b46c154128c97d6e758fa1f4fc7a6e;hp=8d179db7ad1d488a9d53361c414b3adea6d1e5f8;hpb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;p=utils diff --git a/support/general/src/test/java/org/wamblee/test/AssertionUtils.java b/support/general/src/test/java/org/wamblee/test/AssertionUtils.java index 8d179db7..966a52e4 100644 --- a/support/general/src/test/java/org/wamblee/test/AssertionUtils.java +++ b/support/general/src/test/java/org/wamblee/test/AssertionUtils.java @@ -1,12 +1,12 @@ /* * Copyright 2006 the original author or authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,11 +15,16 @@ */ package org.wamblee.test; +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.Set; -import junit.framework.TestCase; /** * Useful assertions for use in test cases. @@ -27,10 +32,11 @@ import junit.framework.TestCase; * @author Erik Brakkee */ public final class AssertionUtils { + private static final Log LOG = LogFactory.getLog(AssertionUtils.class); /** * Disabled constructor. - * + * */ private AssertionUtils() { // Empty @@ -38,19 +44,19 @@ public final class AssertionUtils { /** * Asserts that two object arrays are equal. - * + * * @param aExpected * Expected object array. * @param aActual * Actual object array. */ - public static void assertEquals(Object[] aExpected, Object[] aActual) { + public static void assertEquals(T[] aExpected, T[] aActual) { assertEquals("", aExpected, aActual); } /** * Asserts that two object arrays are equal. - * + * * @param aMsg * Message. * @param aExpected @@ -58,21 +64,21 @@ public final class AssertionUtils { * @param aActual * Actual array. */ - public static void assertEquals(String aMsg, Object[] aExpected, - Object[] aActual) { - TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length, - aActual.length); + public static void assertEquals(String aMsg, T[] aExpected, T[] aActual) { + TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) + + ", actual " + Arrays.asList(aActual) + ": Array lengths ", + aExpected.length, aActual.length); for (int i = 0; i < aExpected.length; i++) { TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i], - aActual[i]); + aActual[i]); } } /** * Asserts that two objects are equal, and in case the object is an Object[] * delegates to {@link #assertEquals(String, Object[], Object[]). - * + * * @param aMsg * Message. * @param aExpected @@ -80,11 +86,10 @@ public final class AssertionUtils { * @param aActual * Actual result. */ - public static void assertEquals(String aMsg, Object aExpected, - Object aActual) { + public static void assertEquals(String aMsg, T aExpected, T aActual) { if (aExpected instanceof Object[]) { AssertionUtils.assertEquals(aMsg, (Object[]) aExpected, - (Object[]) aActual); + (Object[]) aActual); return; } @@ -95,7 +100,7 @@ public final class AssertionUtils { /** * Asserts that two maps are equal by comparing all keys and by checking * that the values for the same keys are the same. - * + * * @param aMsg * Message. * @param aExpectedMap @@ -103,18 +108,44 @@ public final class AssertionUtils { * @param aActual * Actual result. */ - public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) { - TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual - .size()); + public static void assertEquals(String aMsg, + Map aExpectedMap, Map aActual) { + TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), + aActual.size()); Set keys = aExpectedMap.keySet(); for (Iterator i = keys.iterator(); i.hasNext();) { String key = (String) i.next(); TestCase.assertTrue("Map does not containg entry for key:" + key, - aActual.containsKey(key)); + aActual.containsKey(key)); AssertionUtils.assertEquals("Value of key " + key + " of map", - aExpectedMap.get(key), aActual.get(key)); + aExpectedMap.get(key), aActual.get(key)); } } + + /** + * Asserts that an exception occurs. + * @param aRunnable Test cases should create a subclass of this which contains the + * code that should throw an exception. + * @param aType Type of exception that is expected. + */ + public static void assertException(ErroneousCode aObject, Class aType) { + try { + aObject.run(); + throw new RuntimeException("No exception occurred"); + } catch (Throwable t) { + if (aType.isInstance(t)) { + LOG.info("Expected exception occured " + t.getMessage()); + + return; // ok + } else { + throw new RuntimeException(t); + } + } + } + + public static interface ErroneousCode { + void run() throws Exception; + } }