KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > TapestryTestCase


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;
16
17 import java.util.Arrays JavaDoc;
18 import java.util.List JavaDoc;
19
20 import junit.framework.AssertionFailedError;
21
22 import org.apache.hivemind.ClassResolver;
23 import org.apache.hivemind.Location;
24 import org.apache.hivemind.Registry;
25 import org.apache.hivemind.Resource;
26 import org.apache.hivemind.impl.DefaultClassResolver;
27 import org.apache.hivemind.impl.RegistryBuilder;
28 import org.apache.hivemind.test.HiveMindTestCase;
29 import org.apache.hivemind.util.ClasspathResource;
30 import org.apache.tapestry.IBinding;
31 import org.apache.tapestry.IComponent;
32 import org.apache.tapestry.Tapestry;
33 import org.apache.tapestry.binding.BindingSource;
34 import org.apache.tapestry.binding.LiteralBinding;
35 import org.apache.tapestry.coerce.ValueConverter;
36 import org.apache.tapestry.parse.SpecificationParser;
37 import org.apache.tapestry.spec.IApplicationSpecification;
38 import org.apache.tapestry.spec.IComponentSpecification;
39 import org.apache.tapestry.spec.ILibrarySpecification;
40 import org.apache.tapestry.util.IPropertyHolder;
41
42 /**
43  * Base class for Tapestry test cases.
44  *
45  * @author Howard Lewis Ship
46  * @since 2.2
47  */

48
49 public abstract class TapestryTestCase extends HiveMindTestCase
50 {
51     protected static final boolean IS_JDK13 = System.getProperty("java.specification.version")
52             .equals("1.3");
53
54     private ClassResolver _resolver = new DefaultClassResolver();
55
56     /** @since 4.0 */
57     private ValueConverter _valueConverter = new ValueConverter()
58     {
59         public Object JavaDoc coerceValue(Object JavaDoc value, Class JavaDoc desiredType)
60         {
61             return value;
62         }
63     };
64
65     /** @since 4.0 */
66     private class BindingSourceFixture implements BindingSource
67     {
68
69         public IBinding createBinding(IComponent component, String JavaDoc description, String JavaDoc reference,
70                 String JavaDoc defaultBindingType, Location location)
71         {
72             return new LiteralBinding(description, _valueConverter, location, reference);
73         }
74     }
75
76     protected IComponentSpecification parseComponent(String JavaDoc simpleName) throws Exception JavaDoc
77     {
78         SpecificationParser parser = new SpecificationParser(_resolver);
79
80         Resource location = getSpecificationResourceLocation(simpleName);
81
82         return parser.parseComponentSpecification(location);
83     }
84
85     protected IComponentSpecification parsePage(String JavaDoc simpleName) throws Exception JavaDoc
86     {
87         SpecificationParser parser = new SpecificationParser(_resolver);
88
89         parser.setBindingSource(new BindingSourceFixture());
90
91         Resource location = getSpecificationResourceLocation(simpleName);
92
93         return parser.parsePageSpecification(location);
94     }
95
96     protected IApplicationSpecification parseApp(String JavaDoc simpleName) throws Exception JavaDoc
97     {
98         SpecificationParser parser = new SpecificationParser(_resolver);
99
100         parser.setValueConverter(createValueConverter());
101
102         Resource location = getSpecificationResourceLocation(simpleName);
103
104         return parser.parseApplicationSpecification(location);
105     }
106
107     protected Resource getSpecificationResourceLocation(String JavaDoc simpleName)
108     {
109         String JavaDoc adjustedClassName = "/" + getClass().getName().replace('.', '/') + ".class";
110
111         Resource classResource = new ClasspathResource(_resolver, adjustedClassName);
112
113         return classResource.getRelativeResource(simpleName);
114     }
115
116     protected ILibrarySpecification parseLib(String JavaDoc simpleName) throws Exception JavaDoc
117     {
118         SpecificationParser parser = new SpecificationParser(_resolver);
119
120         parser.setValueConverter(createValueConverter());
121
122         Resource location = getSpecificationResourceLocation(simpleName);
123
124         return parser.parseLibrarySpecification(location);
125     }
126
127     public static void checkList(String JavaDoc propertyName, Object JavaDoc[] expected, Object JavaDoc[] actual)
128     {
129         checkList(propertyName, expected, Arrays.asList(actual));
130     }
131
132     public static void checkList(String JavaDoc propertyName, Object JavaDoc[] expected, List JavaDoc actual)
133     {
134         int count = Tapestry.size(actual);
135
136         assertEquals(propertyName + " element count", expected.length, count);
137
138         for (int i = 0; i < count; i++)
139         {
140             assertEquals("propertyName[" + i + "]", expected[i], actual.get(i));
141         }
142     }
143
144     public static void checkProperty(IPropertyHolder h, String JavaDoc propertyName, String JavaDoc expectedValue)
145     {
146         assertEquals("Property " + propertyName + ".", expectedValue, h.getProperty(propertyName));
147     }
148
149     public static void checkException(Throwable JavaDoc ex, String JavaDoc string)
150     {
151         if (ex.getMessage().indexOf(string) >= 0)
152             return;
153
154         throw new AssertionFailedError("Exception " + ex + " does not contain sub-string '"
155                 + string + "'.");
156     }
157
158     private static ValueConverter _sharedValueConverter;
159
160     protected ValueConverter createValueConverter()
161     {
162         // Only build the Registry the first time this is called. The same Registry
163
// can then be used for any remaining calls.
164

165         if (_sharedValueConverter == null)
166         {
167
168             Registry r = RegistryBuilder.constructDefaultRegistry();
169
170             _sharedValueConverter = (ValueConverter) r.getService(
171                     "tapestry.coerce.ValueConverter",
172                     ValueConverter.class);
173         }
174
175         return _sharedValueConverter;
176     }
177 }
Popular Tags