You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Paul Landolt <la...@royalsys.com> on 2002/08/30 15:32:09 UTC

Tomcat 4.1.x, JNDI, and Junit

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


Re: Tomcat 4.1.x, JNDI, and Junit

Posted by Paul Landolt <la...@royalsys.com>.
Well, I've made some progress, but I'm still stuck.

by adding a reference to the apache context factory, I've managed to get an initial context:

  System.setProperty("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");

However, I still cannot resolve "java:comp/env/db/pgDevInt"; It fails with the following exception:

    NamingException: e= javax.naming.NameNotFoundException: Name java:comp is not bound in this Context

So, how do I bind to java:comp? A straight context lookup (like below) does not work either.

   DataSource ds = (DataSource)envContext.lookup("db/pgDevInt");

Anybody have any ideas?

Paul Landolt wrote:

> 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
>
>   ------------------------------------------------------------------------
> package ca.passport.ContentManager.Test;
>
> 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;
>
> /**
>  *  This is the DBConnect Testing Class. It uses JUnit3.7 to test
>  *  out the DB connections through a JNDI resource
>  *
>  *      @author J. Paul Landolt
>  *  @company Passport New Media
>  *  @version $Id:$
>  */
> public class TestDBConnect extends TestCase {
> //      private final static boolean DEBUG = false;                                     // output debug messages to System.err?
>         private final static boolean DEBUG = true;                                      // output debug messages to System.err?
>
>         static String dsTest = null;
>
>     public TestDBConnect(String s) {
>         super(s);
>     }
>
>         public void setUp() {
>         }
>
>         public void tearDown() {
>         }
>
>         /**
>          * Starts the application.
>          *
>          * @param args 'text' will optionally use a text-based test rather than graphical
>          *
>          */
>         public static void main(java.lang.String[] args) {
>                 // Graphic or text UI
>                 if ((args.length > 0) && (args[0].equals("gui"))) {
>                         System.out.println("Testing with GUI");
>                         junit.awtui.TestRunner.main(new String[] { TestDBConnect.class.getName() ,"-noloading"});
>                 }
>                 else {
>                         System.out.println("Testing with Text UI");
>                         junit.textui.TestRunner.main(new String[] { TestDBConnect.class.getName() });
>                 }
>         }
>
>         public static Test suite() {
>                 // Get test Datasource from properties list
>                 dsTest = System.getProperty("test.datasource");
>
>                 TestSuite suite = new TestSuite(TestDBConnect.class);
>                 return suite;
>         }
>
>         /**
>          * Blank Test. Exercises/Tests the test suite
>          *
>          * This test should be in all test classes written. It should
>          * be run before any other code is added in order to verify
>          * it's correct operation.
>          */
>         public static void testNothing() {
>                 System.out.println("testNothing()");
>         }
>
>         /**
>          * Tests the insertion of an element
>          */
>         public static void testDBConnectForRetrieve() {
>                 System.out.println("testDBConnectForRetrieve()");
>
>                 assertNotNull("test.datasource property undefined. Cannot connect to datasource without it", dsTest);
>
>                 if (DEBUG) {
>                         System.out.println("using datasource - " + dsTest);
>                 }
>
> //              System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
>                 System.setProperty("java.naming.provider.url", "127.0.0.1");
>                 System.setProperty("datasource.jndi", dsTest);
>
>                 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()) {
>                                 if (DEBUG) {
>                                         userName = rs.getString("username");
>                                         if (userName != null) {
>                                                 System.out.println("username retrieved is: " + userName);
>                                         }
>                                         else {
>                                                 System.out.println("NULL username retrieved");
>                                         }
>                                 }
>                         }
>                 } catch (java.sql.SQLException e) {
>                         System.out.println("SQLException: e= " + e);
>                         fail("SQL Exception raised during test");
>                 } catch (javax.naming.NamingException e) {
>                         System.out.println("NamingException: e= " + e);
>                         fail("NamingException raised during test");
>                 } catch (Exception e) {
>                         System.out.println("Exception: e= " + e);
>                         fail("Exception raised during test");
>                 } finally {
>                         if (rs != null) {
>                                 try { rs.close(); } catch( java.sql.SQLException e) {};
>                         }
>                         if (ps != null) {
>                                 try { ps.close(); } catch( java.sql.SQLException e) {};
>                         }
>                         if (conn != null) {
>                                 try { conn.close(); } catch( java.sql.SQLException e) {};
>                         }
>                 }
>         }
> }
>
>   ------------------------------------------------------------------------
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>