1 15 package org.apache.tapestry.junit; 16 17 import java.util.Arrays ; 18 import java.util.List ; 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 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 57 private ValueConverter _valueConverter = new ValueConverter() 58 { 59 public Object coerceValue(Object value, Class desiredType) 60 { 61 return value; 62 } 63 }; 64 65 66 private class BindingSourceFixture implements BindingSource 67 { 68 69 public IBinding createBinding(IComponent component, String description, String reference, 70 String defaultBindingType, Location location) 71 { 72 return new LiteralBinding(description, _valueConverter, location, reference); 73 } 74 } 75 76 protected IComponentSpecification parseComponent(String simpleName) throws Exception 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 simpleName) throws Exception 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 simpleName) throws Exception 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 simpleName) 108 { 109 String 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 simpleName) throws Exception 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 propertyName, Object [] expected, Object [] actual) 128 { 129 checkList(propertyName, expected, Arrays.asList(actual)); 130 } 131 132 public static void checkList(String propertyName, Object [] expected, List 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 propertyName, String expectedValue) 145 { 146 assertEquals("Property " + propertyName + ".", expectedValue, h.getProperty(propertyName)); 147 } 148 149 public static void checkException(Throwable ex, String 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 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 |