Package org.mom4j.jndi

Source Code of org.mom4j.jndi.TestJNDI

package org.mom4j.jndi;

import org.mom4j.api.JNDIContextHandler;

import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

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

import junit.framework.Assert;
import junit.framework.TestCase;


public class TestJNDI extends TestCase {

    NamingServer server;
   
    public TestJNDI(String name) {
        super(name);
    }
   
   
    public void setUp() {
        this.server = new NamingServer(8001);
        this.server.start();
    }


    public void tearDown() {
        this.server.stop();
    }


    public void testSuccess()
        throws Exception
    {
        Properties namingProps = new Properties();
       
        namingProps.put(Context.INITIAL_CONTEXT_FACTORY,
                        "org.mom4j.jndi.InitialCtxFactory");
        namingProps.put(Context.PROVIDER_URL,
                        "xcp://localhost:8001");
       
        Context ctx = new InitialContext(namingProps);

        //Try a string
        ctx.bind("string", "string");
        String s = (String)ctx.lookup("string");
        Assert.assertEquals("string - lookup", "string", s);
       
        //Try a long
        long l = 100000L;
        Long L = new Long(l);
        ctx.bind("long", L);
        L = (Long)ctx.lookup("long");
        Assert.assertEquals("long - lookup", l, L.longValue());
       
        //Try an integer
        int i = 10;
        Integer I = new Integer(i);
        ctx.bind("integer", I);
        I = (Integer)ctx.lookup("integer");
        Assert.assertEquals("integer - lookup", i, I.intValue());
       
        //Try a hashtable
        Hashtable ht = new Hashtable();
        ht.put("string", "string");
        ht.put("long", L);
        ht.put("integer", I);
        ctx.bind("hashtable", ht);
        ht = (Hashtable)ctx.lookup("hashtable");
       
        //Try a real big hashmap
        HashMap map = new HashMap();
        for(int k = 0; k < 1000; k++) {
            map.put("key-" + k, new Integer(k));
        }
        ctx.bind("map", map);
        map = (HashMap)ctx.lookup("map");

        /* Test sub context */

        Context subCtx = ctx.createSubcontext("myctx");
        Assert.assertNotNull("subcontext is null", subCtx);

        String name = "name" + System.currentTimeMillis();
        subCtx.bind(name, name);

        Object o = subCtx.lookup(name);
        Assert.assertNotNull("object from subContext is null!", o);
        Assert.assertEquals("object from subContext", name, o);

        try {
            ctx.lookup(name);
            Assert.fail(name + " was not expected in root context");
        } catch(Exception ex) {
            //as expected
        }

        Context ccc = (Context)ctx.lookup("myctx");

        o = ccc.lookup(name);
        Assert.assertNotNull("* object from subContext is null!", o);
        Assert.assertEquals("* object from subContext", name, o);

        String path = "/subctx";
        HashMap data = new HashMap();
        name = "name" + System.currentTimeMillis();
        data.put(name, name);

        CtxHandler handler = new CtxHandler(path, data);

        this.server.addContextHandler(handler);

        Context subCtx2 = (Context)ctx.lookup("subctx");

        o = subCtx2.lookup(name);
        Assert.assertNotNull("object from subContext2 is null!", o);
        Assert.assertEquals("object from subContext2", name, o);

        name = "name" + System.currentTimeMillis();

        subCtx2.bind(name, name);

        Assert.assertEquals("data in handler", name, handler.getData().get(name));

        subCtx2.unbind(name);

        Assert.assertNull("data in handler", handler.getData().get(name));
    }


    class CtxHandler implements JNDIContextHandler {

        private String  path;
        private Map     map;

        public CtxHandler(String path, Map data) {
            this.path = path;
            this.map  = data;
        }

        public Map getData() {
            return this.map;
        }

        public void setContextPath(String path) {
            this.path = path;
        }

        public String getContextPath() {
            return this.path;
        }

        public java.util.Map load() {
            return this.map;
        }

        public void bind(String name, Object object) {
            this.map.put(name, object);
        }

        public void unbind(String name) {
            this.map.remove(name);
        }

    }


}
TOP

Related Classes of org.mom4j.jndi.TestJNDI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.