KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > lib > util > TestStrategyRegistry


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.lib.util;
16
17 import java.io.Serializable JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.hivemind.lib.util.StrategyRegistry;
22 import org.apache.hivemind.lib.util.StrategyRegistryImpl;
23 import org.apache.hivemind.test.HiveMindTestCase;
24
25 /**
26  * Tests the {@link org.apache.hivemind.lib.util.StrategyRegistryImpl} class.
27  *
28  * @author Howard Lewis Ship
29  * @since 1.1
30  */

31
32 public class TestStrategyRegistry extends HiveMindTestCase
33 {
34
35     private StrategyRegistry build()
36     {
37         StrategyRegistry result = new StrategyRegistryImpl();
38
39         result.register(Object JavaDoc.class, "OBJECT");
40         result.register(Object JavaDoc[].class, "OBJECT[]");
41         result.register(String JavaDoc.class, "STRING");
42         result.register(List JavaDoc.class, "LIST");
43         result.register(Map JavaDoc.class, "MAP");
44         result.register(Serializable JavaDoc.class, "SERIALIZABLE");
45         result.register(int[].class, "INT[]");
46         result.register(double.class, "DOUBLE");
47         result.register(Number JavaDoc[].class, "NUMBER[]");
48
49         return result;
50     }
51
52     private void expect(String JavaDoc expected, Class JavaDoc subjectClass)
53     {
54         Object JavaDoc actual = build().getStrategy(subjectClass);
55
56         assertEquals(expected, actual);
57     }
58
59     public void testDefaultMatch()
60     {
61         expect("OBJECT", TestStrategyRegistry.class);
62     }
63
64     public void testClassBeforeInterface()
65     {
66         expect("STRING", String JavaDoc.class);
67     }
68
69     public void testInterfaceMatch()
70     {
71         expect("SERIALIZABLE", Boolean JavaDoc.class);
72     }
73
74     public void testObjectArrayMatch()
75     {
76         expect("OBJECT[]", Object JavaDoc[].class);
77     }
78
79     public void testObjectSubclassArray()
80     {
81         expect("OBJECT[]", String JavaDoc[].class);
82     }
83
84     public void testRegisteredSubclassArray()
85     {
86         expect("NUMBER[]", Number JavaDoc[].class);
87     }
88
89     public void testScalarArrayMatch()
90     {
91         expect("INT[]", int[].class);
92     }
93
94     public void testScalarArrayDefault()
95     {
96         // This won't change, scalar arrays can't be cast to Object[].
97

98         expect("SERIALIZABLE", short[].class);
99     }
100
101     public void testScalar()
102     {
103         expect("DOUBLE", double.class);
104     }
105
106     public void testScalarDefault()
107     {
108         expect("OBJECT", float.class);
109     }
110
111     public void testSearchNoInterfaces()
112     {
113         expect("OBJECT", Object JavaDoc.class);
114     }
115
116     public void testNoMatch()
117     {
118         StrategyRegistry r = new StrategyRegistryImpl();
119
120         r.register(String JavaDoc.class, "STRING");
121
122         try
123         {
124             r.getStrategy(Boolean JavaDoc.class);
125
126             unreachable();
127         }
128         catch (IllegalArgumentException JavaDoc ex)
129         {
130             assertEquals(UtilMessages.strategyNotFound(Boolean JavaDoc.class), ex.getMessage());
131         }
132     }
133
134     public void testToString()
135     {
136         StrategyRegistry r = new StrategyRegistryImpl();
137
138         r.register(String JavaDoc.class, "STRING");
139
140         assertEquals("AdaptorRegistry[java.lang.String=STRING]", r.toString());
141     }
142
143     public void testDuplicateRegistration()
144     {
145         StrategyRegistry r = new StrategyRegistryImpl();
146
147         r.register(String JavaDoc.class, "STRING");
148
149         try
150         {
151             r.register(String JavaDoc.class, "STRING2");
152
153             unreachable();
154         }
155         catch (IllegalArgumentException JavaDoc ex)
156         {
157             assertEquals(UtilMessages.duplicateRegistration(String JavaDoc.class), ex.getMessage());
158         }
159     }
160 }
Popular Tags