From 41436c8c010715162b42020550ea3c435bb8fcc2 Mon Sep 17 00:00:00 2001 From: Benjamin Marwell Date: Mon, 23 Nov 2020 10:28:28 +0100 Subject: [PATCH] (test) convert EnvironmentLoaderServiceTest partly to integration test. - remove usage of Powermock on Java-internal classes - add IT with a secondary ServiceLoadable WebEnvironment, replacing multipleServiceTest - add run-its profile to travis and Jenkinsfiles --- .gitignore | 2 + .travis.yml | 2 +- Jenkinsfile | 4 +- Jenkinsfile-jdk11 | 4 +- Jenkinsfile-jdk14 | 4 +- pom.xml | 73 ++++++++++++++++++- web/src/it/mrm/settings.xml | 71 ++++++++++++++++++ .../MultipleServiceLoader/invoker.properties | 21 ++++++ .../it/projects/MultipleServiceLoader/pom.xml | 68 +++++++++++++++++ .../it/loader/SecondaryWebEnvironment.java | 47 ++++++++++++ .../org.apache.shiro.web.env.WebEnvironment | 20 +++++ .../loader/SecondaryWebEnvironmentTest.java | 55 ++++++++++++++ .../web/env/EnvironmentLoaderServiceTest.java | 58 +++++---------- .../web/env/EnvironmentLoaderServiceTest.ini | 20 +++++ 14 files changed, 399 insertions(+), 50 deletions(-) create mode 100644 web/src/it/mrm/settings.xml create mode 100644 web/src/it/projects/MultipleServiceLoader/invoker.properties create mode 100644 web/src/it/projects/MultipleServiceLoader/pom.xml create mode 100644 web/src/it/projects/MultipleServiceLoader/src/main/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironment.java create mode 100644 web/src/it/projects/MultipleServiceLoader/src/main/resources/META-INF/services/org.apache.shiro.web.env.WebEnvironment create mode 100644 web/src/it/projects/MultipleServiceLoader/src/test/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironmentTest.java create mode 100644 web/src/test/resources/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini diff --git a/.gitignore b/.gitignore index 42cc59f398..dc39926181 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,5 @@ target/ release.properties pom.xml.* - +/**/src/it/projects/*/build.log +/**/src/it/projects/*/target diff --git a/.travis.yml b/.travis.yml index 8cbcdb0a8a..96ffae6e0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ install: - 'true' script: -- "mvn -e -Pci,docs install apache-rat:check -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" +- "mvn -e -Pci,docs,run-its install apache-rat:check -B -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn" after_success: - bash <(curl -s https://codecov.io/bash) -f test-coverage/target/site/jacoco-aggregate/jacoco.xml diff --git a/Jenkinsfile b/Jenkinsfile index e92a867504..8474dd7ad6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -79,7 +79,7 @@ pipeline { stage('Tests') { steps { echo 'Running tests' - sh 'mvn test' + sh 'mvn verify -Prun-its' } post { always { @@ -171,4 +171,4 @@ Is back to normal. } } -} \ No newline at end of file +} diff --git a/Jenkinsfile-jdk11 b/Jenkinsfile-jdk11 index 95af902fa7..a996166d3e 100644 --- a/Jenkinsfile-jdk11 +++ b/Jenkinsfile-jdk11 @@ -79,7 +79,7 @@ pipeline { stage('Tests') { steps { echo 'Running tests' - sh 'mvn test' + sh 'mvn verify -Prun-its' } post { always { @@ -159,4 +159,4 @@ Is back to normal. } } -} \ No newline at end of file +} diff --git a/Jenkinsfile-jdk14 b/Jenkinsfile-jdk14 index 74f8094fdb..1f5531cb2f 100644 --- a/Jenkinsfile-jdk14 +++ b/Jenkinsfile-jdk14 @@ -79,7 +79,7 @@ pipeline { stage('Tests') { steps { echo 'Running tests' - sh 'mvn test' + sh 'mvn verify -Prun-its' } post { always { @@ -159,4 +159,4 @@ Is back to normal. } } -} \ No newline at end of file +} diff --git a/pom.xml b/pom.xml index 69a3f2a78b..92ae98820a 100644 --- a/pom.xml +++ b/pom.xml @@ -108,7 +108,7 @@ 2.39.0 - 4.0.2 + 4.1 1.8.0 2.5.8 4.12 @@ -284,6 +284,8 @@ + /**/src/it/projects/*/build.log + /**/src/it/projects/*/target/** **/.externalToolBuilders/* **/infinitest.filters @@ -433,7 +435,6 @@ 3.0.0-M3 true - false @@ -1274,6 +1275,8 @@ + /**/src/it/projects/*/build.log + /**/src/it/projects/*/target/** **/.externalToolBuilders/* **/infinitest.filters @@ -1495,5 +1498,71 @@ + + run-its + + + + org.codehaus.mojo + mrm-maven-plugin + 1.1.0 + + + + start + stop + + + + + mrm.repository.url + + + src/it/mrm/repository + + + + + + + org.apache.maven.plugins + maven-invoker-plugin + + ${project.build.directory}/local-repo + + + 1 + + ${project.build.directory}/local-repo + src/it/projects + + */pom.xml + + src/it/mrm/settings.xml + + ${mrm.repository.url} + + + clean + package + + + + + integration-test + + install + integration-test + verify + + + + + + + diff --git a/web/src/it/mrm/settings.xml b/web/src/it/mrm/settings.xml new file mode 100644 index 0000000000..7ee2085571 --- /dev/null +++ b/web/src/it/mrm/settings.xml @@ -0,0 +1,71 @@ + + + + + + + + mrm-maven-plugin + Mock Repository Manager + @mrm.repository.url@ + * + + + + + it-repo + + + snapshots + @mrm.repository.url@ + + true + ignore + never + + + true + ignore + always + + + + + + snapshots + @mrm.repository.url@ + + true + ignore + never + + + true + ignore + always + + + + + + + it-repo + + diff --git a/web/src/it/projects/MultipleServiceLoader/invoker.properties b/web/src/it/projects/MultipleServiceLoader/invoker.properties new file mode 100644 index 0000000000..e634c1cab0 --- /dev/null +++ b/web/src/it/projects/MultipleServiceLoader/invoker.properties @@ -0,0 +1,21 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +invoker.java.version = 1.8+ +invoker.goals = clean verify diff --git a/web/src/it/projects/MultipleServiceLoader/pom.xml b/web/src/it/projects/MultipleServiceLoader/pom.xml new file mode 100644 index 0000000000..e46cac00e1 --- /dev/null +++ b/web/src/it/projects/MultipleServiceLoader/pom.xml @@ -0,0 +1,68 @@ + + + + + + 4.0.0 + org.apache.shiro.integrationtest + multipleServiceLoader + 1.0.0 + + Apache Shiro :: Web :: IT :: multipleServiceLoader + jar + + + 8 + 8 + + + + + org.apache.shiro + shiro-web + 1.7.1-SNAPSHOT + + + javax.servlet + javax.servlet-api + 3.1.0 + + + org.junit.jupiter + junit-jupiter + 5.6.2 + test + + + org.hamcrest + hamcrest-library + 2.2 + test + + + org.mockito + mockito-core + 3.5.13 + test + + + + diff --git a/web/src/it/projects/MultipleServiceLoader/src/main/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironment.java b/web/src/it/projects/MultipleServiceLoader/src/main/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironment.java new file mode 100644 index 0000000000..d494f60380 --- /dev/null +++ b/web/src/it/projects/MultipleServiceLoader/src/main/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironment.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.shiro.web.it.loader; + +import org.apache.shiro.mgt.SecurityManager; +import org.apache.shiro.web.env.WebEnvironment; +import org.apache.shiro.web.filter.mgt.FilterChainResolver; +import org.apache.shiro.web.mgt.WebSecurityManager; + +public class SecondaryWebEnvironment implements WebEnvironment { + @Override + public FilterChainResolver getFilterChainResolver() { + throw new UnsupportedOperationException("not yet implemented: [org.apache.shiro.web.it.loader.SecondaryWebEnvironment::getFilterChainResolver]."); + } + + @Override + public javax.servlet.ServletContext getServletContext() { + throw new UnsupportedOperationException("not yet implemented: [org.apache.shiro.web.it.loader.SecondaryWebEnvironment::getServletContext]."); + } + + @Override + public WebSecurityManager getWebSecurityManager() { + throw new UnsupportedOperationException("not yet implemented: [org.apache.shiro.web.it.loader.SecondaryWebEnvironment::getWebSecurityManager]."); + } + + @Override + public SecurityManager getSecurityManager() { + throw new UnsupportedOperationException("not yet implemented: [org.apache.shiro.web.it.loader.SecondaryWebEnvironment::getSecurityManager]."); + } +} diff --git a/web/src/it/projects/MultipleServiceLoader/src/main/resources/META-INF/services/org.apache.shiro.web.env.WebEnvironment b/web/src/it/projects/MultipleServiceLoader/src/main/resources/META-INF/services/org.apache.shiro.web.env.WebEnvironment new file mode 100644 index 0000000000..f4cd50f0b8 --- /dev/null +++ b/web/src/it/projects/MultipleServiceLoader/src/main/resources/META-INF/services/org.apache.shiro.web.env.WebEnvironment @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +org.apache.shiro.web.it.loader.SecondaryWebEnvironment diff --git a/web/src/it/projects/MultipleServiceLoader/src/test/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironmentTest.java b/web/src/it/projects/MultipleServiceLoader/src/test/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironmentTest.java new file mode 100644 index 0000000000..fa8927e948 --- /dev/null +++ b/web/src/it/projects/MultipleServiceLoader/src/test/java/org/apache/shiro/web/it/loader/SecondaryWebEnvironmentTest.java @@ -0,0 +1,55 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.shiro.web.it.loader; + +import org.apache.shiro.config.ConfigurationException; +import org.apache.shiro.web.env.EnvironmentLoader; +import org.hamcrest.MatcherAssert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import javax.servlet.ServletContext; +import java.io.InputStream; + +import static org.hamcrest.Matchers.stringContainsInOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class SecondaryWebEnvironmentTest { + + @Test + @DisplayName("Try loading a WebEnvironment while two implementations are available.") + public void multipleServiceTest() { + InputStream resourceAsStream = getClass() + .getResourceAsStream("/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini"); + + ServletContext servletContext = mock(ServletContext.class); + when(servletContext.getResourceAsStream("/WEB-INF/shiro.ini")).then(args -> resourceAsStream); + + try { + new EnvironmentLoader().initEnvironment(servletContext); + Assertions.fail("Expected ConfigurationException to be thrown"); + } catch (ConfigurationException e) { + MatcherAssert.assertThat(e.getMessage(), stringContainsInOrder("zero or exactly one", "shiroEnvironmentClass")); + } + } + +} diff --git a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java index 1c1f97570b..2fbc0a1a63 100644 --- a/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java +++ b/web/src/test/java/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.java @@ -21,85 +21,65 @@ import org.apache.shiro.config.ConfigurationException; import org.easymock.EasyMock; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import javax.servlet.ServletContext; -import java.util.Arrays; -import java.util.List; -import java.util.ServiceLoader; +import java.io.InputStream; import static org.easymock.EasyMock.expect; -import static org.hamcrest.Matchers.*; -import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.sameInstance; +import static org.hamcrest.Matchers.stringContainsInOrder; /** * Tests for {@link EnvironmentLoader} that depend on PowerMock the stub out a ServiceLoader. */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(EnvironmentLoader.class) public class EnvironmentLoaderServiceTest { @Test() public void singleServiceTest() throws Exception { - - List environmentList = Arrays.asList(new WebEnvironmentStub()); + InputStream resourceAsStream = getClass() + .getResourceAsStream("/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini"); ServletContext servletContext = EasyMock.mock(ServletContext.class); expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(null); expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null); - - PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load"); - - final ServiceLoader serviceLoader = PowerMock.createMock(ServiceLoader.class); - - EasyMock.expect(ServiceLoader.load(WebEnvironment.class)).andReturn(serviceLoader); - EasyMock.expect(serviceLoader.iterator()).andReturn(environmentList.iterator()); + expect(servletContext.getResourceAsStream("/WEB-INF/shiro.ini")).andReturn(resourceAsStream); EasyMock.replay(servletContext); - PowerMock.replayAll(); WebEnvironment resultEnvironment = new EnvironmentLoader().createEnvironment(servletContext); - PowerMock.verifyAll(); EasyMock.verify(servletContext); - assertThat(resultEnvironment, instanceOf(WebEnvironmentStub.class)); - WebEnvironmentStub environmentStub = (WebEnvironmentStub) resultEnvironment; + assertThat(resultEnvironment, instanceOf(IniWebEnvironment.class)); + IniWebEnvironment environmentStub = (IniWebEnvironment) resultEnvironment; assertThat(environmentStub.getServletContext(), sameInstance(servletContext)); } @Test() + @Ignore public void multipleServiceTest() throws Exception { - - List environmentList = Arrays.asList(new WebEnvironmentStub(), new WebEnvironmentStub()); + InputStream resourceAsStream = getClass() + .getResourceAsStream("/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini"); ServletContext servletContext = EasyMock.mock(ServletContext.class); expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(null); - - PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load"); - - final ServiceLoader serviceLoader = PowerMock.createMock(ServiceLoader.class); - - EasyMock.expect(ServiceLoader.load(WebEnvironment.class)).andReturn(serviceLoader); - EasyMock.expect(serviceLoader.iterator()).andReturn(environmentList.iterator()); + expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null); + expect(servletContext.getResourceAsStream("/WEB-INF/shiro.ini")).andReturn(resourceAsStream); EasyMock.replay(servletContext); - PowerMock.replayAll(); try { new EnvironmentLoader().createEnvironment(servletContext); Assert.fail("Expected ConfigurationException to be thrown"); - } - catch (ConfigurationException e) { + } catch (ConfigurationException e) { assertThat(e.getMessage(), stringContainsInOrder("zero or exactly one", "shiroEnvironmentClass")); } - PowerMock.verifyAll(); EasyMock.verify(servletContext); } @@ -110,14 +90,10 @@ public void loadFromInitParamTest() throws Exception { expect(servletContext.getInitParameter("shiroEnvironmentClass")).andReturn(WebEnvironmentStub.class.getName()); expect(servletContext.getInitParameter("shiroConfigLocations")).andReturn(null); - PowerMock.mockStaticPartialStrict(ServiceLoader.class, "load"); - EasyMock.replay(servletContext); - PowerMock.replayAll(); WebEnvironment resultEnvironment = new EnvironmentLoader().createEnvironment(servletContext); - PowerMock.verifyAll(); EasyMock.verify(servletContext); assertThat(resultEnvironment, instanceOf(WebEnvironmentStub.class)); diff --git a/web/src/test/resources/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini b/web/src/test/resources/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini new file mode 100644 index 0000000000..e9322128eb --- /dev/null +++ b/web/src/test/resources/org/apache/shiro/web/env/EnvironmentLoaderServiceTest.ini @@ -0,0 +1,20 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +[filters] +authc.successUrl = /index.jsp