I'm looking to unit test my Java EE code by using the embedded Glassfish container and JUnit. On its own this works ok, but I've discovered that when the Primefaces EventBusFactory is called it fails with a Null Pointer Exception. I'm not sure how to resolve that, or if I should be testing this in another way.
Here is my test:
public class CreateOrderBookTest {
private RequestBean requestBean;
private EJBContainer ejbContainer;
private Context ctx;
#Before
public void setUp() throws Exception {
ejbContainer = EJBContainer.createEJBContainer();
System.out.println("Opening the container" );
ctx = ejbContainer.getContext();
}
#After
public void tearDown() throws Exception {
ejbContainer.close();
System.out.println("Closing the container" );
}
#Test
public void test() throws NamingException {
RequestBean requestBean = (RequestBean) ctx.lookup("java:global/classes/RequestBean");
assertNotNull(requestBean);
requestBean.createBidOrder(50, 1.0, "CLCS", "00001");
}
}
Here is the function I'm testing in a RequestScoped Stateful bean:
public void createBidOrder(Integer amount, Double price, String memberId, String traderId)
{
BidOrder order = new BidOrder(amount,price, em.find(Member.class, memberId),
em.find(Trader.class, new Integer(traderId)));
logger.log(Level.INFO, "RequestBean.createBidOrder Created new order with bidprice {0}",
new Object[]{price});
em.persist(order);
EventBus eventBus = EventBusFactory.getDefault().eventBus();
eventBus.publish("/notify", "order");
}
Related
I am trying to get IEventBroker injected into my code to send out notifications.
Everything else works but eventBroker never gets injected. I do not get any compile time errors.
It just comes up null when the code is executed.
I've trimmed the code because it wouldn't let me submit it.
Thanks for any help in advance!
package com.test.services.internal;
imports ...
#Component
public class EnvironmentServiceImpl implements IEnvironmentService {
#Inject
private IEventBroker eventBroker;
private EntityManagerFactory entityManagerFactory;
private EntityManager entityManager;
#Activate
#SuppressWarnings("unchecked")
protected void activateComponent() {
getAll(environments -> {
if (environments.isEmpty()) {
List<Environment> initialModel = getMockEnvironments();
initialModel.forEach(this::save);
}
});
}
#Deactivate
protected void deactivateComponent() {
}
#Override
public void getAll(Consumer<List<Environment>> taskConsumer) {
eventBroker.post(EnvironmentEventConstants.TOPIC_ENVIRONMENT_LOADED,
createEventData(EnvironmentEventConstants.TOPIC_ENVIRONMENT_LOADED, updateEnvironment.getId()));
}
private Map<String, String> createEventData(String topic, String environmentId) {
}
}
I'm using Spring Boot and Spring Integration Java DSL in my #Configuration class. One of the flows is using DelayHandler with MessageStore, by means of .delay(String groupId, String expression, Consumer endpointConfigurer):
#Bean
public IntegrationFlow errorFlow() {
return IntegrationFlows.from(errorChannel())
...
.delay(...)
...
.get();
}
I was hoping to utilize the reschedulePersistedMessages() functionality of DelayHandler, but I found out the onApplicationEvent(ContextRefreshedEvent event) which invokes it is actually never invoked (?)
I'm not sure, but I suspect this is due to the fact DelayHandler is not registered as a Bean, so registerListeners() in AbstractApplicationContext is not able to automatically register DelayHandler (and registration of non-bean listeners via ApplicationEventMulticaster.addApplicationListener(ApplicationListener listener) is not done for DelayHandler.
Currently I'm using a rather ugly workaround of registering my own listener Bean into which I inject the integration flow Bean, and then invoking the onApplicationEvent() manually after locating the DelayHandler:
#Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Set<Object> integrationComponents = errorFlow.getIntegrationComponents();
for (Object component : integrationComponents) {
if (component instanceof DelayerEndpointSpec) {
Tuple2<ConsumerEndpointFactoryBean, DelayHandler> tuple2 = ((DelayerEndpointSpec) component).get();
tuple2.getT2().onApplicationEvent(event);
return;
}
}
}
Well, yes. This test-case confirm the issue:
#ContextConfiguration
#RunWith(SpringJUnit4ClassRunner.class)
#DirtiesContext
public class DelayerTests {
private static MessageGroupStore messageGroupStore = new SimpleMessageStore();
private static String GROUP_ID = "testGroup";
#BeforeClass
public static void setup() {
messageGroupStore.addMessageToGroup(GROUP_ID, new GenericMessage<>("foo"));
}
#Autowired
private PollableChannel results;
#Test
public void testDelayRescheduling() {
Message<?> receive = this.results.receive(10000);
assertNotNull(receive);
assertEquals("foo", receive.getPayload());
assertEquals(1, messageGroupStore.getMessageGroupCount());
assertEquals(0, messageGroupStore.getMessageCountForAllMessageGroups());
}
#Configuration
#EnableIntegration
public static class ContextConfiguration {
#Bean
public IntegrationFlow delayFlow() {
return flow ->
flow.delay(GROUP_ID, (String) null,
e -> e.messageStore(messageGroupStore)
.id("delayer"))
.channel(c -> c.queue("results"));
}
}
}
Here we go: https://github.com/spring-projects/spring-integration-java-dsl/issues/59.
As a workaround we can do this in our #Configuration:
#Autowired
private ApplicationEventMulticaster multicaster;
#PostConstruct
public void setup() {
this.multicaster.addApplicationListenerBean("delayer.handler");
}
Pay attention to the beanName to register. This is exactly that .id("delayer") from our flow definition plus the .handler suffix for the DelayHandler bean definition.
I am trying out using CDI-Unit to test my Wicket components, which are using CDI for dependency injection. Tests seems to work perfectly in Eclipse but fail during my Maven build and I cant seem to find any hints and what is wrong.
I have created a simple abstract WicketPanel
public abstract class MyPanel extends Panel{
private static final long serialVersionUID = 4132041261965905788L;
private final RepeatingView rw;
#Inject
transient ReflectiveComponentFactory factory;
public MyPanel(String id) {
super(id);
rw = new RepeatingView(OVERLAY_COMPONENT_GROUP_ID);
add(rw);
}
#Override
public <CT extends Component> CT addComponent(Class<CT> componentType) {
return addComponent(componentType, OVERLAY_COMPONENT_ID);
}
protected <CT extends Component> CT addComponent(Class<CT> componentType, String overlayComponentId) {
WebMarkupContainer collapsableGroup = new WebMarkupContainer(rw.newChildId());
rw.add(collapsableGroup);
CT component = factory.createComponent(componentType, overlayComponentId);
collapsableGroup.add(component);
return component;
}
}
And the injection factory:
#ApplicationScoped
public class ReflectiveComponentFactory implements Serializable{
private static final long serialVersionUID = -4587243549845349456L;
public <CT extends Component> CT createComponent(Class<CT> componentType, String componentId){
try {
Constructor<CT> constructor = componentType.getConstructor(String.class);
return constructor.newInstance(componentId);
} catch (ReflectiveOperationException | SecurityException | IllegalArgumentException e) {
throw new ComponentCreationException(e);
}
}
}
And then created a unit test using CDI-Unit:
#RunWith(CdiRunner.class)
#AdditionalClasses(value={ReflectiveComponentFactory.class})
public class MyPanelTest {
private WicketTester tester;
#Inject
private BeanManager beanManager;
#Before
public void setup() {
tester = new WicketTester();
new CdiConfiguration(beanManager).setPropagation(ConversationPropagation.NONE).configure(tester.getApplication());
}
#Test
public void testAddComponentWithClass() {
MyPanelTested myPanel = new MyPanelTested("someId");
TestPanel panel1 = myPanel.addComponent(TestPanel.class);
TestPanel panel2 = myPanel.addComponent(TestPanel.class);
tester.startComponentInPage(myPanel);
tester.assertComponent(panel1.getPageRelativePath(), TestPanel.class);
tester.assertComponent(panel2.getPageRelativePath(), TestPanel.class);
tester.assertComponent(panel1.getPageRelativePath() + ":text", Label.class);
tester.assertComponent(panel2.getPageRelativePath() + ":text", Label.class);
}
}
#SuppressWarnings("serial")
class MyPanelTested extends MyPanel {
public MyPanelTested(String id) {
super(id);
}
}
I have not included TestPanel, but it is extremely simple (and more or less empty).
When I executed this in Eclipse, the test passes with green!
When I execute this with Maven I get the following:
org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.Dependent
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:578)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:608)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:674)
at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:136)
at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:763)
at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:772)
at org.jboss.weld.manager.SimpleInjectionTarget$1.proceed(SimpleInjectionTarget.java:106)
at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
at org.jboss.weld.manager.SimpleInjectionTarget.inject(SimpleInjectionTarget.java:102)
at org.apache.wicket.cdi.NonContextual.postConstruct(NonContextual.java:129)
at org.apache.wicket.cdi.NonContextualManager.postConstruct(NonContextualManager.java:65)
at org.apache.wicket.cdi.DetachEventEmitter.<init>(DetachEventEmitter.java:55)
at org.apache.wicket.cdi.CdiConfiguration.configure(CdiConfiguration.java:196)
.....
Any clues to what I am doing wrong?
The problem was caused by a bug in cdi-unit version 2.0.8. Bryn solved the problem (see https://github.com/BrynCooke/cdi-unit/issues/21) and it should therefore not appear if using cdi-unit 2.0.9 or greater.
I'm trying to intercept the method persist and update of javax.persistence.EntityManager in a Seam 3 project.
In a previous version (Seam 2) of the micro-framework I'm trying to make, I did this using an implementation of org.hibernate.Interceptor and declaring it in the persistence.xml.
But I want something more "CDI-like" now we are in a JEE6 environment.
I want that just before entering in a EntityManager.persist call, an event #BeforeTrackablePersist is thrown. The same way, I want an event #BeforeTrackableUpdate to be thrown before entering in a EntityManager.merge call. Trackable is an interface which some of my Entitys could implement in order to be intercepted before persist or merge.
I'm using Seam 3 (3.1.0.Beta3) Extended Persistence Manager :
public class EntityManagerHandler {
#SuppressWarnings("unused")
#ExtensionManaged
#Produces
#PersistenceUnit
private EntityManagerFactory entityManagerFactory;
}
So I've made a javax.enterprise.inject.spi.Extension, and tryied many ways to do that :
public class TrackableExtension implements Extension {
#Inject #BeforeTrackablePersisted
private Event<Trackable> beforeTrackablePersistedEvent;
#Inject #BeforeTrackableMerged
private Event<Trackable> beforeTrackableMergedEvent;
#SuppressWarnings("unchecked")
public void processEntityManagerTarget(#Observes final ProcessInjectionTarget<EntityManager> event) {
final InjectionTarget<EntityManager> injectionTarget = event.getInjectionTarget();
final InjectionTarget<EntityManager> injectionTargetProxy = (InjectionTarget<EntityManager>) Proxy.newProxyInstance(event.getClass().getClassLoader(), new Class[] {InjectionTarget.class}, new InvocationHandler() {
#Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
if ("produce".equals(method.getName())) {
final CreationalContext<EntityManager> ctx = (CreationalContext<EntityManager>) args[0];
final EntityManager entityManager = decorateEntityManager(injectionTarget, ctx);
return entityManager;
} else {
return method.invoke(injectionTarget, args);
}
}
});
event.setInjectionTarget(injectionTargetProxy);
}
public void processEntityManagerType(#Observes final ProcessAnnotatedType<EntityManager> event) {
final AnnotatedType<EntityManager> type = event.getAnnotatedType();
final AnnotatedTypeBuilder<EntityManager> builder = new AnnotatedTypeBuilder<EntityManager>().readFromType(type);
for (final AnnotatedMethod<? super EntityManager> method : type.getMethods()) {
final String name = method.getJavaMember().getName();
if (StringUtils.equals(name, "persist") || StringUtils.equals(name, "merge")) {
builder.addToMethod(method, TrackableInterceptorBindingLiteral.INSTANCE);
}
}
event.setAnnotatedType(builder.create());
}
public void processEntityManagerBean(#Observes final ProcessBean<EntityManager> event) {
final AnnotatedType<EntityManager> annotatedType = (AnnotatedType<EntityManager>)event.getAnnotated();
// not even called
}
public void processEntityManager(#Observes final ProcessProducer<?, EntityManager> processProducer) {
processProducer.setProducer(decorate(processProducer.getProducer()));
}
private Producer<EntityManager> decorate(final Producer<EntityManager> producer) {
return new Producer<EntityManager>() {
#Override
public EntityManager produce(final CreationalContext<EntityManager> ctx) {
return decorateEntityManager(producer, ctx);
}
#Override
public Set<InjectionPoint> getInjectionPoints() {
return producer.getInjectionPoints();
}
#Override
public void dispose(final EntityManager instance) {
producer.dispose(instance);
}
};
}
private EntityManager decorateEntityManager(final Producer<EntityManager> producer, final CreationalContext<EntityManager> ctx) {
final EntityManager entityManager = producer.produce(ctx);
return (EntityManager) Proxy.newProxyInstance(entityManager.getClass().getClassLoader(), new Class[] {EntityManager.class}, new InvocationHandler() {
#Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
final String methodName = method.getName();
if (StringUtils.equals(methodName, "persist")) {
fireEventIfTrackable(beforeTrackablePersistedEvent, args[0]);
} else if (StringUtils.equals(methodName, "merge")) {
fireEventIfTrackable(beforeTrackableMergedEvent, args[0]);
}
return method.invoke(entityManager, args);
}
private void fireEventIfTrackable(final Event<Trackable> event, final Object entity) {
if (entity instanceof Trackable) {
event.fire(Reflections.<Trackable>cast(entity));
}
}
});
}
}
In all those observer methods, only the second one (processEntityManagerType(#Observes ProcessAnnotatedType<EntityManager>)) is called ! And even with that binding addition to methods persist and merge, my Interceptor is never called (I've of course enabled it with the correct lines in beans.xml, and enabled my extension with the services/javax.enterprise.inject.spi.Extension file).
Something I've thought simple with CDI seems to be actually really hard at last... or perhaps Seam 3 does something which prevent this code from executing correctly...
Does someone know how to handle that ?
I think you're making this a little harder than what it needs to be. Firstly though, JPA and CDI integration isn't very good in Java EE 6, we're very much hoping that changes in Java EE 7 and JPA 2.1.
What you'll want to do is create your own producer for the EntityManager that will delegate to an actual instance of an EntityManager, but also fire your own events when you call the methods you're interested in. Take a look at the Seam Persistence source to see one way this can be done.
As finally my little patch for Seam Persistence was applied in SEAMPERSIST-75, it will be possible in theory to do that by extending org.jboss.seam.persistence.HibernatePersistenceProvider and override the method proxyEntityManager(EntityManager).
I have a chat application that needs to store messages to DB. But connection with DB is a little bit slow, therefore it delays response to chat client.
Is it possible to persist Message entity in separate thread? What I'm actually need in: reduce delay before send-recieve message on client.
I try to do it, but it doen't work.
Dao object:
#Stateless
public class MessagesDAO {
#PersistenceContext(type= PersistenceContextType.EXTENDED)
private EntityManager entityManager;
private PersistenceThread persistenceThread = new PersistenceThread();
//another methods
public void addMessage(Message message) {
Thread thread = new Thread(persistenceThread);
persistenceThread.setMessage(message);
thread.start();
}
private class PersistenceThread implements Runnable {
private Message message;
public void setMessage(Message message) {
this.message = message;
}
public void run() {
entityManager.persist(message);
}
}
}
Interface service that calls DAO to persist new message and then return it to clients:
#Stateless
#Path("/messages")
#Produces("application/xml")
#Consumes("application/xml")
public class MessagesServiceImpl {
#EJB
private MessagesDAO messagesDAO;
#POST
#Broadcast(resumeOnBroadcast = true)
public Message postMessage(Message message) {
messagesDAO.addMessage(message);
return message;
}
#GET
#Path("/wait")
#Suspend(outputComments = false)
public Message waitForLastMessage() {
return null;
}
//another methods
}
Thanks.
Give the #Asynchronous annotation a try:
#Stateless
public class MessagesDAO {
#PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
#Asynchronous
public void addMessage(Message message) {
entityManager.persist(message);
}
}
Just bear in mind that it requires EJB 3.1.