1 15 package org.apache.tapestry.services.impl; 16 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.ClassResolver; 22 import org.apache.hivemind.Location; 23 import org.apache.hivemind.Registry; 24 import org.apache.hivemind.impl.DefaultClassResolver; 25 import org.apache.hivemind.impl.RegistryBuilder; 26 import org.apache.hivemind.test.AggregateArgumentsMatcher; 27 import org.apache.hivemind.test.ArgumentMatcher; 28 import org.apache.hivemind.test.HiveMindTestCase; 29 import org.apache.hivemind.test.TypeMatcher; 30 import org.apache.hivemind.util.ClasspathResource; 31 import org.apache.tapestry.engine.IPropertySource; 32 import org.apache.tapestry.services.ClasspathResourceFactory; 33 import org.apache.tapestry.services.Infrastructure; 34 import org.apache.tapestry.services.RequestGlobals; 35 import org.apache.tapestry.services.ServletRequestServicer; 36 import org.apache.tapestry.services.ResetEventCoordinator; 37 import org.apache.tapestry.web.ServletWebRequest; 38 import org.apache.tapestry.web.ServletWebResponse; 39 import org.apache.tapestry.web.WebRequest; 40 import org.apache.tapestry.web.WebResponse; 41 import org.easymock.MockControl; 42 43 55 public class TestBasicInfrastructure extends HiveMindTestCase 56 { 57 public void testRequestGlobals() 58 { 59 RequestGlobalsImpl si = new RequestGlobalsImpl(); 60 61 HttpServletRequest r = (HttpServletRequest ) newMock(HttpServletRequest .class); 62 HttpServletResponse p = (HttpServletResponse ) newMock(HttpServletResponse .class); 63 64 replayControls(); 65 66 si.store(r, p); 67 68 assertSame(r, si.getRequest()); 69 assertSame(p, si.getResponse()); 70 71 verifyControls(); 72 } 73 74 public void testClasspathResourceFactory() 75 { 76 ClassResolver cr = new DefaultClassResolver(); 77 ClasspathResourceFactoryImpl f = new ClasspathResourceFactoryImpl(); 78 f.setClassResolver(cr); 79 80 String path = "/foo/bar"; 81 82 ClasspathResource expected = new ClasspathResource(cr, path); 83 84 assertEquals(expected, f.newResource(path)); 85 } 86 87 90 public void testClasspathResourceFactoryIntegrated() 91 { 92 ClassResolver cr = new DefaultClassResolver(); 93 94 Registry registry = RegistryBuilder.constructDefaultRegistry(); 95 96 ClasspathResourceFactory f = (ClasspathResourceFactory) registry.getService( 97 "tapestry.ClasspathResourceFactory", 98 ClasspathResourceFactory.class); 99 100 String path = "/foo/bar"; 101 102 ClasspathResource expected = new ClasspathResource(cr, path); 103 104 assertEquals(expected, f.newResource(path)); 105 } 106 107 public void testGlobalPropertyObjectProviderSuccess() 108 { 109 MockControl sourceControl = newControl(IPropertySource.class); 110 IPropertySource source = (IPropertySource) sourceControl.getMock(); 111 112 114 source.getPropertyValue("foo"); 115 sourceControl.setReturnValue("bar"); 116 117 replayControls(); 118 119 PropertyObjectProvider p = new PropertyObjectProvider(); 120 p.setSource(source); 121 122 assertEquals("bar", p.provideObject(null, null, "foo", null)); 123 124 verifyControls(); 125 } 126 127 public void testGlobalPropertyObjectProviderFailure() 128 { 129 Location l = fabricateLocation(223); 130 131 MockControl sourceControl = newControl(IPropertySource.class); 132 IPropertySource source = (IPropertySource) sourceControl.getMock(); 133 134 136 source.getPropertyValue("foo"); 137 sourceControl.setThrowable(new ApplicationRuntimeException("failure")); 138 139 replayControls(); 140 141 PropertyObjectProvider p = new PropertyObjectProvider(); 142 p.setSource(source); 143 144 try 145 { 146 p.provideObject(null, null, "foo", l); 147 } 148 catch (ApplicationRuntimeException ex) 149 { 150 assertEquals("failure", ex.getMessage()); 151 assertEquals(l, ex.getLocation()); 152 } 153 154 verifyControls(); 155 } 156 157 public void testSuccessfulInfrastructureLookup() 158 { 159 MockControl ifrControl = newControl(Infrastructure.class); 160 Infrastructure ifr = (Infrastructure) ifrControl.getMock(); 161 162 ResetEventCoordinator coord = (ResetEventCoordinator) newMock(ResetEventCoordinator.class); 163 164 ifr.getResetEventCoordinator(); 165 ifrControl.setReturnValue(coord); 166 167 replayControls(); 168 169 InfrastructureObjectProvider p = new InfrastructureObjectProvider(); 170 171 p.setInfrastructure(ifr); 172 173 Object actual = p.provideObject(null, null, "resetEventCoordinator", null); 174 175 assertSame(coord, actual); 176 177 verifyControls(); 178 } 179 } | Popular Tags |