You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Raible <ma...@raibledesigns.com> on 2002/02/15 13:00:45 UTC
RE: tomcat datasource - JUnit testing
Thanks - this is exactly what I was looking for.
Now that I have my jndi-lookup in a ServiceLocator class - does anyone
know how to run a Junit test (maybe using StrutsTestCase -
http://strutstestcase.sourceforge.net
<http://strutstestcase.sourceforge.net/> ) to test this in Tomcat?
I have the what you see below, but I get an error.
Any advice is appreciated,
Matt
Error
-------------------------------------
[junit] Running com.onpoint.webapp.test.TestServiceLocator
[junit] null
[junit] NamingException: Cannot instantiate class:
com.sun.jndi.fscontext.RefFSContextFactory
jndi.properties
-------------------------------------
java.naming.factory.initial=com.sun.jndi.fscontext.RefFSContextFactory
java.naming.provider.url=file:.
Ant Task:
-------------------------------------
<target name="test.db" depends="deploy">
<echo message="Running Service Locator Test
-------------------"/>
<junit printsummary="yes" haltonfailure="yes"
haltonerror="yes" fork="yes">
<classpath>
<pathelement
path="${webapp.target}/WEB-INF/classes"/>
</classpath>
<classpath refid="classpath"/>
<formatter type="plain" usefile="false"/>
<test
name="com.onpoint.webapp.test.TestServiceLocator"/>
</junit>
</target>
ServiceLocator.java
-------------------------------------
public class ServiceLocator {
/** singleton's private instance */
private static ServiceLocator me;
static {
me = new ServiceLocator();
}
private ServiceLocator() {}
/** returns the Service Locator instance */
static public ServiceLocator getInstance() {
return me;
}
private DataSource ds = null;
private Connection con = null;
/** Retrieves a connection from the connection pool */
//TODO: Throw a ServiceException, rather than Naming and SQL
public Connection getConnection() {
// Get an InitialContext from Sun's RMI JNDI Provider
Context initCtx = null;
try {
initCtx = new InitialContext();
DataSource ds = (DataSource)
initCtx.lookup(Constants.JNDI_DB);
} catch( NamingException ex ) {
System.err.println("NamingException: " +
ex.getMessage());
}
try {
con = ds.getConnection();
} catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
/** Closes a connection from the connection pool */
public void closeConnection(Connection con) {
try {
con.close();
} catch (SQLException sqle) {
System.err.println("SQLException: " +
sqle.getMessage());
} finally {
if (con != null) {
// try again
try {
con.close();
} catch (SQLException csqle) {
// ignore
}
}
}
}
}
TestServiceLocator.java
-------------------------------------
public void testGetConnection() throws Exception {
try {
con = serviceLocator.getConnection();
} catch( Exception ex ) {
// client handles exception
System.out.println( ex.getMessage());
fail("Unable to retrieve Service Locator Connection: " + ex);
}
assertTrue (con != null);
}
-----Original Message-----
From: Ivan Siviero [mailto:ivan.siviero@concept.it]
Sent: Thursday, February 15, 2001 1:40 AM
To: matt@raibledesigns.com
Subject: tomcat datasource
hi i read your post on struts-user mailing list.
I use tomcat 4.0 with connection pool.
Yes you can use JNDI to access it
You have to specifiy the following rows in the
$TOMCAT_HOME/conf/server.xml in the context of your application.
<Resource name="jndiName" auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jndiName">
<parameter>
<name>user</name>
<value>dbuserValue</value>
</parameter>
<parameter>
<name>password</name>
<value>dbPasswordValue</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>dbDriverClass</value>
</parameter>
<parameter>
<name>driverName</name>
<value>JDBCUrl</value>
</parameter>
</ResourceParams>
to get a connection from your web application (no try catch statement
reported):
Context initCtx = new InitialContext();
envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup(jndiName);
return ds.getConnection();
Hope this helps
Ivan