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 org.apache.tapestry.junit; 016 017 import org.apache.hivemind.ClassResolver; 018 import org.apache.hivemind.Location; 019 import org.apache.hivemind.Registry; 020 import org.apache.hivemind.Resource; 021 import org.apache.hivemind.impl.DefaultClassResolver; 022 import org.apache.hivemind.impl.RegistryBuilder; 023 import org.apache.hivemind.util.ClasspathResource; 024 import org.apache.tapestry.BaseComponentTestCase; 025 import org.apache.tapestry.IBinding; 026 import org.apache.tapestry.IComponent; 027 import org.apache.tapestry.Tapestry; 028 import org.apache.tapestry.binding.BindingSource; 029 import org.apache.tapestry.binding.LiteralBinding; 030 import org.apache.tapestry.coerce.ValueConverter; 031 import org.apache.tapestry.parse.SpecificationParser; 032 import org.apache.tapestry.spec.IApplicationSpecification; 033 import org.apache.tapestry.spec.IComponentSpecification; 034 import org.apache.tapestry.spec.ILibrarySpecification; 035 import org.apache.tapestry.spec.IParameterSpecification; 036 import org.apache.tapestry.util.IPropertyHolder; 037 038 import java.util.Arrays; 039 import java.util.List; 040 041 /** 042 * Base class for Tapestry test cases. 043 * 044 * @author Howard Lewis Ship 045 * @since 2.2 046 */ 047 public abstract class TapestryTestCase extends BaseComponentTestCase 048 { 049 protected static final boolean IS_JDK13 = System.getProperty("java.specification.version").equals("1.3"); 050 051 private ClassResolver _resolver = new DefaultClassResolver(); 052 053 /** @since 4.0 */ 054 private ValueConverter _valueConverter = new ValueConverter() 055 { 056 public Object coerceValue(Object value, Class desiredType) 057 { 058 return value; 059 } 060 }; 061 062 /** @since 4.0 */ 063 private class BindingSourceFixture implements BindingSource 064 { 065 066 public IBinding createBinding(IComponent component, String description, String reference, 067 String defaultBindingType, Location location) 068 { 069 return new LiteralBinding(description, _valueConverter, location, reference); 070 } 071 072 public IBinding createBinding(IComponent component, IParameterSpecification param, String description, String reference, 073 String defaultBindingType, Location location) 074 { 075 return new LiteralBinding(description, _valueConverter, location, reference); 076 } 077 } 078 079 /** @since 4.0 */ 080 protected BindingSourceFixture newBindingSource() 081 { 082 return new BindingSourceFixture(); 083 } 084 085 protected IComponentSpecification parseComponent(String simpleName) throws Exception 086 { 087 SpecificationParser parser = new SpecificationParser(_resolver); 088 089 Resource location = getSpecificationResourceLocation(simpleName); 090 091 return parser.parseComponentSpecification(location); 092 } 093 094 protected IComponentSpecification parsePage(String simpleName) throws Exception 095 { 096 SpecificationParser parser = new SpecificationParser(_resolver); 097 098 parser.setBindingSource(newBindingSource()); 099 100 Resource location = getSpecificationResourceLocation(simpleName); 101 102 return parser.parsePageSpecification(location); 103 } 104 105 protected IApplicationSpecification parseApp(String simpleName) throws Exception 106 { 107 SpecificationParser parser = new SpecificationParser(_resolver); 108 109 parser.setValueConverter(createValueConverter()); 110 111 Resource location = getSpecificationResourceLocation(simpleName); 112 113 return parser.parseApplicationSpecification(location); 114 } 115 116 protected Resource getSpecificationResourceLocation(String simpleName) 117 { 118 String adjustedClassName = "/" + getClass().getName().replace('.', '/') + ".class"; 119 120 Resource classResource = new ClasspathResource(_resolver, adjustedClassName); 121 122 return classResource.getRelativeResource(simpleName); 123 } 124 125 protected ILibrarySpecification parseLib(String simpleName) throws Exception 126 { 127 SpecificationParser parser = new SpecificationParser(_resolver); 128 129 parser.setValueConverter(createValueConverter()); 130 131 Resource location = getSpecificationResourceLocation(simpleName); 132 133 return parser.parseLibrarySpecification(location); 134 } 135 136 public static void checkList(String propertyName, Object[] expected, Object[] actual) 137 { 138 checkList(propertyName, expected, Arrays.asList(actual)); 139 } 140 141 public static void checkList(String propertyName, Object[] expected, List actual) 142 { 143 int count = Tapestry.size(actual); 144 145 assertEquals(expected.length, count); 146 147 for (int i = 0; i < count; i++) 148 { 149 assertEquals(expected[i], actual.get(i)); 150 } 151 } 152 153 public static void checkProperty(IPropertyHolder h, String propertyName, String expectedValue) 154 { 155 assertEquals(expectedValue, h.getProperty(propertyName)); 156 } 157 158 public static void checkException(Throwable ex, String string) 159 { 160 if (ex.getMessage().indexOf(string) >= 0) 161 return; 162 163 throw new AssertionError("Exception " + ex + " does not contain sub-string '" 164 + string + "'."); 165 } 166 167 private static ValueConverter _sharedValueConverter; 168 169 protected ValueConverter createValueConverter() 170 { 171 // Only build the Registry the first time this is called. The same Registry 172 // can then be used for any remaining calls. 173 174 try { 175 176 if (_sharedValueConverter == null) 177 { 178 Registry r = RegistryBuilder.constructDefaultRegistry(); 179 180 _sharedValueConverter = (ValueConverter) r.getService("tapestry.coerce.ValueConverter", ValueConverter.class); 181 } 182 183 return _sharedValueConverter; 184 } catch (Throwable t) { 185 throw new RuntimeException(t); 186 } 187 } 188 189 protected IComponent newComponent() 190 { 191 return newMock(IComponent.class); 192 } 193 }