JUnit-Tests with OpenEJB and Spring

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

JUnit-Tests with OpenEJB and Spring

martin.th.be
Hi

I need your help;). I try to test my Spring-Beans with OpenEJB with JUnit.

After I finally managed to inject the EJBs in my SpringBean,  I get problems with my EntityManager / TransactionManager.
The problem is that doesn't find the Database tables. (Exception: Table not found in statement).

The system works without problems on JBoss, only OpenEJB 'can not handle the classes'.

There are two situations:

1st: When I test my EJB Bean and run it with the InitialContext, then it works.
2nd: When I test my SpringBean with an ApplicationContext and the InitialContext, it will not works :(.

The mapping of entities seems to work well, the Hibernate log expenditure at least look good:
INFO: Mapping collection: de.package.entity.EntityClass -> ENTITY_CLASS

My system is:

Libraries:

OpenEJB 3.1.3-SNAPSHOT (QUARTZ-JOB: QuartzResourceAdapter/JobSpec)
SPRING 3.0.2.RELEASE
Hibernate 3.4.0

---

Application-Context.xml : 

<?xml version="1.0" encoding="UTF-8"?>
<beans ...... default-autowire="byType">
        <context:annotation-config />
        <bean name="TransactionManager"
                class="org.apache.geronimo.transaction.manager.GeronimoTransactionManager" />
        <bean name="SecurityService" class="org.apache.openejb.core.security.SecurityServiceImpl" />
        <bean name="ClassPathApplication" class="org.apache.openejb.spring.ClassPathApplication" />
        <context:component-scan base-package="de.package.service" />
</beans>

---

TestCase : 

@LocalClient
public class NewTestCase
        {
        @Resource
        private UserTransaction userTransaction;

        private static ClassPathXmlApplicationContext applicationContext = null;
   
        @Before
        public void init(){
                getApplicationContext();
               
                Properties properties = new Properties();
                properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
                properties.put("pUnit", "new://Resource?type=DataSource");
                properties.put("pUnit.JdbcUrl", "jdbc:oracle:thin:@localhost:1521:xe");
                properties.put("pUnit.UserName", "user");
                properties.put("pUnit.Password", "pw");
                properties.put("pUnit.JdbcDriver", "oracle.jdbc.driver.OracleDriver");

                InitialContext initialContext = new InitialContext(properties);
        }
       
   protected UserTransaction getTransaction()
      {
      return userTransaction;
      }
         
        private static ApplicationContext getApplicationContext()
          {
          if (applicationContext == null)
                 {
                 applicationContext = new ClassPathXmlApplicationContext("testCase-context.xml");
                 }
          return applicationContext;
          }

        protected Object getBean(Class<?> name)
          {
          return getApplicationContext().getBean(name);
          }
       
        }

---

Test:

public class SpringEJBTest extends NewTestCase
   {
   
   @Test
   public void Hello()
      {
          SpringBean springBean = (SpringBean) getBean(SpringBean.class)
         
      getUserTransaction().begin();
      springBean.getEntityValueById(1L);
      getUserTransaction().rollback();
          }
         
        }

---

 SpringBean - WAR

@Component
@Transactional
public class SpringBeanImpl implements SpringBean
        {
        @Autowired
        private EJBSessionBeanRemote ejbSessionBeanRemote;
       
        public EntityClass getEntityValueById(Long id)
                {
                return ejbSessionBeanRemote.getEntityValueById(id);
                }
        }

---

EJB:

@Stateless
public class EJBSessionBeanImpl implements EJBSessionBeanRemote
   {
   @PersistenceContext
   private EntityManager entityManager;
   
   public EntityClass getEntityValueById(Long id)
        {
        return entityManager.find(EntityClass.class, id);
        }
   }
 


---

It is not possible for me to adjust the EJB-Bean and the Spring-Bean on OpenEJB, because the system should run with JBoss and without OpenEJB.

I hope you can Help me. I don't find any solution on OpenEJB
Reply | Threaded
Open this post in threaded view
|

Re: JUnit-Tests with OpenEJB and Spring

dblevins
Administrator
martin.th.be wrote
Hi

I need your help;). I try to test my Spring-Beans with OpenEJB with JUnit.

After I finally managed to inject the EJBs in my SpringBean,  I get problems with my EntityManager / TransactionManager.
The problem is that doesn't find the Database tables. (Exception: Table not found in statement).

The system works without problems on JBoss, only OpenEJB 'can not handle the classes'.

There are two situations:

1st: When I test my EJB Bean and run it with the InitialContext, then it works.
2nd: When I test my SpringBean with an ApplicationContext and the InitialContext, it will not works :(.

The mapping of entities seems to work well, the Hibernate log expenditure at least look good:
INFO: Mapping collection: de.package.entity.EntityClass -> ENTITY_CLASS

My system is:

Libraries:

OpenEJB 3.1.3-SNAPSHOT (QUARTZ-JOB: QuartzResourceAdapter/JobSpec)
SPRING 3.0.2.RELEASE
Hibernate 3.4.0

---

Application-Context.xml : 

<?xml version="1.0" encoding="UTF-8"?>
<beans ...... default-autowire="byType">
        <context:annotation-config />
        <bean name="TransactionManager"
                class="org.apache.geronimo.transaction.manager.GeronimoTransactionManager" />
        <bean name="SecurityService" class="org.apache.openejb.core.security.SecurityServiceImpl" />
        <bean name="ClassPathApplication" class="org.apache.openejb.spring.ClassPathApplication" />
        <context:component-scan base-package="de.package.service" />
</beans>

---

TestCase : 

@LocalClient
public class NewTestCase
        {
        @Resource
        private UserTransaction userTransaction;

        private static ClassPathXmlApplicationContext applicationContext = null;
   
        @Before
        public void init(){
                getApplicationContext();
               
                Properties properties = new Properties();
                properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
                properties.put("pUnit", "new://Resource?type=DataSource");
                properties.put("pUnit.JdbcUrl", "jdbc:oracle:thin:@localhost:1521:xe");
                properties.put("pUnit.UserName", "user");
                properties.put("pUnit.Password", "pw");
                properties.put("pUnit.JdbcDriver", "oracle.jdbc.driver.OracleDriver");

                InitialContext initialContext = new InitialContext(properties);
        }
       
   protected UserTransaction getTransaction()
      {
      return userTransaction;
      }
         
        private static ApplicationContext getApplicationContext()
          {
          if (applicationContext == null)
                 {
                 applicationContext = new ClassPathXmlApplicationContext("testCase-context.xml");
                 }
          return applicationContext;
          }

        protected Object getBean(Class<?> name)
          {
          return getApplicationContext().getBean(name);
          }
       
        }

---

Test:

public class SpringEJBTest extends NewTestCase
   {
   
   @Test
   public void Hello()
      {
          SpringBean springBean = (SpringBean) getBean(SpringBean.class)
         
      getUserTransaction().begin();
      springBean.getEntityValueById(1L);
      getUserTransaction().rollback();
          }
         
        }

---

 SpringBean - WAR

@Component
@Transactional
public class SpringBeanImpl implements SpringBean
        {
        @Autowired
        private EJBSessionBeanRemote ejbSessionBeanRemote;
       
        public EntityClass getEntityValueById(Long id)
                {
                return ejbSessionBeanRemote.getEntityValueById(id);
                }
        }

---

EJB:

@Stateless
public class EJBSessionBeanImpl implements EJBSessionBeanRemote
   {
   @PersistenceContext
   private EntityManager entityManager;
   
   public EntityClass getEntityValueById(Long id)
        {
        return entityManager.find(EntityClass.class, id);
        }
   }
 


---

It is not possible for me to adjust the EJB-Bean and the Spring-Bean on OpenEJB, because the system should run with JBoss and without OpenEJB.

I hope you can Help me. I don't find any solution on OpenEJB
Dear user, we have recently discovered that in January Nabble changed the "OpenEJB" Nabble Category to a Nabble Forum and allowed posts to be made directly on it.  These posts were never delivered to any mailing list and were not seen by the OpenEJB community.

We are moving all such threads to the "OpenEJB User" Nabble Forum so future replies will go to the mailing lists, but please understand that your original post and all previous replies are not in the Apache archives nor visible to its subscribers.

If you still need help with your question please repost.
Reply | Threaded
Open this post in threaded view
|

Re: JUnit-Tests with OpenEJB and Spring

martin.th.be
Hi dblevins, hi Community

This problem is still exists. =(

I need some 'tutorial' or 'example', to test my Spring-beans with OpenEJB.

Maybe I have in my source code (see above) an error.

We are now switched on the new version of OpenEJB (2.1.3).

I hope again you / the community can help me.

Martin
Reply | Threaded
Open this post in threaded view
|

Re: JUnit-Tests with OpenEJB and Spring

dblevins
Administrator

On Oct 22, 2010, at 12:57 AM, martin.th.be wrote:

>
> Hi dblevins, hi Community
>
> This problem is still exists. =(
>
> I need some 'tutorial' or 'example', to test my Spring-beans with OpenEJB.
>
> Maybe I have in my source code (see above) an error.
>
> We are now switched on the new version of OpenEJB (2.1.3).
>
> I hope again you / the community can help me.

Hi Martin,

Can you post any errors you're getting as well as the OpenEJB log output?


-David