Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.AssertTest

LineHitsNoteSource
1  /*
2   * $Id: AssertTest.java 1095 2008-07-24 09:29:24Z amandel $
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   */
33  package org.jcoderz.commons.util;
34  
35  import java.util.Calendar;
36  import java.util.Date;
37  import junit.framework.TestCase;
38  import org.jcoderz.commons.ArgumentMalformedException;
39  import org.jcoderz.commons.AssertionFailedException;
40  import org.jcoderz.commons.RteLogMessage;
41  
42  /**
43   * Class to test the Assert utility class.
44   *
45   * @author Andreas Mandel
46   */
47100 public 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     {
57100       Assert.notNull("not-null-parameter", PARAMETER_NAME);
58100    }
59  
60     /** Test the not null method (negative). */
61     public final void testNotNullNegative ()
62     {
63        try
64        {
650          Assert.notNull(null, PARAMETER_NAME);
660          fail("Assertion should not pass.");
67        }
68100       catch (ArgumentMalformedException ex)
69        {
70100          assertEquals("Parameter name should be as given.", PARAMETER_NAME,
71                 ex.getParameter(
72                    RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_NAME).get(0));
73100          assertNull("Value must be null.",
74                 ex.getParameter(
75                    RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_VALUE).get(
76                       0));
770       }
78100    }
79  
80     /** Test the not fail method. */
81     public final void testFail ()
82     {
83        try
84        {
850          Assert.fail(BAR_STRING);
860          fail("Assertion fail should not pass.");
87        }
88100       catch (AssertionFailedException ex)
89        {
90100          assertEquals("Parameter message should be as given.",
91               BAR_STRING,
92                 ex.getParameter(
93                    RteLogMessage.AssertionFailed.PARAM_MESSAGE).get(0));
940       }
95100    }
96  
97     /**
98      * Tests the method {@link Assert#assertTrue(String, boolean)}.
99      */
100     public void testAssertTrue ()
101     {
102100       Assert.assertTrue("true condition", true);
103100       Assert.assertTrue(null, true);
104100       assertTrueNegativeTest("false condition");
105100       assertTrueNegativeTest(null);
106100    }
107  
108     /**
109      * Tests the method {@link Assert#assertEquals(String, Object, Object)}.
110      */
111     public void testAssertEqualsObjects ()
112     {
113100       Assert.assertEquals("equal (null, null)", null, null);
114100       Assert.assertEquals("equal (foo, foo)", FOO_STRING, FOO_STRING);
115100       assertEqualsObjectsNegativeTest(FOO_STRING, BAR_STRING);
116100       assertEqualsObjectsNegativeTest(FOO_STRING, null);
117100       assertEqualsObjectsNegativeTest(null, BAR_STRING);
118100       assertEqualsObjectsNegativeTest(new Date(), Calendar.getInstance());
119100    }
120  
121     /**
122      * Tests the method {@link Assert#assertEquals(String, int, int)}.
123      */
124     public void testAssertEqualsInts ()
125     {
126100       Assert.assertEquals("equal", null, null);
127100       Assert.assertEquals("equal", 1, 1);
128100    }
129  
130     private void assertEqualsObjectsNegativeTest (Object expected, Object actual)
131     {
132        try
133        {
1340          Assert.assertEquals("not equal", expected, actual);
1350          fail("Expected AssertionFailedException for objects "
136                 + "that are not equal");
137        }
138100       catch (AssertionFailedException x)
139        {
140           // expected
1410       }
142100    }
143  
144     private void assertTrueNegativeTest (String message)
145     {
146        try
147        {
1480          Assert.assertTrue(message, false);
1490          fail("Expected AssertionFailedException for a condition "
150                 + "that is false");
151        }
152100       catch (AssertionFailedException x)
153        {
154           // expected
1550       }
156100    }
157  }

Findings in this File