KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > junit > framework > Assert


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package junit.framework;
17
18 /**
19  * Translatable version of JUnit's <code>Assert</code>.
20  */

21 public class Assert {
22   public static void assertEquals(boolean obj1, boolean obj2) {
23     assertEquals("", obj1, obj2);
24   }
25
26   public static void assertEquals(byte obj1, byte obj2) {
27     assertEquals("", obj1, obj2);
28   }
29
30   public static void assertEquals(char obj1, char obj2) {
31     assertEquals("", obj1, obj2);
32   }
33
34   public static void assertEquals(double obj1, double obj2, double delta) {
35     assertEquals("", obj1, obj2, delta);
36   }
37
38   public static void assertEquals(float obj1, float obj2, float delta) {
39     assertEquals("", obj1, obj2, delta);
40   }
41
42   public static void assertEquals(int expected, int actual) {
43     assertEquals("", expected, actual);
44   }
45
46   public static void assertEquals(long obj1, long obj2) {
47     assertEquals("", obj1, obj2);
48   }
49
50   public static void assertEquals(Object JavaDoc obj1, Object JavaDoc obj2) {
51     assertEquals("", obj1, obj2);
52   }
53
54   public static void assertEquals(String JavaDoc str, boolean obj1, boolean obj2) {
55     assertEquals(str, Boolean.valueOf(obj1), Boolean.valueOf(obj2));
56   }
57
58   public static void assertEquals(String JavaDoc str, byte obj1, byte obj2) {
59     assertEquals(str, new Byte JavaDoc(obj1), new Byte JavaDoc(obj2));
60   }
61
62   public static void assertEquals(String JavaDoc str, char obj1, char obj2) {
63     assertEquals(str, new Character JavaDoc(obj1), new Character JavaDoc(obj2));
64   }
65
66   public static void assertEquals(String JavaDoc str, double obj1, double obj2,
67       double delta) {
68     if (obj1 == obj2) {
69       return;
70     } else if (Math.abs(obj1 - obj2) <= delta) {
71       return;
72     } else {
73       fail(str + " expected=" + obj1 + " actual=" + obj2 + " delta=" + delta);
74     }
75   }
76
77   public static void assertEquals(String JavaDoc str, float obj1, float obj2,
78       float delta) {
79     if (obj1 == obj2) {
80       return;
81     } else if (Math.abs(obj1 - obj2) <= delta) {
82       return;
83     } else {
84       fail(str + " expected=" + obj1 + " actual=" + obj2 + " delta=" + delta);
85     }
86   }
87
88   public static void assertEquals(String JavaDoc msg, int expected, int actual) {
89     if (expected != actual) {
90       fail(msg + " expected=" + expected + " actual=" + actual);
91     }
92   }
93
94   public static void assertEquals(String JavaDoc str, long obj1, long obj2) {
95     assertEquals(str, new Long JavaDoc(obj1), new Long JavaDoc(obj2));
96   }
97
98   public static void assertEquals(String JavaDoc msg, Object JavaDoc obj1, Object JavaDoc obj2) {
99     if (obj1 == null && obj2 == null) {
100       return;
101     }
102
103     if (obj1 != null && obj1.equals(obj2)) {
104       return;
105     }
106
107     fail(msg + " expected=" + obj1 + " actual=" + obj2);
108   }
109
110   public static void assertFalse(boolean condition) {
111     assertFalse(null, condition);
112   }
113
114   public static void assertFalse(String JavaDoc message, boolean condition) {
115     assertTrue(message, !condition);
116   }
117
118   public static void assertNotNull(Object JavaDoc obj) {
119     assertNotNull(null, obj);
120   }
121
122   public static void assertNotNull(String JavaDoc msg, Object JavaDoc obj) {
123     assertTrue(msg, obj != null);
124   }
125
126   public static void assertNotSame(Object JavaDoc obj1, Object JavaDoc obj2) {
127     assertNotSame(null, obj1, obj2);
128   }
129
130   public static void assertNotSame(String JavaDoc msg, Object JavaDoc obj1, Object JavaDoc obj2) {
131     if (obj1 != obj2) {
132       return;
133     }
134
135     if (msg == null) {
136       msg = "";
137     }
138
139     fail(msg + " expected and actual match");
140   }
141
142   public static void assertNull(Object JavaDoc obj) {
143     assertNull(null, obj);
144   }
145
146   public static void assertNull(String JavaDoc msg, Object JavaDoc obj) {
147     assertTrue(msg, obj == null);
148   }
149
150   public static void assertSame(Object JavaDoc obj1, Object JavaDoc obj2) {
151     assertSame(null, obj1, obj2);
152   }
153
154   public static void assertSame(String JavaDoc msg, Object JavaDoc obj1, Object JavaDoc obj2) {
155     if (obj1 == obj2) {
156       return;
157     }
158
159     if (msg == null) {
160       msg = "";
161     }
162
163     fail(msg + " expected and actual do not match");
164   }
165
166   public static void assertTrue(boolean condition) {
167     assertTrue(null, condition);
168   }
169
170   public static void assertTrue(String JavaDoc message, boolean condition) {
171     if (!condition) {
172       fail(message);
173     }
174   }
175
176   public static void fail() {
177     fail(null);
178   }
179
180   public static void fail(String JavaDoc message) {
181     throw new AssertionFailedError(message);
182   }
183
184   /**
185    * Utility class, no public constructor needed.
186    */

187   protected Assert() {
188   }
189 }
190
Popular Tags