001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016:
017: /*
018: * Portions copyright 2006 Mat Gessel <mat.gessel@gmail.com>; licensed under
019: * Apache 2.0
020: */
021: package com.google.gwt.junit.client;
022:
023: import junit.framework.AssertionFailedError;
024:
025: /**
026: * This class tests our implementation of the GWTTestCase class which provides
027: * the behavior of TestCase that is necessary in GWT.
028: */
029: public class GWTTestCaseTest extends GWTTestCase {
030:
031: private static void assertNotEquals(double a, double b, double delta) {
032: boolean failed = false;
033: try {
034: assertEquals(a, b, delta);
035: } catch (AssertionFailedError e) {
036: // EXPECTED
037: failed = true;
038: }
039:
040: if (!failed) {
041: fail("Expected failure for assertEquals(" + a + ", " + b
042: + ", " + delta + ")");
043: }
044: }
045:
046: private static void assertNotEquals(float a, float b, float delta) {
047: boolean failed = false;
048: try {
049: assertEquals(a, b, delta);
050: } catch (AssertionFailedError e) {
051: // EXPECTED
052: failed = true;
053: }
054:
055: if (!failed) {
056: fail("Expected failure for assertEquals(" + a + ", " + b
057: + ", " + delta + ")");
058: }
059: }
060:
061: public String getModuleName() {
062: return "com.google.gwt.junit.JUnit";
063: }
064:
065: public void testAssertEqualsDouble() {
066: assertEquals(0.0, 0.0, 0.0);
067: assertEquals(1.1, 1.1, 0.0);
068: assertEquals(-1.1, -1.1, 0.0);
069: assertEquals(Float.MIN_VALUE, Float.MIN_VALUE, 0.0);
070: assertEquals(Float.MAX_VALUE, Float.MAX_VALUE, 0.0);
071: assertNotEquals(0.0,
072: 0.00000000000000000000000000000000000000001, 0.0);
073: assertNotEquals(0.0, 0.0000000000000000001, 0.0);
074: assertNotEquals(0.0, 0.000000001, 0.0);
075: assertNotEquals(0.0, 0.0001, 0.0);
076: assertNotEquals(0.0, 0.1, 0.0);
077: assertNotEquals(1.0, 2.0, 0.1);
078: assertNotEquals(2.0, 1.0, 0.1);
079: assertNotEquals(-1.0, -2.0, 0.1);
080: assertNotEquals(-2.0, -1.0, 0.1);
081: }
082:
083: public void testAssertEqualsFloat() {
084: assertEquals(0.0f, 0.0f, 0.0f);
085: assertEquals(1.1f, 1.1f, 0.0f);
086: assertEquals(-1.1f, -1.1f, 0.0f);
087: assertEquals(Float.MIN_VALUE, Float.MIN_VALUE, 0.0f);
088: assertEquals(Float.MAX_VALUE, Float.MAX_VALUE, 0.0f);
089: assertNotEquals(0.0f,
090: 0.00000000000000000000000000000000000000001f, 0.0f);
091: assertNotEquals(0.0f, 0.0000000000000000001f, 0.0f);
092: assertNotEquals(0.0f, 0.000000001f, 0.0f);
093: assertNotEquals(0.0f, 0.0001f, 0.0f);
094: assertNotEquals(0.0f, 0.1f, 0.0f);
095: assertNotEquals(1.0f, 2.0f, 0.1f);
096: assertNotEquals(2.0f, 1.0f, 0.1f);
097: assertNotEquals(-1.0f, -2.0f, 0.1f);
098: assertNotEquals(-2.0f, -1.0f, 0.1f);
099: }
100:
101: /**
102: *
103: */
104: public void testAssertEqualsIntInt() {
105: assertEquals(5, 5);
106:
107: boolean exWasThrown = false;
108: try {
109: assertEquals(5, 4);
110: } catch (Throwable ex) {
111: exWasThrown = true;
112: if (!(ex instanceof AssertionFailedError)) {
113: fail("Unexpected type of exception during assertEquals(int,int) testing");
114: }
115: }
116:
117: if (!exWasThrown) {
118: fail("No exception was thrown during assertEquals(int,int) testing");
119: }
120: }
121:
122: /**
123: *
124: */
125: public void testAssertEqualsObjectObject() {
126: Object obj1 = "String";
127:
128: assertEquals(obj1, obj1);
129:
130: boolean exWasThrown = false;
131: try {
132: Object obj2 = "not String";
133: assertEquals(obj1, obj2);
134: } catch (Throwable ex) {
135: exWasThrown = true;
136: if (!(ex instanceof AssertionFailedError)) {
137: fail("Unexpected type of exception during assertEquals(String,String) testing");
138: }
139: }
140:
141: if (!exWasThrown) {
142: fail("No exception was thrown during assertEquals(String,String) testing");
143: }
144: }
145:
146: /**
147: *
148: */
149: public void testAssertEqualsStringIntInt() {
150: assertEquals("", 5, 5);
151:
152: boolean exWasThrown = false;
153: try {
154: assertEquals("hello", 5, 4);
155: } catch (Throwable ex) {
156: exWasThrown = true;
157: if (!(ex instanceof AssertionFailedError)) {
158: fail("Unexpected type of exception during assertEquals(String,int,int) testing");
159: }
160: }
161:
162: if (!exWasThrown) {
163: fail("No exception was thrown during assertEquals(String,int,int) testing");
164: }
165: }
166:
167: /**
168: *
169: */
170: public void testAssertEqualsStringObjectObject() {
171: Object obj1 = "String";
172:
173: assertEquals("msg", obj1, obj1);
174:
175: boolean exWasThrown = false;
176: try {
177: Object obj2 = "not String";
178: assertEquals("msg", obj1, obj2);
179: } catch (Throwable ex) {
180: exWasThrown = true;
181: if (!(ex instanceof AssertionFailedError)) {
182: fail("Unexpected type of exception during assertEquals(String,Object,Object) testing");
183: }
184: }
185:
186: if (!exWasThrown) {
187: fail("No exception was thrown during assertEquals(String,Object,Object) testing");
188: }
189: }
190:
191: /**
192: *
193: */
194: public void testAssertFalse() {
195: // Should not fail
196: assertFalse(false);
197:
198: // Should not fail
199: assertFalse("We should be okay", false);
200:
201: boolean exWasThrown = false;
202: try {
203: // Should fail
204: assertFalse(true);
205: } catch (Throwable ex) {
206: exWasThrown = true;
207: if (!(ex instanceof AssertionFailedError)) {
208: fail("Unexpected type of exception during assertFalse(boolean) testing");
209: }
210: }
211:
212: if (!exWasThrown) {
213: fail("No exception was thrown");
214: }
215: exWasThrown = false;
216:
217: try {
218: // Should fail
219: assertFalse("This should be okay", true);
220: } catch (Throwable ex) {
221: exWasThrown = true;
222: if (ex instanceof AssertionFailedError) {
223: return;
224: }
225: }
226:
227: if (!exWasThrown)
228: fail("No exception was thrown");
229: else
230: fail("Unexpected exception during assertFalse(String, boolean) testing");
231: }
232:
233: /**
234: *
235: */
236: public void testAssertNotNull() {
237: assertNotNull("Hello");
238: assertNotNull("We should be okay", "Hello");
239:
240: try {
241: assertNotNull("Hello");
242: } catch (Throwable ex) {
243: if (!(ex instanceof AssertionFailedError)) {
244: fail("Unexpected type of exception during assertNotNull(Object) testing");
245: }
246: }
247:
248: try {
249: assertNotNull("This should be okay", null);
250: } catch (Throwable ex) {
251: if (ex instanceof AssertionFailedError) {
252: return;
253: }
254: }
255:
256: fail("Unexpected exception during assertNotNull(String, Object) testing");
257: }
258:
259: /**
260: *
261: */
262: public void testAssertNotSame() {
263: Object obj1 = "Foo";
264: Object obj2 = "Bar";
265:
266: assertNotSame(obj1, obj2);
267: assertNotSame("Hello", obj1, obj2);
268:
269: try {
270: assertNotSame(obj1, "Baz");
271: } catch (Throwable ex) {
272: if (!(ex instanceof AssertionFailedError)) {
273: fail("Unexpected type of exception");
274: }
275: }
276: }
277:
278: /**
279: *
280: */
281: public void testAssertNull() {
282: assertNull(null);
283: assertNull("We should be okay", null);
284:
285: try {
286: assertNull("Hello");
287: } catch (Throwable ex) {
288: if (!(ex instanceof AssertionFailedError)) {
289: fail("Unexpected type of exception during assertNull(Object) testing");
290: }
291: }
292:
293: try {
294: assertNull("This should be okay", "Hello");
295: } catch (Throwable ex) {
296: if (ex instanceof AssertionFailedError) {
297: return;
298: }
299: }
300:
301: fail("Unexpected exception during assertNull(String, Object) testing");
302: }
303:
304: /**
305: *
306: */
307: public void testAssertSame() {
308: // TODO(mmendez): finish this test
309: }
310:
311: /**
312: *
313: */
314: public void testAssertTrue() {
315: assertTrue(true);
316: assertTrue("We should be okay", true);
317:
318: try {
319: assertTrue(false);
320: } catch (Throwable ex) {
321: if (!(ex instanceof AssertionFailedError)) {
322: fail("Unexpected type of exception during assertFalse(boolean) testing");
323: }
324: }
325:
326: try {
327: assertTrue("This should be okay", false);
328: } catch (Throwable ex) {
329: if (ex instanceof AssertionFailedError) {
330: return;
331: }
332: }
333:
334: fail("Unexpected exception during assertTrue(String, boolean) testing");
335: }
336: }
|