JAVA-12424 Renamed spring-ejb to spring-ejb-modules
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful
|
||||
public class StatefulEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
|
||||
@Stateless
|
||||
public class StatelessEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.messagedriven;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.ActivationConfigProperty;
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.DeliveryMode;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destination", propertyValue = "myQueue"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
|
||||
public class RecieverMDB implements MessageListener {
|
||||
|
||||
@Resource
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@Resource(name = "ackQueue")
|
||||
private Queue ackQueue;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
|
||||
TextMessage textMessage = (TextMessage) message;
|
||||
String producerPing = textMessage.getText();
|
||||
|
||||
if (producerPing.equals("marco")) {
|
||||
acknowledge("polo");
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void acknowledge(String text) throws JMSException {
|
||||
|
||||
Connection connection = null;
|
||||
Session session = null;
|
||||
|
||||
try {
|
||||
connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
|
||||
MessageProducer ackSender = session.createProducer(ackQueue);
|
||||
ackSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
|
||||
|
||||
TextMessage message = session.createTextMessage(text);
|
||||
|
||||
ackSender.send(message);
|
||||
} finally {
|
||||
session.close();
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class CounterEJB implements CounterEJBRemote {
|
||||
|
||||
private int count = 1;
|
||||
private String name;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface CounterEJBRemote {
|
||||
int count();
|
||||
String getName();
|
||||
void setName(String name);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateful;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful
|
||||
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
||||
private String name;
|
||||
private List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartEJB() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateful;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface ShoppingCartEJBRemote {
|
||||
|
||||
void addItem(String item);
|
||||
|
||||
List<String> getItems();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
String getName();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateless;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
@Stateless
|
||||
public class FinderEJB implements FinderEJBRemote {
|
||||
|
||||
private Map<String, String> alphabet;
|
||||
|
||||
public FinderEJB() {
|
||||
alphabet = new HashMap<String, String>();
|
||||
alphabet.put("A", "Apple");
|
||||
alphabet.put("B", "Ball");
|
||||
alphabet.put("C", "Cat");
|
||||
alphabet.put("D", "Dog");
|
||||
}
|
||||
|
||||
public String search(String keyword) {
|
||||
return alphabet.get(keyword);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateless;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface FinderEJBRemote {
|
||||
|
||||
String search(String keyword);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.config;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.ejbspringcomparison.spring")
|
||||
@EnableJms
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
|
||||
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
return new ActiveMQConnectionFactory("tcp://localhost:61616");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
JmsTemplate template = new JmsTemplate(connectionFactory());
|
||||
template.setConnectionFactory(connectionFactory());
|
||||
return template;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.messagedriven;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Producer {
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
public void sendMessageToDefaultDestination(final String message) {
|
||||
jmsTemplate.convertAndSend("myQueue", message);
|
||||
}
|
||||
|
||||
public String receiveAck() {
|
||||
return (String) jmsTemplate.receiveAndConvert("ackQueue");
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.messagedriven;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Receiver {
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
@JmsListener(destination = "myQueue")
|
||||
public void receiveMessage(String msg) {
|
||||
sendAck();
|
||||
}
|
||||
|
||||
private void sendAck() {
|
||||
jmsTemplate.convertAndSend("ackQueue", "polo");
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.singleton;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CounterBean {
|
||||
private int count = 1;
|
||||
private String name;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.stateful;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class ShoppingCartBean {
|
||||
|
||||
private String name;
|
||||
private List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartBean() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
@Local
|
||||
public interface CountryState {
|
||||
|
||||
public List<String> getStates(String country);
|
||||
|
||||
public void setStates(String country, List<String> states);
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.ejb.ConcurrencyManagement;
|
||||
import javax.ejb.ConcurrencyManagementType;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
|
||||
public class CountryStateBeanManagedBean implements CountryState {
|
||||
|
||||
private final Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
|
||||
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
|
||||
List<String> states = new ArrayList<String>();
|
||||
states.add("Texas");
|
||||
states.add("Alabama");
|
||||
states.add("Alaska");
|
||||
states.add("Arizona");
|
||||
states.add("Arkansas");
|
||||
|
||||
countryStatesMap.put("UnitedStates", states);
|
||||
}
|
||||
|
||||
public List<String> getStates(String country) {
|
||||
return countryStatesMap.get(country);
|
||||
}
|
||||
|
||||
public synchronized void setStates(String country, List<String> states) {
|
||||
countryStatesMap.put(country, states);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.ejb.ConcurrencyManagement;
|
||||
import javax.ejb.ConcurrencyManagementType;
|
||||
import javax.ejb.Lock;
|
||||
import javax.ejb.LockType;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
|
||||
public class CountryStateContainerManagedBean implements CountryState {
|
||||
|
||||
private Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
|
||||
|
||||
@Lock(LockType.WRITE)
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
|
||||
List<String> states = new ArrayList<String>();
|
||||
states.add("Texas");
|
||||
states.add("Alabama");
|
||||
states.add("Alaska");
|
||||
states.add("Arizona");
|
||||
states.add("Arkansas");
|
||||
|
||||
countryStatesMap.put("UnitedStates", states);
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public List<String> getStates(String country) {
|
||||
return countryStatesMap.get(country);
|
||||
}
|
||||
|
||||
@Lock(LockType.WRITE)
|
||||
public void setStates(String country, List<String> states) {
|
||||
countryStatesMap.put(country, states);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+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