mardi 19 mai 2015

Unable to connect to database with JUNIT and OPENJPA

I am facing an issue while connecting to an oracle database with my JUNIT code(By default it tries to connect to HSQL). I am using openjpa as a persistence provider. I have tried a number of approaches which include specifying the connection settings in the persistence.xml and Specifying connection settings in Test class.

    public class CollateralHandlerTest {

    private static InitialContext ctx;

    @BeforeClass
    public static void initContainer() throws Exception {
        final Properties props = new Properties();
        props.put("tomee.ejbcontainer.http.port", 1800);
        props.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory");
        props.put("ProvAppCLJPA.openjpa.ConnectionURL", "jdbc:oracle:thin:@localhost:1521:XE");
        props.put("ProvAppCLJPA.openjpa.ConnectionPassword", "XXXX");
        props.put("ProvAppCLJPA.openjpa.ConnectionDriverName", "oracle.jdbc.OracleDriver");
        props.put("ProvAppCLJPA.openjpa.ConnectionUserName", "PROVAPPCL_DE");
        ctx = new InitialContext(props);

    }

    @Test
    public void testCollateralHandler() throws NamingException, ProvAppException {
        Object object = ctx.lookup("CollateralHandlerLocal");
        assertTrue(object instanceof ICollateralHandler);
        ICollateralHandler collHandler = (ICollateralHandler) ctx.lookup("CollateralHandlerLocal");
        List<Customer> cust = collHandler.getAllCollateral("XXXX");
    }
}

I get an error at the last line of the test method while executing

 final Query query = entityManager.createNamedQuery("");
    query.getResultList();

in the "getAllCollateral " function.

I get the following exception

 <openjpa-2.2.0-r422266:1244990 fatal user error> org.apache.openjpa.util.UserException: A connection could not be obtained for driver class "null" and URL "null".  You may have specified an invalid URL.
        at org.apache.openjpa.jdbc.schema.DataSourceFactory.newConnectException(DataSourceFactory.java:255)
        at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:241)
   --Cut Short 
    Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (invalid authorization specification - not found: SA)
        at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
    --Cut Short
    Caused by: java.sql.SQLInvalidAuthorizationSpecException: invalid authorization specification - not found: SA
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
        at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
        --Cut Short
    Caused by: org.hsqldb.HsqlException: invalid authorization specification - not found: SA
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.rights.UserManager.get(Unknown Source)
        at org.hsqldb.rights.UserManager.getUser(Unknown Source)
        at org.hsqldb.Database.connect(Unknown Source)
        at org.hsqldb.DatabaseManager.newSession(Unknown Source)
        ... 130 more

somehow it tries to picks up the HSQL db as the database and I don't know why it doesn't pick up the oracle settings. I have also gone through the following URL

http://ift.tt/1Fu5RDo

and tried to set the connection properties in the VM arguments. Please Help !!!

Aucun commentaire:

Enregistrer un commentaire