| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 44 | | | |
| 45 | | | @author |
| 46 | | | |
| 47 | 100 | | 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 | | | |
| 55 | | | public final void testNotNullPositive () |
| 56 | | | { |
| 57 | 100 | | Assert.notNull("not-null-parameter", PARAMETER_NAME); |
| 58 | 100 | | } |
| 59 | | | |
| 60 | | | |
| 61 | | | public final void testNotNullNegative () |
| 62 | | | { |
| 63 | | | try |
| 64 | | | { |
| 65 | 0 | | Assert.notNull(null, PARAMETER_NAME); |
| 66 | 0 | | fail("Assertion should not pass."); |
| 67 | | | } |
| 68 | 100 | | catch (ArgumentMalformedException ex) |
| 69 | | | { |
| 70 | 100 | | assertEquals("Parameter name should be as given.", PARAMETER_NAME, |
| 71 | | | ex.getParameter( |
| 72 | | | RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_NAME).get(0)); |
| 73 | 100 | | assertNull("Value must be null.", |
| 74 | | | ex.getParameter( |
| 75 | | | RteLogMessage.ArgumentMalformed.PARAM_ARGUMENT_VALUE).get( |
| 76 | | | 0)); |
| 77 | 0 | | } |
| 78 | 100 | | } |
| 79 | | | |
| 80 | | | |
| 81 | | | public final void testFail () |
| 82 | | | { |
| 83 | | | try |
| 84 | | | { |
| 85 | 0 | | Assert.fail(BAR_STRING); |
| 86 | 0 | | fail("Assertion fail should not pass."); |
| 87 | | | } |
| 88 | 100 | | catch (AssertionFailedException ex) |
| 89 | | | { |
| 90 | 100 | | assertEquals("Parameter message should be as given.", |
| 91 | | | BAR_STRING, |
| 92 | | | ex.getParameter( |
| 93 | | | RteLogMessage.AssertionFailed.PARAM_MESSAGE).get(0)); |
| 94 | 0 | | } |
| 95 | 100 | | } |
| 96 | | | |
| 97 | | | |
| 98 | | | {@link } |
| 99 | | | |
| 100 | | | public void testAssertTrue () |
| 101 | | | { |
| 102 | 100 | | Assert.assertTrue("true condition", true); |
| 103 | 100 | | Assert.assertTrue(null, true); |
| 104 | 100 | | assertTrueNegativeTest("false condition"); |
| 105 | 100 | | assertTrueNegativeTest(null); |
| 106 | 100 | | } |
| 107 | | | |
| 108 | | | |
| 109 | | | {@link } |
| 110 | | | |
| 111 | | | public void testAssertEqualsObjects () |
| 112 | | | { |
| 113 | 100 | | Assert.assertEquals("equal (null, null)", null, null); |
| 114 | 100 | | Assert.assertEquals("equal (foo, foo)", FOO_STRING, FOO_STRING); |
| 115 | 100 | | assertEqualsObjectsNegativeTest(FOO_STRING, BAR_STRING); |
| 116 | 100 | | assertEqualsObjectsNegativeTest(FOO_STRING, null); |
| 117 | 100 | | assertEqualsObjectsNegativeTest(null, BAR_STRING); |
| 118 | 100 | | assertEqualsObjectsNegativeTest(new Date(), Calendar.getInstance()); |
| 119 | 100 | | } |
| 120 | | | |
| 121 | | | |
| 122 | | | {@link } |
| 123 | | | |
| 124 | | | public void testAssertEqualsInts () |
| 125 | | | { |
| 126 | 100 | | Assert.assertEquals("equal", null, null); |
| 127 | 100 | | Assert.assertEquals("equal", 1, 1); |
| 128 | 100 | | } |
| 129 | | | |
| 130 | | | private void assertEqualsObjectsNegativeTest (Object expected, Object actual) |
| 131 | | | { |
| 132 | | | try |
| 133 | | | { |
| 134 | 0 | | Assert.assertEquals("not equal", expected, actual); |
| 135 | 0 | | fail("Expected AssertionFailedException for objects " |
| 136 | | | + "that are not equal"); |
| 137 | | | } |
| 138 | 100 | | catch (AssertionFailedException x) |
| 139 | | | { |
| 140 | | | |
| 141 | 0 | | } |
| 142 | 100 | | } |
| 143 | | | |
| 144 | | | private void assertTrueNegativeTest (String message) |
| 145 | | | { |
| 146 | | | try |
| 147 | | | { |
| 148 | 0 | | Assert.assertTrue(message, false); |
| 149 | 0 | | fail("Expected AssertionFailedException for a condition " |
| 150 | | | + "that is false"); |
| 151 | | | } |
| 152 | 100 | | catch (AssertionFailedException x) |
| 153 | | | { |
| 154 | | | |
| 155 | 0 | | } |
| 156 | 100 | | } |
| 157 | | | } |