(no commit message)
authorErik Brakkee <erik@brakkee.org>
Tue, 11 May 2010 19:18:07 +0000 (19:18 +0000)
committerErik Brakkee <erik@brakkee.org>
Tue, 11 May 2010 19:18:07 +0000 (19:18 +0000)
security/jpatest/src/test/java/org/wamblee/security/authentication/jpa/UserAdministrationTester.java
test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java
test/enterprise/src/main/java/org/wamblee/support/persistence/DatabaseUtils.java
test/enterprise/src/test/java/org/wamblee/support/jndi/StubInitiaContextFactoryTest.java

index 634e10ed5ddd39f09329a9e619c450b136a7c088..a4917983b7168058b904f00d3ca85be2775f3a85 100644 (file)
@@ -43,6 +43,7 @@ public class UserAdministrationTester {
     private JpaTester jpaTester;
     private Cache<String, User> 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();
index 5300a8b6a5f230ca84ea375b28dcedd03723b50a..d6f90fee5b384ca5c500d69e94bbeb45e236109a 100644 (file)
@@ -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());
     }
 }
index 9ad6c9129251c4cd44c5c62c86c431c42ec4d203..23938768223259fe70ae76d100388cad789cb0ad 100644 (file)
@@ -108,6 +108,7 @@ public class DatabaseUtils {
                 LOG.log(Level.WARNING, "Could not close connection", e);
             }
         }
+        connections.clear();
     }
 
     public IDatabaseTester createDbTester() throws Exception {
index f6a5781042fd68d2b32f9f086ce54d6d60ec10e4..c0638148447e82f53df25df37faccb29f24686cb 100644 (file)
@@ -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);
     }
 }