1 15 package org.apache.tapestry; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Location; 19 import org.apache.hivemind.test.HiveMindTestCase; 20 import org.easymock.MockControl; 21 22 28 public class TestTapestryUtils extends HiveMindTestCase 29 { 30 private IRequestCycle newCycle() 31 { 32 return (IRequestCycle) newMock(IRequestCycle.class); 33 } 34 35 private IRequestCycle newCycle(String key, Object attribute) 36 { 37 MockControl control = newControl(IRequestCycle.class); 38 IRequestCycle cycle = (IRequestCycle) control.getMock(); 39 40 cycle.getAttribute(key); 41 control.setReturnValue(attribute); 42 43 return cycle; 44 } 45 46 private IForm newForm() 47 { 48 return (IForm) newMock(IForm.class); 49 } 50 51 private PageRenderSupport newPageRenderSupport() 52 { 53 return (PageRenderSupport) newMock(PageRenderSupport.class); 54 } 55 56 private IComponent newComponent() 57 { 58 return (IComponent) newMock(IComponent.class); 59 } 60 61 public void testStoreUniqueAttributeSuccess() 62 { 63 Object newInstance = new Object (); 64 65 MockControl control = newControl(IRequestCycle.class); 66 IRequestCycle cycle = (IRequestCycle) control.getMock(); 67 68 String key = "foo.bar.Baz"; 69 70 cycle.getAttribute(key); 71 control.setReturnValue(null); 72 73 cycle.setAttribute(key, newInstance); 74 75 replayControls(); 76 77 TapestryUtils.storeUniqueAttribute(cycle, key, newInstance); 78 79 verifyControls(); 80 } 81 82 public void testStoreUniqueAttributeFailure() 83 { 84 Object existing = "*EXISTING*"; 85 Object newInstance = "*NEW*"; 86 87 String key = "foo.bar.Baz"; 88 89 IRequestCycle cycle = newCycle(key, existing); 90 91 replayControls(); 92 93 try 94 { 95 TapestryUtils.storeUniqueAttribute(cycle, key, newInstance); 96 unreachable(); 97 } 98 catch (IllegalStateException ex) 99 { 100 assertEquals(TapestryMessages.nonUniqueAttribute(newInstance, key, existing), ex 101 .getMessage()); 102 } 103 104 verifyControls(); 105 } 106 107 public void testGetPageRenderSupportSuccess() 108 { 109 IComponent component = newComponent(); 110 PageRenderSupport support = newPageRenderSupport(); 111 IRequestCycle cycle = newCycle(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, support); 112 113 replayControls(); 114 115 PageRenderSupport actual = TapestryUtils.getPageRenderSupport(cycle, component); 116 117 assertSame(support, actual); 118 119 verifyControls(); 120 } 121 122 public void testRemovePageRenderSupport() 123 { 124 IRequestCycle cycle = newCycle(); 125 126 cycle.removeAttribute(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE); 127 128 replayControls(); 129 130 TapestryUtils.removePageRenderSupport(cycle); 131 132 verifyControls(); 133 } 134 135 public void testRemoveForm() 136 { 137 IRequestCycle cycle = newCycle(); 138 139 cycle.removeAttribute(TapestryUtils.FORM_ATTRIBUTE); 140 141 replayControls(); 142 143 TapestryUtils.removeForm(cycle); 144 145 verifyControls(); 146 } 147 148 public void testGetFormSuccess() 149 { 150 IComponent component = newComponent(); 151 IForm form = newForm(); 152 IRequestCycle cycle = newCycle(TapestryUtils.FORM_ATTRIBUTE, form); 153 154 replayControls(); 155 156 IForm actual = TapestryUtils.getForm(cycle, component); 157 158 assertSame(form, actual); 159 160 verifyControls(); 161 } 162 163 public void testGetPageRenderSupportFailure() 164 { 165 Location l = newLocation(); 166 MockControl control = newControl(IComponent.class); 167 IComponent component = (IComponent) control.getMock(); 168 169 component.getExtendedId(); 170 control.setReturnValue("Foo/bar", 1); 171 172 component.getLocation(); 173 control.setReturnValue(l); 174 175 component.getExtendedId(); 176 control.setReturnValue("Foo/bar", 1); 177 178 IRequestCycle cycle = newCycle(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, null); 179 180 replayControls(); 181 182 try 183 { 184 TapestryUtils.getPageRenderSupport(cycle, component); 185 unreachable(); 186 } 187 catch (ApplicationRuntimeException ex) 188 { 189 assertEquals(TapestryMessages.noPageRenderSupport(component), ex.getMessage()); 190 assertSame(l, ex.getLocation()); 191 } 192 193 verifyControls(); 194 } 195 196 public void testGetFormFailure() 197 { 198 Location l = newLocation(); 199 MockControl control = newControl(IComponent.class); 200 IComponent component = (IComponent) control.getMock(); 201 202 component.getExtendedId(); 203 control.setReturnValue("Foo/bar", 1); 204 205 component.getLocation(); 206 control.setReturnValue(l); 207 208 component.getExtendedId(); 209 control.setReturnValue("Foo/bar", 1); 210 211 IRequestCycle cycle = newCycle(TapestryUtils.FORM_ATTRIBUTE, null); 212 213 replayControls(); 214 215 try 216 { 217 TapestryUtils.getForm(cycle, component); 218 unreachable(); 219 } 220 catch (ApplicationRuntimeException ex) 221 { 222 assertEquals(TapestryMessages.noForm(component), ex.getMessage()); 223 assertSame(l, ex.getLocation()); 224 } 225 226 verifyControls(); 227 } 228 229 public void testSplitBlank() 230 { 231 assertListsEqual(new String [0], TapestryUtils.split(null)); 232 assertListsEqual(new String [0], TapestryUtils.split("")); 233 } 234 235 public void testSplitWithDelimiter() 236 { 237 assertListsEqual(new String [] 238 { "fred", "barney" }, TapestryUtils.split("fred|barney", '|')); 239 } 240 241 public void testSplitNormal() 242 { 243 assertListsEqual(new String [] 244 { "fred", "barney" }, TapestryUtils.split("fred,barney")); 245 } 246 247 public void testSplitNoDelimiter() 248 { 249 assertListsEqual(new String [] 250 { "no-delimiter" }, TapestryUtils.split("no-delimiter")); 251 } 252 253 public void testTrailingDelimiter() 254 { 255 assertListsEqual(new String [] 256 { "fred", "barney", "" }, TapestryUtils.split("fred,barney,")); 257 } 258 259 public void testEveryDelimiterCounts() 260 { 261 assertListsEqual(new String [] 262 { "", "fred", "", "barney", "", "" }, TapestryUtils.split(",fred,,barney,,")); 263 } 264 265 } | Popular Tags |