5 Replies Latest reply on Mar 2, 2005 6:29 PM by Todd Merrill

    What data source is my CMP bound to?

    Todd Merrill Newbie

      Hello,

      For unit testing, I've created a test roll to JBoss 4.01 and I'm performing my EJB unit tests with JUnitEE. I'm using xdoclet in Ant to change the datasource in the jbosscmp-jdbc.xml file to a test database.

      I want to write a server side test to confirm that the CMP beans I'm testing are in fact correctly bound to my test database (They are, I know, because my tests are running correctly, but I'd like to put this test in to assure that if someone accidentally changes the datasource in Ant, the test will fail before any data is changed).

      How do I get the name of the datasource that a CMP bean or group of CMP beans have actually been bound to after deployment? Is there a specific JNDI entry? I'd like to avoid making changes to the beans themselves, but I will if that's the only way.

      Any suggestions would be helpful.

        • 1. Re: What data source is my CMP bound to?
          Luca Stancapiano Apprentice

          hi feigling.....I guess you need to know a manner to get the datasource jndiname of an entity. Then if you are in your junit test application you can do this:

          String jndiName = " ...... jndi name of your entity.......";
          ObjectName name = new ObjectName("jndiName="+jndiName+",service=EJB");
          InitialContext ic = new InitialContext();
          RMIAdaptor server = (RMIAdaptor)lookup("jmx/rmi/RMIAdaptor");

          String[] sig = {};
          Object[] opArgs = {};

          JDBCEntityMetaData jamd = new JDBCEntityMetadata(
          server.invoke(name,"getBeanMetaData().getApplicationMetaData())",opArgs,sig),
          server.invoke(name,"getClassLoader()",opArgs,sig)
          );

          • 2. Re: What data source is my CMP bound to?
            Luca Stancapiano Apprentice

            sorry ...i continue.....


            String dataSource = jamd.getDataSource();


            if your application test is an mbean you can put off the part of lookup jmx and you can to write so:

            MBeanServer server = this.getServer();


            instead of:

            RMIAdaptor server = (RMIAdaptor)lookup("jmx/rmi/RMIAdaptor");

            • 3. Re: What data source is my CMP bound to?
              Todd Merrill Newbie

              Hello sviluppatorefico!

              Thanks for the help, but I'm still having problems. In the code you gave me, the returned type for the 'name' ObjectName var is org.jboss.ejb.EntityContainer, which does not contain a getBeanMetaData() method, so it throws a ReflectionException. There is a getEJBMetaData() method, but that's just an EJB interface. I didn't see anything in the interface that could get me to a BeanMetaData interface. Perhaps you left something out?

              • 4. Re: What data source is my CMP bound to?
                Todd Merrill Newbie

                Hello again,

                Well, I figured out the BeanMetaData issue. BeanMetaData is just an attribute of the EntityContainer instance.

                I couldn't get a ClassLoader from the EntityContainer though - no getClass(), no getClassLoader(). I created a JDBCEntityMetaData object using the ClassLoader from the EntityMetaData attribute and managed to create an instance of JDBCEntityMetaData, but the getDataSource() method returns null.

                Any ideas?

                • 5. Re: What data source is my CMP bound to?
                  Todd Merrill Newbie

                  So no one out there knows how to do this? Anyone?