KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > config > TestConfigurationPoint


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 hivemind.test.config;
16
17 import hivemind.test.config.impl.BooleanHolder;
18 import hivemind.test.config.impl.Child;
19 import hivemind.test.config.impl.Datum;
20 import hivemind.test.config.impl.DatumHolder;
21 import hivemind.test.config.impl.FrobableHolder;
22 import hivemind.test.config.impl.IntHolder;
23 import hivemind.test.config.impl.Parent;
24 import hivemind.test.config.impl.ResourceHolder;
25
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import org.apache.hivemind.ApplicationRuntimeException;
30 import org.apache.hivemind.Element;
31 import org.apache.hivemind.Registry;
32 import org.apache.hivemind.Resource;
33 import org.apache.hivemind.impl.RegistryBuilder;
34 import org.apache.hivemind.impl.XmlModuleReader;
35 import org.apache.hivemind.util.ClasspathResource;
36 import org.apache.hivemind.xml.XmlTestCase;
37
38 /**
39  * A number of tests related to processing of extension points.
40  *
41  * @author Howard Lewis Ship
42  */

43 public class TestConfigurationPoint extends XmlTestCase
44 {
45
46     public void testEmpty() throws Exception JavaDoc
47     {
48         Registry r = buildFrameworkRegistry("Empty.xml");
49
50         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Empty");
51
52         assertEquals(0, l.size());
53     }
54
55     public void testSimple() throws Exception JavaDoc
56     {
57         Registry r = buildFrameworkRegistry("Simple.xml");
58
59         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Simple");
60
61         assertEquals(2, l.size());
62
63         Datum d = (Datum) l.get(0);
64
65         assertEquals("key1", d.getKey());
66         assertEquals("value1", d.getValue());
67         assertNotNull(d.getLocation());
68
69         d = (Datum) l.get(1);
70
71         assertEquals("key2", d.getKey());
72         assertEquals("value2", d.getValue());
73     }
74
75     public void testNullElement() throws Exception JavaDoc
76     {
77         Registry r = buildFrameworkRegistry("Null.xml");
78
79         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Null");
80
81         assertEquals(1, l.size());
82
83         assertNull(l.get(0));
84     }
85
86     public void testAttributeDefaults() throws Exception JavaDoc
87     {
88         Registry r = buildFrameworkRegistry("AttributeDefaults.xml");
89
90         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.AttributeDefaults");
91
92         assertEquals(1, l.size());
93         Datum d = (Datum) l.get(0);
94
95         assertEquals("DEFAULT_KEY", d.getKey());
96         assertNull(d.getValue());
97     }
98
99     public void testNested() throws Exception JavaDoc
100     {
101         Registry r = buildFrameworkRegistry("Nested.xml");
102
103         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Nested");
104
105         assertEquals(1, l.size());
106
107         DatumHolder h = (DatumHolder) l.get(0);
108
109         assertListsEqual(new Object JavaDoc[]
110         { "fred", "wilma" }, h.getKeys());
111
112         Datum d = h.getDatum("fred");
113         assertNotNull(d.getLocation());
114         assertEquals("barney", d.getValue());
115     }
116
117     public void testStructured() throws Exception JavaDoc
118     {
119         Registry r = buildFrameworkRegistry("Structured.xml");
120
121         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Structured");
122
123         assertEquals(2, l.size());
124
125         Datum d = (Datum) l.get(0);
126
127         assertEquals("key_1", d.getKey());
128         assertEquals("value_1", d.getValue());
129         assertNotNull(d.getLocation());
130
131         d = (Datum) l.get(1);
132
133         assertEquals("key_2", d.getKey());
134         assertEquals("value_2", d.getValue());
135     }
136
137     public void testSetParent() throws Exception JavaDoc
138     {
139         Registry r = buildFrameworkRegistry("SetParent.xml");
140
141         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.SetParent");
142
143         assertEquals(1, l.size());
144
145         Parent p1 = (Parent) l.get(0);
146
147         assertEquals("key1", p1.getKey());
148         assertEquals("value1", p1.getValue());
149
150         l = p1.getChildren();
151         assertEquals(2, l.size());
152
153         Child c1 = (Child) l.get(0);
154
155         assertSame(p1, c1.getParent());
156
157         assertEquals("detailkey1", c1.getKey());
158         assertEquals("detailvalue1", c1.getValue());
159
160         Child c2 = (Child) l.get(1);
161
162         assertSame(p1, c2.getParent());
163
164         assertEquals("detailkey2", c2.getKey());
165         assertEquals("detailvalue2", c2.getValue());
166     }
167
168     public void testBooleanTranslator() throws Exception JavaDoc
169     {
170         interceptLogging();
171
172         Registry r = buildFrameworkRegistry("BooleanTranslator.xml");
173
174         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.BooleanTranslator");
175
176         // Trigger the proxy
177

178         l.size();
179
180         assertLoggedMessagePattern("Unable to process attribute value \\(of element flag\\): "
181                 + "'maybe' is not a boolean value \\(which should be either 'true' or 'false'\\)\\.");
182
183         assertEquals(3, l.size());
184
185         BooleanHolder h = (BooleanHolder) l.get(0);
186
187         assertEquals(true, h.getValue());
188
189         h = (BooleanHolder) l.get(1);
190         assertEquals(false, h.getValue());
191
192         h = (BooleanHolder) l.get(2);
193         assertEquals(false, h.getValue());
194     }
195
196     public void testIntTranslator() throws Exception JavaDoc
197     {
198         interceptLogging();
199
200         Registry r = buildFrameworkRegistry("IntTranslator.xml");
201
202         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.IntTranslator");
203
204         // Convert the proxy into a real list.
205

206         l.size();
207
208         List JavaDoc events = getInterceptedLogEvents();
209
210         assertLoggedMessagePattern("Unable to process attribute value \\(of element int\\): "
211                 + "Value 2 is less than minimum value 5\\.", events);
212         assertLoggedMessagePattern("Value 12 is greater than maximum value 10\\.", events);
213         assertLoggedMessagePattern("'fred' is not an integer value\\.", events);
214
215         assertEquals(5, l.size());
216
217         IntHolder h = (IntHolder) l.get(0);
218
219         assertEquals(7, h.getValue());
220
221         h = (IntHolder) l.get(1);
222         assertEquals(0, h.getValue());
223
224         h = (IntHolder) l.get(2);
225
226         assertEquals(0, h.getValue());
227
228         h = (IntHolder) l.get(3);
229
230         assertEquals(0, h.getValue());
231
232         h = (IntHolder) l.get(3);
233
234         assertEquals(0, h.getValue());
235
236     }
237
238     public void testInstanceTranslator() throws Exception JavaDoc
239     {
240         Registry r = buildFrameworkRegistry("InstanceTranslator.xml");
241
242         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.InstanceTranslator");
243
244         assertEquals(1, l.size());
245
246         FrobableHolder h = (FrobableHolder) l.get(0);
247
248         Frobable f = h.getFrobable();
249
250         assertNotNull(f);
251         assertEquals(true, f.frob());
252     }
253
254     public void testSymbols() throws Exception JavaDoc
255     {
256         interceptLogging();
257
258         Registry r = buildFrameworkRegistry("Symbols.xml");
259
260         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Symbols");
261
262         assertEquals(3, l.size());
263
264         Datum d = (Datum) l.get(0);
265
266         assertEquals("wife", d.getKey());
267         assertEquals("wilma", d.getValue());
268
269         d = (Datum) l.get(1);
270
271         assertEquals("husband", d.getKey());
272         assertEquals("fred", d.getValue());
273
274         d = (Datum) l.get(2);
275
276         assertEquals("work", d.getKey());
277         assertEquals("${work}", d.getValue());
278
279         assertLoggedMessagePattern("No value available for symbol 'work'");
280     }
281
282     public void testNoSchema() throws Exception JavaDoc
283     {
284         Registry r = buildFrameworkRegistry("NoSchema.xml");
285
286         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.NoSchema");
287
288         assertEquals(2, l.size());
289
290         Element e = (Element) l.get(0);
291         assertEquals("datum", e.getElementName());
292         assertEquals("key1", e.getAttributeValue("key"));
293         assertEquals("value1", e.getAttributeValue("value"));
294
295         // Show that symbols are NOT expanded in non-schema
296
// contributions.
297

298         e = (Element) l.get(1);
299         assertEquals("datum", e.getElementName());
300         assertEquals("key2", e.getAttributeValue("key"));
301         assertEquals("${value2}", e.getAttributeValue("value"));
302     }
303
304     public void testLocalized() throws Exception JavaDoc
305     {
306         Registry r = buildFrameworkRegistry("Localized.xml");
307
308         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Localized");
309         assertEquals(1, l.size());
310
311         Datum d = (Datum) l.get(0);
312
313         assertEquals("message", d.getKey());
314         assertEquals("Some Damn Thing", d.getValue());
315     }
316
317     public void testElementsProxyList() throws Exception JavaDoc
318     {
319         Registry r = buildFrameworkRegistry("Simple.xml");
320
321         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Simple");
322
323         assertEquals("<InnerProxy for hivemind.test.config.Simple(java.util.List)>", l.toString());
324
325         assertEquals(true, l.equals(l));
326         assertEquals(false, l.equals(null));
327
328         assertEquals(2, l.size());
329
330         List JavaDoc l2 = (List JavaDoc) r.getConfiguration("hivemind.test.config.Simple");
331
332         assertNotSame(l, l2);
333         assertEquals(l, l2);
334
335         assertEquals(l2.toString(), l.toString());
336     }
337
338     public void testTooFew() throws Exception JavaDoc
339     {
340
341         interceptLogging(RegistryBuilder.class.getName());
342
343         Registry r = buildFrameworkRegistry("TooFew.xml");
344
345         r.getConfiguration("hivemind.test.config.TooFew");
346
347         assertLoggedMessage("Configuration point hivemind.test.config.TooFew contains no contributions but expects at least one contribution.");
348
349     }
350
351     public void testTooMany() throws Exception JavaDoc
352     {
353         interceptLogging(RegistryBuilder.class.getName());
354
355         Registry r = buildFrameworkRegistry("TooMany.xml");
356
357         r.getConfiguration("hivemind.test.config.TooMany");
358
359         assertLoggedMessage("Configuration point hivemind.test.config.TooMany contains 2 contributions but expects an optional contribution.");
360     }
361
362     public void testBadAttributes() throws Exception JavaDoc
363     {
364         Registry r = buildFrameworkRegistry("BadAttributes.xml");
365
366         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.BadAttributes");
367         try
368         {
369             l.size();
370
371             unreachable();
372         }
373         catch (ApplicationRuntimeException ex)
374         {
375             Throwable JavaDoc t = findNestedException(ex);
376             assertExceptionSubstring(t, "Element datum (at");
377             assertExceptionSubstring(
378                     t,
379                     "Attribute 'xey' is not defined in the schema. Attribute 'key' is required but no value was provided.");
380         }
381     }
382
383     public void testBadElement() throws Exception JavaDoc
384     {
385         Registry r = buildFrameworkRegistry("BadElement.xml");
386
387         interceptLogging("hivemind.test.config");
388
389         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.BadElement");
390
391         assertEquals(1, l.size());
392
393         assertLoggedMessagePattern("Error at .*?: Element xatum is not allowed here\\.");
394     }
395
396     public void testCustomRule() throws Exception JavaDoc
397     {
398         Registry r = buildFrameworkRegistry("CustomRule.xml");
399
400         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.CustomRule");
401
402         Datum d = (Datum) l.get(0);
403
404         // Put this check second, just to get some code coverage
405
// on ElementsInnerProxyList
406

407         assertEquals(2, l.size());
408
409         assertEquals("hivemind.test.config", d.getContributingModule().getModuleId());
410     }
411
412     public void testCustomRuleFailure() throws Exception JavaDoc
413     {
414         try
415         {
416             parse("CustomRuleFailure.xml");
417
418             unreachable();
419         }
420         catch (ApplicationRuntimeException ex)
421         {
422             assertExceptionSubstring(
423                     ex,
424                     "Unable to create instance of Rule class hivemind.test.config.XetContributingModuleRule");
425         }
426     }
427
428     public void testResourceTranslator() throws Exception JavaDoc
429     {
430         RegistryBuilder builder = new RegistryBuilder();
431         builder.autoDetectModules();
432
433         Resource moduleResource = new ClasspathResource(_resolver,
434                 "/hivemind/test/config/ResourceTranslator.xml");
435
436         XmlModuleReader reader = new XmlModuleReader(builder.getRegistryDefinition(),
437                 _resolver, builder.getErrorHandler());
438         reader.readModule(moduleResource);
439
440         Registry r = builder.constructRegistry(Locale.FRENCH);
441         
442         interceptLogging();
443
444         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.ResourceTranslator");
445
446         assertEquals(4, l.size());
447
448         ResourceHolder h = (ResourceHolder) l.get(0);
449
450         assertEquals(moduleResource.getRelativeResource("Empty.xml"), h.getResource());
451
452         h = (ResourceHolder) l.get(1);
453
454         assertEquals(moduleResource.getRelativeResource("Localized_fr.properties"), h.getResource());
455
456         h = (ResourceHolder) l.get(2);
457         assertNull(h.getResource());
458
459         h = (ResourceHolder) l.get(3);
460         assertNull(h.getResource());
461
462         assertLoggedMessagePattern("Unable to process content of element resource: "
463                 + "Unable to localize resource DoesNotExist\\.xml for module hivemind\\.test\\.config\\.");
464     }
465
466     public void testShutdown() throws Exception JavaDoc
467     {
468         Registry r = buildFrameworkRegistry("Simple.xml");
469
470         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.config.Simple");
471
472         assertEquals(2, l.size());
473
474         r.shutdown();
475
476         try
477         {
478             l.size();
479         }
480         catch (ApplicationRuntimeException ex)
481         {
482             assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown.");
483         }
484
485     }
486
487     /**
488      * Test for contribution to unknown configuration extension point.
489      */

490
491     public void testUnknownContribution() throws Exception JavaDoc
492     {
493         interceptLogging();
494
495         buildFrameworkRegistry("UnknownContribution.xml");
496
497         assertLoggedMessagePattern("Module hivemind\\.test\\.config has contributed to unknown configuration point UnresolvedSchema\\. "
498                 + "The contribution has been ignored\\.");
499
500     }
501 }
Popular Tags