root/trunk/src/java/org/jcoderz/commons/util/Assert.java

Revision 1171, 5.6 kB (checked in by amandel, 2 months ago)

New assert fail that tales a cause exception.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.util;
34
35import org.jcoderz.commons.ArgumentMalformedException;
36import org.jcoderz.commons.AssertionFailedException;
37
38/**
39 * Utility class for assertions.
40 *
41 * @author Andreas Mandel
42 */
43public final class Assert
44{
45   /** Private constructor to avoid instantiation of utility class. */
46   private Assert ()
47   {
48      // utility class -- no instances are allowed.
49   }
50
51   /**
52    * Asserts that an object isn't null.
53    * If it is a null reference an ArgumentMalformedException is
54    * thrown with a appropriate message.
55    *
56    * @param parameter object to be tested against null.
57    * @param argumentName name of the provided argument within the
58    *        used interface.
59    */
60   public static void notNull (Object parameter, String argumentName)
61   {
62      if (parameter == null)
63      {
64         throw new ArgumentMalformedException(argumentName, null,
65               "Argument " + argumentName + " must not be null");
66      }
67   }
68
69   /**
70    * Asserts that two integers are equal. If they are not
71    * an AssertionFailedException is thrown with the given message.
72    * @param message The message for the condition. This message is
73    *       used in the exception if the integers are not equal.
74    * @param expected the expected integer.
75    * @param actual the actual integer.
76    * @throws AssertionFailedException if the two objects are not equal.
77    */
78   public static void assertEquals (String message, int expected, int actual)
79         throws AssertionFailedException
80   {
81      assertEquals(message, new Integer(expected), new Integer(actual));
82   }
83
84   /**
85    * Asserts that two objects are equal. If they are not
86    * an AssertionFailedException is thrown with the given message.
87    * @param message The message for the condition. This message is
88    *       used in the exception if the objects are not equal.
89    * @param expected the expected object.
90    * @param actual the actual object.
91    * @throws AssertionFailedException if the two objects are not equal.
92    */
93   public static void assertEquals (String message, Object expected,
94         Object actual)
95         throws AssertionFailedException
96   {
97      if (!ObjectUtil.equals(expected, actual))
98      {
99         String newMessage = "";
100         if (message != null)
101         {
102            newMessage = message + " ";
103         }
104         throw new AssertionFailedException(newMessage + "expected:<"
105               + expected + "> but was:<" + actual + ">");
106      }
107   }
108
109   /**
110    * Asserts that a condition is <tt>true</tt>. If it isn't it throws
111    * an AssertionFailedException with the given message.
112    *
113    * @param message The message for the condition. This message is
114    *       used in the exception if the condition is <tt>false</tt>.
115    * @param condition the condition to test.
116    * @throws AssertionFailedException if the condition is <tt>false</tt>.
117    */
118   public static void assertTrue (String message, boolean condition)
119         throws AssertionFailedException
120   {
121      if (!condition)
122      {
123         throw new AssertionFailedException(message);
124      }
125   }
126
127   /**
128    * Can be called if an assertion already failed. This can be used at
129    * code positions that should never be reached at all. It throws
130    * an AssertionFailedException with the given message.
131    *
132    * @param message The message to be used in the exception.
133    * @throws AssertionFailedException always.
134    */
135   public static void fail (String message)
136         throws AssertionFailedException
137   {
138      throw new AssertionFailedException(message);
139   }
140
141   /**
142    * Can be called if an exception is unexpectedly caught. This can
143    * be used at catch blocks that should never be reached at all.
144    * It throws an AssertionFailedException with the given nested
145    * exception.
146    *
147    * @param message The message to be used in the exception.
148    * @param ex the exception that was not expected
149    * @throws AssertionFailedException always.
150    */
151   public static void fail (String message, Throwable ex)
152       throws AssertionFailedException
153   {
154     throw new AssertionFailedException(message, ex);
155   }
156}
Note: See TracBrowser for help on using the browser.