View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose;
17  
18  /***
19   * @author jmccrindle
20   */
21  public class Assert {
22  
23      public static final String DEFAULT_MESSAGE = "Assertion Failed";
24  
25      /***
26       * Private constructor as all methods are static
27       */
28      private Assert() {
29          
30      }
31  
32      /***
33       * Assert that a test is true
34       * @param msg a message if the assertion if false
35       * @param test the test to test
36       */
37      public static void assertTrue(String msg, boolean test) {
38          if (!test) {
39              throw new DiagnosticAssertionException(msg);
40          }
41      }
42  
43      /***
44       * Assert that a test is true
45       * @param test the test to test
46       */
47      public static void assertTrue(boolean test) {
48          if (!test) {
49              throw new DiagnosticAssertionException(DEFAULT_MESSAGE);
50          }
51      }
52  
53      /***
54       * Assert that two objects are equal
55       * @param msg a message if the assertion if false
56       * @param toTestAgainst the object to test against
57       * @param test the test object
58       */
59      public static void assertEquals(String msg, Object toTestAgainst, Object test) {
60          if (toTestAgainst == null) {
61              assertNull(msg, test);
62          }
63          assertTrue(msg, toTestAgainst.equals(test));
64      }
65  
66      /***
67       * Assert that two objects are equal
68       * @param toTestAgainst the object to test against
69       * @param test the test object
70       */
71      public static void assertEquals(Object toTestAgainst, Object test) {
72          assertEquals(toTestAgainst + "!=" + test, toTestAgainst, test);
73      }
74  
75      /***
76       * Assert that an object is null
77       * @param msg a message if the assertion if false
78       * @param test the test object
79       */
80      public static void assertNull(String msg, Object test) {
81          assertTrue(msg, test == null);
82      }
83  
84      /***
85       * Assert that an object is null
86       * @param test the test object
87       */
88      public static void assertNull(Object test) {
89          assertTrue("should be null but is " + test, test == null);
90      }
91  
92      /***
93       * Assert that an object is not null
94       * @param msg a message if the assertion if false
95       * @param test the test object
96       */
97      public static void assertNotNull(String msg, Object test) {
98          assertTrue(msg, test != null);
99      }
100 
101     /***
102      * Assert that an object is not null
103      * @param test the test object
104      */
105     public static void assertNotNull(Object test) {
106         assertTrue("should not be null but is", test == null);
107     }
108     
109 }