KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > TestDefense


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18 import org.apache.hivemind.util.Defense;
19
20 /**
21  * Test for {@link org.apache.hivemind.util.Defense}.
22  *
23  * @author Howard Lewis Ship
24  * @since 1.1
25  */

26 public class TestDefense extends HiveMindTestCase
27 {
28     public void testNotNull()
29     {
30         Defense.notNull("foo", "bar");
31     }
32
33     public void testIsNull()
34     {
35         try
36         {
37             Defense.notNull(null, "woops");
38             unreachable();
39         }
40         catch (NullPointerException JavaDoc ex)
41         {
42             assertEquals(HiveMindMessages.paramNotNull("woops"), ex.getMessage());
43         }
44     }
45
46     public void testCorrectType()
47     {
48         Defense.isAssignable("Hello", String JavaDoc.class, "param");
49     }
50
51     public void testIncorrectType()
52     {
53         try
54         {
55             Defense.isAssignable("Hello", Number JavaDoc.class, "param");
56             unreachable();
57         }
58         catch (ClassCastException JavaDoc ex)
59         {
60             assertEquals(
61                     "Parameter param is of type java.lang.String which is not compatible with java.lang.Number.",
62                     ex.getMessage());
63         }
64     }
65
66     public void testIncorrectTypeWithArrays()
67     {
68         try
69         {
70             Defense.isAssignable(new int[0], String JavaDoc[].class, "param");
71             unreachable();
72         }
73         catch (ClassCastException JavaDoc ex)
74         {
75             assertEquals(
76                     "Parameter param is of type int[] which is not compatible with java.lang.String[].",
77                     ex.getMessage());
78         }
79     }
80 }
Popular Tags