001 // Copyright 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; 016 017 import org.apache.hivemind.ApplicationRuntimeException; 018 import org.apache.hivemind.Location; 019 import static org.easymock.EasyMock.expect; 020 import org.testng.annotations.Test; 021 022 /** 023 * Tests for {@link org.apache.tapestry.TapestryUtils}. 024 * 025 * @author Howard M. Lewis Ship 026 * @since 4.0 027 */ 028 @Test 029 public class TapestryUtilsTest extends BaseComponentTestCase 030 { 031 032 private IRequestCycle newCycle(String key, Object attribute) 033 { 034 IRequestCycle cycle = newMock(IRequestCycle.class); 035 036 trainGetAttribute(cycle, key, attribute); 037 038 return cycle; 039 } 040 041 public void testStoreUniqueAttributeSuccess() 042 { 043 Object newInstance = new Object(); 044 IRequestCycle cycle = newCycle(); 045 046 String key = "foo.bar.Baz"; 047 048 expect(cycle.getAttribute(key)).andReturn(null); 049 050 cycle.setAttribute(key, newInstance); 051 052 replay(); 053 054 TapestryUtils.storeUniqueAttribute(cycle, key, newInstance); 055 056 verify(); 057 } 058 059 public void testStoreUniqueAttributeFailure() 060 { 061 Object existing = "*EXISTING*"; 062 Object newInstance = "*NEW*"; 063 064 String key = "foo.bar.Baz"; 065 066 IRequestCycle cycle = newCycle(key, existing); 067 068 replay(); 069 070 try 071 { 072 TapestryUtils.storeUniqueAttribute(cycle, key, newInstance); 073 unreachable(); 074 } 075 catch (IllegalStateException ex) 076 { 077 assertEquals(TapestryMessages.nonUniqueAttribute(newInstance, key, existing), ex 078 .getMessage()); 079 } 080 081 verify(); 082 } 083 084 public void testGetPageRenderSupportSuccess() 085 { 086 IComponent component = newComponent(); 087 PageRenderSupport support = newPageRenderSupport(); 088 IRequestCycle cycle = newCycle(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, support); 089 090 replay(); 091 092 PageRenderSupport actual = TapestryUtils.getPageRenderSupport(cycle, component); 093 094 assertSame(support, actual); 095 096 verify(); 097 } 098 099 public void testRemovePageRenderSupport() 100 { 101 IRequestCycle cycle = newMock(IRequestCycle.class); 102 103 cycle.removeAttribute(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE); 104 105 replay(); 106 107 TapestryUtils.removePageRenderSupport(cycle); 108 109 verify(); 110 } 111 112 public void testRemoveForm() 113 { 114 IRequestCycle cycle = newMock(IRequestCycle.class); 115 116 cycle.removeAttribute(TapestryUtils.FORM_ATTRIBUTE); 117 118 replay(); 119 120 TapestryUtils.removeForm(cycle); 121 122 verify(); 123 } 124 125 public void testGetFormSuccess() 126 { 127 IComponent component = newComponent(); 128 IForm form = newForm(); 129 IRequestCycle cycle = newCycle(TapestryUtils.FORM_ATTRIBUTE, form); 130 131 replay(); 132 133 IForm actual = TapestryUtils.getForm(cycle, component); 134 135 assertSame(form, actual); 136 137 verify(); 138 } 139 140 public void testGetPageRenderSupportFailure() 141 { 142 IComponent component = newMock(IComponent.class); 143 IRequestCycle cycle = newCycle(TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, null); 144 145 expect(component.getExtendedId()).andReturn("Foo/bar").anyTimes(); 146 147 Location l = newLocation(); 148 expect(component.getLocation()).andReturn(l); 149 150 replay(); 151 152 try 153 { 154 TapestryUtils.getPageRenderSupport(cycle, component); 155 unreachable(); 156 } 157 catch (ApplicationRuntimeException ex) 158 { 159 assertEquals(TapestryMessages.noPageRenderSupport(component), ex.getMessage()); 160 assertSame(l, ex.getLocation()); 161 } 162 163 verify(); 164 } 165 166 public void testGetFormFailure() 167 { 168 Location l = newLocation(); 169 IComponent component = newMock(IComponent.class); 170 171 IRequestCycle cycle = newCycle(TapestryUtils.FORM_ATTRIBUTE, null); 172 173 expect(component.getExtendedId()).andReturn("Foo/bar").anyTimes(); 174 expect(component.getLocation()).andReturn(l); 175 176 replay(); 177 178 try 179 { 180 TapestryUtils.getForm(cycle, component); 181 unreachable(); 182 } 183 catch (ApplicationRuntimeException ex) 184 { 185 assertEquals(TapestryMessages.noForm(component), ex.getMessage()); 186 assertSame(l, ex.getLocation()); 187 } 188 189 verify(); 190 } 191 192 public void testSplitBlank() 193 { 194 assertListEquals(new String[0], TapestryUtils.split(null)); 195 assertListEquals(new String[0], TapestryUtils.split("")); 196 } 197 198 public void testSplitWithDelimiter() 199 { 200 assertListEquals(new String[] 201 { "fred", "barney" }, TapestryUtils.split("fred|barney", '|')); 202 } 203 204 public void testSplitNormal() 205 { 206 assertListEquals(new String[] 207 { "fred", "barney" }, TapestryUtils.split("fred,barney")); 208 } 209 210 public void testSplitNoDelimiter() 211 { 212 assertListEquals(new String[] 213 { "no-delimiter" }, TapestryUtils.split("no-delimiter")); 214 } 215 216 public void testTrailingDelimiter() 217 { 218 assertListEquals(new String[] 219 { "fred", "barney", "" }, TapestryUtils.split("fred,barney,")); 220 } 221 222 public void testEveryDelimiterCounts() 223 { 224 assertListEquals(new String[] 225 { "", "fred", "", "barney", "", "" }, TapestryUtils.split(",fred,,barney,,")); 226 } 227 228 public void testCapitalizeNothing() 229 { 230 assertEquals(null, TapestryUtils.capitalize(null)); 231 assertEquals("", TapestryUtils.capitalize("")); 232 } 233 234 public void testCapitalizeNormal() 235 { 236 assertEquals("Test", TapestryUtils.capitalize("test")); 237 assertEquals("Test", TapestryUtils.capitalize("Test")); 238 assertEquals("123abc", TapestryUtils.capitalize("123abc")); 239 } 240 241 public void testEnquote() 242 { 243 assertEquals("'simple'", TapestryUtils.enquote("simple")); 244 245 assertEquals("'this is a \\\\backslash\\\\'", TapestryUtils 246 .enquote("this is a \\backslash\\")); 247 248 assertEquals("'this is a \\'single quote\\''", TapestryUtils 249 .enquote("this is a 'single quote'")); 250 } 251 252 public void testEnquoteNull() 253 { 254 assertEquals("''", TapestryUtils.enquote(null)); 255 } 256 257 public void testConvertTapestryIdToNMToken() 258 { 259 assertEquals("abc", TapestryUtils.convertTapestryIdToNMToken("abc")); 260 assertEquals("abc", TapestryUtils.convertTapestryIdToNMToken("$abc")); 261 assertEquals("a_b_c", TapestryUtils.convertTapestryIdToNMToken("$a$b$c")); 262 } 263 264 public void testBuildClientElementReference() 265 { 266 assertEquals("document.getElementById('foo')", TapestryUtils 267 .buildClientElementReference("foo")); 268 } 269 270 public void testGetComponent() 271 { 272 IComponent container = newComponent(); 273 IComponent containee = newComponent(); 274 275 trainGetComponent(container, "fred", containee); 276 277 replay(); 278 279 assertSame(containee, TapestryUtils.getComponent(container, "fred", IComponent.class, null)); 280 281 verify(); 282 } 283 284 public void testGetComponentWrongType() 285 { 286 IComponent container = newComponent(); 287 IComponent containee = newComponent(); 288 Location l = newLocation(); 289 290 trainGetComponent(container, "fred", containee); 291 trainGetExtendedId(containee, "Flintstone/fred"); 292 293 replay(); 294 295 try 296 { 297 TapestryUtils.getComponent(container, "fred", String.class, l); 298 unreachable(); 299 } 300 catch (ApplicationRuntimeException ex) 301 { 302 assertEquals( 303 "Component Flintstone/fred is not assignable to type java.lang.String.", 304 ex.getMessage()); 305 assertSame(l, ex.getLocation()); 306 } 307 308 verify(); 309 310 } 311 312 public void testGetComponentDoesNotExist() 313 { 314 IComponent container = newComponent(); 315 Location l = newLocation(); 316 317 Throwable t = new RuntimeException("Poof!"); 318 319 expect(container.getComponent("fred")).andThrow(t); 320 321 replay(); 322 323 try 324 { 325 TapestryUtils.getComponent(container, "fred", IComponent.class, l); 326 unreachable(); 327 } 328 catch (ApplicationRuntimeException ex) 329 { 330 assertEquals("Poof!", ex.getMessage()); 331 assertSame(l, ex.getLocation()); 332 assertSame(t, ex.getRootCause()); 333 } 334 335 verify(); 336 } 337 }