KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > impl > TestSchemaProcessor


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.impl;
16
17 import hivemind.test.services.StringHolder;
18 import hivemind.test.services.impl.StringHolderImpl;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 import org.apache.hivemind.Location;
30 import org.apache.hivemind.SymbolExpander;
31 import org.apache.hivemind.TranslatorManager;
32 import org.apache.hivemind.internal.Module;
33 import org.apache.hivemind.internal.RegistryInfrastructure;
34 import org.apache.hivemind.schema.SchemaProcessor;
35 import org.apache.hivemind.schema.Translator;
36 import org.apache.hivemind.schema.impl.AttributeModelImpl;
37 import org.apache.hivemind.schema.impl.ElementModelImpl;
38 import org.apache.hivemind.schema.impl.SchemaImpl;
39 import org.apache.hivemind.schema.rules.CreateObjectRule;
40 import org.apache.hivemind.schema.rules.InvokeParentRule;
41 import org.apache.hivemind.schema.rules.NullTranslator;
42 import org.apache.hivemind.schema.rules.ReadAttributeRule;
43 import org.apache.hivemind.schema.rules.ReadContentRule;
44 import org.apache.hivemind.test.HiveMindTestCase;
45 import org.easymock.MockControl;
46
47 /**
48  * Tests for {@link org.apache.hivemind.schema.SchemaProcessor} and
49  * {@link org.apache.hivemind.impl.SchemaElement}.
50  *
51  * @author Howard Lewis Ship
52  */

53 public class TestSchemaProcessor extends HiveMindTestCase
54 {
55
56     public void testGetContentTranslator()
57     {
58         MockControl control = newControl(Module.class);
59         Module m = (Module) control.getMock();
60         
61         MockControl registryControl = newControl(RegistryInfrastructure.class);
62         RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
63         
64         MockControl tmControl = newControl(TranslatorManager.class);
65         TranslatorManager tm = (TranslatorManager) tmControl.getMock();
66
67         MockControl symbolExpanderControl = newControl(SymbolExpander.class);
68         SymbolExpander symbolExpander = (SymbolExpander) symbolExpanderControl.getMock();
69
70         ElementModelImpl em = new ElementModelImpl("module");
71
72         em.setElementName("fred");
73         em.setContentTranslator("smart");
74
75         em.addRule(new CreateObjectRule(StringHolderImpl.class.getName()));
76
77         ReadContentRule rule = new ReadContentRule();
78         rule.setPropertyName("value");
79
80         em.addRule(rule);
81
82         em.addRule(new InvokeParentRule("addElement"));
83
84         SchemaImpl schema = new SchemaImpl("module");
85         schema.addElementModel(em);
86
87         SchemaProcessorImpl p = new SchemaProcessorImpl(null, schema);
88
89         ElementImpl element = new ElementImpl();
90         element.setElementName("fred");
91         element.setContent("flintstone");
92
93         List JavaDoc elements = Collections.singletonList(element);
94         
95         m.getRegistry();
96         control.setReturnValue(registry);
97         
98         registry.getModule("module");
99         registryControl.setReturnValue(m);
100
101         m.resolveType("hivemind.test.services.impl.StringHolderImpl");
102         control.setReturnValue(StringHolderImpl.class);
103
104         m.getService(SymbolExpander.class);
105         control.setReturnValue(symbolExpander);
106
107         symbolExpander.expandSymbols("flintstone", null);
108         symbolExpanderControl.setReturnValue("flintstone");
109         
110         m.getService(TranslatorManager.class);
111         control.setReturnValue(tm);
112
113         tm.getTranslator("smart");
114         tmControl.setReturnValue(new NullTranslator());
115          
116         replayControls();
117
118         List JavaDoc dest = new ArrayList JavaDoc();
119         p.process(dest, elements, m);
120
121         assertEquals(1, dest.size());
122         StringHolder h = (StringHolder) dest.get(0);
123
124         assertEquals("flintstone", h.getValue());
125
126         verifyControls();
127     }
128
129     public void testGetContentTranslatorUnspecified()
130     {
131         MockControl control = newControl(Module.class);
132         Module m = (Module) control.getMock();
133         
134         MockControl registryControl = newControl(RegistryInfrastructure.class);
135         RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
136
137         MockControl symbolExpanderControl = newControl(SymbolExpander.class);
138         SymbolExpander symbolExpander = (SymbolExpander) symbolExpanderControl.getMock();
139
140         ElementModelImpl em = new ElementModelImpl("module");
141
142         em.setElementName("fred");
143         // No content handler specified
144

145         em.addRule(new CreateObjectRule(StringHolderImpl.class.getName()));
146
147         ReadContentRule rule = new ReadContentRule();
148         rule.setPropertyName("value");
149
150         em.addRule(rule);
151
152         em.addRule(new InvokeParentRule("addElement"));
153
154         SchemaImpl schema = new SchemaImpl("module");
155         schema.addElementModel(em);
156
157         SchemaProcessorImpl p = new SchemaProcessorImpl(null, schema);
158
159         ElementImpl element = new ElementImpl();
160         element.setElementName("fred");
161         element.setContent("flintstone");
162
163         List JavaDoc elements = Collections.singletonList(element);
164         
165         m.getRegistry();
166         control.setReturnValue(registry);
167         
168         registry.getModule(schema.getDefiningModuleId());
169         registryControl.setReturnValue(m);
170
171         m.resolveType("hivemind.test.services.impl.StringHolderImpl");
172         control.setReturnValue(StringHolderImpl.class);
173
174         m.getService(SymbolExpander.class);
175         control.setReturnValue(symbolExpander);
176
177         symbolExpander.expandSymbols("flintstone", null);
178         symbolExpanderControl.setReturnValue("flintstone");
179
180         replayControls();
181
182         List JavaDoc dest = new ArrayList JavaDoc();
183         p.process(dest, elements, m);
184
185         assertEquals(1, dest.size());
186         StringHolder h = (StringHolder) dest.get(0);
187
188         assertEquals("flintstone", h.getValue());
189
190         verifyControls();
191     }
192
193     public void testGetAttributeTranslator()
194     {
195         MockControl control = newControl(Module.class);
196         Module m = (Module) control.getMock();
197         
198         MockControl registryControl = newControl(RegistryInfrastructure.class);
199         RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
200
201         MockControl tmControl = newControl(TranslatorManager.class);
202         TranslatorManager tm = (TranslatorManager) tmControl.getMock();
203
204         MockControl symbolExpanderControl = newControl(SymbolExpander.class);
205         SymbolExpander symbolExpander = (SymbolExpander) symbolExpanderControl.getMock();
206
207         ElementModelImpl em = new ElementModelImpl("module");
208
209         AttributeModelImpl am = new AttributeModelImpl();
210         am.setName("wife");
211         am.setTranslator("service");
212
213         em.setElementName("fred");
214         em.addAttributeModel(am);
215
216         em.addRule(new CreateObjectRule(StringHolderImpl.class.getName()));
217
218         ReadAttributeRule rule = new ReadAttributeRule();
219         rule.setPropertyName("value");
220         rule.setAttributeName("wife");
221
222         em.addRule(rule);
223
224         em.addRule(new InvokeParentRule("addElement"));
225
226         SchemaImpl schema = new SchemaImpl("module");
227         schema.addElementModel(em);
228
229         SchemaProcessorImpl p = new SchemaProcessorImpl(null, schema);
230
231         ElementImpl element = new ElementImpl();
232         element.setElementName("fred");
233         element.addAttribute(new AttributeImpl("wife", "wilma"));
234
235         List JavaDoc elements = Collections.singletonList(element);
236         
237         m.getRegistry();
238         control.setReturnValue(registry);
239         
240         registry.getModule("module");
241         registryControl.setReturnValue(m);
242
243         m.resolveType("hivemind.test.services.impl.StringHolderImpl");
244         control.setReturnValue(StringHolderImpl.class);
245
246         m.getService(SymbolExpander.class);
247         control.setReturnValue(symbolExpander);
248
249         symbolExpander.expandSymbols("wilma", null);
250         symbolExpanderControl.setReturnValue("wilma");
251         
252         m.getService(TranslatorManager.class);
253         control.setReturnValue(tm);
254
255         tm.getTranslator("service");
256         tmControl.setReturnValue(new NullTranslator());
257
258         replayControls();
259
260         List JavaDoc dest = new ArrayList JavaDoc();
261         p.process(dest, elements, m);
262
263         assertEquals(1, dest.size());
264         StringHolder h = (StringHolder) dest.get(0);
265
266         assertEquals("wilma", h.getValue());
267
268         verifyControls();
269     }
270
271     /**
272      * Tests for when the stack is empty.
273      */

274     public void testStackEmpty()
275     {
276         SchemaProcessor sp = new SchemaProcessorImpl(null, null);
277
278         try
279         {
280             sp.pop();
281             unreachable();
282         }
283         catch (ArrayIndexOutOfBoundsException JavaDoc ex)
284         {
285
286         }
287
288         try
289         {
290             sp.peek();
291             unreachable();
292         }
293         catch (ArrayIndexOutOfBoundsException JavaDoc ex)
294         {
295         }
296
297     }
298
299     public void testKeyedElement()
300     {
301         ElementModelImpl em = new ElementModelImpl("module");
302         
303         MockControl tmControl = newControl(TranslatorManager.class);
304         TranslatorManager tm = (TranslatorManager) tmControl.getMock();
305         
306         MockControl registryControl = newControl(RegistryInfrastructure.class);
307         RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
308
309         MockControl symbolExpanderControl = newControl(SymbolExpander.class);
310         SymbolExpander symbolExpander = (SymbolExpander) symbolExpanderControl.getMock();
311
312         em.setElementName("cartoon");
313         em.setKeyAttribute("name");
314
315         AttributeModelImpl am = new AttributeModelImpl();
316         am.setName("name");
317         am.setTranslator("cartoon");
318
319         em.addAttributeModel(am);
320         
321         em.addRule(new CreateObjectRule("StringHolderImpl"));
322         
323         ReadContentRule rule = new ReadContentRule();
324         rule.setPropertyName("value");
325
326         em.addRule(rule);
327         
328         em.addRule(new InvokeParentRule("addElement"));
329
330         SchemaImpl schema = new SchemaImpl("module");
331         schema.addElementModel(em);
332
333         MockControl control = newControl(Module.class);
334         Module m = (Module) control.getMock();
335
336         SchemaProcessorImpl p = new SchemaProcessorImpl(null, schema);
337
338         ElementImpl element = new ElementImpl();
339         element.setElementName("cartoon");
340         element.setContent("${fred}");
341         element.addAttribute(new AttributeImpl("name", "${flintstone}"));
342
343         List JavaDoc elements = Collections.singletonList(element);
344
345         m.getRegistry();
346         control.setReturnValue(registry);
347         
348         registry.getModule("module");
349         registryControl.setReturnValue(m);
350
351         m.resolveType("StringHolderImpl");
352         control.setReturnValue(StringHolderImpl.class);
353
354         m.getService(SymbolExpander.class);
355         control.setReturnValue(symbolExpander);
356         
357         symbolExpander.expandSymbols("${fred}", null);
358         symbolExpanderControl.setReturnValue("fred");
359
360         symbolExpander.expandSymbols("${flintstone}", null);
361         symbolExpanderControl.setReturnValue("flintstone");
362
363         MockControl tControl = newControl(Translator.class);
364         Translator t = (Translator) tControl.getMock();
365
366         tm.getTranslator("cartoon");
367         tmControl.setReturnValue(t);
368
369         m.getService(TranslatorManager.class);
370         control.setReturnValue(tm);
371
372         Object JavaDoc flintstoneKey = new Object JavaDoc();
373         t.translate(m, Object JavaDoc.class, "flintstone", element.getLocation());
374         tControl.setReturnValue(flintstoneKey);
375
376         replayControls();
377
378         Map JavaDoc dest = new HashMap JavaDoc();
379         p.process(dest, elements, m);
380
381         assertEquals(1, dest.size());
382         StringHolder h = (StringHolder) dest.get(flintstoneKey);
383
384         assertNotNull(h);
385         assertEquals("fred", h.getValue());
386
387         verifyControls();
388     }
389
390     /**
391      * Test contributing 2 elements from 2 modules to a configuration-point with an attribute that
392      * is marked unique and is translated by translator 'qualified-id'. Both contributed elements
393      * use same untranslated value in the unique attribute. Fixes HIVEMIND-100.
394      */

395     public void testUniqueElement()
396     {
397         ElementModelImpl em = new ElementModelImpl("module");
398
399         em.setElementName("cartoon");
400
401         AttributeModelImpl am = new AttributeModelImpl();
402         am.setName("name");
403         am.setTranslator("qualified-id");
404         am.setUnique(true);
405
406         em.addAttributeModel(am);
407
408         em.addRule(new CreateObjectRule("StringHolderImpl"));
409
410         ReadAttributeRule rule = new ReadAttributeRule();
411         rule.setAttributeName("name");
412         rule.setPropertyName("value");
413
414         em.addRule(rule);
415
416         em.addRule(new InvokeParentRule("addElement"));
417
418         SchemaImpl schema = new SchemaImpl("module");
419         schema.addElementModel(em);
420
421         MockControl control1 = newControl(Module.class);
422         Module m1 = (Module) control1.getMock();
423
424         MockControl control2 = newControl(Module.class);
425         Module m2 = (Module) control2.getMock();
426         
427         MockControl registryControl = newControl(RegistryInfrastructure.class);
428         RegistryInfrastructure registry = (RegistryInfrastructure) registryControl.getMock();
429
430         MockControl symbolExpanderControl = newControl(SymbolExpander.class);
431         SymbolExpander symbolExpander = (SymbolExpander) symbolExpanderControl.getMock();
432
433         SchemaProcessorImpl p = new SchemaProcessorImpl(null, schema);
434
435         Location location1 = newLocation();
436         ElementImpl element1 = new ElementImpl();
437         element1.setElementName("cartoon");
438         element1.addAttribute(new AttributeImpl("name", "flintstone"));
439         element1.setLocation(location1);
440
441         List JavaDoc elements1 = Collections.singletonList(element1);
442
443         Location location2 = newLocation();
444         ElementImpl element2 = new ElementImpl();
445         element2.setElementName("cartoon");
446         element2.addAttribute(new AttributeImpl("name", "flintstone"));
447         element2.setLocation(location2);
448
449         List JavaDoc elements2 = Collections.singletonList(element2);
450
451         MockControl tControl1 = newControl(Translator.class);
452         Translator t1 = (Translator) tControl1.getMock();
453         
454         MockControl tmControl = newControl(TranslatorManager.class);
455         TranslatorManager tm = (TranslatorManager) tmControl.getMock();
456         
457         m1.getRegistry();
458         control1.setReturnValue(registry);
459         
460         registry.getModule("module");
461         registryControl.setReturnValue(m1);
462
463         m2.getRegistry();
464         control2.setReturnValue(registry);
465         
466         registry.getModule("module");
467         registryControl.setReturnValue(m2);
468         
469         m1.resolveType("StringHolderImpl");
470         control1.setReturnValue(StringHolderImpl.class);
471
472         m1.getService(SymbolExpander.class);
473         control1.setReturnValue(symbolExpander);
474         
475         symbolExpander.expandSymbols("flintstone", location1);
476         symbolExpanderControl.setReturnValue("flintstone");
477         
478         m1.getService(TranslatorManager.class);
479         control1.setReturnValue(tm);
480         
481         symbolExpander.expandSymbols("flintstone", location1);
482         symbolExpanderControl.setReturnValue("flintstone");
483
484         String JavaDoc flintstoneKeyModule1 = "m1.flintstone";
485         t1.translate(m1, String JavaDoc.class, "flintstone", element1.getLocation());
486         tControl1.setReturnValue(flintstoneKeyModule1);
487
488         t1.translate(m1, Object JavaDoc.class, "flintstone", element1.getLocation());
489         tControl1.setReturnValue(flintstoneKeyModule1);
490
491         m2.resolveType("StringHolderImpl");
492         control2.setReturnValue(StringHolderImpl.class);
493
494         symbolExpander.expandSymbols("flintstone", location2);
495         symbolExpanderControl.setReturnValue("flintstone");
496         
497         symbolExpander.expandSymbols("flintstone", location2);
498         symbolExpanderControl.setReturnValue("flintstone");
499
500         tm.getTranslator("qualified-id");
501         tmControl.setReturnValue(t1);
502
503         tm.getTranslator("qualified-id");
504         tmControl.setReturnValue(t1);
505
506         MockControl tControl2 = newControl(Translator.class);
507         Translator t2 = (Translator) tControl2.getMock();
508
509         tm.getTranslator("qualified-id");
510         tmControl.setReturnValue(t2);
511         
512         String JavaDoc flintstoneKeyModule2 = "m2.flintstone";
513         t2.translate(m2, String JavaDoc.class, "flintstone", element2.getLocation());
514         tControl2.setReturnValue(flintstoneKeyModule2);
515         
516         t2.translate(m2, Object JavaDoc.class, "flintstone", element2.getLocation());
517         tControl2.setReturnValue(flintstoneKeyModule2);
518
519         tm.getTranslator("qualified-id");
520         tmControl.setReturnValue(t2);
521
522
523         replayControls();
524
525         Map JavaDoc dest = new HashMap JavaDoc();
526         p.process(dest, elements1, m1);
527         p.process(dest, elements2, m2);
528
529         assertEquals(2, dest.size());
530
531         Set JavaDoc keys = new TreeSet JavaDoc();
532         for (Iterator JavaDoc iter = dest.values().iterator(); iter.hasNext();)
533         {
534             StringHolderImpl element = (StringHolderImpl) iter.next();
535             keys.add(element.getValue());
536         }
537
538         assertTrue(keys.contains(flintstoneKeyModule1));
539         assertTrue(keys.contains(flintstoneKeyModule2));
540
541         verifyControls();
542     }
543
544     public void testGetAttributeDefault()
545     {
546         ElementModelImpl em = new ElementModelImpl("module");
547         em.setElementName("fred");
548
549         AttributeModelImpl am = new AttributeModelImpl();
550         am.setName("wife");
551         am.setDefault("wilma");
552
553         em.addAttributeModel(am);
554
555         SchemaElement sel = new SchemaElement(null, em);
556
557         assertEquals("wilma", sel.getAttributeDefault("wife"));
558         assertNull(sel.getAttributeDefault("husband"));
559     }
560 }
Popular Tags