KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > parse > TestSpecificationParser


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.tapestry.junit.parse;
16
17 import java.util.List JavaDoc;
18
19 import org.apache.hivemind.Locatable;
20 import org.apache.tapestry.bean.BindingBeanInitializer;
21 import org.apache.tapestry.bean.LightweightBeanInitializer;
22 import org.apache.tapestry.junit.TapestryTestCase;
23 import org.apache.tapestry.spec.BindingType;
24 import org.apache.tapestry.spec.IApplicationSpecification;
25 import org.apache.tapestry.spec.IAssetSpecification;
26 import org.apache.tapestry.spec.IBeanSpecification;
27 import org.apache.tapestry.spec.IBindingSpecification;
28 import org.apache.tapestry.spec.IComponentSpecification;
29 import org.apache.tapestry.spec.IContainedComponent;
30 import org.apache.tapestry.spec.IExtensionSpecification;
31 import org.apache.tapestry.spec.ILibrarySpecification;
32 import org.apache.tapestry.spec.IListenerBindingSpecification;
33 import org.apache.tapestry.spec.IParameterSpecification;
34 import org.apache.tapestry.spec.IPropertySpecification;
35 import org.apache.tapestry.spec.InjectSpecification;
36 import org.apache.tapestry.spec.ListenerBindingSpecification;
37 import org.apache.tapestry.util.xml.DocumentParseException;
38
39 /**
40  * Tests the specification parser (which reads page and component specifications). Came into being
41  * somewhat late, so it just tests new features for the meantime.
42  *
43  * @author Howard Lewis Ship
44  * @since 2.0.4
45  */

46
47 public class TestSpecificationParser extends TapestryTestCase
48 {
49
50     private void checkLine(Locatable locatable, int line)
51     {
52         assertEquals("Line", line, locatable.getLocation().getLineNumber());
53     }
54
55     /**
56      * Test 3.0 <message-binding> element.
57      */

58
59     public void tesMessageBinding() throws Exception JavaDoc
60     {
61         IComponentSpecification spec = parseComponent("TestMessageBinding.jwc");
62
63         IBindingSpecification bs = spec.getComponent("hello").getBinding("value");
64
65         assertEquals("type", BindingType.PREFIXED, bs.getType());
66         assertEquals("key", "message:label.hello", bs.getValue());
67
68         checkLine(bs, 25);
69     }
70
71     /**
72      * Tests the 4.0 style <binding> element.
73      */

74
75     public void testBinding40() throws Exception JavaDoc
76     {
77         IComponentSpecification spec = parseComponent("Binding40.jwc");
78         IContainedComponent cc = spec.getComponent("component");
79
80         IBindingSpecification bs = cc.getBinding("simple");
81
82         assertEquals(BindingType.PREFIXED, bs.getType());
83         assertEquals("message:some-key", bs.getValue());
84
85         bs = cc.getBinding("enclosed");
86
87         assertEquals(BindingType.PREFIXED, bs.getType());
88         assertEquals("ognl:zip.zap.zoop", bs.getValue());
89     }
90
91     /**
92      * Test valid parameter name.
93      *
94      * @since 2.2
95      */

96
97     public void testValidParameterName() throws Exception JavaDoc
98     {
99         IComponentSpecification spec = parseComponent("ValidParameterName.jwc");
100
101         IParameterSpecification ps = spec.getParameter("valid");
102
103         assertNotNull("Parameter specification.", ps);
104         checkLine(ps, 24);
105     }
106
107     /**
108      * Test invalid parameter name.
109      *
110      * @since 2.2
111      */

112
113     public void testInvalidParameterName() throws Exception JavaDoc
114     {
115         try
116         {
117             parseComponent("InvalidParameterName.jwc");
118
119             unreachable();
120         }
121         catch (DocumentParseException ex)
122         {
123             checkException(ex, "in-valid");
124             checkException(ex, "Parameter");
125         }
126     }
127
128     /**
129      * Test invalid component id.
130      *
131      * @since 2.2
132      */

133
134     public void testInvalidComponentId() throws Exception JavaDoc
135     {
136         try
137         {
138             parseComponent("InvalidComponentId.jwc");
139
140             unreachable();
141         }
142         catch (DocumentParseException ex)
143         {
144             checkException(ex, "in.valid");
145             checkException(ex, "component id");
146         }
147     }
148
149     /**
150      * Test invalid library id in a library specification.
151      *
152      * @since 2.2
153      */

154
155     public void testInvalidLibraryId() throws Exception JavaDoc
156     {
157         try
158         {
159             parseLib("InvalidLibraryId.library");
160
161             unreachable();
162         }
163         catch (DocumentParseException ex)
164         {
165             checkException(ex, "in.valid");
166             checkException(ex, "library id");
167         }
168     }
169
170     /**
171      * Parse a valid library.
172      *
173      * @since 2.2
174      */

175
176     public void testValidLibrary() throws Exception JavaDoc
177     {
178         ILibrarySpecification spec = parseLib("ValidLibrary.library");
179
180         checkLine(spec, 24);
181
182         checkList("pageNames", new String JavaDoc[]
183         { "FirstPage", "SecondPage" }, spec.getPageNames());
184
185         checkList("componentAliases", new String JavaDoc[]
186         { "FirstComponent", "SecondComponent" }, spec.getComponentTypes());
187
188         checkList("libraryIds", new String JavaDoc[]
189         { "lib1", "lib2" }, spec.getLibraryIds());
190
191         assertNotNull(spec.getSpecificationLocation());
192     }
193
194     /**
195      * Test invalid parameter name.
196      *
197      * @since 2.2
198      */

199
200     public void testInvalidAssetName() throws Exception JavaDoc
201     {
202         try
203         {
204             parseComponent("InvalidAssetName.jwc");
205
206             unreachable();
207         }
208         catch (DocumentParseException ex)
209         {
210             checkException(ex, "in.valid");
211             checkException(ex, "asset name");
212         }
213     }
214
215     /**
216      * Test invalid page name.
217      *
218      * @since 2.2
219      */

220
221     public void testInvalidPageName() throws Exception JavaDoc
222     {
223         try
224         {
225             parseApp("InvalidPageName.application");
226
227             unreachable();
228         }
229         catch (DocumentParseException ex)
230         {
231             checkException(ex, "in$valid");
232             checkException(ex, "page name");
233         }
234     }
235
236     /**
237      * Test invalid component type ("alias" in older parlance).
238      *
239      * @since 2.2
240      */

241
242     public void testInvalidComponentAlias() throws Exception JavaDoc
243     {
244         try
245         {
246             parseApp("InvalidComponentAlias.application");
247
248             unreachable();
249         }
250         catch (DocumentParseException ex)
251         {
252             checkException(ex, "Invalid$Component");
253             checkException(ex, "type");
254         }
255     }
256
257     /**
258      * Test invalid extension name.
259      *
260      * @since 2.2
261      */

262
263     public void testInvalidExtensionName() throws Exception JavaDoc
264     {
265         try
266         {
267             parseApp("InvalidExtensionName.application");
268
269             unreachable();
270         }
271         catch (DocumentParseException ex)
272         {
273             checkException(ex, "Invalid$Extension");
274             checkException(ex, "extension name");
275         }
276     }
277
278     /**
279      * Test case where the document does not have a DOCTYPE
280      *
281      * @since 2.2
282      */

283
284     public void testMissingDoctype() throws Exception JavaDoc
285     {
286         try
287         {
288             parseApp("MissingDoctype.application");
289
290             unreachable();
291         }
292         catch (DocumentParseException ex)
293         {
294             // XML parsers tend to generate different exception messages,
295
// so make the condition as unspecific as possible
296
checkException(ex, "DOCTYPE");
297         }
298     }
299
300     /**
301      * Test case where the public id of the document is not known.
302      */

303
304     public void testInvalidPublicId() throws Exception JavaDoc
305     {
306         try
307         {
308             parseApp("InvalidPublicId.application");
309
310             unreachable();
311         }
312         catch (DocumentParseException ex)
313         {
314             checkException(ex, "has an unexpected public id");
315         }
316     }
317
318     /**
319      * Test an an application specification can omit the name and engine-class attributes.
320      *
321      * @since 3.0
322      */

323
324     public void testNulledApplication() throws Exception JavaDoc
325     {
326         IApplicationSpecification spec = parseApp("NulledApplication.application");
327
328         assertNull(spec.getEngineClassName());
329         assertNull(spec.getName());
330         checkLine(spec, 25);
331
332         assertNotNull(spec.getSpecificationLocation());
333     }
334
335     /**
336      * Test new DTD 1.4 syntax for declaring components.
337      *
338      * @since 3.0
339      */

340
341     public void testComponentType() throws Exception JavaDoc
342     {
343         IApplicationSpecification spec = parseApp("ComponentType.application");
344
345         assertEquals("/path/Fred.jwc", spec.getComponentSpecificationPath("Fred"));
346     }
347
348     /**
349      * Test omitting the class name from a component specification (new, in DTD 1.4).
350      */

351
352     public void testNulledComponent() throws Exception JavaDoc
353     {
354         IComponentSpecification spec = parseComponent("NulledComponent.jwc");
355
356         assertNull(spec.getComponentClassName());
357         checkLine(spec, 22);
358
359         assertEquals(false, spec.isPageSpecification());
360         assertNotNull(spec.getSpecificationLocation());
361
362         assertEquals(false, spec.isDeprecated());
363     }
364
365     /**
366      * Test omitting the class name from a component specification (new, in DTD 1.4).
367      */

368
369     public void testNulledPage() throws Exception JavaDoc
370     {
371         IComponentSpecification spec = parsePage("NulledPage.page");
372
373         assertNull(spec.getComponentClassName());
374         checkLine(spec, 22);
375
376         assertEquals(true, spec.isPageSpecification());
377         assertNotNull(spec.getSpecificationLocation());
378     }
379
380     /**
381      * Test the value attribute for the property element (which is new in DTD 1.4).
382      *
383      * @since 3.0
384      */

385
386     public void testPropertyValue() throws Exception JavaDoc
387     {
388         IComponentSpecification spec = parsePage("PropertyValue.page");
389
390         checkLine(spec, 22);
391
392         assertEquals("rubble", spec.getProperty("barney"));
393         assertEquals("flintstone", spec.getProperty("wilma"));
394     }
395
396     /**
397      * Tests the new (in DTD 1.4) value attribute on static-binding element.
398      *
399      * @since 3.0
400      */

401
402     public void testStaticBindingValue() throws Exception JavaDoc
403     {
404         IComponentSpecification spec = parsePage("StaticBindingValue.page");
405
406         checkLine(spec, 22);
407
408         IContainedComponent c = spec.getComponent("c");
409
410         checkLine(c, 24);
411
412         IBindingSpecification b = c.getBinding("fred");
413         checkLine(b, 25);
414
415         assertEquals("literal:flintstone", b.getValue());
416
417         b = c.getBinding("barney");
418         checkLine(b, 26);
419
420         assertEquals("literal:rubble", b.getValue());
421
422         b = c.getBinding("rock");
423         checkLine(b, 27);
424         assertEquals("literal:hudson", b.getValue());
425     }
426
427     public void testAttributeAndBody() throws Exception JavaDoc
428     {
429         try
430         {
431             parsePage("AttributeAndBody.page");
432
433             unreachable();
434         }
435         catch (DocumentParseException ex)
436         {
437             checkException(
438                     ex,
439                     "It is not valid to specify a value for attribute 'value' of <static-binding> and provide a value in the body of the element.");
440         }
441     }
442
443     /**
444      * Tests the new (in DTD 1.4) value attribute on a configure element.
445      *
446      * @since 3.0
447      */

448
449     public void testConfigureValue() throws Exception JavaDoc
450     {
451         ILibrarySpecification spec = parseLib("ConfigureValue.library");
452
453         checkLine(spec, 22);
454         checkLine(spec.getExtensionSpecification("bedrock"), 24);
455
456         Bedrock bedrock = (Bedrock) spec.getExtension("bedrock", Bedrock.class);
457
458         assertEquals("flintstone", bedrock.getFred());
459     }
460
461     /**
462      * Tests the new &lt;listener-binding&gt; element in the 1.4 DTD.
463      *
464      * @since 3.0
465      */

466
467     public void testListenerBinding() throws Exception JavaDoc
468     {
469         IComponentSpecification spec = parsePage("ListenerBinding.page");
470
471         checkLine(spec, 22);
472         IContainedComponent c = spec.getComponent("c");
473
474         checkLine(c, 24);
475
476         IListenerBindingSpecification lbs = (ListenerBindingSpecification) c.getBinding("listener");
477
478         checkLine(lbs, 25);
479
480         String JavaDoc expectedScript = buildExpectedScript(new String JavaDoc[]
481         { "if page.isFormInputValid():", " cycle.page = \"Results\"", "else:",
482                 " page.message = \"Please fix errors before continuing.\";" });
483
484         assertEquals("jython", lbs.getLanguage());
485         assertEquals(expectedScript, lbs.getScript());
486     }
487
488     private String JavaDoc buildExpectedScript(String JavaDoc[] lines)
489     {
490         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
491
492         for (int i = 0; i < lines.length; i++)
493         {
494             if (i > 0)
495                 buffer.append("\n");
496
497             buffer.append(lines[i]);
498         }
499
500         return buffer.toString();
501     }
502
503     /** @since 3.0 * */
504
505     public void testPropertySpecifications() throws Exception JavaDoc
506     {
507         IComponentSpecification spec = parsePage("PropertySpecifications.page");
508
509         checkList("propertySpecificationNames", new String JavaDoc[]
510         { "bool", "init", "longInitialValue", "persist" }, spec.getPropertySpecificationNames());
511
512         IPropertySpecification ps = spec.getPropertySpecification("bool");
513         assertEquals("name", "bool", ps.getName());
514         assertEquals("persistent", false, ps.isPersistent());
515         assertEquals("type", "boolean", ps.getType());
516         assertNull("initialValue", ps.getInitialValue());
517         checkLine(ps, 24);
518
519         ps = spec.getPropertySpecification("init");
520         assertEquals("name", "init", ps.getName());
521         assertEquals("persistent", false, ps.isPersistent());
522         assertNull("type", ps.getType());
523
524         // Starting with release 4.0, the initial value is a binding reference
525
// with an appropriate prefix. In 3.0 it was always an OGNL expression.
526

527         assertEquals("initialValue", "ognl:pageName", ps.getInitialValue());
528         checkLine(ps, 26);
529
530         ps = spec.getPropertySpecification("persist");
531         assertEquals("name", "persist", ps.getName());
532         assertEquals("persistent", true, ps.isPersistent());
533         assertNull("type", ps.getType());
534         assertNull("initialValue", ps.getInitialValue());
535         checkLine(ps, 25);
536
537         ps = spec.getPropertySpecification("longInitialValue");
538         assertEquals("ognl:long.initial.value", ps.getInitialValue());
539
540         ps = spec.getPropertySpecification("unknown");
541
542         assertNull("Unknown PropertySpecification", ps);
543     }
544
545     /** @since 3.0 * */
546
547     public void testMissingRequiredExtendedAttribute() throws Exception JavaDoc
548     {
549         try
550         {
551             parsePage("MissingRequiredExtendedAttribute.page");
552
553             unreachable();
554         }
555         catch (DocumentParseException ex)
556         {
557             checkException(
558                     ex,
559                     "Element <binding> does not specify a value for attribute 'expression', or contain a body value.");
560         }
561     }
562
563     /** @since 3.0 * */
564
565     public void testMessageBeanInitializer() throws Exception JavaDoc
566     {
567         IComponentSpecification spec = parsePage("MessageBeanInitializer.page");
568
569         IBeanSpecification bs = spec.getBeanSpecification("fred");
570         checkLine(bs, 24);
571         BindingBeanInitializer i = (BindingBeanInitializer) bs.getInitializers().get(0);
572
573         assertEquals("barney", i.getPropertyName());
574         assertEquals("message:rubble", i.getBindingReference());
575         checkLine(i, 25);
576     }
577
578     /**
579      * Tests the DTD 3.0 <set-property>element
580      *
581      * @since 4.0
582      */

583
584     public void testExpressionBeanInitializer() throws Exception JavaDoc
585     {
586         IComponentSpecification spec = parsePage("ExpressionBeanInitializer_3_0.page");
587
588         IBeanSpecification bs = spec.getBeanSpecification("zebean");
589
590         BindingBeanInitializer i = (BindingBeanInitializer) bs.getInitializers().get(0);
591
592         assertEquals("barney", i.getPropertyName());
593         assertEquals("ognl:rubble", i.getBindingReference());
594
595         i = (BindingBeanInitializer) bs.getInitializers().get(1);
596
597         assertEquals("fred", i.getPropertyName());
598         assertEquals("ognl:flintstone", i.getBindingReference());
599     }
600
601     /** @since 4.0 */
602
603     public void testBeanSet() throws Exception JavaDoc
604     {
605         IComponentSpecification spec = parsePage("BeanSet.page");
606         IBeanSpecification bs = spec.getBeanSpecification("target");
607
608         BindingBeanInitializer i = (BindingBeanInitializer) bs.getInitializers().get(0);
609
610         assertEquals("literal", i.getPropertyName());
611         assertEquals("literal-string", i.getBindingReference());
612
613     }
614
615     public void testInheritInformalParameters() throws Exception JavaDoc
616     {
617         IComponentSpecification spec = parseComponent("TestInheritInformal.jwc");
618
619         IContainedComponent border = spec.getComponent("border");
620         assertEquals(border.getInheritInformalParameters(), false);
621
622         IContainedComponent textField = spec.getComponent("textField");
623         assertEquals(textField.getInheritInformalParameters(), true);
624     }
625
626     /** @since 4.0 */
627
628     public void testConfigureExtension() throws Exception JavaDoc
629     {
630         IApplicationSpecification spec = parseApp("ConfigureExtension.application");
631         IExtensionSpecification es = spec.getExtensionSpecification("my-extension");
632
633         // Note: this is in transition; under 3.0 and earlier, the spec parser was
634
// responsible for converting values into object types ... that is now
635
// done futher down stream.
636

637         assertEquals("-227", es.getConfiguration().get("long"));
638         assertEquals("22.7", es.getConfiguration().get("double"));
639         assertEquals("true", es.getConfiguration().get("boolean"));
640         assertEquals("An extended string.", es.getConfiguration().get("string"));
641     }
642
643     public void testConfigureExtensionProperty() throws Exception JavaDoc
644     {
645         IApplicationSpecification spec = parseApp("ConfigureExtension.application");
646         IExtensionSpecification es = spec.getExtensionSpecification("my-extension");
647
648         assertEquals("my-value", es.getProperty("my-property"));
649     }
650
651     /** @since 4.0 */
652
653     public void testComponentProperty() throws Exception JavaDoc
654     {
655         IComponentSpecification cs = parseComponent("ComponentProperty.jwc");
656
657         IContainedComponent cc = cs.getComponent("body");
658
659         assertEquals("my-value", cc.getProperty("my-property"));
660     }
661
662     /** @since 4.0 */
663
664     public void testComponentInjectProperty() throws Exception JavaDoc
665     {
666         IComponentSpecification cs = parseComponent("ComponentInjectProperty.jwc");
667
668         IContainedComponent cc = cs.getComponent("body");
669
670         assertEquals("myProperty", cc.getPropertyName());
671
672         cc = cs.getComponent("fred");
673
674         assertNull(cc.getPropertyName());
675     }
676
677     /** @since 4.0 */
678
679     public void testBeanDescription() throws Exception JavaDoc
680     {
681         IComponentSpecification cs = parseComponent("BeanDescription.jwc");
682         IBeanSpecification bs = cs.getBeanSpecification("mybean");
683
684         assertEquals("Description of mybean.", bs.getDescription());
685         assertNotNull(bs.getLocation());
686     }
687
688     /** @since 4.0 */
689
690     public void testBeanProperty() throws Exception JavaDoc
691     {
692         IComponentSpecification cs = parseComponent("BeanDescription.jwc");
693         IBeanSpecification bs = cs.getBeanSpecification("mybean");
694
695         assertEquals("myvalue", bs.getProperty("myproperty"));
696     }
697
698     /**
699      * @since 4.0
700      */

701
702     public void testBeanInject() throws Exception JavaDoc
703     {
704         IComponentSpecification cs = parseComponent("BeanInject.jwc");
705         IBeanSpecification bs = cs.getBeanSpecification("bean");
706         assertEquals("myProperty", bs.getPropertyName());
707     }
708
709     /**
710      * @since 4.0
711      */

712
713     public void testBeanInitializer() throws Exception JavaDoc
714     {
715         IComponentSpecification cs = parseComponent("BeanInitializer.jwc");
716         IBeanSpecification bs = cs.getBeanSpecification("bean");
717
718         List JavaDoc l = bs.getInitializers();
719         LightweightBeanInitializer lbi = (LightweightBeanInitializer) l.get(0);
720
721         assertEquals("foo=bar", lbi.getPropertyName());
722     }
723
724     /** @since 4.0 */
725
726     public void testLibraryDescription() throws Exception JavaDoc
727     {
728         ILibrarySpecification ls = parseLib("LibraryDescription.library");
729
730         assertEquals("Often, these are just placeholders.", ls.getDescription());
731     }
732
733     /** @since 4.0 */
734
735     public void testPageDescription() throws Exception JavaDoc
736     {
737         IComponentSpecification spec = parsePage("PageDescription.page");
738
739         assertEquals("Description of this page.", spec.getDescription());
740     }
741
742     /**
743      * Excercies the check that the correct root element is used.
744      *
745      * @since 4.0
746      */

747
748     public void testRootElementMismatch() throws Exception JavaDoc
749     {
750         try
751         {
752             parseComponent("NulledPage.page");
753             unreachable();
754         }
755         catch (Exception JavaDoc ex)
756         {
757             checkException(
758                     ex,
759                     "Incorrect document type; expected page-specification but received component-specification.");
760         }
761     }
762
763     /**
764      * Checks to make sure that a application or library may not defined a lbirary with id
765      * 'framework'.
766      *
767      * @since 4.0
768      */

769
770     public void testLibraryFrameworkNamespace() throws Exception JavaDoc
771     {
772         try
773         {
774             parseLib("LibraryFrameworkNamespace.library");
775             unreachable();
776         }
777         catch (Exception JavaDoc ex)
778         {
779             checkException(ex, "The library id 'framework' is reserved and may not be used.");
780         }
781     }
782
783     /**
784      * Tests that a &lt;component&gt; element may not have both type and copy-of attributes.
785      *
786      * @since 4.0
787      */

788
789     public void testComponentWithTypeAndCopyOf() throws Exception JavaDoc
790     {
791         try
792         {
793             parseComponent("ComponentWithTypeAndCopyOf.jwc");
794             unreachable();
795         }
796         catch (Exception JavaDoc ex)
797         {
798             checkException(ex, "Contained component bad contains both type and copy-of attributes.");
799         }
800     }
801
802     /**
803      * Tests that &lt;component&gt; must have either type or copy-of attribute.
804      *
805      * @since 4.0
806      */

807
808     public void testComponentWithoutType() throws Exception JavaDoc
809     {
810         try
811         {
812             parseComponent("ComponentWithoutType.jwc");
813             unreachable();
814         }
815         catch (Exception JavaDoc ex)
816         {
817             checkException(
818                     ex,
819                     "Contained component bad does not specify attribute type or copy-of.");
820         }
821     }
822
823     /**
824      * Tests the use of copy-of attribute inside &lt;component&gt;.
825      *
826      * @since 4.0
827      */

828
829     public void testComponentCopyOf() throws Exception JavaDoc
830     {
831         IComponentSpecification cs = parseComponent("ComponentCopyOf.jwc");
832
833         IContainedComponent source = cs.getComponent("source");
834         IContainedComponent copy = cs.getComponent("copy");
835         IContainedComponent override = cs.getComponent("override");
836
837         assertEquals("Insert", source.getType());
838         assertEquals("Insert", copy.getType());
839         assertEquals("Insert", override.getType());
840
841         IBindingSpecification b = source.getBinding("value");
842
843         assertEquals(BindingType.PREFIXED, b.getType());
844         assertEquals("ognl:date", b.getValue());
845
846         assertSame(b, copy.getBinding("value"));
847
848         IBindingSpecification b2 = override.getBinding("value");
849         assertEquals("ognl:tomorrow", b2.getValue());
850
851         b = copy.getBinding("foo");
852
853         assertSame(b, override.getBinding("foo"));
854
855         b = copy.getBinding("formatter");
856
857         assertSame(b, override.getBinding("formatter"));
858     }
859
860     /**
861      * And here's what happens when copy-of doesn't match a known component.
862      *
863      * @since 4.0
864      */

865     public void testComponentBadCopy()
866     {
867         try
868         {
869             parseComponent("ComponentBadCopy.jwc");
870             unreachable();
871         }
872         catch (Exception JavaDoc ex)
873         {
874             checkException(ex, "Unable to copy component missing, which does not exist.");
875         }
876     }
877
878     /**
879      * Check that &lt;service&gt; elements are ignored properly.
880      *
881      * @since 4.0
882      */

883
884     public void testServiceElement() throws Exception JavaDoc
885     {
886         interceptLogging("org.apache.tapestry");
887
888         parseLib("ServiceElement.library");
889
890         assertLoggedMessagePattern("The <service> element is no longer supported");
891     }
892
893     /** @since 4.0 */
894     public void testMeta() throws Exception JavaDoc
895     {
896         ILibrarySpecification spec = parseLib("Meta.library");
897
898         assertEquals("bar", spec.getProperty("foo"));
899         assertEquals("long value", spec.getProperty("long"));
900     }
901
902     /** @since 4.0 */
903     public void testInject() throws Exception JavaDoc
904     {
905         IComponentSpecification spec = parseComponent("Inject.jwc");
906
907         List JavaDoc l = spec.getInjectSpecifications();
908
909         assertEquals(2, l.size());
910
911         InjectSpecification i1 = (InjectSpecification) l.get(0);
912
913         assertEquals("fred", i1.getProperty());
914         assertEquals("object", i1.getType());
915         assertEquals("flintstone", i1.getObject());
916         assertNotNull(i1.getLocation());
917
918         InjectSpecification i2 = (InjectSpecification) l.get(1);
919         assertEquals("barney", i2.getProperty());
920         assertEquals("state", i2.getType());
921         assertEquals("rubble", i2.getObject());
922         assertNotNull(i2.getLocation());
923     }
924
925     /**
926      * Test that the new &lt;property&gt; element (was &lt;property-specification&gt; in release
927      * 3.0) works correctly.
928      *
929      * @since 4.0
930      */

931
932     public void testProperty() throws Exception JavaDoc
933     {
934         IComponentSpecification spec = parsePage("Property.page");
935
936         checkList("propertySpecificationNames", new String JavaDoc[]
937         { "bool", "init", "longInit", "persist" }, spec.getPropertySpecificationNames());
938
939         IPropertySpecification ps = spec.getPropertySpecification("bool");
940         assertEquals("name", "bool", ps.getName());
941         assertEquals("persistent", false, ps.isPersistent());
942
943         // In a 4.0 DTD, type is always null.
944
assertNull("type", ps.getType());
945
946         // Note that no prefix is added. Initial value will be a string literal,
947
// or have a prefix and be something else.
948

949         assertNull("initialValue", ps.getInitialValue());
950         checkLine(ps, 24);
951
952         ps = spec.getPropertySpecification("init");
953         assertEquals("name", "init", ps.getName());
954         assertEquals("persistent", false, ps.isPersistent());
955
956         assertEquals("initialValue", "ognl:pageName", ps.getInitialValue());
957         checkLine(ps, 26);
958
959         ps = spec.getPropertySpecification("persist");
960         assertEquals("name", "persist", ps.getName());
961         assertEquals("persistent", true, ps.isPersistent());
962         assertNull("initialValue", ps.getInitialValue());
963         checkLine(ps, 25);
964
965         ps = spec.getPropertySpecification("longInit");
966         assertEquals("message:long-init-key", ps.getInitialValue());
967
968         ps = spec.getPropertySpecification("unknown");
969
970         assertNull("Unknown PropertySpecification", ps);
971     }
972
973     /**
974      * Tests parameters specification from a 3.0 DTD
975      *
976      * @since 4.0
977      */

978
979     public void testParameter_3_0() throws Exception JavaDoc
980     {
981         IComponentSpecification spec = parseComponent("Parameter_3_0.jwc");
982
983         IParameterSpecification ps = spec.getParameter("noDefault");
984
985         assertEquals("noDefault", ps.getPropertyName());
986         assertEquals("noDefault", ps.getParameterName());
987         assertEquals(true, ps.isRequired());
988         assertEquals("bar", ps.getType());
989         assertNull(ps.getDefaultValue());
990         assertNull(ps.getDefaultBindingType());
991         assertEquals(false, ps.isDeprecated());
992
993         ps = spec.getParameter("withDefault");
994         assertEquals("withDefault", ps.getParameterName());
995         assertNull(ps.getType());
996         assertEquals(false, ps.isRequired());
997
998         // For 3.0 DTDs, where the default value was always an OGNL expression,
999
// the parser will provide the "ognl:" prefix.
1000

1001        assertEquals("ognl:an.expression", ps.getDefaultValue());
1002
1003        ps = spec.getParameter("withDescription");
1004        assertEquals("A parameter with a description.", ps.getDescription());
1005
1006        ps = spec.getParameter("altName");
1007        assertEquals("altNameParameter", ps.getPropertyName());
1008
1009        ps = spec.getParameter("directionIn");
1010        assertEquals(true, ps.getCache());
1011
1012        ps = spec.getParameter("directionAuto");
1013        assertEquals(false, ps.getCache());
1014    }
1015
1016    /**
1017     * Tests the new way default-value is interpreted (as a binding-like value, prefixed to indicate
1018     * type).
1019     *
1020     * @since 4.0
1021     */

1022
1023    public void testParameter() throws Exception JavaDoc
1024    {
1025        IComponentSpecification spec = parseComponent("Parameter.jwc");
1026
1027        IParameterSpecification ps = spec.getParameter("noDefault");
1028
1029        assertNull(ps.getDefaultValue());
1030        assertNull(ps.getDefaultBindingType());
1031        assertEquals(true, ps.getCache());
1032        assertTrue(ps.getAliasNames().isEmpty());
1033        assertEquals(false, ps.isDeprecated());
1034
1035        ps = spec.getParameter("literalDefault");
1036
1037        assertEquals("literal-value", ps.getDefaultValue());
1038
1039        ps = spec.getParameter("expressionDefault");
1040
1041        assertEquals("ognl:an.expression", ps.getDefaultValue());
1042
1043        ps = spec.getParameter("defaultBindingType");
1044
1045        assertEquals("ognl", ps.getDefaultBindingType());
1046
1047        ps = spec.getParameter("noCache");
1048        assertEquals(false, ps.getCache());
1049
1050        ps = spec.getParameter("withAliases");
1051        assertListsEqual(new String JavaDoc[]
1052        { "fred", "barney" }, ps.getAliasNames().toArray());
1053
1054        assertSame(ps, spec.getParameter("fred"));
1055        assertSame(ps, spec.getParameter("barney"));
1056
1057        ps = spec.getParameter("deprecated");
1058        assertEquals(true, ps.isDeprecated());
1059    }
1060
1061    /**
1062     * Tests that assets read using the 3.0 DTD are converted properly into paths with the proper
1063     * prefix.
1064     *
1065     * @since 4.0
1066     */

1067    public void testAssets_3_0() throws Exception JavaDoc
1068    {
1069        IComponentSpecification cs = parsePage("Assets_3_0.page");
1070
1071        IAssetSpecification as = cs.getAsset("mycontext");
1072
1073        assertEquals("context:path/to/context", as.getPath());
1074
1075        as = cs.getAsset("myprivate");
1076
1077        assertEquals("classpath:path/to/private", as.getPath());
1078
1079        as = cs.getAsset("myexternal");
1080
1081        assertEquals("http://myexternal/asset", as.getPath());
1082
1083        assertListsEqual(new String JavaDoc[]
1084        { "mycontext", "myexternal", "myprivate" }, cs.getAssetNames());
1085    }
1086
1087    /** @since 4.0 */
1088
1089    public void testAssets() throws Exception JavaDoc
1090    {
1091        IComponentSpecification cs = parsePage("Assets.page");
1092
1093        IAssetSpecification as = cs.getAsset("myasset");
1094
1095        assertEquals("path/to/asset", as.getPath());
1096        assertEquals("myProperty", as.getPropertyName());
1097    }
1098
1099    /** @since 4.0 */
1100
1101    public void testDeprecatedComponent() throws Exception JavaDoc
1102    {
1103        IComponentSpecification cs = parseComponent("DeprecatedComponent.jwc");
1104
1105        assertEquals(true, cs.isDeprecated());
1106    }
1107}
Popular Tags