001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.core.util;
017:
018: import java.util.IllegalFormatException;
019:
020: import junit.framework.TestCase;
021:
022: /**
023: * This class tests {@link AssertionUtils}.
024: */
025: public class AssertionUtilsTest extends TestCase {
026: private static final String BAD_FORMAT = "%q";
027:
028: public void testBooleanUnboxing() {
029: AssertionUtils.assertThat(Boolean.TRUE);
030: try {
031: AssertionUtils.assertThat(Boolean.FALSE);
032: fail("didn't throw AssertionError");
033: } catch (AssertionError e) {
034: // good
035: }
036: }
037:
038: public void testBoxingMessage() {
039: try {
040: AssertionUtils.assertThat(false, 42);
041: fail("didn't throw AssertionError");
042: } catch (AssertionError e) {
043: // good
044: assertEquals("42", e.getMessage());
045: }
046: }
047:
048: public void testFormatting() {
049: try {
050: AssertionUtils.assertThat(false, "and %s", "foo");
051: fail("didn't throw AssertionError");
052: } catch (AssertionError e) {
053: // good
054: assertEquals("and foo", e.getMessage());
055: }
056: }
057:
058: public void testBadFormatting() {
059: try {
060: AssertionUtils.assertThat(false, BAD_FORMAT, "foo");
061: fail("didn't throw IllegalFormatException");
062: } catch (IllegalFormatException e) {
063: // good
064: }
065: }
066:
067: public void testNoArgsNoFormatting() {
068: try {
069: AssertionUtils.assertThat(false, BAD_FORMAT);
070: fail("didn't throw AssertionError");
071: } catch (AssertionError e) {
072: // good
073: assertEquals(BAD_FORMAT, e.getMessage());
074: }
075: }
076:
077: public void testVarArgsFormatting() {
078: try {
079: AssertionUtils.assertThat(false, "and %s %s", "foo", "bar");
080: fail("didn't throw AssertionError");
081: } catch (AssertionError e) {
082: // good
083: assertEquals("and foo bar", e.getMessage());
084: }
085: }
086:
087: public void testNullDetail() {
088: try {
089: AssertionUtils.assertThat(false, null);
090: fail("didn't throw AssertionError");
091: } catch (AssertionError e) {
092: // good
093: assertEquals("null", e.getMessage());
094: }
095: }
096:
097: public void testNullDetailPrintf() {
098: try {
099: AssertionUtils.assertThat(false, null, "foo");
100: fail("didn't throw NullPointerException");
101: } catch (NullPointerException e) {
102: // good
103: }
104: }
105: }
|