Skip to content

Commit

Permalink
Test CDI and EJB injection with appClientSupport for InstantOn
Browse files Browse the repository at this point in the history
  • Loading branch information
anjumfatima90 committed Feb 12, 2025
1 parent 719959a commit 3bc7f89
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -68,6 +68,27 @@ public static void afterClass() throws Exception {
*/
@Test
public void injectGlobal_EJB() throws Exception {
checkInjection();
}

/**
* Tests that a remote EJB is injected.
*/
@Test
public void inject_EJB() throws Exception {
checkInjection();
}

// Assisted by watsonx Code Assistant
/**
* This method tests the injection of CDI.
*/
@Test
public void inject_CDI() throws Exception {
checkInjection();
}

private void checkInjection() throws Exception {
String methodName = testName.getMethodName();
int idx = -1;
if ((idx = methodName.indexOf("_EE")) != -1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* Copyright (c) 2024, 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -14,26 +14,41 @@
package io.openliberty.checkpoint.fat.appclientsupport;

import javax.ejb.EJB;
import javax.inject.Inject;

import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleInjectionBeanRemote;
import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleCDIInjection;
import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleEJBInjectionBeanRemote;
import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleGlobalEJBInjectionBeanRemote;

public class InjectionClientMain {

private final static String NAME_EJB_GLOBAL = "java:global/InjectionApp/InjectionAppEJB/SimpleInjectionBean!io.openliberty.checkpoint.fat.appclientsupport.view.SimpleInjectionBeanRemote";
private final static String NAME_EJB_GLOBAL = "java:global/InjectionApp/InjectionAppEJB/SimpleGlobalEJBInjectionBean!io.openliberty.checkpoint.fat.appclientsupport.view.SimpleGlobalEJBInjectionBeanRemote";

@EJB(lookup = NAME_EJB_GLOBAL)
public static SimpleInjectionBeanRemote injectedGlobalEjb;
public static SimpleGlobalEJBInjectionBeanRemote injectedGlobalEjb;

@EJB
public static SimpleEJBInjectionBeanRemote injectedEjb;

@Inject
public static SimpleCDIInjection cdiBean;

public static void main(String[] args) {
System.out.println("main - entry");

if (checkEJB(injectedGlobalEjb))
if (checkEJBWithLookup(injectedGlobalEjb))
System.out.println("injectGlobal_EJB-PASSED");

if (checkEJB(injectedEjb))
System.out.println("inject_EJB-PASSED");

if (checkCDI(cdiBean))
System.out.println("inject_CDI-PASSED");

System.out.println("main - exit");
}

private static boolean checkEJB(SimpleInjectionBeanRemote ejb) {
private static boolean checkEJBWithLookup(SimpleGlobalEJBInjectionBeanRemote ejb) {
boolean b;
try {
b = ejb != null && ejb.add(4, 8) == 12;
Expand All @@ -44,4 +59,26 @@ private static boolean checkEJB(SimpleInjectionBeanRemote ejb) {
return b;
}

private static boolean checkEJB(SimpleEJBInjectionBeanRemote ejb) {
boolean b;
try {
b = ejb != null && ejb.subtract(12, 4) == 8;
} catch (Exception ex) {
ex.printStackTrace();
b = false;
}
return b;
}

private static boolean checkCDI(SimpleCDIInjection cdiBean) {
boolean b;
try {
b = cdiBean != null && cdiBean.multiply(2, 4) == 8;
} catch (Exception ex) {
ex.printStackTrace();
b = false;
}
return b;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd"
version="4.0"
bean-discovery-mode="all">
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
<display-name>InjectionAppEJB</display-name>
<enterprise-beans>
<session>
<ejb-name>SimpleInjectionBean</ejb-name>
<ejb-name>SimpleGlobalEJBInjectionBean</ejb-name>
<env-entry>
<description>Simple entry bound in java:global</description>
<env-entry-name>java:global/env/globalEnvEntry</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>I am global</env-entry-value>
</env-entry>
</session>

<session>
<ejb-name>SimpleEJBInjectionBean</ejb-name>
</session>
</enterprise-beans>
</ejb-jar>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package io.openliberty.checkpoint.fat.appclientsupport;

import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleCDIInjection;

public class SimpleCDIInjectionBean implements SimpleCDIInjection {
@Override
public int multiply(int x, int y) {
return x * y;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package io.openliberty.checkpoint.fat.appclientsupport;

import javax.ejb.Remote;
import javax.ejb.Singleton;

import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleEJBInjectionBeanRemote;

@Singleton
@Remote(SimpleEJBInjectionBeanRemote.class)
public class SimpleEJBInjectionBean implements SimpleEJBInjectionBeanRemote {

@Override
public int subtract(int x, int y) {
return x - y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import javax.ejb.Remote;
import javax.ejb.Singleton;

import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleInjectionBeanRemote;
import io.openliberty.checkpoint.fat.appclientsupport.view.SimpleGlobalEJBInjectionBeanRemote;

@Singleton
@Remote(SimpleInjectionBeanRemote.class)
public class SimpleInjectionBean implements SimpleInjectionBeanRemote {
@Remote(SimpleGlobalEJBInjectionBeanRemote.class)
public class SimpleGlobalEJBInjectionBean implements SimpleGlobalEJBInjectionBeanRemote {

@Override
public int add(int x, int y) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package io.openliberty.checkpoint.fat.appclientsupport.view;

public interface SimpleCDIInjection {

public int multiply(int x, int y);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
* Copyright (c) 2025 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/

package io.openliberty.checkpoint.fat.appclientsupport.view;

public interface SimpleEJBInjectionBeanRemote {

public int subtract(int x, int y);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package io.openliberty.checkpoint.fat.appclientsupport.view;

public interface SimpleInjectionBeanRemote {
public interface SimpleGlobalEJBInjectionBeanRemote {

public int add(int x, int y);
}

0 comments on commit 3bc7f89

Please sign in to comment.