-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17488 from WhiteCat22/resteasy_cdi_spi
update the restfulWS-3.0 feature to use the CDIExtensionMetadata SPI
- Loading branch information
Showing
16 changed files
with
350 additions
and
59 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
50 changes: 50 additions & 0 deletions
50
...jaxrs.2.0.cdi.1.2_fat/fat/src/com/ibm/ws/jaxrs20/cdi12/fat/test/CDIInjectIntoAppTest.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,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.test; | ||
|
||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.ibm.websphere.simplicity.ShrinkHelper; | ||
import com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.CDIInjectIntoAppTestServlet; | ||
|
||
import componenttest.annotation.Server; | ||
import componenttest.annotation.SkipForRepeat; | ||
import componenttest.annotation.TestServlet; | ||
import componenttest.custom.junit.runner.FATRunner; | ||
import componenttest.topology.impl.LibertyServer; | ||
import componenttest.topology.utils.FATServletClient; | ||
|
||
@RunWith(FATRunner.class) | ||
@SkipForRepeat("EE9_FEATURES") // skip because cdi injection in Application subclasses require the @ApplicationPath annotation in our EE9 implementation | ||
public class CDIInjectIntoAppTest extends FATServletClient { | ||
|
||
public static final String APP_NAME = "cdiinjectintoapp"; | ||
public static final String SERVER_NAME = APP_NAME; | ||
|
||
@Server(SERVER_NAME) | ||
@TestServlet(servlet = CDIInjectIntoAppTestServlet.class, contextRoot = APP_NAME) | ||
public static LibertyServer server; | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
WebArchive war = ShrinkHelper.defaultDropinApp(server, APP_NAME, "com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp"); | ||
|
||
server.startServer(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
server.stopServer(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
dev/com.ibm.ws.jaxrs.2.0.cdi.1.2_fat/publish/servers/cdiinjectintoapp/bootstrap.properties
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,3 @@ | ||
bootstrap.include=../testports.properties | ||
#com.ibm.ws.logging.max.files=1 | ||
#com.ibm.ws.logging.trace.specification=LogService=all:RESTfulWS=all |
13 changes: 13 additions & 0 deletions
13
dev/com.ibm.ws.jaxrs.2.0.cdi.1.2_fat/publish/servers/cdiinjectintoapp/server.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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<server> | ||
<featureManager> | ||
<feature>componenttest-1.0</feature> | ||
<feature>jaxrs-2.0</feature> | ||
<feature>cdi-1.2</feature> | ||
</featureManager> | ||
<include location="../fatTestPorts.xml"/> | ||
|
||
<javaPermission className="java.util.PropertyPermission" name="*" actions="read" /> | ||
<javaPermission className="java.net.URLPermission" name="http://localhost:8010/cdiinjectintoapp/app/-" actions="GET:"/> | ||
<javaPermission className="java.net.SocketPermission" name="*" actions="connect,resolve"/> | ||
</server> |
15 changes: 15 additions & 0 deletions
15
...ibm.ws.jaxrs.2.0.cdi.1.2_fat/test-applications/cdiinjectintoapp/resources/WEB-INF/web.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,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<web-app id="HelloworldServlet" version="3.0" | ||
xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee | ||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> | ||
<servlet> | ||
<servlet-name>com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.MyApplication</servlet-name> | ||
</servlet> | ||
<!-- SERVLET MAPPINGS --> | ||
<servlet-mapping> | ||
<servlet-name>com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp.MyApplication</servlet-name> | ||
<url-pattern>/app/*</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
59 changes: 59 additions & 0 deletions
59
...ntoapp/src/com/ibm/ws/jaxrs20/cdi12/fat/cdiinjectintoapp/CDIInjectIntoAppTestServlet.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,59 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
|
||
import org.junit.Test; | ||
|
||
import componenttest.app.FATServlet; | ||
import javax.servlet.annotation.WebServlet; | ||
|
||
@SuppressWarnings("serial") | ||
@WebServlet("/CDIInjectIntoAppTestServlet") | ||
public class CDIInjectIntoAppTestServlet extends FATServlet { | ||
|
||
@Test | ||
public void testCanInjectIntoAppAndAppIntoResource() throws Exception { | ||
assertEquals("SUCCESS", testResource("checkAppInjection")); | ||
} | ||
|
||
@Test | ||
public void testInjectedBeansHaveCorrectScopes() throws Exception { | ||
assertEquals("1 - 1", testResource("1")); | ||
assertEquals("2 - 2", testResource("1")); | ||
assertEquals("1 - 3", testResource("2")); | ||
assertEquals("3 - 4", testResource("1")); | ||
assertEquals("2 - 5", testResource("2")); | ||
} | ||
|
||
private String testResource(String resourcePath) throws Exception { | ||
URI uri = URI.create("http://localhost:" + System.getProperty("bvt.prop.HTTP_default") + "/cdiinjectintoapp/app/resource/" + resourcePath); | ||
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection(); | ||
assertEquals(200, conn.getResponseCode()); | ||
return readEntity(conn.getInputStream()); | ||
} | ||
|
||
private String readEntity(InputStream is) throws Exception { | ||
StringBuilder sb = new StringBuilder(); | ||
byte[] b = new byte[256]; | ||
int i = is.read(b); | ||
while (i > 0) { | ||
sb.append(new String(b, 0, i)); | ||
i = is.read(b); | ||
} | ||
return sb.toString().trim(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...cdiinjectintoapp/src/com/ibm/ws/jaxrs20/cdi12/fat/cdiinjectintoapp/InvocationCounter.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,18 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp; | ||
|
||
public interface InvocationCounter { | ||
|
||
int invoke(); | ||
|
||
int getInvocations(); | ||
} |
31 changes: 31 additions & 0 deletions
31
...njectintoapp/src/com/ibm/ws/jaxrs20/cdi12/fat/cdiinjectintoapp/InvocationCounterImpl.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,31 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class InvocationCounterImpl implements InvocationCounter { | ||
|
||
AtomicInteger count = new AtomicInteger(0); | ||
|
||
@Override | ||
public int invoke() { | ||
return count.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public int getInvocations() { | ||
return count.get(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ons/cdiinjectintoapp/src/com/ibm/ws/jaxrs20/cdi12/fat/cdiinjectintoapp/MyApplication.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,46 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp; | ||
|
||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.ApplicationPath; | ||
import javax.ws.rs.core.Application; | ||
|
||
//Test that CDI injects without the @ApplicationPath annotation, use web.xml instead | ||
//@ApplicationPath("/app") | ||
public class MyApplication extends Application { | ||
|
||
@Inject | ||
InvocationCounter counter; | ||
|
||
@Override | ||
public Map<String, Object> getProperties() { | ||
System.out.println("counter=" + counter); | ||
return Collections.singletonMap("counter", counter); | ||
} | ||
|
||
InvocationCounter getCounter() { | ||
return counter; | ||
} | ||
|
||
@Override | ||
public Set<Class<?>> getClasses() { | ||
Set<Class<?>> classes = new HashSet<Class<?>>(); | ||
classes.add(MyResource.class); | ||
return classes; | ||
|
||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ations/cdiinjectintoapp/src/com/ibm/ws/jaxrs20/cdi12/fat/cdiinjectintoapp/MyResource.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,68 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 IBM Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* IBM Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
package com.ibm.ws.jaxrs20.cdi12.fat.cdiinjectintoapp; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.core.Application; | ||
import javax.ws.rs.core.Context; | ||
|
||
@Path("/resource") | ||
public class MyResource { | ||
|
||
static Map<String,AtomicInteger> resourceCounterMap = new HashMap<>(); | ||
|
||
static { | ||
resourceCounterMap.put("1", new AtomicInteger(0)); | ||
resourceCounterMap.put("2", new AtomicInteger(0)); | ||
} | ||
|
||
@Context | ||
Application app; | ||
|
||
@GET | ||
@Path("checkAppInjection") | ||
public String checkAppInjectionNotNull() { | ||
// Do not update any counters for this method. | ||
if (app == null) { | ||
return "Failed to inject app into MyResource"; | ||
} | ||
Map<String, Object> props = app.getProperties(); | ||
if (props == null) { | ||
return "Application#getProperties is null"; | ||
} | ||
Object counter = props.get("counter"); | ||
if (counter == null) { | ||
return "Counter that is supposed to be injected into MyApplication is null"; | ||
} | ||
return "SUCCESS"; | ||
} | ||
|
||
@GET | ||
@Path("1") | ||
public String one() { | ||
int resourceCount = resourceCounterMap.get("1").incrementAndGet(); | ||
int appCount = ((InvocationCounter) app.getProperties().get("counter")).invoke(); | ||
return "" + resourceCount + " - " + appCount; | ||
} | ||
|
||
@GET | ||
@Path("2") | ||
public String two() { | ||
int resourceCount = resourceCounterMap.get("2").incrementAndGet(); | ||
int appCount = ((InvocationCounter) app.getProperties().get("counter")).invoke(); | ||
return "" + resourceCount + " - " + appCount; | ||
} | ||
} |
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
Oops, something went wrong.