001: // Copyright 2004, 2005 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 hivemind.test.config;
016:
017: import hivemind.test.FrameworkTestCase;
018: import hivemind.test.config.impl.BooleanHolder;
019: import hivemind.test.config.impl.Child;
020: import hivemind.test.config.impl.Datum;
021: import hivemind.test.config.impl.DatumHolder;
022: import hivemind.test.config.impl.FrobableHolder;
023: import hivemind.test.config.impl.IntHolder;
024: import hivemind.test.config.impl.Parent;
025: import hivemind.test.config.impl.ResourceHolder;
026:
027: import java.util.List;
028: import java.util.Locale;
029:
030: import org.apache.hivemind.ApplicationRuntimeException;
031: import org.apache.hivemind.Element;
032: import org.apache.hivemind.Registry;
033: import org.apache.hivemind.Resource;
034: import org.apache.hivemind.impl.RegistryBuilder;
035: import org.apache.hivemind.impl.XmlModuleDescriptorProvider;
036: import org.apache.hivemind.util.ClasspathResource;
037:
038: /**
039: * A number of tests related to processing of extension points.
040: *
041: * @author Howard Lewis Ship
042: */
043: public class TestConfigurationPoint extends FrameworkTestCase {
044:
045: public void testEmpty() throws Exception {
046: Registry r = buildFrameworkRegistry("Empty.xml");
047:
048: List l = r.getConfiguration("hivemind.test.config.Empty");
049:
050: assertEquals(0, l.size());
051: }
052:
053: public void testSimple() throws Exception {
054: Registry r = buildFrameworkRegistry("Simple.xml");
055:
056: List l = r.getConfiguration("hivemind.test.config.Simple");
057:
058: assertEquals(2, l.size());
059:
060: Datum d = (Datum) l.get(0);
061:
062: assertEquals("key1", d.getKey());
063: assertEquals("value1", d.getValue());
064: assertNotNull(d.getLocation());
065:
066: d = (Datum) l.get(1);
067:
068: assertEquals("key2", d.getKey());
069: assertEquals("value2", d.getValue());
070: }
071:
072: public void testNullElement() throws Exception {
073: Registry r = buildFrameworkRegistry("Null.xml");
074:
075: List l = r.getConfiguration("hivemind.test.config.Null");
076:
077: assertEquals(1, l.size());
078:
079: assertNull(l.get(0));
080: }
081:
082: public void testAttributeDefaults() throws Exception {
083: Registry r = buildFrameworkRegistry("AttributeDefaults.xml");
084:
085: List l = r
086: .getConfiguration("hivemind.test.config.AttributeDefaults");
087:
088: assertEquals(1, l.size());
089: Datum d = (Datum) l.get(0);
090:
091: assertEquals("DEFAULT_KEY", d.getKey());
092: assertNull(d.getValue());
093: }
094:
095: public void testNested() throws Exception {
096: Registry r = buildFrameworkRegistry("Nested.xml");
097:
098: List l = r.getConfiguration("hivemind.test.config.Nested");
099:
100: assertEquals(1, l.size());
101:
102: DatumHolder h = (DatumHolder) l.get(0);
103:
104: assertListsEqual(new Object[] { "fred", "wilma" }, h.getKeys());
105:
106: Datum d = h.getDatum("fred");
107: assertNotNull(d.getLocation());
108: assertEquals("barney", d.getValue());
109: }
110:
111: public void testStructured() throws Exception {
112: Registry r = buildFrameworkRegistry("Structured.xml");
113:
114: List l = r.getConfiguration("hivemind.test.config.Structured");
115:
116: assertEquals(2, l.size());
117:
118: Datum d = (Datum) l.get(0);
119:
120: assertEquals("key_1", d.getKey());
121: assertEquals("value_1", d.getValue());
122: assertNotNull(d.getLocation());
123:
124: d = (Datum) l.get(1);
125:
126: assertEquals("key_2", d.getKey());
127: assertEquals("value_2", d.getValue());
128: }
129:
130: public void testSetParent() throws Exception {
131: Registry r = buildFrameworkRegistry("SetParent.xml");
132:
133: List l = r.getConfiguration("hivemind.test.config.SetParent");
134:
135: assertEquals(1, l.size());
136:
137: Parent p1 = (Parent) l.get(0);
138:
139: assertEquals("key1", p1.getKey());
140: assertEquals("value1", p1.getValue());
141:
142: l = p1.getChildren();
143: assertEquals(2, l.size());
144:
145: Child c1 = (Child) l.get(0);
146:
147: assertSame(p1, c1.getParent());
148:
149: assertEquals("detailkey1", c1.getKey());
150: assertEquals("detailvalue1", c1.getValue());
151:
152: Child c2 = (Child) l.get(1);
153:
154: assertSame(p1, c2.getParent());
155:
156: assertEquals("detailkey2", c2.getKey());
157: assertEquals("detailvalue2", c2.getValue());
158: }
159:
160: public void testBooleanTranslator() throws Exception {
161: interceptLogging();
162:
163: Registry r = buildFrameworkRegistry("BooleanTranslator.xml");
164:
165: List l = r
166: .getConfiguration("hivemind.test.config.BooleanTranslator");
167:
168: // Trigger the proxy
169:
170: l.size();
171:
172: assertLoggedMessagePattern("Unable to process attribute value \\(of element flag\\): "
173: + "'maybe' is not a boolean value \\(which should be either 'true' or 'false'\\)\\.");
174:
175: assertEquals(3, l.size());
176:
177: BooleanHolder h = (BooleanHolder) l.get(0);
178:
179: assertEquals(true, h.getValue());
180:
181: h = (BooleanHolder) l.get(1);
182: assertEquals(false, h.getValue());
183:
184: h = (BooleanHolder) l.get(2);
185: assertEquals(false, h.getValue());
186: }
187:
188: public void testIntTranslator() throws Exception {
189: interceptLogging();
190:
191: Registry r = buildFrameworkRegistry("IntTranslator.xml");
192:
193: List l = r
194: .getConfiguration("hivemind.test.config.IntTranslator");
195:
196: // Convert the proxy into a real list.
197:
198: l.size();
199:
200: List events = getInterceptedLogEvents();
201:
202: assertLoggedMessagePattern(
203: "Unable to process attribute value \\(of element int\\): "
204: + "Value 2 is less than minimum value 5\\.",
205: events);
206: assertLoggedMessagePattern(
207: "Value 12 is greater than maximum value 10\\.", events);
208: assertLoggedMessagePattern("'fred' is not an integer value\\.",
209: events);
210:
211: assertEquals(5, l.size());
212:
213: IntHolder h = (IntHolder) l.get(0);
214:
215: assertEquals(7, h.getValue());
216:
217: h = (IntHolder) l.get(1);
218: assertEquals(0, h.getValue());
219:
220: h = (IntHolder) l.get(2);
221:
222: assertEquals(0, h.getValue());
223:
224: h = (IntHolder) l.get(3);
225:
226: assertEquals(0, h.getValue());
227:
228: h = (IntHolder) l.get(3);
229:
230: assertEquals(0, h.getValue());
231:
232: }
233:
234: public void testInstanceTranslator() throws Exception {
235: Registry r = buildFrameworkRegistry("InstanceTranslator.xml");
236:
237: List l = r
238: .getConfiguration("hivemind.test.config.InstanceTranslator");
239:
240: assertEquals(1, l.size());
241:
242: FrobableHolder h = (FrobableHolder) l.get(0);
243:
244: Frobable f = h.getFrobable();
245:
246: assertNotNull(f);
247: assertEquals(true, f.frob());
248: }
249:
250: public void testSymbols() throws Exception {
251: interceptLogging();
252:
253: Registry r = buildFrameworkRegistry("Symbols.xml");
254:
255: List l = r.getConfiguration("hivemind.test.config.Symbols");
256:
257: assertEquals(3, l.size());
258:
259: Datum d = (Datum) l.get(0);
260:
261: assertEquals("wife", d.getKey());
262: assertEquals("wilma", d.getValue());
263:
264: d = (Datum) l.get(1);
265:
266: assertEquals("husband", d.getKey());
267: assertEquals("fred", d.getValue());
268:
269: d = (Datum) l.get(2);
270:
271: assertEquals("work", d.getKey());
272: assertEquals("${work}", d.getValue());
273:
274: assertLoggedMessagePattern("No value available for symbol 'work'");
275: }
276:
277: public void testNoSchema() throws Exception {
278: Registry r = buildFrameworkRegistry("NoSchema.xml");
279:
280: List l = r.getConfiguration("hivemind.test.config.NoSchema");
281:
282: assertEquals(2, l.size());
283:
284: Element e = (Element) l.get(0);
285: assertEquals("datum", e.getElementName());
286: assertEquals("key1", e.getAttributeValue("key"));
287: assertEquals("value1", e.getAttributeValue("value"));
288:
289: // Show that symbols are NOT expanded in non-schema
290: // contributions.
291:
292: e = (Element) l.get(1);
293: assertEquals("datum", e.getElementName());
294: assertEquals("key2", e.getAttributeValue("key"));
295: assertEquals("${value2}", e.getAttributeValue("value"));
296: }
297:
298: public void testLocalized() throws Exception {
299: Registry r = buildFrameworkRegistry("Localized.xml");
300:
301: List l = r.getConfiguration("hivemind.test.config.Localized");
302: assertEquals(1, l.size());
303:
304: Datum d = (Datum) l.get(0);
305:
306: assertEquals("message", d.getKey());
307: assertEquals("Some Damn Thing", d.getValue());
308: }
309:
310: public void testElementsProxyList() throws Exception {
311: Registry r = buildFrameworkRegistry("Simple.xml");
312:
313: List l = r.getConfiguration("hivemind.test.config.Simple");
314:
315: assertEquals(
316: "<Element List Proxy for hivemind.test.config.Simple>",
317: l.toString());
318:
319: assertEquals(true, l.equals(l));
320: assertEquals(false, l.equals(null));
321:
322: assertEquals(2, l.size());
323:
324: List l2 = r.getConfiguration("hivemind.test.config.Simple");
325:
326: assertNotSame(l, l2);
327: assertEquals(l, l2);
328:
329: assertEquals(l2.toString(), l.toString());
330: }
331:
332: public void testTooFew() throws Exception {
333:
334: interceptLogging(RegistryBuilder.class.getName());
335:
336: Registry r = buildFrameworkRegistry("TooFew.xml");
337:
338: r.getConfiguration("hivemind.test.config.TooFew");
339:
340: assertLoggedMessage("Configuration point hivemind.test.config.TooFew contains no contributions but expects at least one contribution.");
341:
342: }
343:
344: public void testTooMany() throws Exception {
345: interceptLogging();
346:
347: Registry r = buildFrameworkRegistry("TooMany.xml");
348:
349: r.getConfiguration("hivemind.test.config.TooMany");
350:
351: assertLoggedMessage("Configuration point hivemind.test.config.TooMany contains 2 contributions but expects an optional contribution.");
352: }
353:
354: public void testBadAttributes() throws Exception {
355: Registry r = buildFrameworkRegistry("BadAttributes.xml");
356:
357: List l = r
358: .getConfiguration("hivemind.test.config.BadAttributes");
359:
360: try {
361: l.size();
362:
363: unreachable();
364: } catch (ApplicationRuntimeException ex) {
365: Throwable t = findNestedException(ex);
366: assertExceptionSubstring(t, "Element datum (at");
367: assertExceptionSubstring(
368: t,
369: "Attribute 'xey' is not defined in the schema. Attribute 'key' is required but no value was provided.");
370: }
371: }
372:
373: public void testBadElement() throws Exception {
374: Registry r = buildFrameworkRegistry("BadElement.xml");
375:
376: interceptLogging("hivemind.test.config");
377:
378: List l = r.getConfiguration("hivemind.test.config.BadElement");
379:
380: assertEquals(1, l.size());
381:
382: assertLoggedMessagePattern("Error at .*?: Element xatum is not allowed here\\.");
383: }
384:
385: public void testCustomRule() throws Exception {
386: Registry r = buildFrameworkRegistry("CustomRule.xml");
387:
388: List l = r.getConfiguration("hivemind.test.config.CustomRule");
389:
390: Datum d = (Datum) l.get(0);
391:
392: // Put this check second, just to get some code coverage
393: // on ElementsInnerProxyList
394:
395: assertEquals(2, l.size());
396:
397: assertEquals("hivemind.test.config", d.getContributingModule()
398: .getModuleId());
399: }
400:
401: public void testCustomRuleFailure() throws Exception {
402: try {
403: parse("CustomRuleFailure.xml");
404:
405: unreachable();
406: } catch (ApplicationRuntimeException ex) {
407: assertExceptionSubstring(
408: ex,
409: "Unable to create instance of Rule class hivemind.test.config.XetContributingModuleRule");
410: }
411: }
412:
413: public void testResourceTranslator() throws Exception {
414: RegistryBuilder builder = new RegistryBuilder();
415:
416: Resource moduleResource = new ClasspathResource(_resolver,
417: "/hivemind/test/config/ResourceTranslator.xml");
418:
419: builder
420: .addModuleDescriptorProvider(new XmlModuleDescriptorProvider(
421: _resolver));
422: builder
423: .addModuleDescriptorProvider(new XmlModuleDescriptorProvider(
424: _resolver, moduleResource));
425:
426: Registry r = builder.constructRegistry(Locale.FRENCH);
427:
428: List l = r
429: .getConfiguration("hivemind.test.config.ResourceTranslator");
430:
431: interceptLogging();
432:
433: assertEquals(4, l.size());
434:
435: ResourceHolder h = (ResourceHolder) l.get(0);
436:
437: assertEquals(moduleResource.getRelativeResource("Empty.xml"), h
438: .getResource());
439:
440: h = (ResourceHolder) l.get(1);
441:
442: assertEquals(moduleResource
443: .getRelativeResource("Localized_fr.properties"), h
444: .getResource());
445:
446: h = (ResourceHolder) l.get(2);
447: assertNull(h.getResource());
448:
449: h = (ResourceHolder) l.get(3);
450: assertNull(h.getResource());
451:
452: assertLoggedMessagePattern("Unable to process content of element resource: "
453: + "Unable to localize resource DoesNotExist\\.xml for module hivemind\\.test\\.config\\.");
454: }
455:
456: public void testShutdown() throws Exception {
457: Registry r = buildFrameworkRegistry("Simple.xml");
458:
459: List l = r.getConfiguration("hivemind.test.config.Simple");
460:
461: assertEquals(2, l.size());
462:
463: r.shutdown();
464:
465: try {
466: l.size();
467: } catch (ApplicationRuntimeException ex) {
468: assertExceptionSubstring(ex,
469: "The HiveMind Registry has been shutdown.");
470: }
471:
472: }
473:
474: /**
475: * Test for contribution to unknown configuration extension point.
476: */
477:
478: public void testUnknownContribution() throws Exception {
479: interceptLogging();
480:
481: buildFrameworkRegistry("UnknownContribution.xml");
482:
483: assertLoggedMessagePattern("Module hivemind\\.test\\.config has contributed to unknown configuration point UnresolvedSchema\\. "
484: + "The contribution has been ignored\\.");
485:
486: }
487: }
|