001 // Copyright 2004, 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.services.impl; 016 017 import static org.easymock.EasyMock.expect; 018 019 import org.apache.hivemind.ApplicationRuntimeException; 020 import org.apache.hivemind.ClassResolver; 021 import org.apache.hivemind.Location; 022 import org.apache.hivemind.Registry; 023 import org.apache.hivemind.impl.DefaultClassResolver; 024 import org.apache.hivemind.impl.RegistryBuilder; 025 import org.apache.hivemind.util.ClasspathResource; 026 import org.apache.tapestry.BaseComponentTestCase; 027 import org.apache.tapestry.engine.IPropertySource; 028 import org.apache.tapestry.services.ClasspathResourceFactory; 029 import org.apache.tapestry.services.Infrastructure; 030 import org.apache.tapestry.services.ResetEventHub; 031 import org.testng.annotations.Test; 032 033 import javax.servlet.http.HttpServletRequest; 034 import javax.servlet.http.HttpServletResponse; 035 036 /** 037 * Tests for: 038 * <ul> 039 * <li>{@link org.apache.tapestry.services.impl.ApplicationGlobalsImpl} 040 * <li>{@link org.apache.tapestry.services.impl.InfrastructureObjectProvider} 041 * </ul> 042 * 043 * @author Howard Lewis Ship 044 * @since 4.0 045 */ 046 @Test 047 public class TestBasicInfrastructure extends BaseComponentTestCase 048 { 049 public void testRequestGlobals() 050 { 051 RequestGlobalsImpl si = new RequestGlobalsImpl(); 052 053 HttpServletRequest r = newMock(HttpServletRequest.class); 054 HttpServletResponse p = newMock(HttpServletResponse.class); 055 056 replay(); 057 058 si.store(r, p); 059 060 assertSame(r, si.getRequest()); 061 assertSame(p, si.getResponse()); 062 063 verify(); 064 } 065 066 public void testClasspathResourceFactory() 067 { 068 ClassResolver cr = new DefaultClassResolver(); 069 ClasspathResourceFactoryImpl f = new ClasspathResourceFactoryImpl(); 070 f.setClassResolver(cr); 071 072 String path = "/foo/bar"; 073 074 ClasspathResource expected = new ClasspathResource(cr, path); 075 076 assertEquals(expected, f.newResource(path)); 077 } 078 079 /** 080 * Validate that the factory is declared properly in the module deployment descriptor. 081 */ 082 public void testClasspathResourceFactoryIntegrated() 083 { 084 ClassResolver cr = new DefaultClassResolver(); 085 086 Registry registry = RegistryBuilder.constructDefaultRegistry(); 087 088 ClasspathResourceFactory f = (ClasspathResourceFactory) registry.getService( 089 "tapestry.ClasspathResourceFactory", 090 ClasspathResourceFactory.class); 091 092 String path = "/foo/bar"; 093 094 ClasspathResource expected = new ClasspathResource(cr, path); 095 096 assertEquals(expected, f.newResource(path)); 097 } 098 099 public void testGlobalPropertyObjectProviderSuccess() 100 { 101 IPropertySource source = newMock(IPropertySource.class); 102 103 // Training 104 105 expect(source.getPropertyValue("foo")).andReturn("bar"); 106 107 replay(); 108 109 PropertyObjectProvider p = new PropertyObjectProvider(); 110 p.setSource(source); 111 112 assertEquals("bar", p.provideObject(null, null, "foo", null)); 113 114 verify(); 115 } 116 117 public void testGlobalPropertyObjectProviderFailure() 118 { 119 Location l = fabricateLocation(223); 120 121 IPropertySource source = newMock(IPropertySource.class); 122 123 // Training 124 125 expect(source.getPropertyValue("foo")).andThrow(new ApplicationRuntimeException("failure")); 126 127 replay(); 128 129 PropertyObjectProvider p = new PropertyObjectProvider(); 130 p.setSource(source); 131 132 try 133 { 134 p.provideObject(null, null, "foo", l); 135 } 136 catch (ApplicationRuntimeException ex) 137 { 138 assertEquals("failure", ex.getMessage()); 139 assertEquals(l, ex.getLocation()); 140 } 141 142 verify(); 143 } 144 145 public void testSuccessfulInfrastructureLookup() 146 { 147 Infrastructure ifr = newMock(Infrastructure.class); 148 149 ResetEventHub coord = newMock(ResetEventHub.class); 150 151 expect(ifr.getResetEventHub()).andReturn(coord); 152 153 replay(); 154 155 InfrastructureObjectProvider p = new InfrastructureObjectProvider(); 156 157 p.setInfrastructure(ifr); 158 159 Object actual = p.provideObject(null, null, "resetEventHub", null); 160 161 assertSame(coord, actual); 162 163 verify(); 164 } 165 }