package org.mockejb.jndi;
import java.util.ArrayList;
import java.util.Collection;
import javax.naming.*;
import junit.framework.*;
public class MockContextTest extends TestCase {
private Context initialCtx;
private Context compCtx;
private Context envCtx;
private Context ejbCtx;
public MockContextTest(String name) {
super(name);
}
protected void setUp() throws Exception {
MockContextFactory.setAsInitial();
initialCtx = new InitialContext();
initialCtx.bind("java:comp/env/ejb/Dummy", null);
compCtx = (Context)initialCtx.lookup("java:comp");
envCtx = (Context)compCtx.lookup("env");
ejbCtx = (Context)envCtx.lookup("ejb");
}
protected void tearDown() throws Exception {
clearContext(initialCtx);
ejbCtx = null;
envCtx = null;
compCtx = null;
initialCtx = null;
MockContextFactory.revertSetAsInitial();
}
private void clearContext(Context context) throws NamingException {
for (NamingEnumeration e = context.listBindings("");
e.hasMoreElements();
) {
Binding binding = (Binding) e.nextElement();
if (binding.getObject() instanceof Context) {
clearContext((Context) binding.getObject());
}
context.unbind(binding.getName());
}
}
public void testSubcontextCreationOfDuplicates() throws NamingException {
try {
initialCtx.createSubcontext("java:comp");
fail();
} catch (NameAlreadyBoundException ex) {}
try {
compCtx.createSubcontext("env/ejb");
fail();
} catch (NameAlreadyBoundException ex) {}
}
public void testSubcontextNonEmptyDestruction() throws NamingException {
ejbCtx.bind("EmptyTest", "EmptyTest Object");
try {
initialCtx.destroySubcontext("java:comp");
fail();
} catch (ContextNotEmptyException ex) {}
try {
initialCtx.destroySubcontext("java:comp/env/ejb");
fail();
} catch (ContextNotEmptyException ex) {}
try {
envCtx.destroySubcontext("ejb");
fail();
} catch (ContextNotEmptyException ex) {}
}
public void testSubcontextDestruction() throws NamingException {
Context sub1 = ejbCtx.createSubcontext("sub1");
Context sub2 = ejbCtx.createSubcontext("sub2");
Context sub3 = envCtx.createSubcontext("sub3");
initialCtx.destroySubcontext("java:comp/env/ejb/sub1");
ejbCtx.destroySubcontext("sub2");
envCtx.destroySubcontext("sub3");
try {
ejbCtx.lookup("sub1");
fail();
} catch (NameNotFoundException ex) {}
try {
envCtx.lookup("ejb/sub2");
fail();
} catch (NameNotFoundException ex) {}
try {
initialCtx.lookup("java:comp/sub3");
fail();
} catch (NameNotFoundException ex) {}
}
public void testSubcontextInvokingMethodsOnDestroyedContext()
throws NamingException {
Context sub = ejbCtx.createSubcontext("subXX");
initialCtx.destroySubcontext("java:comp/env/ejb/subXX");
try {
sub.bind("SomeName", "SomeObject");
fail();
} catch (NoPermissionException ex) {}
try {
sub.unbind("SomeName");
fail();
} catch (NoPermissionException ex) {}
try {
sub.createSubcontext("SomeSubcontext");
fail();
} catch (NoPermissionException ex) {}
try {
sub.destroySubcontext("DummyName");
fail();
} catch (NoPermissionException ex) {}
try {
sub.list("");
fail();
} catch (NoPermissionException ex) {}
try {
sub.lookup("DummyName");
fail();
} catch (NoPermissionException ex) {}
try {
sub.composeName("name", "prefix");
fail();
} catch (NoPermissionException ex) {}
try {
MockContextNameParser parser = new MockContextNameParser();
sub.composeName(parser.parse("a"), parser.parse("b"));
fail();
} catch (NoPermissionException ex) {}
}
public void testBindLookup() throws NamingException {
Object o1 = new String("Test object for atomic binding");
Object o2 = new String("Test object for compound binding");
Object o3 = new String("Test object for complex compound binding");
ejbCtx.bind("AtomicNull", null);
ejbCtx.bind("AtomicObject", o1);
initialCtx.bind("java:comp/env/CompoundNull", null);
initialCtx.bind("java:comp/env/CompoundObject", o2);
initialCtx.bind("java:comp/env/ejb/subToCreate1/subToCreate2/oo", o3);
assertNull(ejbCtx.lookup("AtomicNull"));
assertSame(ejbCtx.lookup("AtomicObject"), o1);
assertNull(compCtx.lookup("env/CompoundNull"));
assertSame(initialCtx.lookup("java:comp/env/CompoundObject"), o2);
assertSame(ejbCtx.lookup("subToCreate1/subToCreate2/oo"), o3);
}
public void testUnbind() throws NamingException {
envCtx.bind("testUnbindName1", null);
compCtx.bind("env/ejb/testUnbindName2", new String("Test unbind object"));
initialCtx.unbind("java:comp/env/testUnbindName1");
ejbCtx.unbind("testUnbindName2");
try {
envCtx.lookup("testUnbindName1");
fail();
} catch (NameNotFoundException ex) {}
try {
initialCtx.lookup("java:comp/env/ejb/testUnbindName2");
fail();
} catch (NameNotFoundException ex) {}
try {
ejbCtx.unbind("This name does not exist in the context");
} catch (Exception ex) {
fail();
}
try {
compCtx.unbind("env/ejb/ejb1/somename");
fail();
} catch (NameNotFoundException ex) {}
}
public void testListBindings() throws NamingException {
Object o1 = new String("Test list bindings 1");
Object o2 = new String("Test list bindings 2");
compCtx.bind("env/ejb/testListBindings1", o1);
envCtx.bind("testListBindings2", o2);
ejbCtx.bind("testListBindings3", null);
verifyListBindingsResult(envCtx, "", o1, o2);
verifyListBindingsResult(initialCtx, "java:comp/env", o1, o2);
}
private void verifyListBindingsResult(Context c, String name, Object o1, Object o2) throws NamingException {
boolean ejbFoundFlg = false;
boolean o2FoundFlg = false;
boolean ejbO1FoundFlg = false;
boolean ejbNullFoundFlg = false;
for (NamingEnumeration en = c.listBindings(name); en.hasMore(); ) {
Binding b = (Binding)en.next();
if (b.getName().equals("ejb")) {
assertEquals(b.getObject(), ejbCtx);
ejbFoundFlg = true;
Context nextCon = (Context)b.getObject();
for (NamingEnumeration en1 = nextCon.listBindings(""); en1.hasMore(); ) {
Binding b1 = (Binding)en1.next();
if (b1.getName().equals("testListBindings1")) {
assertEquals(b1.getObject(), o1);
ejbO1FoundFlg = true;
} else if (b1.getName().equals("testListBindings3")) {
assertNull(b1.getObject());
ejbNullFoundFlg = true;
}
}
} else if (b.getName().equals("testListBindings2")) {
assertEquals(b.getObject(), o2);
o2FoundFlg = true;
}
}
if (!(ejbFoundFlg && o2FoundFlg && ejbO1FoundFlg && ejbNullFoundFlg)) {
fail();
}
}
public void testCompositeNameWithLeadingTrailingEmptyComponents() throws Exception {
MockContext c = new MockContext(null);
Object o = new Object();
c.rebind("/a/b/c/", o);
assertEquals(c.lookup("a/b/c"), o);
assertEquals(c.lookup("///a/b/c///"), o);
}
public void testLookup() throws Exception {
MockContext mockCtx = new MockContext( null );
Object obj = new Object();
mockCtx.rebind("a/b/c/d", obj );
assertEquals(obj, mockCtx.lookup("a/b/c/d"));
mockCtx.bind("a", obj );
assertEquals(obj, mockCtx.lookup("a"));
}
public void testGetCompositeName() throws Exception {
MockContext mockCtx = new MockContext( null );
mockCtx.rebind("a/b/c/d", new Object());
MockContext subCtx;
subCtx = (MockContext)mockCtx.lookup("a");
assertEquals("a", subCtx.getCompoundStringName());
subCtx = (MockContext)mockCtx.lookup("a/b/c");
assertEquals("a/b/c", subCtx.getCompoundStringName());
}
public void testDelegateContext() throws Exception {
ArrayList recordedLookups = new ArrayList();
Context ctx = new MockContext ( new RecordingMockContext( recordedLookups ));
String wrongName;
wrongName = "mockejb";
ctx.lookup( wrongName );
assertEquals(1, recordedLookups.size() );
assertEquals(wrongName, recordedLookups.get(0));
recordedLookups.clear();
wrongName = "mockejb/a";
ctx.lookup( wrongName );
assertEquals(1, recordedLookups.size() );
assertEquals(wrongName, recordedLookups.get(0));
recordedLookups.clear();
ctx.rebind("mockejb/dummy", new Object());
wrongName = "mockejb/a";
ctx.lookup( wrongName );
assertEquals(1, recordedLookups.size() );
assertEquals(wrongName, recordedLookups.get(0));
}
class RecordingMockContext extends MockContext {
private Collection recordedLookups;
public RecordingMockContext( Collection recordedNames ){
super( null );
this.recordedLookups = recordedNames;
}
public Object lookup( Name name ) throws NamingException {
recordedLookups.add( name.toString() );
return new Object();
}
public Object lookup( String name ) throws NamingException {
recordedLookups.add( name );
return new Object();
}
}
public void testTwoSeparatorNames() throws NamingException {
MockContext ctx = new MockContext(null);
Object obj = new Object();
ctx.bind("a/b.c.d/e", obj );
assertEquals(ctx.lookup("a/b/c/d/e"), obj);
assertEquals(ctx.lookup("a.b/c.d.e"), obj);
assertTrue(ctx.lookup("a.b.c.d") instanceof Context);
}
}