Hi,
Looks like there might be some confusion. Not sure if MFO is supposed
to be an @Entity bean or a EJB SessionBean (@Stateful, @Stateless,
@Singleton).
There is a slightly different approach you could to take aside from
starting transactions and persisting the Entity right in the test
case. Overall it involves wrapping your EntityManager access with a
session bean. It essentially turns this code:
EntityManager em = null;
try {
em = emf.createEntityManager();
em.getTransaction().begin();
MFO m = new MFO();
m.setName("Hello MFO");
em.persist(m);
assert "success".equals(m.getName());
} catch (Exception e) {
em.getTransaction().rollback();
e.printStackTrace();
} finally {
if (null != em) {
em.close();
}
}
Into this:
@Stateful
public class MyBean implements MyLocal {
@PersistenceContext(unitName = "mfo-unit", type =
PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public void addMFO(MFO mfo) throws Exception {
entityManager.persist(mfo);
}
}
In this setup, the EntityManager will automatically be enrolled in
transactions and when the addMFO method is called a transaction will
automatically be started. Two birds with one stone.
This example shows the basic layout:
http://openejb.apache.org/3.0/injection-of-entitymanager-example.html
You're pretty close, the toplink system properties would be added to
persistence.xml file like this one:
http://svn.apache.org/repos/asf/openejb/tags/openejb-3.1/examples/jpa-eclipselink/src/main/resources/META-INF/persistence.xml
Then in your test case you lookup the Stateful session bean using the
JNDI name printed in the log output (typically beanClass.simpleName()
+ "Local").
In the example there's a "Movie" Entity and a "Movies" Session that is
used to persist/remove/etc. the "Movie" Entities. You don't need to
follow that naming convention. I picked it up the naming convention
from the java.util.Arrays and java.util.Collections classes -- the
plural seems more elegant to me than "Helper" as in ArrayHelper and
CollectionHelper (or "Manager" or "Util"). Just my preference.
-David
On Mar 13, 2009, at 2:26 AM, jooocz wrote:
Good day to everyone.
I'm trying to explore OpenEJB with TestNG using and IDE, Netbeans
5.5.1.
When I try to run my test file, I'm receiving this error.
javax.naming.NameNotFoundException: Name "MFO" not found.
org
.apache.openejb.core.ivm.naming.IvmContext.federate(IvmContext.java:
172)
org.apache.openejb.core.ivm.naming.IvmContext.lookup(IvmContext.java:
129)
org
.apache
.openejb.core.ivm.naming.ContextWrapper.lookup(ContextWrapper.java:
115)
javax.naming.InitialContext.lookup(InitialContext.java:392)
ph.dost.performers.exec4.test.UserTest.init(UserTest.java:81)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun
.reflect
.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun
.reflect
.DelegatingMethodAccessorImpl
.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:
398)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:145)
org.testng.internal.Invoker.invokeConfigurations(Invoker.java:82)
org
.testng
.internal
.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:167)
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
org.testng.TestRunner.runWorkers(TestRunner.java:712)
org.testng.TestRunner.privateRun(TestRunner.java:582)
org.testng.TestRunner.run(TestRunner.java:477)
org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
org.testng.SuiteRunner.run(SuiteRunner.java:198)
org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
org.testng.TestNG.runSuitesLocally(TestNG.java:790)
org.testng.TestNG.run(TestNG.java:708)
org.testng.TestNG.privateMain(TestNG.java:860)
org.testng.TestNG.main(TestNG.java:833)
Here's my test file.
UserTest.java
public class UserTest {
private EntityManagerFactory emf;
@Test
public void testUser() {
System.out.println("UserTest.testUser: start");
System.out.println("UserTest.testUser: emf is " + (null !=
emf ?
"not null." : "null.") );
EntityManager em = null;
try {
em = emf.createEntityManager();
em.getTransaction().begin();
MFO m = new MFO();
m.setName("Hello MFO");
em.persist(m);
assert "success".equals(m.getName());
} catch (Exception e) {
em.getTransaction().rollback();
e.printStackTrace();
} finally {
if (null != em) {
em.close();
}
}
System.out.println("UserTest.testUser: end");
}
static MFO m;
@BeforeClass
public void init() throws Exception {
System.out.println("UserTest.init: now instantiating
emf. . .");
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
properties.put("openejbDatasoure",
"new://Resource?type=DataSource");
properties.put("openejbDatasoure.JdbcDriver",
"com.mysql.jdbc.Driver");
properties.put("openejbDatasoure.JdbcUrl",
"jdbc:mysql://localhost:3306/dost_performers");
properties.put("openejbDatasoure.UserName", "root");
properties.put("openejbDatasoure.Password", "mysql");
System.getProperties().setProperty("toplink.target-server",
"org.apache.openejb.toplink.openejb.OpenEJBTransactionController");
System.getProperties().setProperty("toplink.ddl-generation",
"create-table");
System.getProperties().setProperty("toplink.logging.level",
"INFO");
System.getProperties().setProperty("toplink.create-ddl-jdbc-file-
name",
"create.sql");
System.getProperties().setProperty("toplink.ddl-generation.output-
mode",
"both");
InitialContext initialContext = new InitialContext(properties);
m = (MFO) initialContext.lookup("MFO");
emf =
Persistence.createEntityManagerFactory("ExecutiveSupport4PU");
System.out.println("UserTest.init: emf is " + (null != emf ?
"not
null." : "null.") );
}
@AfterClass
public void destroy() {
System.out.println("UserTest.destroy: now closing emf. . .");
emf.close();
}
}
What should I put in initialContext.lookup()? Name of what?
I would really appreciate for any help.
Thanks :working:
--
View this message in context:
http://www.nabble.com/javax.naming.NameNotFoundException-tp22492443p22492443.html
Sent from the OpenEJB User mailing list archive at Nabble.com.