1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.plugins.guice;
17  
18  import static org.easymock.EasyMock.anyObject;
19  import static org.easymock.EasyMock.createNiceMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.expectLastCall;
22  import static org.easymock.EasyMock.getCurrentArguments;
23  import static org.easymock.EasyMock.replay;
24  
25  import java.util.Hashtable;
26  
27  import javax.servlet.ServletContext;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.easymock.IAnswer;
32  import org.junit.Test;
33  import org.seasar.cubby.internal.controller.ThreadContext;
34  import org.seasar.cubby.internal.controller.ThreadContext.Command;
35  import org.seasar.cubby.plugin.PluginRegistry;
36  import org.seasar.cubby.routing.PathResolver;
37  import org.seasar.cubby.routing.impl.PathResolverImpl;
38  import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
39  import org.seasar.cubby.spi.ConverterProvider;
40  import org.seasar.cubby.spi.ProviderFactory;
41  
42  import com.google.inject.Guice;
43  import com.google.inject.Inject;
44  import com.google.inject.Injector;
45  
46  public class AbstractCubbyModuleTest {
47  
48  	@Test
49  	public void configure() throws Exception {
50  		HttpServletRequest request = createNiceMock(HttpServletRequest.class);
51  		HttpServletResponse response = createNiceMock(HttpServletResponse.class);
52  		ServletContext servletContext = createNiceMock(ServletContext.class);
53  		expect(servletContext.getInitParameter("cubby.guice.module"))
54  				.andStubReturn(TestModule.class.getName());
55  		final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
56  		expect(servletContext.getAttribute((String) anyObject()))
57  				.andStubAnswer(new IAnswer<Object>() {
58  
59  					public Object answer() throws Throwable {
60  						return attributes.get(getCurrentArguments()[0]);
61  					}
62  
63  				});
64  		servletContext.setAttribute((String) anyObject(), anyObject());
65  		expectLastCall().andAnswer(new IAnswer<Void>() {
66  
67  			public Void answer() throws Throwable {
68  				attributes.put((String) getCurrentArguments()[0],
69  						getCurrentArguments()[1]);
70  				return null;
71  			}
72  
73  		});
74  		replay(servletContext);
75  
76  		PluginRegistry pluginRegistry = PluginRegistry.getInstance();
77  		GuicePlugin guicePlugin = new GuicePlugin();
78  		guicePlugin.initialize(servletContext);
79  		pluginRegistry.register(guicePlugin);
80  
81  		Injector injector = Guice.createInjector(new TestModule());
82  		System.out.println(injector);
83  		Foo foo = injector.getInstance(Foo.class);
84  		System.out.println(foo.pathResolver);
85  		ThreadContext.runInContext(request, response, new Command() {
86  
87  			public void execute(HttpServletRequest request,
88  					HttpServletResponse response) throws Exception {
89  				ConverterProvider converterProvider = ProviderFactory
90  						.get(ConverterProvider.class);
91  				System.out.println(converterProvider);
92  			}
93  
94  		});
95  	}
96  
97  	public static class TestModule extends AbstractCubbyModule {
98  
99  		@Override
100 		protected PathResolver getPathResolver() {
101 			return new PathResolverImpl(new PathTemplateParserImpl());
102 		}
103 
104 	}
105 
106 	public static class Foo {
107 		@Inject
108 		public PathResolver pathResolver;
109 	}
110 
111 }