001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.util;
016:
017: import static org.apache.tapestry.ioc.internal.util.Defense.cast;
018: import static org.apache.tapestry.ioc.internal.util.Defense.notBlank;
019: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
020:
021: import org.apache.tapestry.ioc.internal.util.Defense;
022: import org.apache.tapestry.ioc.test.TestBase;
023: import org.testng.annotations.Test;
024:
025: public class DefenseTest extends TestBase {
026:
027: /**
028: * Check that {@link Defense#notNull(T, String)} returns a non-null value.
029: */
030: @Test
031: public void parameter_not_null() {
032: assertSame(this , notNull(this , "foo"));
033: }
034:
035: /**
036: * Check that {@link Defense#notNull(T, String)} throws NPE when null, and check the message.
037: */
038:
039: @Test
040: public void parameter_is_null() {
041: try {
042: notNull(null, "foo");
043: unreachable();
044: } catch (IllegalArgumentException ex) {
045: assertEquals(ex.getMessage(), "Parameter foo was null.");
046: }
047: }
048:
049: @Test
050: public void non_blank_parameter_is_null() {
051: try {
052: notBlank(null, "bar");
053: unreachable();
054: } catch (IllegalArgumentException ex) {
055: assertEquals(ex.getMessage(),
056: "Parameter bar was null or contained only whitespace.");
057: }
058: }
059:
060: @Test
061: public void non_blank_parameter_is_only_whitespace() {
062: try {
063: notBlank(" \t\n", "baz");
064: unreachable();
065: } catch (IllegalArgumentException ex) {
066: assertEquals(ex.getMessage(),
067: "Parameter baz was null or contained only whitespace.");
068: }
069: }
070:
071: @Test
072: public void non_blank_parameter_is_valid() {
073: assertEquals("fred", notBlank(" fred\n", "biff"));
074: }
075:
076: @Test
077: public void cast_is_also_not_null() {
078: try {
079: cast(null, String.class, "fred");
080: unreachable();
081: } catch (IllegalArgumentException ex) {
082: assertEquals(ex.getMessage(), "Parameter fred was null.");
083: }
084: }
085:
086: @Test
087: public void succesful_cast() {
088: StringBuffer b = new StringBuffer();
089:
090: Appendable a = cast(b, Appendable.class, "fred");
091:
092: assertSame(a, b);
093: }
094:
095: @Test
096: public void bad_cast() {
097: StringBuffer b = new StringBuffer("fred-value");
098:
099: try {
100: cast(b, String.class, "fred");
101: unreachable();
102: } catch (IllegalArgumentException ex) {
103: assertEquals(ex.getMessage(),
104: "Parameter fred (fred-value) is not assignable to type java.lang.String.");
105: }
106: }
107: }
|