-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable to use @context in constructors of classes instantiated by CDI
Signed-off-by: jansupol <[email protected]>
- Loading branch information
Showing
10 changed files
with
321 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
tests/integration/cdi-integration/cdi-resource-with-at-context/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2021 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 | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.glassfish.jersey.tests.integration.cdi</groupId> | ||
<artifactId>cdi-integration-project</artifactId> | ||
<version>2.35-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>cdi-resource-with-at-context</artifactId> | ||
|
||
<name>jersey-tests-resource-with-at-context</name> | ||
|
||
<description>CDI works for a resource class with constructor with @Context</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>jakarta.ws.rs</groupId> | ||
<artifactId>jakarta.ws.rs-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.annotation</groupId> | ||
<artifactId>jakarta.annotation-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.enterprise</groupId> | ||
<artifactId>cdi-api</artifactId> | ||
<version>2.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.servlet</groupId> | ||
<artifactId>jakarta.servlet-api</artifactId> | ||
<version>${servlet4.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.test-framework.providers</groupId> | ||
<artifactId>jersey-test-framework-provider-grizzly2</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.ext.cdi</groupId> | ||
<artifactId>jersey-weld2-se</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.weld.se</groupId> | ||
<artifactId>weld-se-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.ext.cdi</groupId> | ||
<artifactId>jersey-cdi1x</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.ext.cdi</groupId> | ||
<artifactId>jersey-cdi-rs-inject</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.media</groupId> | ||
<artifactId>jersey-media-sse</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.containers</groupId> | ||
<artifactId>jersey-container-servlet-core</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
24 changes: 24 additions & 0 deletions
24
...e-with-at-context/src/main/java/org/glassfish/jersey/tests/cdi/resourceatcontext/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2021 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.tests.cdi.resourceatcontext; | ||
|
||
import org.glassfish.jersey.server.ResourceConfig; | ||
|
||
public class App extends ResourceConfig { | ||
public App() { | ||
register(ResourceWithConstructor.class); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...c/main/java/org/glassfish/jersey/tests/cdi/resourceatcontext/ResourceWithConstructor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2021 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.tests.cdi.resourceatcontext; | ||
|
||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
|
||
@Path("/") | ||
public class ResourceWithConstructor { | ||
HttpHeaders headers; | ||
|
||
@Inject | ||
BeanManager beanManager; | ||
|
||
public ResourceWithConstructor(@Context HttpHeaders headers) { | ||
this.headers = headers; | ||
} | ||
|
||
@GET | ||
@Path("get") | ||
public String get() { | ||
return headers != null && beanManager != null ? "OK" : "NOK"; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ration/cdi-integration/cdi-resource-with-at-context/src/main/resources/META-INF/beans.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2021 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 | ||
--> | ||
|
||
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee | ||
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" | ||
bean-discovery-mode="none"> | ||
</beans> |
91 changes: 91 additions & 0 deletions
91
...st/java/org/glassfish/jersey/tests/cdi/resourceatcontext/ResourceWithConstructorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2021 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.tests.cdi.resourceatcontext; | ||
|
||
import org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import org.glassfish.jersey.servlet.ServletProperties; | ||
import org.glassfish.jersey.test.DeploymentContext; | ||
import org.glassfish.jersey.test.JerseyTest; | ||
import org.glassfish.jersey.test.ServletDeploymentContext; | ||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; | ||
import org.glassfish.jersey.test.spi.TestContainerException; | ||
import org.glassfish.jersey.test.spi.TestContainerFactory; | ||
import org.jboss.weld.environment.se.Weld; | ||
import org.junit.Assert; | ||
import org.junit.Assume; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.enterprise.inject.spi.BeanManager; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.Response; | ||
|
||
public class ResourceWithConstructorTest extends JerseyTest { | ||
private Weld weld; | ||
|
||
@Before | ||
public void setup() { | ||
Assume.assumeTrue(Hk2InjectionManagerFactory.isImmediateStrategy()); | ||
} | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
if (Hk2InjectionManagerFactory.isImmediateStrategy()) { | ||
weld = new Weld(); | ||
weld.initialize(); | ||
super.setUp(); | ||
} | ||
} | ||
|
||
@Override | ||
public void tearDown() throws Exception { | ||
if (Hk2InjectionManagerFactory.isImmediateStrategy()) { | ||
weld.shutdown(); | ||
super.tearDown(); | ||
} | ||
} | ||
|
||
@Override | ||
protected Application configure() { | ||
return new App(); | ||
} | ||
|
||
@Override | ||
protected TestContainerFactory getTestContainerFactory() throws TestContainerException { | ||
return new GrizzlyWebTestContainerFactory(); | ||
} | ||
|
||
@Override | ||
protected DeploymentContext configureDeployment() { | ||
return ServletDeploymentContext.builder(configure()) | ||
.initParam(ServletProperties.JAXRS_APPLICATION_CLASS, App.class.getName()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testContextInConstructorAndInjectInClass() throws InterruptedException { | ||
try (Response r = target().path("get").request().get()) { | ||
Assert.assertEquals("OK", r.readEntity(String.class)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters