Menu

#15 JNDI Mock Context

open
nobody
None
5
2004-02-23
2004-02-23
Anonymous
No

It will be great if there is also a JNDI mock context
in the Struts Test Case distribution.

I have coded one which rely one a HashTable for
setting/getting values in the JNDI context.

I haven't had the time to write all the methods, and
also haven't time to code an implementation which rely
on a context descriptor (like myapp.xml) for finding
the JNDI name/values [and this approach is limited to
specific servlet containers [i was thinking to Tomcat
which have different context descriptors than Weblogic
or Jboss for example).

so anybody who want a -limited- JNDI implementation
could use the attached file.

Discussion

  • Nobody/Anonymous

    A trivial (and far from complete) JNDI context implementation

     
  • Nobody/Anonymous

    Logged In: NO

    As a starting point for using this poor JNDI implementation
    you can use this TestCase :

    /*
    * Created: 13 fvr. 2004
    * Author: lmaitre (ludovic dot maitre at dbfc dot fr)
    * This file is licensed under the same license than
    StrutsTestCase
    */
    package fr.factory.test.jndi;

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

    import junit.framework.TestCase;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
    * Class TestMockContext
    * @author lmaitre
    */
    public class TestMockContext extends TestCase {

    private Log log = LogFactory.getLog(TestMockContext.class);

    /**
    *
    */
    public TestMockContext() {
    super();
    }
    /**
    * @param name
    */
    public TestMockContext(String name) {
    super(name);
    }

    /* (non-Javadoc)
    * @see junit.framework.TestCase#setUp()
    */
    protected void setUp() throws Exception {
    //set up JNDI mock factory (our extension to Struts test Case)

    System.setProperty("java.naming.factory.initial","fr.factory.test.jndi.MockInitialContextFactory");

    Context initialContext = new InitialContext();
    Context env =
    initialContext.createSubcontext("java:comp/env");
    if (env!=null) {
    log.debug("Populating JNDI context...");

    env.bind("log4j-init-file","/WEB-INF/properties/log4j.properties");
    env.bind("export-folder","c:/temp/exports");
    env.bind("root-folder","c:/temp");
    env.bind("app-annee-reference","2004-01-01");
    }
    else
    log.debug("Cannot retrieve env context!");
    initialContext.rebind("java:comp/env",env);
    log.debug("LogFile value is actually
    :"+env.lookup("log4j-init-file"));
    log.debug("LogFile value is actually
    :"+((Context)initialContext.lookup("java:comp/env")).lookup("log4j-init-file"));
    log.debug("InitialContext is actually
    :"+initialContext.hashCode());
    }

    final public void testMockContext() {
    try {
    log.debug("Instantiating initial context...");
    Context ic = new InitialContext();
    log.debug("InitialContext is actually :"+ic.hashCode());
    assertTrue(ic!=null);
    log.debug("Instantiating env context...");
    Context env = (Context)ic.lookup("java:comp/env");
    assertTrue(env!=null);
    log.debug("Lookup value in context");
    String logInitFile = (String) env.lookup("log4j-init-file");
    assertTrue(logInitFile!=null);
    } catch (NamingException e) {
    log.debug("Exception :" + e.getMessage());
    e.printStackTrace();
    }

    }

    }

     
  • Marvin D. Toll

    Marvin D. Toll - 2004-07-09

    Logged In: YES
    user_id=934620

    For Mock object support in concert with StrutsTestCase you
    may want to consider using JSLIM (
    https://sourceforge.net/projects/jslim/ ).

     

Log in to post a comment.