JAVA-12424 Renamed spring-ejb to spring-ejb-modules
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.ejb.stateful.EJBClient1;
|
||||
import com.baeldung.ejb.stateful.EJBClient2;
|
||||
import com.baeldung.ejb.stateful.StatefulEJB;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class StatefulEJBIntegrationTest {
|
||||
|
||||
@Inject
|
||||
private EJBClient1 ejbClient1;
|
||||
|
||||
@Inject
|
||||
private EJBClient2 ejbClient2;
|
||||
|
||||
@Test
|
||||
public void givenOneStatefulBean_whenTwoClientsSetValueOnBean_thenClientStateIsMaintained() {
|
||||
|
||||
// act
|
||||
ejbClient1.statefulEJB.name = "Client 1";
|
||||
ejbClient2.statefulEJB.name = "Client 2";
|
||||
|
||||
// assert
|
||||
Assert.assertNotEquals(ejbClient1.statefulEJB.name, ejbClient2.statefulEJB.name);
|
||||
Assert.assertEquals("Client 1", ejbClient1.statefulEJB.name);
|
||||
Assert.assertEquals("Client 2", ejbClient2.statefulEJB.name);
|
||||
|
||||
}
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClass(StatefulEJB.class)
|
||||
.addClass(EJBClient1.class)
|
||||
.addClass(EJBClient2.class)
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.ejb.stateless.EJBClient1;
|
||||
import com.baeldung.ejb.stateless.EJBClient2;
|
||||
import com.baeldung.ejb.stateless.StatelessEJB;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
@Ignore("Will be fixed in BAEL-4422")
|
||||
public class StatelessEJBIntegrationTest {
|
||||
|
||||
@Inject
|
||||
private EJBClient1 ejbClient1;
|
||||
|
||||
@Inject
|
||||
private EJBClient2 ejbClient2;
|
||||
|
||||
@Test
|
||||
public void givenOneStatelessBean_whenStateIsSetInOneBean_secondBeanShouldHaveSameState() {
|
||||
|
||||
// act
|
||||
ejbClient1.statelessEJB.name = "Client 1";
|
||||
|
||||
// assert
|
||||
Assert.assertEquals("Client 1", ejbClient1.statelessEJB.name);
|
||||
Assert.assertEquals("Client 1", ejbClient2.statelessEJB.name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenOneStatelessBean_whenStateIsSetInBothBeans_secondBeanShouldHaveSecondBeanState() {
|
||||
|
||||
// act
|
||||
ejbClient1.statelessEJB.name = "Client 1";
|
||||
ejbClient2.statelessEJB.name = "Client 2";
|
||||
|
||||
// assert
|
||||
Assert.assertEquals("Client 2", ejbClient2.statelessEJB.name);
|
||||
|
||||
}
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClass(StatelessEJB.class)
|
||||
.addClass(EJBClient1.class)
|
||||
.addClass(EJBClient2.class)
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.EJB;
|
||||
import javax.ejb.embeddable.EJBContainer;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.ejbspringcomparison.ejb.singleton.CounterEJBRemote;
|
||||
import com.baeldung.ejbspringcomparison.ejb.stateful.ShoppingCartEJBRemote;
|
||||
import com.baeldung.ejbspringcomparison.ejb.stateless.FinderEJBRemote;
|
||||
|
||||
public class EJBUnitTest {
|
||||
|
||||
private static EJBContainer ejbContainer = null;
|
||||
|
||||
private static Context context = null;
|
||||
|
||||
@Resource
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@EJB
|
||||
private FinderEJBRemote alphabetFinder;
|
||||
|
||||
@Resource(name = "myQueue")
|
||||
private Queue myQueue;
|
||||
|
||||
@Resource(name = "ackQueue")
|
||||
private Queue ackQueue;
|
||||
|
||||
@BeforeClass
|
||||
public static void start() throws NamingException {
|
||||
ejbContainer = EJBContainer.createEJBContainer();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeContext() throws NamingException {
|
||||
context = ejbContainer.getContext();
|
||||
context.bind("inject", this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||
|
||||
int count = 0;
|
||||
CounterEJBRemote firstCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
firstCounter.setName("first");
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count = firstCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(10, count);
|
||||
assertEquals("first", firstCounter.getName());
|
||||
|
||||
CounterEJBRemote secondCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
|
||||
int count2 = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count2 = secondCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(20, count2);
|
||||
assertEquals("first", secondCounter.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
bathingCart.setName("bathingCart");
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
assertEquals("bathingCart", bathingCart.getName());
|
||||
|
||||
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
assertNull(fruitCart.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatelessBean_whenSearchForA_thenApple() throws NamingException {
|
||||
|
||||
assertEquals("Apple", alphabetFinder.search("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMDB_whenMessageSent_thenAcknowledgementReceived() throws InterruptedException, JMSException, NamingException {
|
||||
|
||||
Connection connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
MessageProducer producer = session.createProducer(myQueue);
|
||||
producer.send(session.createTextMessage("marco"));
|
||||
MessageConsumer response = session.createConsumer(ackQueue);
|
||||
|
||||
assertEquals("polo", ((TextMessage) response.receive(1000)).getText());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() throws NamingException {
|
||||
context.unbind("inject");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void closeContext() throws NamingException {
|
||||
context.close();
|
||||
ejbContainer.close();
|
||||
}
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.ejbspringcomparison.spring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import com.baeldung.ejbspringcomparison.spring.config.ApplicationConfig;
|
||||
import com.baeldung.ejbspringcomparison.spring.messagedriven.Producer;
|
||||
import com.baeldung.ejbspringcomparison.spring.singleton.CounterBean;
|
||||
import com.baeldung.ejbspringcomparison.spring.stateful.ShoppingCartBean;
|
||||
|
||||
public class SpringUnitTest {
|
||||
|
||||
private static AnnotationConfigApplicationContext context = null;
|
||||
|
||||
@ClassRule
|
||||
public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
this.getBrokerService()
|
||||
.setUseJmx(true);
|
||||
try {
|
||||
this.getBrokerService()
|
||||
.addConnector("tcp://localhost:61616");
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||
|
||||
CounterBean firstCounter = context.getBean(CounterBean.class);
|
||||
firstCounter.setName("first");
|
||||
int count = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count = firstCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(10, count);
|
||||
assertEquals("first", firstCounter.getName());
|
||||
|
||||
CounterBean secondCounter = context.getBean(CounterBean.class);
|
||||
|
||||
int count2 = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count2 = secondCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(20, count2);
|
||||
assertEquals("first", secondCounter.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
bathingCart.setName("bathingCart");
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
|
||||
assertEquals("bathingCart", bathingCart.getName());
|
||||
|
||||
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
assertNull(fruitCart.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJMSBean_whenMessageSent_thenAcknowledgementReceived() throws NamingException {
|
||||
Producer producer = context.getBean(Producer.class);
|
||||
producer.sendMessageToDefaultDestination("marco");
|
||||
|
||||
assertEquals("polo", producer.receiveAck());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void closeContext() throws NamingException {
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.embeddable.EJBContainer;
|
||||
import javax.naming.Context;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CountryStateCacheBeanUnitTest {
|
||||
|
||||
private EJBContainer ejbContainer = null;
|
||||
|
||||
private Context context = null;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
ejbContainer = EJBContainer.createEJBContainer();
|
||||
context = ejbContainer.getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean");
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallGetStatesFromBeanManagedBean_ReturnsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean");
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSetStatesFromContainerManagedBean_SetsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean");
|
||||
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
|
||||
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSetStatesFromBeanManagedBean_SetsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean");
|
||||
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
|
||||
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (ejbContainer != null)
|
||||
ejbContainer.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<arquillian xmlns="http://jboss.org/schema/arquillian" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
|
||||
|
||||
<container qualifier="wildfly-managed" default="true">
|
||||
<configuration>
|
||||
<property name="jbossHome">target/wildfly-8.2.1.Final</property>
|
||||
<property name="serverConfig">standalone.xml</property>
|
||||
<property name="outputToConsole">true</property>
|
||||
<property name="managementPort">9990</property>
|
||||
<property name="javaVmArguments">-Djboss.http.port=8734</property>
|
||||
</configuration>
|
||||
</container>
|
||||
|
||||
<container qualifier="wildfly-remote">
|
||||
<configuration>
|
||||
<property name="managementAddress">127.0.0.1</property>
|
||||
<property name="managementPort">9990</property>
|
||||
<property name="username">admin</property>
|
||||
<property name="password">pass</property>
|
||||
<property name="allowConnectingToRunningServer">true</property>
|
||||
</configuration>
|
||||
</container>
|
||||
|
||||
</arquillian>
|
||||
@@ -0,0 +1,2 @@
|
||||
handlers = java.util.logging.ConsoleHandler
|
||||
java.util.logging.ConsoleHandler.level = SEVERE
|
||||
Reference in New Issue
Block a user