View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.env;
20  
21  import org.apache.shiro.config.ConfigurationException;
22  import org.easymock.EasyMock;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.powermock.api.easymock.PowerMock;
27  import org.powermock.core.classloader.annotations.PrepareForTest;
28  import org.powermock.modules.junit4.PowerMockRunner;
29  
30  import javax.servlet.ServletContext;
31  import java.util.Arrays;
32  import java.util.List;
33  import java.util.ServiceLoader;
34  
35  import static org.easymock.EasyMock.expect;
36  import static org.hamcrest.Matchers.*;
37  import static org.hamcrest.MatcherAssert.*;
38  
39  /**
40   * Tests for {@link EnvironmentLoader} that depend on PowerMock the stub out a ServiceLoader.
41   */
42  @RunWith(PowerMockRunner.class)
43  @PrepareForTest(EnvironmentLoader.class)
44  public class EnvironmentLoaderServiceTest {
45  
46      @Test()
47      public void singleServiceTest() throws Exception {
48  
49          List<WebEnvironmentStub> environmentList = Arrays.asList(new WebEnvironmentStub());
50  
51          ServletContext servletContext = EasyMock.mock(ServletContext.class);
52          expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(null);
53          expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null);
54  
55          PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load");
56  
57          final ServiceLoader serviceLoader = PowerMock.createMock(ServiceLoader.class);
58  
59          EasyMock.expect(ServiceLoader.load(WebEnvironment.class)).andReturn(serviceLoader);
60          EasyMock.expect(serviceLoader.iterator()).andReturn(environmentList.iterator());
61  
62          EasyMock.replay(servletContext);
63          PowerMock.replayAll();
64  
65          WebEnvironment resultEnvironment = new EnvironmentLoader().createEnvironment(servletContext);
66  
67          PowerMock.verifyAll();
68          EasyMock.verify(servletContext);
69  
70          assertThat(resultEnvironment, instanceOf(WebEnvironmentStub.class));
71          WebEnvironmentStubache/shiro/web/env/WebEnvironmentStub.html#WebEnvironmentStub">WebEnvironmentStub environmentStub = (WebEnvironmentStub) resultEnvironment;
72  
73          assertThat(environmentStub.getServletContext(), sameInstance(servletContext));
74      }
75  
76      @Test()
77      public void multipleServiceTest() throws Exception {
78  
79          List<WebEnvironmentStub> environmentList = Arrays.asList(new WebEnvironmentStubironmentStub.html#WebEnvironmentStub">WebEnvironmentStub(), new WebEnvironmentStub());
80  
81          ServletContext servletContext = EasyMock.mock(ServletContext.class);
82          expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(null);
83  
84          PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load");
85  
86          final ServiceLoader serviceLoader = PowerMock.createMock(ServiceLoader.class);
87  
88          EasyMock.expect(ServiceLoader.load(WebEnvironment.class)).andReturn(serviceLoader);
89          EasyMock.expect(serviceLoader.iterator()).andReturn(environmentList.iterator());
90  
91          EasyMock.replay(servletContext);
92          PowerMock.replayAll();
93  
94          try {
95              new EnvironmentLoader().createEnvironment(servletContext);
96              Assert.fail("Expected ConfigurationException to be thrown");
97          }
98          catch (ConfigurationException e) {
99              assertThat(e.getMessage(), stringContainsInOrder("zero or exactly one", "shiroEnvironmentClass"));
100         }
101 
102         PowerMock.verifyAll();
103         EasyMock.verify(servletContext);
104     }
105 
106     @Test()
107     public void loadFromInitParamTest() throws Exception {
108 
109         ServletContext servletContext = EasyMock.mock(ServletContext.class);
110         expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(WebEnvironmentStub.class.getName());
111         expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null);
112 
113         PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load");
114 
115         EasyMock.replay(servletContext);
116         PowerMock.replayAll();
117 
118         WebEnvironment resultEnvironment = new EnvironmentLoader().createEnvironment(servletContext);
119 
120         PowerMock.verifyAll();
121         EasyMock.verify(servletContext);
122 
123         assertThat(resultEnvironment, instanceOf(WebEnvironmentStub.class));
124         WebEnvironmentStubache/shiro/web/env/WebEnvironmentStub.html#WebEnvironmentStub">WebEnvironmentStub environmentStub = (WebEnvironmentStub) resultEnvironment;
125 
126         assertThat(environmentStub.getServletContext(), sameInstance(servletContext));
127     }
128 
129 }