From cec7705dcab128859522c26069f8518ec883b0b6 Mon Sep 17 00:00:00 2001 From: erik Date: Tue, 11 May 2010 19:18:07 +0000 Subject: [PATCH] --- .../jpa/UserAdministrationTester.java | 7 ++- .../support/jndi/StubInitialContext.java | 21 +++++-- .../support/persistence/DatabaseUtils.java | 1 + .../jndi/StubInitiaContextFactoryTest.java | 55 +++++++++++++++++-- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java index 634e10ed..a4917983 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java @@ -43,6 +43,7 @@ public class UserAdministrationTester { private JpaTester jpaTester; private Cache userCache; private UserAdministration userAdmin; + private MessageDigester passwordDigester; public UserAdministrationTester() { persistenceUnit = new SecurityPersistenceUnit(); @@ -60,7 +61,7 @@ public class UserAdministrationTester { NameValidator passwordValidator = new RegexpNameValidator(".{5,}", "INVALID_PASSWORD", "Password must have at least 5 characters"); - MessageDigester passwordDigester = new Md5HexMessageDigester(); + passwordDigester = new Md5HexMessageDigester(); UserSet userset = new JpaUserSet(userCache, passwordValidator, passwordDigester, factory.getTransactionScopedEntityManager()); GroupSet groupset = new JpaGroupSet(factory @@ -90,6 +91,10 @@ public class UserAdministrationTester { public UserAdministration getUserAdministration() { return userAdmin; } + + public MessageDigester getPasswordEncoder() { + return passwordDigester; + } public void stop() throws Exception { jpaTester.stop(); diff --git a/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java b/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java index 5300a8b6..d6f90fee 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java +++ b/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java @@ -34,6 +34,11 @@ public class StubInitialContext extends InitialContext { public void bind(String aName, Object aObj) throws NamingException { bindings.put(aName, aObj); } + + @Override + public void unbind(String aName) throws NamingException { + bindings.remove(aName); + } @Override public Object lookup(String aName) throws NamingException { @@ -43,13 +48,19 @@ public class StubInitialContext extends InitialContext { } return value; } + + @Override + public void bind(Name aName, Object aObj) throws NamingException { + bind(aName.toString(), aObj); + } + + @Override + public void unbind(Name aName) throws NamingException { + unbind(aName.toString()); + } @Override public Object lookup(Name aName) throws NamingException { - Object value = super.lookup(aName.toString()); - if (value == null) { - throw new NameNotFoundException(aName.toString()); - } - return value; + return lookup(aName.toString()); } } diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java index 9ad6c912..23938768 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java @@ -108,6 +108,7 @@ public class DatabaseUtils { LOG.log(Level.WARNING, "Could not close connection", e); } } + connections.clear(); } public IDatabaseTester createDbTester() throws Exception { diff --git a/test/enterprise/src/test/java/org/wamblee/support/jndi/StubInitiaContextFactoryTest.java b/test/enterprise/src/test/java/org/wamblee/support/jndi/StubInitiaContextFactoryTest.java index f6a57810..c0638148 100644 --- a/test/enterprise/src/test/java/org/wamblee/support/jndi/StubInitiaContextFactoryTest.java +++ b/test/enterprise/src/test/java/org/wamblee/support/jndi/StubInitiaContextFactoryTest.java @@ -17,6 +17,7 @@ package org.wamblee.support.jndi; import static junit.framework.Assert.*; +import javax.naming.CompositeName; import javax.naming.InitialContext; import javax.naming.NameNotFoundException; import javax.naming.NamingException; @@ -27,6 +28,8 @@ import org.junit.Test; public class StubInitiaContextFactoryTest { + private static final String JNDI_NAME = "a/b"; + @Before @After public void setUp() { @@ -36,27 +39,71 @@ public class StubInitiaContextFactoryTest { @Test(expected = NamingException.class) public void testLookupNotRegistered() throws Exception { InitialContext ctx = new InitialContext(); - ctx.bind("a/b", "hallo"); + ctx.bind(JNDI_NAME, "hallo"); } + + // String based lookups. @Test public void testLookup() throws Exception { StubInitialContextFactory.register(); InitialContext ctx = new InitialContext(); - ctx.bind("a/b", "hallo"); + ctx.bind(JNDI_NAME, "hallo"); ctx = new InitialContext(); - Object obj = ctx.lookup("a/b"); + Object obj = ctx.lookup(JNDI_NAME); assertEquals("hallo", obj); } + + @Test(expected = NameNotFoundException.class) + public void testUnbind() throws Exception { + testLookup(); + InitialContext ctx = new InitialContext(); + ctx.unbind(JNDI_NAME); + ctx = new InitialContext(); + ctx.lookup(JNDI_NAME); + + } @Test(expected = NameNotFoundException.class) public void testLookupFails() throws Exception { StubInitialContextFactory.register(); InitialContext ctx = new InitialContext(); - Object obj = ctx.lookup("a/b"); + Object obj = ctx.lookup(JNDI_NAME); + } + + // Name based lookups + + @Test + public void testLookupName() throws Exception { + StubInitialContextFactory.register(); + + InitialContext ctx = new InitialContext(); + ctx.bind(new CompositeName(JNDI_NAME), "hallo"); + + ctx = new InitialContext(); + Object obj = ctx.lookup(new CompositeName(JNDI_NAME)); + + assertEquals("hallo", obj); + } + + @Test(expected = NameNotFoundException.class) + public void testUnbindName() throws Exception { + testLookup(); + InitialContext ctx = new InitialContext(); + ctx.unbind(new CompositeName(JNDI_NAME)); + ctx = new InitialContext(); + ctx.lookup(new CompositeName(JNDI_NAME)); + } + + @Test(expected = NameNotFoundException.class) + public void testLookupFailsName() throws Exception { + StubInitialContextFactory.register(); + + InitialContext ctx = new InitialContext(); + Object obj = ctx.lookup(JNDI_NAME); } } -- 2.31.1