/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.test.naming.test;
import org.jboss.test.JBossTestCase;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NameNotFoundException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.Name;
import javax.naming.NameClassPair;
import java.util.Properties;
/** Simple unit tests for the jndi implementation. Note that there cannot
* be any security related tests in this file as typically it is not run
* with the right classpath resources for that.
*/
public class SimpleUnitTestCase extends JBossTestCase
{
/**
* Constructor for the SimpleUnitTestCase object
*
* @param name Test name
*/
public SimpleUnitTestCase(String name)
{
super(name);
}
/**
* Tests that the second time you create a subcontext you get an exception.
*
* @exception Exception Description of Exception
*/
public void testCreateSubcontext() throws Exception
{
getLog().debug("+++ testCreateSubcontext");
InitialContext ctx = getInitialContext();
ctx.createSubcontext("foo");
try
{
ctx.createSubcontext("foo");
fail("Second createSubcontext(foo) did NOT fail");
}
catch (NameAlreadyBoundException e)
{
getLog().debug("Second createSubcontext(foo) failed as expected");
}
ctx.createSubcontext("foo/bar");
ctx.unbind("foo/bar");
ctx.unbind("foo");
}
/** Lookup a name to test basic connectivity and lookup of a known name
*
* @throws Exception
*/
public void testLookup() throws Exception
{
getLog().debug("+++ testLookup");
InitialContext ctx = getInitialContext();
Object obj = ctx.lookup("");
getLog().debug("lookup('') = "+obj);
}
/** List the root context
*
* @throws Exception
*/
public void testListing() throws Exception
{
log.debug("+++ testListing");
InitialContext ctx = getInitialContext();
NamingEnumeration names = ctx.list("");
int count = 0;
while( names.hasMore() )
{
NameClassPair ncp = (NameClassPair) names.next();
log.info(ncp);
count ++;
}
assertTrue("list count > 0 ", count > 0);
ctx.close();
}
public void testNameChanges() throws Exception
{
getLog().debug("+++ testLookup");
InitialContext ctx = getInitialContext();
Name name = ctx.getNameParser("").parse("jnp://localhost/jmx");
Name copy = (Name) name.clone();
Object obj = ctx.lookup(name);
getLog().debug("lookup("+name+") = "+obj);
assertTrue("name.equals(copy), name="+name, name.equals(copy));
}
/** Lookup a name to test basic connectivity and lookup of a known name
*
* @throws Exception
*/
public void testLookupFailures() throws Exception
{
getLog().debug("+++ testLookupFailures");
// Look a name that does not exist
Properties env = new Properties();
InitialContext ctx = new InitialContext(env);
try
{
Object obj = ctx.lookup("__bad_name__");
fail("lookup(__bad_name__) should have thrown an exception, obj="+obj);
}
catch(NameNotFoundException e)
{
getLog().debug("lookup(__bad_name__) failed as expected", e);
}
// Do a lookup on an server port that does not exist
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:65535/");
env.setProperty("jnp.disableDiscovery", "true");
getLog().debug("Creating InitialContext with env="+env);
try
{
ctx = new InitialContext(env);
Object obj = ctx.lookup("");
fail("lookup('') should have thrown an exception, obj="+obj);
}
catch(NamingException e)
{
getLog().debug("lookup('') failed as expected", e);
}
}
}