001: package com.bm.testsuite;
002:
003: import java.lang.reflect.Field;
004: import java.util.Collection;
005: import org.jmock.MockObjectTestCase;
006:
007: import com.bm.introspectors.Property;
008: import com.bm.utils.BeanEqualsTester;
009:
010: /**
011: * Superclass of all EJB3unit methods. An extension of junit.framework.TestCase
012: * that adds those methods that we really wish were part of JUnit.
013: *
014: * @author Daniel Wiese
015: * @since 09.02.2006
016: */
017: public class BaseTest extends MockObjectTestCase {
018:
019: /**
020: * Create an instance.
021: *
022: * @param name
023: * The name of the test
024: */
025: public BaseTest(final String name) {
026: super (name);
027: }
028:
029: /**
030: * Default costructor.
031: */
032: public BaseTest() {
033: super ();
034: }
035:
036: /**
037: * Assert that the two collections are the same irrespective of order.
038: *
039: * @param a
040: * The first collection
041: * @param b
042: * The second collection
043: * @param <T> -
044: * the type of the collection
045: */
046: public <T> void assertCollectionsEqual(
047: final Collection<? extends T> a,
048: final Collection<? extends T> b) {
049: BeanEqualsTester.testColletionsForEqual(a, b);
050: }
051:
052: /**
053: * Assert that the specified condition is false. Older versions of junit
054: * have assertTrue() but not assertFalse so we add it here to be sure that
055: * it is present.
056: *
057: * @param description
058: * The failure message to be used if the condition is not false.
059: * @param condition
060: * The value to check.
061: */
062: public static void assertFalse(final String description,
063: final boolean condition) {
064: if (condition) {
065: fail(description + ": Expected false");
066: }
067: }
068:
069: /**
070: * Assert that the specified condition is false. Older versions of junit
071: * have assertTrue() but not assertFalse so we add it here to be sure that
072: * it is present.
073: *
074: * @param condition
075: * The value to check.
076: */
077: public static void assertFalse(final boolean condition) {
078: if (condition) {
079: fail("Expected false");
080: }
081: }
082:
083: /**
084: * Assert that the specified object is an instance of this class.
085: *
086: * @param label
087: * A description of the test
088: * @param object
089: * The object to test
090: * @param clazz
091: * The class
092: * @param <T> -
093: * the type of the object
094: */
095: public <T> void assertInstanceOf(final String label,
096: final T object, final Class<? extends T> clazz) {
097: if (!clazz.isAssignableFrom(object.getClass())) {
098: fail(label + ": object [" + object
099: + "] is not an instance of class ["
100: + clazz.getName() + "]");
101: }
102: }
103:
104: /**
105: * Assert that the specified object is an instance of this class.
106: *
107: * @param object
108: * The object to test
109: * @param clazz
110: * The class
111: * @param <T> -
112: * the type of the object
113: */
114: public <T> void assertInstanceOf(final T object,
115: final Class<? extends T> clazz) {
116: this .assertInstanceOf("", object, clazz);
117: }
118:
119: /**
120: * We donīt want that test fails because htey have no test method.
121: *
122: * @author Daniel Wiese
123: * @since 23.04.2006
124: */
125: public void testNothing() {
126: // intentionally left blank
127: }
128:
129: /**
130: * Sets a value for a field in the tested-bean instance.
131: *
132: * @author Daniel Wiese
133: * @since 02.05.2006
134: * @param fieldName -
135: * the name of the field
136: * @param toSet -
137: * the value to set
138: */
139: protected void setValueForField(Object forObject, String fieldName,
140: Object toSet) {
141: try {
142: final Field field = forObject.getClass().getDeclaredField(
143: fieldName);
144: Property prop = new Property(field);
145: prop.setField(forObject, toSet);
146: } catch (SecurityException e) {
147: throw new RuntimeException(e);
148: } catch (NoSuchFieldException e) {
149: throw new RuntimeException(e);
150: } catch (IllegalAccessException e) {
151: throw new RuntimeException(e);
152: }
153: }
154: }
|