001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit;
022:
023: public final class Assert {
024:
025: public static Throwable expect(Class exception, CodeBlock block) {
026: Throwable e = getThrowable(block);
027: assertThrowable(exception, e);
028: return e;
029: }
030:
031: public static Throwable expect(Class exception, Class cause,
032: CodeBlock block) {
033: Throwable e = getThrowable(block);
034: assertThrowable(exception, e);
035: assertThrowable(cause, TestPlatform.getExceptionCause(e));
036: return e;
037: }
038:
039: private static void assertThrowable(Class exception, Throwable e) {
040: if (e == null) {
041: fail("Exception '" + exception.getName() + "' expected");
042: }
043: if (exception.isInstance(e))
044: return;
045: fail("Expecting '" + exception.getName() + "' but got '"
046: + e.getClass().getName() + "'", e);
047: }
048:
049: private static Throwable getThrowable(CodeBlock block) {
050: try {
051: block.run();
052: } catch (Throwable e) {
053: return e;
054: }
055: return null;
056: }
057:
058: public static void fail() {
059: fail("FAILURE");
060: }
061:
062: public static void fail(String msg) {
063: throw new AssertionException(msg);
064: }
065:
066: public static void fail(String msg, Throwable cause) {
067: throw new AssertionException(msg, cause);
068: }
069:
070: public static void isTrue(boolean condition) {
071: isTrue(condition, "FAILURE");
072: }
073:
074: public static void isTrue(boolean condition, String msg) {
075: if (condition)
076: return;
077: fail(msg);
078: }
079:
080: public static void isNull(Object reference) {
081: isNull(reference, failureMessage("null", reference));
082: }
083:
084: public static void isNull(Object reference, String message) {
085: if (reference != null) {
086: fail(message);
087: }
088: }
089:
090: public static void isNotNull(Object reference) {
091: isNotNull(reference, failureMessage("not null", reference));
092: }
093:
094: public static void isNotNull(Object reference, String message) {
095: if (reference == null) {
096: fail(message);
097: }
098: }
099:
100: public static void areEqual(boolean expected, boolean actual) {
101: if (expected == actual)
102: return;
103: fail(failureMessage(new Boolean(expected), new Boolean(actual)));
104: }
105:
106: public static void areEqual(int expected, int actual) {
107: areEqual(expected, actual, null);
108: }
109:
110: public static void areEqual(int expected, int actual, String message) {
111: if (expected == actual)
112: return;
113: fail(failureMessage(new Integer(expected), new Integer(actual),
114: message));
115: }
116:
117: public static void areEqual(double expected, double actual) {
118: areEqual(expected, actual, null);
119: }
120:
121: public static void areEqual(double expected, double actual,
122: String message) {
123: if (expected == actual)
124: return;
125: fail(failureMessage(new Double(expected), new Double(actual),
126: message));
127: }
128:
129: public static void areEqual(long expected, long actual) {
130: if (expected == actual)
131: return;
132: fail(failureMessage(new Long(expected), new Long(actual)));
133: }
134:
135: public static void areEqual(Object expected, Object actual,
136: String message) {
137: if (objectsAreEqual(expected, actual))
138: return;
139: fail(failureMessage(expected, actual, message));
140: }
141:
142: public static void areEqual(Object expected, Object actual) {
143: areEqual(expected, actual, null);
144: }
145:
146: public static void areSame(Object expected, Object actual) {
147: if (expected == actual)
148: return;
149: fail(failureMessage(expected, actual));
150: }
151:
152: public static void areNotSame(Object expected, Object actual) {
153: if (expected != actual)
154: return;
155: fail("Expecting not '" + expected + "'.");
156: }
157:
158: private static String failureMessage(Object expected, Object actual) {
159: return failureMessage(expected, actual, null);
160: }
161:
162: private static String failureMessage(Object expected,
163: Object actual, String customMessage) {
164: return failureMessage(expected, actual, "", customMessage);
165: }
166:
167: private static String failureMessage(Object expected,
168: Object actual, final String cmpOper, String customMessage) {
169: return (customMessage == null ? "" : customMessage + ": ")
170: + "Expected " + cmpOper + "'" + expected
171: + "' but was '" + actual + "'";
172: }
173:
174: private static boolean objectsAreEqual(Object expected,
175: Object actual) {
176: return expected == actual
177: || (expected != null && actual != null && expected
178: .equals(actual));
179: }
180:
181: public static void isFalse(boolean condition) {
182: isTrue(!condition);
183: }
184:
185: public static void isFalse(boolean condition, String message) {
186: isTrue(!condition, message);
187: }
188:
189: public static void isInstanceOf(Class expectedClass, Object actual) {
190: isTrue(expectedClass.isInstance(actual), failureMessage(
191: expectedClass, actual == null ? null : actual
192: .getClass()));
193: }
194:
195: public static void isGreater(long expected, long actual) {
196: if (actual > expected)
197: return;
198: fail(failureMessage(new Long(expected), new Long(actual),
199: "greater than ", null));
200: }
201:
202: public static void isGreaterOrEqual(long expected, long actual) {
203: if (actual >= expected)
204: return;
205: fail(expected, actual, "greater than or equal to ");
206: }
207:
208: public static void isSmaller(long expected, long actual) {
209: if (actual < expected)
210: return;
211: fail(failureMessage(new Long(expected), new Long(actual),
212: "smaller than ", null));
213: }
214:
215: private static void fail(long expected, long actual,
216: final String operator) {
217: fail(failureMessage(new Long(expected), new Long(actual),
218: operator, null));
219: }
220:
221: public static void areNotEqual(long expected, long actual) {
222: if (actual != expected)
223: return;
224: fail(expected, actual, "not equal to ");
225: }
226:
227: public static void areNotEqual(Object notExpected, Object actual) {
228: if (!objectsAreEqual(notExpected, actual))
229: return;
230: fail("Expecting not '" + notExpected + "'");
231: }
232:
233: public static void equalsAndHashcode(Object obj, Object same,
234: Object other) {
235: areEqual(obj, obj);
236: areEqual(obj, same);
237: areNotEqual(obj, other);
238: areEqual(obj.hashCode(), same.hashCode());
239: areEqual(same, obj);
240: areNotEqual(other, obj);
241: areNotEqual(obj, null);
242: }
243: }
|