1 15 package org.apache.tapestry.components; 16 17 import java.io.CharArrayWriter ; 18 import java.io.PrintWriter ; 19 20 import org.apache.hivemind.Location; 21 import org.apache.hivemind.test.HiveMindTestCase; 22 import org.apache.tapestry.IBinding; 23 import org.apache.tapestry.IComponent; 24 import org.apache.tapestry.IMarkupWriter; 25 import org.apache.tapestry.IPage; 26 import org.apache.tapestry.IRender; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.markup.AsciiMarkupFilter; 29 import org.apache.tapestry.markup.MarkupWriterImpl; 30 import org.apache.tapestry.spec.IComponentSpecification; 31 import org.apache.tapestry.spec.IParameterSpecification; 32 import org.apache.tapestry.test.Creator; 33 import org.easymock.MockControl; 34 35 39 public abstract class BaseComponentTestCase extends HiveMindTestCase 40 { 41 private Creator _creator; 42 43 protected Creator getCreator() 44 { 45 if (_creator == null) 46 _creator = new Creator(); 47 48 return _creator; 49 } 50 51 protected CharArrayWriter _charArrayWriter; 52 53 protected IMarkupWriter newBufferWriter() 54 { 55 _charArrayWriter = new CharArrayWriter (); 56 PrintWriter pw = new PrintWriter (_charArrayWriter); 57 58 return new MarkupWriterImpl("text/html", pw, new AsciiMarkupFilter()); 59 } 60 61 protected void assertBuffer(String expected) 62 { 63 String actual = _charArrayWriter.toString(); 64 65 assertEquals("Buffered markup writer content.", expected, actual); 66 67 _charArrayWriter.reset(); 68 } 69 70 protected Object newInstance(Class componentClass) 71 { 72 return newInstance(componentClass, null); 73 } 74 75 protected Object newInstance(Class componentClass, String propertyName, Object propertyValue) 76 { 77 return getCreator().newInstance(componentClass, new Object [] 78 { propertyName, propertyValue }); 79 } 80 81 protected Object newInstance(Class componentClass, Object [] properties) 82 { 83 return getCreator().newInstance(componentClass, properties); 84 } 85 86 protected IRequestCycle newCycle() 87 { 88 return (IRequestCycle) newMock(IRequestCycle.class); 89 } 90 91 protected IRequestCycle newCycle(boolean rewinding) 92 { 93 MockControl control = newControl(IRequestCycle.class); 94 IRequestCycle cycle = (IRequestCycle) control.getMock(); 95 96 trainIsRewinding(control, cycle, rewinding); 97 98 return cycle; 99 } 100 101 protected void trainIsRewinding(MockControl control, IRequestCycle cycle, boolean rewinding) 102 { 103 cycle.isRewinding(); 104 control.setReturnValue(rewinding); 105 } 106 107 protected IRequestCycle newCycle(String pageName, IPage page) 108 { 109 MockControl control = newControl(IRequestCycle.class); 110 IRequestCycle cycle = (IRequestCycle) control.getMock(); 111 112 cycle.getPage(pageName); 113 control.setReturnValue(page); 114 115 return cycle; 116 } 117 118 protected IMarkupWriter newWriter() 119 { 120 return (IMarkupWriter) newMock(IMarkupWriter.class); 121 } 122 123 protected IBinding newBinding(Object value) 124 { 125 MockControl control = newControl(IBinding.class); 126 IBinding binding = (IBinding) control.getMock(); 127 128 binding.getObject(); 129 control.setReturnValue(value); 130 131 return binding; 132 } 133 134 protected IBinding newBinding(Location location) 135 { 136 MockControl control = newControl(IBinding.class); 137 IBinding binding = (IBinding) control.getMock(); 138 139 binding.getLocation(); 140 control.setReturnValue(location); 141 142 return binding; 143 } 144 145 protected IComponent newComponent(String extendedId, Location location) 146 { 147 MockControl control = newControl(IComponent.class); 148 IComponent component = (IComponent) control.getMock(); 149 150 component.getExtendedId(); 151 control.setReturnValue(extendedId); 152 153 component.getLocation(); 154 control.setReturnValue(location); 155 156 return component; 157 } 158 159 protected IComponentSpecification newSpec(String parameterName, IParameterSpecification pspec) 160 { 161 MockControl control = newControl(IComponentSpecification.class); 162 IComponentSpecification spec = (IComponentSpecification) control.getMock(); 163 164 spec.getParameter(parameterName); 165 control.setReturnValue(pspec); 166 167 return spec; 168 } 169 170 protected IRender newRender() 171 { 172 return (IRender) newMock(IRender.class); 173 } 174 175 protected IPage newPage() 176 { 177 return (IPage) newMock(IPage.class); 178 } 179 180 protected IPage newPage(String name) 181 { 182 MockControl control = newControl(IPage.class); 183 IPage page = (IPage) control.getMock(); 184 185 page.getPageName(); 186 control.setReturnValue(name); 187 188 return page; 189 } 190 191 protected void trainGetAttribute(MockControl cyclec, IRequestCycle cycle, String key, Object value) 192 { 193 cycle.getAttribute(key); 194 cyclec.setReturnValue(value); 195 } 196 197 } | Popular Tags |