root/trunk/test/java/org/jcoderz/commons/util/AssertTest.java

Revision 1095, 5.0 kB (checked in by amandel, 4 years ago)

Add support for the comparable interface in the strong types Numeric oder RestrictedString?

  • 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 java.util.Calendar;
36import java.util.Date;
37import junit.framework.TestCase;
38import org.jcoderz.commons.ArgumentMalformedException;
39import org.jcoderz.commons.AssertionFailedException;
40import org.jcoderz.commons.RteLogMessage;
41
42/**
43 * Class to test the Assert utility class.
44 *
45 * @author Andreas Mandel
46 */
47public class AssertTest
48      extends TestCase
49{
50   private static final String BAR_STRING = "bar";
51   private static final String FOO_STRING = "foo";
52   private static final String PARAMETER_NAME = "parameter-name";
53
54   /** Test the not null method (positive). */
55   public final void testNotNullPositive ()
56   {
57      Assert.notNull("not-null-parameter", PARAMETER_NAME);
58   }
59
60   /** Test the not null method (negative). */
61   public final void testNotNullNegative ()
62   {
63      try
64      {
65         Assert.notNull(null, PARAMETER_NAME);
66         fail("Assertion should not pass.");
67      }
68      catch (ArgumentMalformedException ex)
69      {
70         assertEquals("Parameter name should be as given.", PARAMETER_NAME,
71               ex.getParameter(
72                  RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_NAME).get(0));
73         assertNull("Value must be null.",
74               ex.getParameter(
75                  RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_VALUE).get(
76                     0));
77      }
78   }
79
80   /** Test the not fail method. */
81   public final void testFail ()
82   {
83      try
84      {
85         Assert.fail(BAR_STRING);
86         fail("Assertion fail should not pass.");
87      }
88      catch (AssertionFailedException ex)
89      {
90         assertEquals("Parameter message should be as given.",
91             BAR_STRING,
92               ex.getParameter(
93                  RteLogMessage.AssertionFailed.PARAM_MESSAGE).get(0));
94      }
95   }
96
97   /**
98    * Tests the method {@link Assert#assertTrue(String, boolean)}.
99    */
100   public void testAssertTrue ()
101   {
102      Assert.assertTrue("true condition", true);
103      Assert.assertTrue(null, true);
104      assertTrueNegativeTest("false condition");
105      assertTrueNegativeTest(null);
106   }
107
108   /**
109    * Tests the method {@link Assert#assertEquals(String, Object, Object)}.
110    */
111   public void testAssertEqualsObjects ()
112   {
113      Assert.assertEquals("equal (null, null)", null, null);
114      Assert.assertEquals("equal (foo, foo)", FOO_STRING, FOO_STRING);
115      assertEqualsObjectsNegativeTest(FOO_STRING, BAR_STRING);
116      assertEqualsObjectsNegativeTest(FOO_STRING, null);
117      assertEqualsObjectsNegativeTest(null, BAR_STRING);
118      assertEqualsObjectsNegativeTest(new Date(), Calendar.getInstance());
119   }
120
121   /**
122    * Tests the method {@link Assert#assertEquals(String, int, int)}.
123    */
124   public void testAssertEqualsInts ()
125   {
126      Assert.assertEquals("equal", null, null);
127      Assert.assertEquals("equal", 1, 1);
128   }
129
130   private void assertEqualsObjectsNegativeTest (Object expected, Object actual)
131   {
132      try
133      {
134         Assert.assertEquals("not equal", expected, actual);
135         fail("Expected AssertionFailedException for objects "
136               + "that are not equal");
137      }
138      catch (AssertionFailedException x)
139      {
140         // expected
141      }
142   }
143
144   private void assertTrueNegativeTest (String message)
145   {
146      try
147      {
148         Assert.assertTrue(message, false);
149         fail("Expected AssertionFailedException for a condition "
150               + "that is false");
151      }
152      catch (AssertionFailedException x)
153      {
154         // expected
155      }
156   }
157}
Note: See TracBrowser for help on using the browser.