001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.util;
005:
006: import com.tc.test.TCTestCase;
007:
008: import java.util.ArrayList;
009:
010: /**
011: * Unit test for {@link Assert}.
012: */
013: public class AssertTest extends TCTestCase {
014:
015: public void testAssertNoBlankElements() {
016: try {
017: Assert.assertNoBlankElements(null);
018: fail("Didn't get NPE on no array");
019: } catch (NullPointerException npe) {
020: // ok
021: }
022:
023: try {
024: Assert.assertNoBlankElements(new String[] { "x", "y", null,
025: "z" });
026: fail("Didn't get NPE on null element");
027: } catch (NullPointerException npe) {
028: // ok
029: }
030:
031: try {
032: Assert.assertNoBlankElements(new String[] { "x", "y", "",
033: "z" });
034: fail("Didn't get IAE on empty string");
035: } catch (IllegalArgumentException iae) {
036: // ok
037: }
038:
039: try {
040: Assert.assertNoBlankElements(new String[] { "x", "y",
041: " ", "z" });
042: fail("Didn't get IAE on blank string");
043: } catch (IllegalArgumentException iae) {
044: // ok
045: }
046: }
047:
048: public void testAssertNoNullElements() {
049: try {
050: Assert.assertNoNullElements(null);
051: fail("null passed check");
052: } catch (NullPointerException npe) {
053: // ok
054: }
055:
056: Assert.assertNoNullElements(new Object[] {});
057:
058: Object[] values = new Object[] { new Object(), null };
059:
060: try {
061: Assert.assertNoNullElements(values);
062: fail("array with null passed check");
063: } catch (NullPointerException npe) {
064: // ok
065: }
066:
067: values[1] = new Object();
068:
069: Assert.assertNoNullElements(values);
070: }
071:
072: public void testEval() throws Exception {
073: Assert.eval(true);
074: Assert.eval("foo", true);
075:
076: try {
077: Assert.eval(false);
078: fail("Didn't get exception on eval(false)");
079: } catch (TCAssertionError tcae) {
080: // ok
081: }
082:
083: try {
084: Assert.eval("foo", false);
085: fail("Didn't get exception on eval(\"foo\", false)");
086: } catch (TCAssertionError tcae) {
087: assertTrue(tcae.getMessage().indexOf("foo") >= 0);
088: }
089: }
090:
091: public void testFail() throws Exception {
092: TCAssertionError error = Assert.failure("foo");
093: assertTrue(error.getMessage().indexOf("foo") >= 0);
094: }
095:
096: public void testAssertNotNull() throws Exception {
097: Assert.assertNotNull(new Object());
098: Assert.assertNotNull("foo", new Object());
099:
100: try {
101: Assert.assertNotNull(null);
102: fail("Didn't get exception on assertNotNull(null)");
103: } catch (NullPointerException npe) {
104: // ok
105: }
106:
107: try {
108: Assert.assertNotNull("foo", null);
109: fail("Didn't get exception on assertNotNull(\"foo\", null)");
110: } catch (NullPointerException npe) {
111: assertTrue(npe.getMessage().indexOf("foo") >= 0);
112: }
113: }
114:
115: public void testAssertNull() throws Exception {
116: Assert.assertNull(null);
117: Assert.assertNull("foo", null);
118:
119: try {
120: Assert.assertNull(new Object());
121: fail("Didn't get assertion");
122: } catch (TCAssertionError e) {
123: // expected
124: }
125:
126: try {
127: Assert.assertNull("foo", new Object());
128: fail("Didn't get assertion");
129: } catch (TCAssertionError e) {
130: // expected
131: }
132: }
133:
134: public void testAssertNotEmpty() throws Exception {
135: Assert.assertNotEmpty("x");
136: Assert.assertNotEmpty("foo", "x");
137:
138: try {
139: Assert.assertNotEmpty(null);
140: fail("Didn't get exception on assertNotEmpty(null)");
141: } catch (NullPointerException npe) {
142: // ok
143: }
144:
145: try {
146: Assert.assertNotEmpty("");
147: fail("Didn't get exception on assertNotEmpty(\"\")");
148: } catch (IllegalArgumentException iae) {
149: // ok
150: }
151:
152: try {
153: Assert.assertNotEmpty("foo", null);
154: fail("Didn't get exception on assertNotEmpty(\"foo\", null)");
155: } catch (NullPointerException npe) {
156: assertTrue(npe.getMessage().indexOf("foo") >= 0);
157: }
158:
159: try {
160: Assert.assertNotEmpty("foo", "");
161: fail("Didn't get exception on assertNotEmpty(\"foo\", \"\")");
162: } catch (IllegalArgumentException iae) {
163: assertTrue(iae.getMessage().indexOf("foo") >= 0);
164: }
165: }
166:
167: public void testAssertNotBlank() throws Exception {
168: Assert.assertNotBlank("x");
169: Assert.assertNotBlank("foo", "x");
170:
171: try {
172: Assert.assertNotBlank(null);
173: fail("Didn't get exception on assertNotBlank(null)");
174: } catch (NullPointerException npe) {
175: // ok
176: }
177:
178: try {
179: Assert.assertNotBlank("");
180: fail("Didn't get exception on assertNotBlank(\"\")");
181: } catch (IllegalArgumentException iae) {
182: // ok
183: }
184:
185: try {
186: Assert.assertNotBlank(" ");
187: fail("Didn't get exception on assertNotBlank(\" \")");
188: } catch (IllegalArgumentException iae) {
189: // ok
190: }
191:
192: try {
193: Assert.assertNotBlank("foo", null);
194: fail("Didn't get exception on assertNotBlank(\"foo\", null)");
195: } catch (NullPointerException npe) {
196: assertTrue(npe.getMessage().indexOf("foo") >= 0);
197: }
198:
199: try {
200: Assert.assertNotBlank("foo", "");
201: fail("Didn't get exception on assertNotBlank(\"foo\", \"\")");
202: } catch (IllegalArgumentException iae) {
203: assertTrue(iae.getMessage().indexOf("foo") >= 0);
204: }
205:
206: try {
207: Assert.assertNotBlank("foo", " ");
208: fail("Didn't get exception on assertNotBlank(\"foo\", \" \")");
209: } catch (IllegalArgumentException iae) {
210: assertTrue(iae.getMessage().indexOf("foo") >= 0);
211: }
212: }
213:
214: public final void testAssertContainsElement() throws Exception {
215: try {
216: Assert.assertContainsElement(null, null);
217: fail("Should not be able to pass null array");
218: } catch (NullPointerException npe) {
219: // Expected
220: }
221: String foo = "foo";
222: Object[] objectArray = new Object[] { new Object(), foo, null,
223: new Integer(1), new ArrayList() };
224: for (int pos = 0; pos < objectArray.length; pos++) {
225: Assert.assertContainsElement(objectArray, objectArray[pos]);
226: }
227: try {
228: Assert.assertContainsElement(objectArray, new String(foo));
229: throw new RuntimeException(
230: "Array does not contain the specified element");
231: } catch (TCAssertionError tcae) {
232: // Expected
233: }
234: }
235:
236: }
|