Hi Andreas,

Try modifying your test code as so:


    public class OpenEJBTest {

        private InitialContext ctx;

        @Before
        public void onBefore() throws Exception {
            Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
            ctx = new InitialContext(props);
        }


        @After
        public void onAfter() throws Exception {
            ctx.close();
        }

        @Test
        public void test1() throws Exception {
            InitialContext ctx = new InitialContext();
            // test some lookups
UserTransaction utx = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
            Assert.assertNotNull(utx);

TransactionManager tm = (TransactionManager) ctx.lookup("java:comp/TransactionManager");
            Assert.assertNotNull(tm);

        }

        @Test
        public void test2() {
            System.out.println("run test2");

        }
    }


Essentially, all the code you had to check if OpenEJB was initialized, etc. is all handled for you automatically. The first time you create an InitialContext with the LocalInitialContextFactory we initialize OpenEJB only if it hasn't been initialized already. Also if you call close() on the InitialContext instance that "booted" OpenEJB then it will cleanly destroy OpenEJB. If you call close() on any other InitialContext it will do nothing.


-David


On Sep 17, 2008, at 4:41 AM, Andreas Karalus wrote:


I attached a simple JUnit to show that OpenEJB.destroy does not work well in
3.1-SNAPSHOT (with 3.0 the same code works). Maybe it helps...

public class OpenEJBTest {
        
        @Before
        public void onBefore() throws Exception{
        
                if(! OpenEJB.isInitialized()){
                        Properties props = new Properties();
                        props.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
                        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
                        
                        InitialContext ctx = new InitialContext(props);         
        
                }
                
        }
        
        
        @After
        public  void onAfter() throws Exception{
                if(OpenEJB.isInitialized()){
                        OpenEJB.destroy();
                }
        }
        
        @Test
        public void test1() throws Exception{
                InitialContext ctx = new InitialContext();
                // test some lookups
                UserTransaction utx = (UserTransaction)
ctx.lookup("java:comp/UserTransaction");
                Assert.assertNotNull(utx);
                
                TransactionManager tm = (TransactionManager)
ctx.lookup("java:comp/TransactionManager");
                Assert.assertNotNull(tm);
                
        }
        
        @Test
        public void test2(){
                // this is just to test startup shutdown of openejb
              // I run well with 3.0 but I fail with 3.1-SNAPSHOT
                System.out.println("run test2");
                
        }

}




Andreas Karalus wrote:

Hi David,

thank you for your efforts, I really appreciate it.
I tried the new SNAPSHOT libraries, and the lookup works very well. Thank
you again for providing a great support. However, I run into other
problems (maybe because of SNAPSHOT?). I just want to give feedback on it.

- the startup/shutdown behaviour of openejb may have changed? I start
openejb in every junit test @BeforeClass, and call OpenEJB.destroy() in @AfterClass. Running a single test works fine, running all tests together
(e.g. maven test) fail after the first test. If I do not call
OpenEJB.destroy() in @AfterClass, all tests run again fine, so I suppose
that OpenEJB.destroy() is the cause. Below is a stacktrace.



--
View this message in context: 
http://www.nabble.com/UserTransaction-in-openejb-tp19313741p19529660.html
Sent from the OpenEJB Dev mailing list archive at Nabble.com.



Reply via email to