Whew! I think I'm really close to configuring my test! Things are slowly
improving.
To make things clear, MFO is an @Entity.
Now, I tried the approach you suggested.
Here it is :
@Stateful
public class MFOBeanImpl implements MFOBean {
@PersistenceContext(unitName="edostPerformers-ejbPU",
type=PersistenceContextType.EXTENDED)
private EntityManager em;
public void addMFO(MFO mfo) {
em.persist(mfo);
}
public void deleteMFO(MFO mfo) {
em.persist(mfo);
}
public List<MFO> getMfos() {
Query query = em.createQuery("SELECT m from MFO as m");
return query.getResultList();
}
}
And here's my test :
public class UserTest {
@Test
public void init() throws Exception {
System.out.println("======= init() =======");
Enumeration<URL> ejbJars =
this.getClass().getClassLoader().getResources("META-INF/ejb-jar.xml");
while (ejbJars.hasMoreElements()) {
URL url = ejbJars.nextElement();
System.out.println("app = " + url);
}
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
//properties.setProperty("openejb.deployments.classpath.include",
".*build/test/classes.*");
properties.put("mysql/dostperformers",
"new://Resource?type=DataSource");
properties.put("mysql/dostperformers.JdbcDriver",
"com.mysql.jdbc.Driver");
properties.put("mysql/dostperformers.JdbcUrl",
"jdbc:mysql://localhost:3306/dost_performers");
properties.put("mysql/dostperformers.UserName", "root");
properties.put("mysql/dostperformers.Password", "mysql");
//properties.put("openejb.deployments.classpath.ear", "true");
properties.put("openejb.validation.output.level", "VERBOSE");
//properties.put("openejb.authentication.realmName", "jdbc");
properties.put("openejb.descriptors.output", true);
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");
System.out.println("======= INITIAL CONTEXT =======");
InitialContext initialContext = new InitialContext(properties);
initialContext = new InitialContext(properties);
System.out.println("======= LOOKUP =======");
MFOBean m = (MFOBean) initialContext.lookup("MFOBeanImplLocal");
m.addMFO(new MFO(23L,"Create MFO"));
m.addMFO(new MFO(12L,"Create Indicator"));
List<MFO> list = m.getMfos();
Assert.assertEquals(list.size(), 2, "Got it!");
}
}
NameNotFoundException was solved.
My problem now when I run the file :
INFO - .................
INFO - Jndi(name=UserAccountFacadeRemote) -->
Ejb(deployment-id=UserAccountFacade)
INFO - Jndi(name=DostGroupFacadeRemote) -->
Ejb(deployment-id=DostGroupFacade)
INFO - Jndi(name=PerformersLogFacadeRemote) -->
Ejb(deployment-id=PerformersLogFacade)
INFO - Jndi(name=BudgetMatrixBeanRemote) -->
Ejb(deployment-id=BudgetMatrixBean)
INFO - Jndi(name=MFOBeanImplLocal) --> Ejb(deployment-id=MFOBeanImpl)
ERROR - Jndi name could not be bound; it may be taken by another ejb.
Jndi(name=openejb/Deployment/BudgetMatrixBean/ph.gov.dost.performers.session.BudgetMatrixRemote)
INFO - Undeploying app: classpath.ear
ERROR - Application could not be deployed: classpath.ear
org.apache.openejb.OpenEJBException: Creating application failed:
classpath.ear: Unable to bind business remote deployment in jndi.
at
org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:650)
at
org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:447)
at
org.apache.openejb.assembler.classic.Assembler.buildContainerSystem(Assembler.java:365)
at
org.apache.openejb.assembler.classic.Assembler.build(Assembler.java:277)
at org.apache.openejb.OpenEJB$Instance.<init>(OpenEJB.java:150)
at org.apache.openejb.OpenEJB.init(OpenEJB.java:299)
at org.apache.openejb.OpenEJB.init(OpenEJB.java:278)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.apache.openejb.loader.OpenEJBInstance.init(OpenEJBInstance.java:36)
at
org.apache.openejb.client.LocalInitialContextFactory.init(LocalInitialContextFactory.java:69)
at
org.apache.openejb.client.LocalInitialContextFactory.init(LocalInitialContextFactory.java:52)
at
org.apache.openejb.client.LocalInitialContextFactory.getInitialContext(LocalInitialContextFactory.java:41)
at
javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at
javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.InitialContext.<init>(InitialContext.java:197)
at ph.dost.performers.exec4.test.UserTest.init(UserTest.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:478)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:617)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:885)
at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
at org.testng.TestRunner.runWorkers(TestRunner.java:712)
at org.testng.TestRunner.privateRun(TestRunner.java:582)
at org.testng.TestRunner.run(TestRunner.java:477)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:324)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:319)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:292)
at org.testng.SuiteRunner.run(SuiteRunner.java:198)
at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:823)
at org.testng.TestNG.runSuitesLocally(TestNG.java:790)
at org.testng.TestNG.run(TestNG.java:708)
at org.testng.TestNG.privateMain(TestNG.java:860)
at org.testng.TestNG.main(TestNG.java:833)
Caused by: java.lang.RuntimeException: Unable to bind business remote
deployment in jndi.
at
org.apache.openejb.assembler.classic.JndiBuilder.bind(JndiBuilder.java:337)
at
org.apache.openejb.assembler.classic.JndiBuilder.build(JndiBuilder.java:75)
at
org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:548)
... 41 more
Caused by: javax.naming.NameAlreadyBoundException:
openejb/Deployment/BudgetMatrixBean/ph.gov.dost.performers.session.BudgetMatrixRemote
at
org.apache.openejb.assembler.classic.JndiBuilder.bind(JndiBuilder.java:399)
at
org.apache.openejb.assembler.classic.JndiBuilder.bind(JndiBuilder.java:331)
... 43 more
======= LOOKUP =======
===============================================
ph.dost.performers.exec4.test.UserTest
Total tests run: 1, Failures: 1, Skips: 0
===============================================
Java Result: 1
View test results in build/test/results/index.html
/home/jocel/svndostperf1/trunk/PROTOTYPE/ExecutiveSupport4/nbproject/build-impl.xml:878:
Some tests failed; see details above.
BUILD FAILED (total time: 8 seconds)
My app failed to deploy because of this.. ERROR - Jndi name could not be
bound; it may be taken by another ejb.
Jndi(name=openejb/Deployment/BudgetMatrixBean/dost.performers.session.BudgetMatrixRemote)
Could not bound, why? Am I forgetting something here?
For clarifications.. In my package [dost.performers.session] I had the ff.
java interfaces and classes.
BudgetMatrixBean implements BudgetMatrixRemote
DostGroupFacade implements DostGroupFacadeRemote
PerformersLogFacade implements PerformersLogFacadeRemote
UserAccountFacade implements UserAccountFacadeRemote
MFOBeanImpl implements MFOBean (This has been added by me for trying
OpenEJB)
This app has been coded by our developer team. Should I change the name of
the classes in this package? How can I accomodate testing with beans using
@Remote and @Local?
Hope I didn't confuse you guys. Our team got a really big system to test. I
need a thorough research with this.
Thank you David for your non-stop help. I really appreciate it.
David Blevins wrote:
>
> 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
>
--
View this message in context:
http://www.nabble.com/Re%3A-javax.naming.NameNotFoundException-tp22545367p22574132.html
Sent from the OpenEJB User mailing list archive at Nabble.com.