tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Landolt <land...@royalsys.com>
Subject Tomcat 4.1.x, JNDI, and Junit
Date Fri, 30 Aug 2002 13:32:09 GMT
Hello,

Has anyone managed to use a JUnit test framework to connect to a JNDI resource
provided by Tomcat? I'd like to be able to test my Java Database code using
JUnit.

When I used to use Weblogic, we could leave Weblogic running and then execute
JUnit test cases external of the web app while connecting to Weblogic's JNDI
resources. I'd like to perform the same task with TOMCAT. I've got the JNDI
services running and working, but I'd like to execute JUnit tests outside of
Tomcat and still use the Tomcat JNDI services that I've set up.

I have managed to configure a Tomcat JNDI resource to my Postgres Developer
Integration database. The resource is named, oddly enough, "db/pgDevInt". I've
created a successful demo Java Server Page to check the connectivity.  Here is a
code snippet from the page:

<%@page import = "java.util.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.sql.*"%>
<%@page import = "javax.sql.*"%>
<%@page import = "javax.naming.Context"%>
<%@page import = "javax.naming.InitialContext"%>

[...]
<%
 Connection conn = null;
 PreparedStatement ps = null;
 ResultSet rs = null;

 try {
  conn = null;
  ps = null;
  rs = null;

  InitialContext ctx = new InitialContext();
  DataSource ds = (DataSource)ctx.lookup("java:comp/env/db/pgDevInt");
  conn = ds.getConnection();
  ps = conn.prepareStatement("select username from login");
  rs = ps.executeQuery();

  String userName = null;
        while (rs.next()) {
[...]
        }
[...]
}
%>

This works without a hitch.

I would like to use a very similar snippet in a Junta test harness. I assume you
need to set some system properties before attempting to get the InitialContext,
but I'm not sure how to proceed.

Here is a snippet from my (currently not working JUnit test case):

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

[...]

public static void testDBConnectForRetrieve() {

    System.setProperty("java.naming.provider.url", "127.0.0.1");

    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        conn = null;
        ps = null;
        rs = null;

        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/db/pgDevInt");
        conn = ds.getConnection();
        ps = conn.prepareStatement("select username from login");
        rs = ps.executeQuery();
    }
}

When Junit attempts to get an InitialContext, the following exception is raised:

    [junit] NamingException: e= javax.naming.NoInitialContextException:
    Need to specify class name in environment or system property, or as
    an applet parameter,  or in an application resource
    file:  java.naming.factory.initial

I've attached the entire JUnit test case in the event that someone would like to
examine it (or use it)
If anybody has successfully managed to do this, I'd love to hear from you. If
others have suggestions,  I'm all ears (figuratively speaking)

    ...Paul


Mime
View raw message