You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Aleksandr Ivanov <al...@gmail.com> on 2010/04/06 22:58:30 UTC

OpenEjb + PowerMock

Hello list,

I'm using OpenEjb to unit test my ejb3 code. My application has a
third party dependency, which I'm mocking with PowerMock +EasyMock.
Without mocking OpenEjb works perfectly fine - fetches all
configuration and binds all beans to proper JNDI. I have to use
PowerMock to mock one static service (which is called from EJB)
When PowerMock is used  I'm having trouble in initializing
persistence.xml. XML unmarshaller complains (throws xml parsing
exception) about persistence.xml default namespace.
When I delete the namespace - the code is deployed perfectly and
container is initialized with PowerMock configured. After that I'm
able to run any unit test.
The downside of removing the namespace is that when I try to deploy
the .ear on the real container (JBoss 6.0 M2) it fails with the
complaint about missing namespace (according to J2EE spec the
namespace should be present).

So I my question is - can I programmatically disable persistence.xml
validation when deploying to openEjb?

Thanks in advance,

-- 
Aleksandr Ivanov

Re: OpenEjb + PowerMock

Posted by Aleksandr Ivanov <al...@gmail.com>.
Hello, thanks for the reply.

I cannot give you the actual test class, but hopefully I can spread
some light on this one.
This is the part of the test case which fails.

import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import static org.powermock.api.easymock.PowerMock.replay;
import static org.powermock.api.easymock.PowerMock.verify;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class) //THIS is a relevant part. This
MockRunner does the magic of mocking the static service. IF I remove
this, everything works fine, but static service is not mocked.
@PrepareForTest(ContextManager.class) // service class.
public class SiteControllerBeanTest {
	
	private InitialContext context;
	
	@Before
	public void setUp() throws Exception {
		
		DbUnitUtil.executeDbUnitScript("dbunit/cleanup_dataset.xml",
DatabaseOperation.DELETE_ALL);
		
		Properties p = new Properties();
		p.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
		
		p.put("siteControllerDS", "new://Resource?type=DataSource");
		p.put("siteControllerDS.JdbcDriver", "org.postgresql.Driver");
		p.put("siteControllerDS.JdbcUrl",
"jdbc:postgresql://localhost:5432/site_controller");
		p.put("siteControllerDS.UserName", "controller");
		p.put("siteControllerDS.Password", "controller");
		p.put("openejb.validation.output.level", "verbose");
		context = new InitialContext(p);
	}
	
	@Test
	@SuppressWarnings("unchecked")
	public void testClientUnknownOnline() throws Exception {
                             //with the namespace set this is never
executed. Test crashes in the setup method.
		mockStatic(ContextManager.class); //here I'm mocking the static interface
		...
				
	}
	
}


I've managed to get around this problem by creating a class
org.apache.openejb.jee.jpa.unit.PersistenceFilter and overriding one
method inside.
After that I'm placing this class upper in the classpath, so it
"overrides" openejb class with the same name. I know it's a hack, but
it does it job for the testing
and I don't have to recompile the openejb from source.

 public static class PersistenceFilter extends XMLFilterImpl {
        private static final InputSource EMPTY_INPUT_SOURCE = new
InputSource(new ByteArrayInputStream(new byte[0]));

        public PersistenceFilter(XMLReader xmlReader) {
            super(xmlReader);
        }

        public InputSource resolveEntity(String publicId, String
systemId) throws SAXException, IOException {
            return EMPTY_INPUT_SOURCE;
        }

        @Override
        //this is custom override.
        public void startElement(String arg0, String arg1, String
arg2, Attributes arg3) throws SAXException {
             //no matter what the namespace is - it will be empty :)
	super.startElement("", arg1, arg2, arg3);
        }

    }

Hope this helps :)


Best,

Aleksandr

2010/4/12 Jean-Louis MONTEIRO <je...@atosorigin.com>:
>
> Hello,
>
> Thanks for pointing that.
> do you have a short example to reproduce the issue?
> In the mid time i'll give it a try.
>
> Jean-Louis
>
> --
> View this message in context: http://n4.nabble.com/OpenEjb-PowerMock-tp1753497p1836697.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>



-- 
Aleksandr Ivanov

Re: OpenEjb + PowerMock

Posted by Jean-Louis MONTEIRO <je...@atosorigin.com>.
Hello,

Thanks for pointing that.
do you have a short example to reproduce the issue?
In the mid time i'll give it a try.

Jean-Louis

-- 
View this message in context: http://n4.nabble.com/OpenEjb-PowerMock-tp1753497p1836697.html
Sent from the OpenEJB User mailing list archive at Nabble.com.