KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > resolver > TestComponentSpecificationResolver


1 // Copyright 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.tapestry.resolver;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.hivemind.ApplicationRuntimeException;
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.Resource;
21 import org.apache.tapestry.INamespace;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.engine.ISpecificationSource;
24 import org.apache.tapestry.spec.IComponentSpecification;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.resolver.ComponentSpecificationResolverImpl}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestComponentSpecificationResolver extends AbstractSpecificationResolverTestCase
34 {
35     private void trainIsDeprecated(MockControl control, IComponentSpecification spec,
36             boolean isDeprecated)
37     {
38         spec.isDeprecated();
39         control.setReturnValue(isDeprecated);
40     }
41
42     protected ISpecificationSource newSource(Resource resource, IComponentSpecification spec)
43     {
44         MockControl control = newControl(ISpecificationSource.class);
45         ISpecificationSource source = (ISpecificationSource) control.getMock();
46
47         source.getComponentSpecification(resource);
48         control.setReturnValue(spec);
49
50         return source;
51     }
52
53     protected ISpecificationSource newSource(INamespace framework)
54     {
55         MockControl control = newControl(ISpecificationSource.class);
56         ISpecificationSource source = (ISpecificationSource) control.getMock();
57
58         source.getFrameworkNamespace();
59         control.setReturnValue(framework);
60
61         return source;
62     }
63
64     private ISpecificationResolverDelegate newDelegate(IRequestCycle cycle, INamespace namespace,
65             IComponentSpecification spec)
66     {
67         MockControl control = newControl(ISpecificationResolverDelegate.class);
68         ISpecificationResolverDelegate delegate = (ISpecificationResolverDelegate) control
69                 .getMock();
70
71         delegate.findComponentSpecification(cycle, namespace, "DelegateComponent");
72         control.setReturnValue(spec);
73
74         return delegate;
75     }
76
77     public void testFoundInNamespace()
78     {
79         IRequestCycle cycle = newCycle();
80         Location l = newLocation();
81
82         MockControl specc = newControl(IComponentSpecification.class);
83         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
84
85         MockControl control = newControl(INamespace.class);
86         INamespace namespace = (INamespace) control.getMock();
87
88         namespace.containsComponentType("MyComponent");
89         control.setReturnValue(true);
90
91         namespace.getComponentSpecification("MyComponent");
92         control.setReturnValue(spec);
93
94         trainIsDeprecated(specc, spec, false);
95
96         replayControls();
97
98         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
99
100         resolver.resolve(cycle, namespace, "MyComponent", l);
101
102         assertSame(spec, resolver.getSpecification());
103         assertSame(namespace, resolver.getNamespace());
104
105         verifyControls();
106     }
107
108     public void testDeprecated()
109     {
110         IRequestCycle cycle = newCycle();
111         Location l = newLocation();
112
113         MockControl specc = newControl(IComponentSpecification.class);
114         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
115
116         MockControl control = newControl(INamespace.class);
117         INamespace namespace = (INamespace) control.getMock();
118
119         namespace.containsComponentType("MyComponent");
120         control.setReturnValue(true);
121
122         namespace.getComponentSpecification("MyComponent");
123         control.setReturnValue(spec);
124
125         trainIsDeprecated(specc, spec, true);
126
127         Log log = (Log) newMock(Log.class);
128         
129         log.error("Component 'MyComponent' (at classpath:/org/apache/tapestry/resolver/TestComponentSpecificationResolver, line 1) is deprecated, and will likely be removed in a later release. Consult its documentation to find a replacement component.");
130
131         replayControls();
132
133         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
134         resolver.setLog(log);
135
136         resolver.resolve(cycle, namespace, "MyComponent", l);
137
138         assertSame(spec, resolver.getSpecification());
139         assertSame(namespace, resolver.getNamespace());
140
141         verifyControls();
142     }
143
144     public void testFoundInChildNamespace()
145     {
146         IRequestCycle cycle = newCycle();
147         Location l = newLocation();
148
149         MockControl specc = newControl(IComponentSpecification.class);
150         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
151
152         MockControl namespacec = newControl(INamespace.class);
153         INamespace namespace = (INamespace) namespacec.getMock();
154
155         MockControl libraryc = newControl(INamespace.class);
156         INamespace library = (INamespace) libraryc.getMock();
157
158         namespace.getChildNamespace("lib");
159         namespacec.setReturnValue(library);
160
161         library.containsComponentType("MyComponent");
162         libraryc.setReturnValue(true);
163
164         library.getComponentSpecification("MyComponent");
165         libraryc.setReturnValue(spec);
166
167         trainIsDeprecated(specc, spec, false);
168
169         replayControls();
170
171         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
172
173         resolver.resolve(cycle, namespace, "lib:MyComponent", l);
174
175         assertSame(spec, resolver.getSpecification());
176         assertSame(library, resolver.getNamespace());
177
178         verifyControls();
179     }
180
181     public void testSearchFoundRelative()
182     {
183         IRequestCycle cycle = newCycle();
184         Location l = newLocation();
185
186         MockControl specc = newControl(IComponentSpecification.class);
187         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
188
189         MockControl logc = newControl(Log.class);
190         Log log = (Log) logc.getMock();
191
192         MockControl namespacec = newControl(INamespace.class);
193         INamespace namespace = (INamespace) namespacec.getMock();
194
195         Resource namespaceLocation = newResource("LibraryStandin.library");
196         Resource specLocation = namespaceLocation.getRelativeResource("MyComponent.jwc");
197
198         ISpecificationSource source = newSource(specLocation, spec);
199
200         namespace.containsComponentType("MyComponent");
201         namespacec.setReturnValue(false);
202
203         train(log, logc, ResolverMessages.resolvingComponent("MyComponent", namespace));
204
205         namespace.getSpecificationLocation();
206         namespacec.setReturnValue(namespaceLocation);
207
208         train(log, logc, ResolverMessages.checkingResource(specLocation));
209         train(log, logc, ResolverMessages.installingComponent("MyComponent", namespace, spec));
210
211         namespace.installComponentSpecification("MyComponent", spec);
212
213         trainIsDeprecated(specc, spec, false);
214
215         replayControls();
216
217         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
218         resolver.setLog(log);
219         resolver.setSpecificationSource(source);
220
221         resolver.resolve(cycle, namespace, "MyComponent", l);
222
223         assertSame(spec, resolver.getSpecification());
224         assertSame(namespace, resolver.getNamespace());
225
226         verifyControls();
227     }
228
229     public void testFoundInFrameworkNamespace()
230     {
231         IRequestCycle cycle = newCycle();
232         Location l = newLocation();
233
234         MockControl specc = newControl(IComponentSpecification.class);
235         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
236
237         MockControl logc = newControl(Log.class);
238         Log log = (Log) logc.getMock();
239
240         MockControl namespacec = newControl(INamespace.class);
241         INamespace namespace = (INamespace) namespacec.getMock();
242
243         MockControl frameworkc = newControl(INamespace.class);
244         INamespace framework = (INamespace) frameworkc.getMock();
245
246         Resource namespaceLocation = newResource("LibraryStandin.library");
247
248         namespace.containsComponentType("FrameworkComponent");
249         namespacec.setReturnValue(false);
250
251         train(log, logc, ResolverMessages.resolvingComponent("FrameworkComponent", namespace));
252
253         namespace.getSpecificationLocation();
254         namespacec.setReturnValue(namespaceLocation);
255
256         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
257                 .getRelativeResource("FrameworkComponent.jwc")));
258
259         namespace.isApplicationNamespace();
260         namespacec.setReturnValue(false);
261
262         ISpecificationSource source = newSource(framework);
263
264         framework.containsComponentType("FrameworkComponent");
265         frameworkc.setReturnValue(true);
266
267         framework.getComponentSpecification("FrameworkComponent");
268         frameworkc.setReturnValue(spec);
269
270         train(log, logc, ResolverMessages
271                 .installingComponent("FrameworkComponent", namespace, spec));
272         namespace.installComponentSpecification("FrameworkComponent", spec);
273
274         trainIsDeprecated(specc, spec, false);
275
276         replayControls();
277
278         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
279         resolver.setLog(log);
280         resolver.setSpecificationSource(source);
281
282         resolver.resolve(cycle, namespace, "FrameworkComponent", l);
283
284         assertSame(spec, resolver.getSpecification());
285         assertSame(namespace, resolver.getNamespace());
286
287         verifyControls();
288     }
289
290     public void testProvidedByDelegate()
291     {
292         IRequestCycle cycle = newCycle();
293         Location l = newLocation();
294
295         MockControl specc = newControl(IComponentSpecification.class);
296         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
297
298         MockControl logc = newControl(Log.class);
299         Log log = (Log) logc.getMock();
300
301         MockControl namespacec = newControl(INamespace.class);
302         INamespace namespace = (INamespace) namespacec.getMock();
303
304         MockControl frameworkc = newControl(INamespace.class);
305         INamespace framework = (INamespace) frameworkc.getMock();
306
307         ISpecificationResolverDelegate delegate = newDelegate(cycle, namespace, spec);
308
309         Resource namespaceLocation = newResource("LibraryStandin.library");
310
311         namespace.containsComponentType("DelegateComponent");
312         namespacec.setReturnValue(false);
313
314         train(log, logc, ResolverMessages.resolvingComponent("DelegateComponent", namespace));
315
316         namespace.getSpecificationLocation();
317         namespacec.setReturnValue(namespaceLocation);
318
319         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
320                 .getRelativeResource("DelegateComponent.jwc")));
321
322         namespace.isApplicationNamespace();
323         namespacec.setReturnValue(false);
324
325         ISpecificationSource source = newSource(framework);
326
327         framework.containsComponentType("DelegateComponent");
328         frameworkc.setReturnValue(false);
329
330         log.isDebugEnabled();
331         logc.setReturnValue(false);
332         
333         namespace.installComponentSpecification("DelegateComponent", spec);
334         
335         trainIsDeprecated(specc, spec, false);
336
337         replayControls();
338
339         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
340         resolver.setLog(log);
341         resolver.setSpecificationSource(source);
342         resolver.setDelegate(delegate);
343
344         resolver.resolve(cycle, namespace, "DelegateComponent", l);
345
346         assertSame(spec, resolver.getSpecification());
347         assertSame(namespace, resolver.getNamespace());
348
349         verifyControls();
350     }
351
352     public void testNotFound()
353     {
354         IRequestCycle cycle = newCycle();
355         Location l = newLocation();
356
357         MockControl logc = newControl(Log.class);
358         Log log = (Log) logc.getMock();
359
360         MockControl namespacec = newControl(INamespace.class);
361         INamespace namespace = (INamespace) namespacec.getMock();
362
363         MockControl frameworkc = newControl(INamespace.class);
364         INamespace framework = (INamespace) frameworkc.getMock();
365
366         ISpecificationResolverDelegate delegate = newDelegate(cycle, namespace, null);
367
368         Resource namespaceLocation = newResource("LibraryStandin.library");
369
370         namespace.containsComponentType("DelegateComponent");
371         namespacec.setReturnValue(false);
372
373         train(log, logc, ResolverMessages.resolvingComponent("DelegateComponent", namespace));
374
375         namespace.getSpecificationLocation();
376         namespacec.setReturnValue(namespaceLocation);
377
378         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
379                 .getRelativeResource("DelegateComponent.jwc")));
380
381         namespace.isApplicationNamespace();
382         namespacec.setReturnValue(false);
383
384         ISpecificationSource source = newSource(framework);
385
386         framework.containsComponentType("DelegateComponent");
387         frameworkc.setReturnValue(false);
388
389         replayControls();
390
391         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
392         resolver.setLog(log);
393         resolver.setSpecificationSource(source);
394         resolver.setDelegate(delegate);
395
396         try
397         {
398             resolver.resolve(cycle, namespace, "DelegateComponent", l);
399             unreachable();
400         }
401         catch (ApplicationRuntimeException ex)
402         {
403             assertEquals(
404                     "Component 'DelegateComponent' not found in EasyMock for interface org.apache.tapestry.INamespace.",
405                     ex.getMessage());
406             assertSame(l, ex.getLocation());
407         }
408
409         verifyControls();
410     }
411
412     /**
413      * Test for checking inside the WEB-INF/app folder (app is the application id, i.e., the servlet
414      * name).
415      */

416
417     public void testFoundInAppFolder()
418     {
419         IRequestCycle cycle = newCycle();
420         Location l = newLocation();
421
422         MockControl specc = newControl(IComponentSpecification.class);
423         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
424
425         MockControl logc = newControl(Log.class);
426         Log log = (Log) logc.getMock();
427
428         Resource contextRoot = newResource("context/");
429
430         MockControl namespacec = newControl(INamespace.class);
431         INamespace namespace = (INamespace) namespacec.getMock();
432
433         Resource namespaceLocation = newResource("LibraryStandin.library");
434         Resource specLocation = contextRoot.getRelativeResource("WEB-INF/myapp/MyAppComponent.jwc");
435
436         ISpecificationSource source = newSource(specLocation, spec);
437
438         namespace.containsComponentType("MyAppComponent");
439         namespacec.setReturnValue(false);
440
441         train(log, logc, ResolverMessages.resolvingComponent("MyAppComponent", namespace));
442
443         namespace.getSpecificationLocation();
444         namespacec.setReturnValue(namespaceLocation);
445
446         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
447                 .getRelativeResource("MyAppComponent.jwc")));
448
449         namespace.isApplicationNamespace();
450         namespacec.setReturnValue(true);
451
452         train(log, logc, ResolverMessages.checkingResource(specLocation));
453         train(log, logc, ResolverMessages.installingComponent("MyAppComponent", namespace, spec));
454
455         namespace.installComponentSpecification("MyAppComponent", spec);
456
457         trainIsDeprecated(specc, spec, false);
458
459         replayControls();
460
461         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
462         resolver.setLog(log);
463         resolver.setSpecificationSource(source);
464         resolver.setContextRoot(contextRoot);
465         resolver.setApplicationId("myapp");
466         resolver.initializeService();
467
468         resolver.resolve(cycle, namespace, "MyAppComponent", l);
469
470         assertSame(spec, resolver.getSpecification());
471         assertSame(namespace, resolver.getNamespace());
472
473         verifyControls();
474     }
475
476     public void testFoundInWebInfFolder()
477     {
478         IRequestCycle cycle = newCycle();
479         Location l = newLocation();
480
481         MockControl specc = newControl(IComponentSpecification.class);
482         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
483
484         MockControl logc = newControl(Log.class);
485         Log log = (Log) logc.getMock();
486
487         Resource contextRoot = newResource("context/");
488
489         MockControl namespacec = newControl(INamespace.class);
490         INamespace namespace = (INamespace) namespacec.getMock();
491
492         Resource namespaceLocation = newResource("LibraryStandin.library");
493         Resource specLocation = contextRoot.getRelativeResource("WEB-INF/MyWebInfComponent.jwc");
494
495         ISpecificationSource source = newSource(specLocation, spec);
496
497         namespace.containsComponentType("MyWebInfComponent");
498         namespacec.setReturnValue(false);
499
500         train(log, logc, ResolverMessages.resolvingComponent("MyWebInfComponent", namespace));
501
502         namespace.getSpecificationLocation();
503         namespacec.setReturnValue(namespaceLocation);
504
505         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
506                 .getRelativeResource("MyWebInfComponent.jwc")));
507
508         namespace.isApplicationNamespace();
509         namespacec.setReturnValue(true);
510
511         train(log, logc, ResolverMessages.checkingResource(contextRoot
512                 .getRelativeResource("WEB-INF/myapp/MyWebInfComponent.jwc")));
513         train(log, logc, ResolverMessages.checkingResource(specLocation));
514         train(log, logc, ResolverMessages.installingComponent("MyWebInfComponent", namespace, spec));
515
516         namespace.installComponentSpecification("MyWebInfComponent", spec);
517
518         trainIsDeprecated(specc, spec, false);
519
520         replayControls();
521
522         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
523         resolver.setLog(log);
524         resolver.setSpecificationSource(source);
525         resolver.setContextRoot(contextRoot);
526         resolver.setApplicationId("myapp");
527         resolver.initializeService();
528
529         resolver.resolve(cycle, namespace, "MyWebInfComponent", l);
530
531         assertSame(spec, resolver.getSpecification());
532         assertSame(namespace, resolver.getNamespace());
533
534         verifyControls();
535     }
536
537     public void testFoundInContextRoot()
538     {
539         IRequestCycle cycle = newCycle();
540         Location l = newLocation();
541
542         MockControl specc = newControl(IComponentSpecification.class);
543         IComponentSpecification spec = (IComponentSpecification) specc.getMock();
544
545         MockControl logc = newControl(Log.class);
546         Log log = (Log) logc.getMock();
547
548         Resource contextRoot = newResource("context/");
549
550         MockControl namespacec = newControl(INamespace.class);
551         INamespace namespace = (INamespace) namespacec.getMock();
552
553         Resource namespaceLocation = newResource("LibraryStandin.library");
554         Resource specLocation = contextRoot.getRelativeResource("ContextRootComponent.jwc");
555
556         ISpecificationSource source = newSource(specLocation, spec);
557
558         namespace.containsComponentType("ContextRootComponent");
559         namespacec.setReturnValue(false);
560
561         train(log, logc, ResolverMessages.resolvingComponent("ContextRootComponent", namespace));
562
563         namespace.getSpecificationLocation();
564         namespacec.setReturnValue(namespaceLocation);
565
566         train(log, logc, ResolverMessages.checkingResource(namespaceLocation
567                 .getRelativeResource("ContextRootComponent.jwc")));
568
569         namespace.isApplicationNamespace();
570         namespacec.setReturnValue(true);
571
572         train(log, logc, ResolverMessages.checkingResource(contextRoot
573                 .getRelativeResource("WEB-INF/myapp/ContextRootComponent.jwc")));
574         train(log, logc, ResolverMessages.checkingResource(contextRoot
575                 .getRelativeResource("WEB-INF/ContextRootComponent.jwc")));
576         train(log, logc, ResolverMessages.checkingResource(specLocation));
577         train(log, logc, ResolverMessages.installingComponent(
578                 "ContextRootComponent",
579                 namespace,
580                 spec));
581
582         trainIsDeprecated(specc, spec, false);
583
584         namespace.installComponentSpecification("ContextRootComponent", spec);
585
586         replayControls();
587
588         ComponentSpecificationResolverImpl resolver = new ComponentSpecificationResolverImpl();
589         resolver.setLog(log);
590         resolver.setSpecificationSource(source);
591         resolver.setContextRoot(contextRoot);
592         resolver.setApplicationId("myapp");
593         resolver.initializeService();
594
595         resolver.resolve(cycle, namespace, "ContextRootComponent", l);
596
597         assertSame(spec, resolver.getSpecification());
598         assertSame(namespace, resolver.getNamespace());
599
600         verifyControls();
601     }
602 }
Popular Tags