From 37a222e0f31a0e712dcdaa58a2c1145f173dd705 Mon Sep 17 00:00:00 2001 From: Daniel Kec Date: Wed, 30 Oct 2019 20:53:16 +0100 Subject: [PATCH] Spring 5 integration tests Signed-off-by: Daniel Kec --- ext/spring5/pom.xml | 16 ++ tests/integration/pom.xml | 1 + tests/integration/spring5/README.txt | 30 ++++ tests/integration/spring5/pom.xml | 132 ++++++++++++++ .../spring/test/AccountJerseyResource.java | 164 +++++++++++++++++ .../server/spring/test/AccountService.java | 33 ++++ .../spring/test/AccountServiceImpl.java | 63 +++++++ .../spring/test/AccountSpringResource.java | 167 ++++++++++++++++++ .../spring/test/ControllerResource.java | 49 +++++ .../jersey/server/spring/test/Endpoint.java | 34 ++++ .../server/spring/test/EndpointResource.java | 47 +++++ .../spring/test/HK2ServicePerLookup.java | 25 +++ .../spring/test/HK2ServiceRequestScoped.java | 25 +++ .../spring/test/HK2ServiceSingleton.java | 25 +++ .../server/spring/test/MyApplication.java | 50 ++++++ .../spring/test/RepositoryResource.java | 49 +++++ .../server/spring/test/ServiceResource.java | 49 +++++ .../src/main/resources/applicationContext.xml | 69 ++++++++ .../spring5/src/main/webapp/WEB-INF/web.xml | 50 ++++++ .../spring/test/AccountResourceITCase.java | 47 +++++ .../spring/test/AccountResourceTestBase.java | 83 +++++++++ .../spring/test/JerseyManagedITCase.java | 54 ++++++ .../server/spring/test/ResourceTestBase.java | 67 +++++++ .../test/SpringManagedControllerITCase.java | 53 ++++++ .../test/SpringManagedEndpointITCase.java | 54 ++++++ .../spring/test/SpringManagedITCase.java | 54 ++++++ .../test/SpringManagedRepositoryITCase.java | 53 ++++++ .../test/SpringManagedServiceITCase.java | 53 ++++++ .../jersey/server/spring/test/TestUtil.java | 52 ++++++ 29 files changed, 1648 insertions(+) create mode 100644 tests/integration/spring5/README.txt create mode 100644 tests/integration/spring5/pom.xml create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountJerseyResource.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountService.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountServiceImpl.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountSpringResource.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ControllerResource.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/Endpoint.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/EndpointResource.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServicePerLookup.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceRequestScoped.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceSingleton.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/MyApplication.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/RepositoryResource.java create mode 100644 tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ServiceResource.java create mode 100644 tests/integration/spring5/src/main/resources/applicationContext.xml create mode 100644 tests/integration/spring5/src/main/webapp/WEB-INF/web.xml create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceTestBase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/JerseyManagedITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/ResourceTestBase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedControllerITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedEndpointITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedRepositoryITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedServiceITCase.java create mode 100644 tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/TestUtil.java diff --git a/ext/spring5/pom.xml b/ext/spring5/pom.xml index 29fec65251..3f4eb21fd4 100644 --- a/ext/spring5/pom.xml +++ b/ext/spring5/pom.xml @@ -88,6 +88,10 @@ org.glassfish.hk2 hk2-api + + org.springframework + spring-context + @@ -109,6 +113,18 @@ + + org.springframework + spring-context + ${spring5.version} + + + commons-logging + commons-logging + + + + org.springframework spring-web diff --git a/tests/integration/pom.xml b/tests/integration/pom.xml index dbd46420ad..0b9a841f40 100644 --- a/tests/integration/pom.xml +++ b/tests/integration/pom.xml @@ -124,6 +124,7 @@ servlet-tests sonar-test spring4 + spring5 tracing-support microprofile/config/helidon microprofile/config/webapp diff --git a/tests/integration/spring5/README.txt b/tests/integration/spring5/README.txt new file mode 100644 index 0000000000..9a105f7bac --- /dev/null +++ b/tests/integration/spring5/README.txt @@ -0,0 +1,30 @@ + +tests +===== + +Tests are located in jersey-spring-test module. +The module contains a test webapp and test code. +The tests can be run in Jersey test container or an external container. + +- Running tests in Jersey test container + mvn clean test + +- Running tests in an external container + build the test app + deploy to an external container + configure container connection info in jersey-spring-test/pom.xml, if needed + run tests in integration test mode: + mvn -Pit verify + +- Running tests in embedded Jetty instance + build the test app + deploy to Jetty: + mvn -Pjetty jetty:run + run tests in integration test mode in another console session: + mvn -Pit verify + +test class naming conventions +- *ITTest.java: run in unit and IT test mode +- *Test.java: run as unit tests +- *IT.java: run as IT tests + diff --git a/tests/integration/spring5/pom.xml b/tests/integration/spring5/pom.xml new file mode 100644 index 0000000000..f32bb4d597 --- /dev/null +++ b/tests/integration/spring5/pom.xml @@ -0,0 +1,132 @@ + + + + + + 4.0.0 + + + org.glassfish.jersey.tests.integration + project + 2.30-SNAPSHOT + + + spring5 + + war + jersey-tests-integration-spring5 + + + Jersey tests for Spring 5 integration + + + + + + jakarta.servlet + jakarta.servlet-api + ${servlet4.version} + provided + + + + jakarta.ws.rs + jakarta.ws.rs-api + compile + + + + org.glassfish.jersey.ext + jersey-spring5 + ${project.version} + compile + + + + org.glassfish.jersey.test-framework + jersey-test-framework-core + test + + + + org.glassfish.jersey.containers + jersey-container-servlet + runtime + + + + org.glassfish.jersey.test-framework.providers + jersey-test-framework-provider-external + test + + + + commons-logging + commons-logging + runtime + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.apache.maven.plugins + maven-failsafe-plugin + + + org.eclipse.jetty + jetty-maven-plugin + + + / + .*$ + + + + + + + + + delayed-strategy-skip-test + + + org.glassfish.jersey.injection.manager.strategy + delayed + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + + true + + + + + + + diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountJerseyResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountJerseyResource.java new file mode 100644 index 0000000000..d804575d64 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountJerseyResource.java @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.math.BigDecimal; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.MediaType; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +/** + * Jersey managed JAX-RS resource for testing jersey-spring. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +@Path("/jersey/account") +public class AccountJerseyResource { + + @Inject + @Named("AccountService-singleton") + private AccountService accountServiceInject; + + @Autowired + @Qualifier("AccountService-singleton") + private AccountService accountServiceAutowired; + + @Inject + @Named("AccountService-request-1") + private AccountService accountServiceRequest1; + + @Autowired + @Qualifier("AccountService-request-1") + private AccountService accountServiceRequest2; + + @Autowired + @Qualifier("AccountService-prototype-1") + private AccountService accountServicePrototype1; + + @Autowired + @Qualifier("AccountService-prototype-1") + private AccountService accountServicePrototype2; + + @Autowired + private HttpServletRequest httpServletRequest; + + @Inject + private HK2ServiceSingleton hk2Singleton; + + @Inject + private HK2ServiceRequestScoped hk2RequestScoped; + + @Inject + private HK2ServicePerLookup hk2PerLookup; + + private String message = "n/a"; + + // resource methods for testing resource class scope + @GET + @Path("message") + public String getMessage() { + return message; + } + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(final String message) { + this.message = message; + return message; + } + + // JERSEY-2506 FIX VERIFICATION + @GET + @Path("server") + public String verifyServletRequestInjection() { + return "PASSED: " + httpServletRequest.getServerName(); + } + + @GET + @Path("singleton/server") + public String verifyServletRequestInjectionIntoSingleton() { + return accountServiceInject.verifyServletRequestInjection(); + } + + @GET + @Path("singleton/autowired/server") + public String verifyServletRequestInjectionIntoAutowiredSingleton() { + return accountServiceAutowired.verifyServletRequestInjection(); + } + + @GET + @Path("request/server") + public String verifyServletRequestInjectionIntoRequestScopedBean() { + return accountServiceRequest1.verifyServletRequestInjection(); + } + + @GET + @Path("prototype/server") + public String verifyServletRequestInjectionIntoPrototypeScopedBean() { + return accountServicePrototype1.verifyServletRequestInjection(); + } + + // resource methods for testing singleton scoped beans + @GET + @Path("singleton/inject/{accountId}") + public BigDecimal getAccountBalanceSingletonInject(@PathParam("accountId") final String accountId) { + return accountServiceInject.getAccountBalance(accountId); + } + + @GET + @Path("singleton/autowired/{accountId}") + public BigDecimal getAccountBalanceSingletonAutowired(@PathParam("accountId") final String accountId) { + return accountServiceAutowired.getAccountBalance(accountId); + } + + @PUT + @Path("singleton/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public void setAccountBalanceSingleton(@PathParam("accountId") final String accountId, final String balance) { + accountServiceInject.setAccountBalance(accountId, new BigDecimal(balance)); + } + + // resource methods for testing request scoped beans + @PUT + @Path("request/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public BigDecimal setAccountBalanceRequest(@PathParam("accountId") final String accountId, final String balance) { + accountServiceRequest1.setAccountBalance(accountId, new BigDecimal(balance)); + return accountServiceRequest2.getAccountBalance(accountId); + } + + // resource methods for testing prototype scoped beans + @PUT + @Path("prototype/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public BigDecimal setAccountBalancePrototype(@PathParam("accountId") final String accountId, final String balance) { + accountServicePrototype1.setAccountBalance(accountId, new BigDecimal(balance)); + return accountServicePrototype2.getAccountBalance(accountId); + } +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountService.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountService.java new file mode 100644 index 0000000000..70d90b05d5 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountService.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.math.BigDecimal; + +/** + * Simple account service to testify injection into different scopes. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public interface AccountService { + + void setAccountBalance(String accountId, BigDecimal balance); + + BigDecimal getAccountBalance(String accountId); + + String verifyServletRequestInjection(); +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountServiceImpl.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountServiceImpl.java new file mode 100644 index 0000000000..022cf1a078 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountServiceImpl.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; + +/** + * AccountService implementation. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class AccountServiceImpl implements AccountService { + + private Map accounts = new HashMap<>(); + private BigDecimal defaultAccountBalance; + + // JERSEY-2506 FIX VERIFICATION + @Autowired + private HttpServletRequest httpServletRequest; + + @Override + public void setAccountBalance(String accountId, BigDecimal balance) { + accounts.put(accountId, balance); + } + + @Override + public BigDecimal getAccountBalance(String accountId) { + BigDecimal balance = accounts.get(accountId); + if (balance == null) { + return defaultAccountBalance; + } + return balance; + } + + public void setDefaultAccountBalance(String defaultAccountBalance) { + this.defaultAccountBalance = new BigDecimal(defaultAccountBalance); + } + + public String verifyServletRequestInjection() { + return "PASSED: " + httpServletRequest.getServerName(); + } + +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountSpringResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountSpringResource.java new file mode 100644 index 0000000000..ba2db2dde1 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/AccountSpringResource.java @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.math.BigDecimal; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.MediaType; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Component; + +/** + * Spring managed JAX-RS resource for testing jersey-spring. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +@Path("/spring/account") +@Component +public class AccountSpringResource { + + @Inject + @Named("AccountService-singleton") + private AccountService accountServiceInject; + + @Autowired + @Qualifier("AccountService-singleton") + private AccountService accountServiceAutowired; + + @Inject + @Named("AccountService-request-1") + private AccountService accountServiceRequest1; + + @Autowired + @Qualifier("AccountService-request-1") + private AccountService accountServiceRequest2; + + @Autowired + @Qualifier("AccountService-prototype-1") + private AccountService accountServicePrototype1; + + @Autowired + @Qualifier("AccountService-prototype-1") + private AccountService accountServicePrototype2; + + @Autowired + private HttpServletRequest httpServletRequest; + + @Inject + private HK2ServiceSingleton hk2Singleton; + + @Inject + private HK2ServiceRequestScoped hk2RequestScoped; + + @Inject + private HK2ServicePerLookup hk2PerLookup; + + private String message = "n/a"; + + // resource methods for testing resource class scope + @GET + @Path("message") + public String getMessage() { + return message; + } + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(String message) { + this.message = message; + return message; + } + + // JERSEY-2506 FIX VERIFICATION + @GET + @Path("server") + public String verifyServletRequestInjection() { + return "PASSED: " + httpServletRequest.getServerName(); + } + + @GET + @Path("singleton/server") + public String verifyServletRequestInjectionIntoSingleton() { + return accountServiceInject.verifyServletRequestInjection(); + } + + @GET + @Path("singleton/autowired/server") + public String verifyServletRequestInjectionIntoAutowiredSingleton() { + return accountServiceAutowired.verifyServletRequestInjection(); + } + + @GET + @Path("request/server") + public String verifyServletRequestInjectionIntoRequestScopedBean() { + return accountServiceRequest1.verifyServletRequestInjection(); + } + + @GET + @Path("prototype/server") + public String verifyServletRequestInjectionIntoPrototypeScopedBean() { + return accountServicePrototype1.verifyServletRequestInjection(); + } + + // resource methods for testing singleton scoped beans + @GET + @Path("singleton/inject/{accountId}") + public BigDecimal getAccountBalanceSingletonInject(@PathParam("accountId") String accountId) { + return accountServiceInject.getAccountBalance(accountId); + } + + @GET + @Path("singleton/autowired/{accountId}") + public BigDecimal getAccountBalanceSingletonAutowired(@PathParam("accountId") String accountId) { + return accountServiceAutowired.getAccountBalance(accountId); + } + + @PUT + @Path("singleton/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public void setAccountBalanceSingleton(@PathParam("accountId") String accountId, String balance) { + accountServiceInject.setAccountBalance(accountId, new BigDecimal(balance)); + } + + // resource methods for testing request scoped beans + @PUT + @Path("request/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public BigDecimal setAccountBalanceRequest(@PathParam("accountId") String accountId, String balance) { + accountServiceRequest1.setAccountBalance(accountId, new BigDecimal(balance)); + return accountServiceRequest2.getAccountBalance(accountId); + } + + // resource methods for testing prototype scoped beans + @PUT + @Path("prototype/{accountId}") + @Consumes(MediaType.TEXT_PLAIN) + public BigDecimal setAccountBalancePrototype(@PathParam("accountId") String accountId, String balance) { + accountServicePrototype1.setAccountBalance(accountId, new BigDecimal(balance)); + return accountServicePrototype2.getAccountBalance(accountId); + } + +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ControllerResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ControllerResource.java new file mode 100644 index 0000000000..c45e2e245e --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ControllerResource.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +import org.springframework.stereotype.Controller; + +/** + * @author Konrad Garus (konrad.garus at gmail.com) + */ +@Controller +@Path("/spring/controller") +public class ControllerResource { + + private String message; + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(final String message) { + this.message = message; + return message; + } + + @GET + @Path("message") + public String getMessage() { + return message; + } +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/Endpoint.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/Endpoint.java new file mode 100644 index 0000000000..80f5d8c1ae --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/Endpoint.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.stereotype.Component; + +/** + * @author Konrad Garus (konrad.garus at gmail.com) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Component +public @interface Endpoint { + +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/EndpointResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/EndpointResource.java new file mode 100644 index 0000000000..5dd6e5c01c --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/EndpointResource.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +/** + * @author Konrad Garus (konrad.garus at gmail.com) + */ +@Endpoint +@Path("/spring/endpoint") +public class EndpointResource { + + private String message; + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(final String message) { + this.message = message; + return message; + } + + @GET + @Path("message") + public String getMessage() { + return message; + } +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServicePerLookup.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServicePerLookup.java new file mode 100644 index 0000000000..4b6bbf57ae --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServicePerLookup.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +/** + * Type to be handled as HK2 per-lookup bean. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class HK2ServicePerLookup { +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceRequestScoped.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceRequestScoped.java new file mode 100644 index 0000000000..3bde89d214 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceRequestScoped.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +/** + * Type to be handled as HK2 request scoped bean. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class HK2ServiceRequestScoped { +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceSingleton.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceSingleton.java new file mode 100644 index 0000000000..424f1866e8 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/HK2ServiceSingleton.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +/** + * Type to be handled as HK2 singleton. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class HK2ServiceSingleton { +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/MyApplication.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/MyApplication.java new file mode 100644 index 0000000000..66f0b6d88e --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/MyApplication.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.core.Application; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import org.glassfish.jersey.internal.inject.AbstractBinder; +import org.glassfish.jersey.internal.inject.Binder; +import org.glassfish.jersey.internal.inject.InjectionManager; +import org.glassfish.jersey.internal.inject.PerLookup; +import org.glassfish.jersey.process.internal.RequestScoped; + +/** + * JAX-RS application class for configuring injectable services in HK2 registry for testing purposes. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class MyApplication extends Application { + + @Inject + public MyApplication(final InjectionManager injectionManager) { + Binder binder = new AbstractBinder() { + @Override + protected void configure() { + bindAsContract(HK2ServiceSingleton.class).in(Singleton.class); + bindAsContract(HK2ServiceRequestScoped.class).in(RequestScoped.class); + bindAsContract(HK2ServicePerLookup.class).in(PerLookup.class); + } + }; + + injectionManager.register(binder); + } +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/RepositoryResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/RepositoryResource.java new file mode 100644 index 0000000000..97b0bf0e33 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/RepositoryResource.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +import org.springframework.stereotype.Repository; + +/** + * @author Konrad Garus (konrad.garus at gmail.com) + */ +@Repository +@Path("/spring/repository") +public class RepositoryResource { + + private String message; + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(final String message) { + this.message = message; + return message; + } + + @GET + @Path("message") + public String getMessage() { + return message; + } +} diff --git a/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ServiceResource.java b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ServiceResource.java new file mode 100644 index 0000000000..b47214d2a2 --- /dev/null +++ b/tests/integration/spring5/src/main/java/org/glassfish/jersey/server/spring/test/ServiceResource.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.MediaType; + +import org.springframework.stereotype.Service; + +/** + * @author Konrad Garus (konrad.garus at gmail.com) + */ +@Service +@Path("/spring/service") +public class ServiceResource { + + private String message; + + @PUT + @Path("message") + @Consumes(MediaType.TEXT_PLAIN) + public String setMessage(final String message) { + this.message = message; + return message; + } + + @GET + @Path("message") + public String getMessage() { + return message; + } +} diff --git a/tests/integration/spring5/src/main/resources/applicationContext.xml b/tests/integration/spring5/src/main/resources/applicationContext.xml new file mode 100644 index 0000000000..c7e421eaa3 --- /dev/null +++ b/tests/integration/spring5/src/main/resources/applicationContext.xml @@ -0,0 +1,69 @@ + + + + + + + + + + org.springframework.beans.factory.annotation.Autowired + org.springframework.beans.factory.annotation.Value + + + + + + + + + org.springframework.beans.factory.annotation.Qualifier + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/integration/spring5/src/main/webapp/WEB-INF/web.xml b/tests/integration/spring5/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..4004dd3c3c --- /dev/null +++ b/tests/integration/spring5/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,50 @@ + + + + + + jersey-spring-test + + + + + + org.glassfish.jersey.server.spring.test.MyApplication + + + org.glassfish.jersey.server.spring.test.MyApplication + /* + + + + + diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceITCase.java new file mode 100644 index 0000000000..370faf2c3d --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceITCase.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.test.external.ExternalTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerException; +import org.glassfish.jersey.test.spi.TestContainerFactory; +import org.glassfish.jersey.test.JerseyTest; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class AccountResourceITCase extends JerseyTest { + + @Override + protected Application configure() { + return new Application(); + } + + @Override + protected TestContainerFactory getTestContainerFactory() throws TestContainerException { + return new ExternalTestContainerFactory(); + } + + @Test + public void testGet() throws Exception { + final String r = target().path("/jersey/account/message").request().get(String.class); + assertEquals(r, "n/a"); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceTestBase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceTestBase.java new file mode 100644 index 0000000000..f63fd1216c --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/AccountResourceTestBase.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import java.math.BigDecimal; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.junit.Test; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; + +/** + * Base class for JAX-RS resource tests. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public abstract class AccountResourceTestBase extends ResourceTestBase { + + // test singleton scoped Spring bean injection using @Inject + @Autowired + @Test + public void testSingletonScopedSpringService() { + final BigDecimal newBalance = new BigDecimal(Math.random()); + final WebTarget t = target(getResourceFullPath()); + + t.path("/singleton/xyz123").request().put(Entity.entity(newBalance.toString(), MediaType.TEXT_PLAIN_TYPE)); + final BigDecimal balance = t.path("/singleton/autowired/xyz123").request().get(BigDecimal.class); + assertEquals(newBalance, balance); + } + + @Test + public void testRequestScopedSpringService() { + final BigDecimal newBalance = new BigDecimal(Math.random()); + final WebTarget t = target(getResourceFullPath()); + final BigDecimal balance = t.path("request/abc456").request().put(Entity.text(newBalance), BigDecimal.class); + assertEquals(newBalance, balance); + } + + @Test + public void testPrototypeScopedSpringService() { + final BigDecimal newBalance = new BigDecimal(Math.random()); + final WebTarget t = target(getResourceFullPath()); + final BigDecimal balance = t.path("prototype/abc456").request().put(Entity.text(newBalance), BigDecimal.class); + assertEquals(new BigDecimal("987.65"), balance); + } + + @Test + public void testServletInjection() { + final WebTarget t = target(getResourceFullPath()); + + String server = t.path("server").request().get(String.class); + assertThat(server, startsWith("PASSED: ")); + + server = t.path("singleton/server").request().get(String.class); + assertThat(server, startsWith("PASSED: ")); + + server = t.path("singleton/autowired/server").request().get(String.class); + assertThat(server, startsWith("PASSED: ")); + + server = t.path("request/server").request().get(String.class); + assertThat(server, startsWith("PASSED: ")); + + server = t.path("prototype/server").request().get(String.class); + assertThat(server, startsWith("PASSED: ")); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/JerseyManagedITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/JerseyManagedITCase.java new file mode 100644 index 0000000000..f7eccef515 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/JerseyManagedITCase.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Jersey managed JAX-RS resources. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class JerseyManagedITCase extends AccountResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(AccountJerseyResource.class); + } + + @Override + protected String getResourcePath() { + return "/jersey/account"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals("n/a", msg); + } + +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/ResourceTestBase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/ResourceTestBase.java new file mode 100644 index 0000000000..4836a7cb01 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/ResourceTestBase.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.server.spring.SpringLifecycleListener; +import org.glassfish.jersey.server.spring.scope.RequestContextFilter; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.glassfish.jersey.test.external.ExternalTestContainerFactory; +import org.glassfish.jersey.test.spi.TestContainerException; +import org.glassfish.jersey.test.spi.TestContainerFactory; + +/** + * Base class for JAX-RS resource tests. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public abstract class ResourceTestBase extends JerseyTest { + + private static final String TEST_WEBAPP_CONTEXT_PATH = "jersey.spring.test.contextPath"; + private static final String TEST_CONTAINER_FACTORY_EXTERNAL = "org.glassfish.jersey.test.external" + + ".ExternalTestContainerFactory"; + + @Override + protected TestContainerFactory getTestContainerFactory() throws TestContainerException { + return new ExternalTestContainerFactory(); + } + + @Override + protected Application configure() { + final ResourceConfig rc = new ResourceConfig() + .register(SpringLifecycleListener.class) + .register(RequestContextFilter.class); + TestUtil.registerHK2Services(rc); + rc.property("contextConfigLocation", "classpath:applicationContext.xml"); + return configure(rc); + } + + protected abstract ResourceConfig configure(ResourceConfig rc); + + protected abstract String getResourcePath(); + + protected String getResourceFullPath() { + final String containerFactory = System.getProperty(TestProperties.CONTAINER_FACTORY); + if (TEST_CONTAINER_FACTORY_EXTERNAL.equals(containerFactory)) { + return System.getProperty(TEST_WEBAPP_CONTEXT_PATH) + getResourcePath(); + } + return getResourcePath(); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedControllerITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedControllerITCase.java new file mode 100644 index 0000000000..05aaea81d6 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedControllerITCase.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Spring managed JAX-RS resources with @Controller archetype. + * + * @author Konrad Garus (konrad.garus at gmail.com) + */ +public class SpringManagedControllerITCase extends ResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(ControllerResource.class); + } + + @Override + protected String getResourcePath() { + return "/spring/controller"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals(message, msg); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedEndpointITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedEndpointITCase.java new file mode 100644 index 0000000000..986abc831e --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedEndpointITCase.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Spring managed JAX-RS resources with custom composite + * annotation that derives from @Component. + * + * @author Konrad Garus (konrad.garus at gmail.com) + */ +public class SpringManagedEndpointITCase extends ResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(EndpointResource.class); + } + + @Override + protected String getResourcePath() { + return "/spring/endpoint"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals(message, msg); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedITCase.java new file mode 100644 index 0000000000..5ddf2a6588 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedITCase.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Spring managed JAX-RS resources. + * + * @author Marko Asplund (marko.asplund at yahoo.com) + */ +public class SpringManagedITCase extends AccountResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(AccountSpringResource.class); + } + + @Override + protected String getResourcePath() { + return "/spring/account"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals(message, msg); + } + +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedRepositoryITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedRepositoryITCase.java new file mode 100644 index 0000000000..7b1d2005c4 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedRepositoryITCase.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Spring managed JAX-RS resources with @Repository archetype. + * + * @author Konrad Garus (konrad.garus at gmail.com) + */ +public class SpringManagedRepositoryITCase extends ResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(RepositoryResource.class); + } + + @Override + protected String getResourcePath() { + return "/spring/repository"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals(message, msg); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedServiceITCase.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedServiceITCase.java new file mode 100644 index 0000000000..0404467675 --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/SpringManagedServiceITCase.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; + +import org.glassfish.jersey.server.ResourceConfig; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Tests for Spring managed JAX-RS resources with @Service archetype. + * + * @author Konrad Garus (konrad.garus at gmail.com) + */ +public class SpringManagedServiceITCase extends ResourceTestBase { + + @Override + protected ResourceConfig configure(final ResourceConfig rc) { + return rc.register(ServiceResource.class); + } + + @Override + protected String getResourcePath() { + return "/spring/service"; + } + + @Test + public void testResourceScope() { + final WebTarget t = target(getResourceFullPath()); + final String message = "hello, world"; + final String echo = t.path("message").request().put(Entity.text(message), String.class); + assertEquals(message, echo); + final String msg = t.path("message").request().get(String.class); + assertEquals(message, msg); + } +} diff --git a/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/TestUtil.java b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/TestUtil.java new file mode 100644 index 0000000000..35062d878c --- /dev/null +++ b/tests/integration/spring5/src/test/java/org/glassfish/jersey/server/spring/test/TestUtil.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.server.spring.test; + +import javax.inject.Singleton; + +import org.glassfish.jersey.internal.inject.PerLookup; +import org.glassfish.jersey.process.internal.RequestScoped; +import org.glassfish.jersey.server.ResourceConfig; + +import org.glassfish.hk2.utilities.BuilderHelper; +import org.glassfish.hk2.utilities.binding.AbstractBinder; + +class TestUtil { + + public static ResourceConfig registerHK2Services(final ResourceConfig rc) { + rc + .register(new AbstractBinder() { + @Override + protected void configure() { + bind(BuilderHelper.link(HK2ServiceSingleton.class).in(Singleton.class).build()); + } + }) + .register(new AbstractBinder() { + @Override + protected void configure() { + bind(BuilderHelper.link(HK2ServiceRequestScoped.class).in(RequestScoped.class).build()); + } + }) + .register(new AbstractBinder() { + @Override + protected void configure() { + bind(BuilderHelper.link(HK2ServicePerLookup.class).in(PerLookup.class).build()); + } + }); + return rc; + } +}