    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.servlet;
    17
    18  import static org.easymock.EasyMock.createMock;
    19  import static org.easymock.EasyMock.expect;
    20  import static org.easymock.EasyMock.replay;
    21  import static org.easymock.EasyMock.verify;
    22  import static org.junit.Assert.assertFalse;
    23  import static org.junit.Assert.assertNotNull;
    24  import static org.junit.Assert.assertNull;
    25  import static org.junit.Assert.assertTrue;
    26  import static org.junit.Assert.fail;
    27
    28  import java.util.Arrays;
    29  import java.util.Collection;
    30
    31  import javax.servlet.Servlet;
    32  import javax.servlet.ServletConfig;
    33  import javax.servlet.ServletContext;
    34  import javax.servlet.ServletException;
    35
    36  import org.junit.Test;
    37  import org.seasar.cubby.internal.plugin.PluginManager;
    38  import org.seasar.cubby.plugin.AbstractPlugin;
    39  import org.seasar.cubby.plugin.Plugin;
    40  import org.seasar.cubby.plugin.PluginRegistry;
    41  import org.seasar.cubby.spi.Provider;
    42
    43  public class CubbyServletTest {
    44
    45  	private boolean initialized = false;
    46
    47  	private boolean ready = false;
    48
    49  	private boolean destroyed = false;
    50
    51  	@Test
    52  	public void initialize() throws ServletException {
    53  		ServletContext servletContext = createMock(ServletContext.class);
    54  		ServletConfig servletConfig = createMock(ServletConfig.class);
    55  		expect(servletConfig.getServletContext()).andReturn(servletContext);
    56  		replay(servletContext, servletConfig);
    57
    58  		Servlet servlet = new CubbyServlet() {
    59
    60  			private static final long serialVersionUID = 1L;
    61
    62  			@Override
    63  			protected PluginManager buildPluginManager() {
    64  				PluginManager pluginManager = new PluginManager(PluginRegistry
    65  						.getInstance()) {
    66
    67  					@Override
    68  					protected Collection<Plugin> loadPlugins() {
    69  						return Arrays
    70  								.asList(new Plugin[] { new AssertPlugin() });
    71  					}
    72
    73  				};
    74  				return pluginManager;
    75  			}
    76
    77  		};
    78
    79  		PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    80
    81  		assertNull(pluginRegistry.getPlugin(AssertPlugin.class));
    82  		try {
    83  			pluginRegistry.getProvider(AssertProvider.class);
    84  			fail();
    85  		} catch (IllegalArgumentException e) {
    86  			// ok
    87  		}
    88
    89  		assertFalse(initialized);
    90  		assertFalse(ready);
    91  		servlet.init(servletConfig);
    92  		assertTrue(initialized);
    93  		assertTrue(ready);
    94
    95  		assertNotNull(pluginRegistry.getPlugin(AssertPlugin.class));
    96  		assertNotNull(pluginRegistry.getProvider(AssertProvider.class));
    97
    98  		assertFalse(destroyed);
    99  		servlet.destroy();
    100 		assertTrue(destroyed);
    101
    102 		verify(servletContext, servletConfig);
    103 	}
    104
    105 	private class AssertProvider implements Provider {
    106
    107 	}
    108
    109 	private class AssertPlugin extends AbstractPlugin {
    110
    111 		public AssertPlugin() {
    112 			support(AssertProvider.class);
    113 		}
    114
    115 		@Override
    116 		public void initialize(ServletContext servletContext) throws Exception {
    117 			super.initialize(servletContext);
    118 			initialized = true;
    119 		}
    120
    121 		@Override
    122 		public void ready() throws Exception {
    123 			super.ready();
    124 			ready = true;
    125 		}
    126
    127 		@Override
    128 		public void destroy() {
    129 			super.destroy();
    130 			destroyed = true;
    131 		}
    132
    133 		public <S extends Provider> S getProvider(Class<S> service) {
    134 			if (AssertProvider.class.equals(service)) {
    135 				return service.cast(new AssertProvider());
    136 			}
    137 			return null;
    138 		}
    139
    140 	}
    141
    142 }