1 16 17 package org.springframework.jndi; 18 19 import javax.naming.Context ; 20 import javax.naming.NamingException ; 21 22 import junit.framework.TestCase; 23 import org.easymock.MockControl; 24 25 30 public class JndiTemplateTests extends TestCase { 31 32 public void testLookupSucceeds() throws Exception { 33 Object o = new Object (); 34 String name = "foo"; 35 MockControl mc = MockControl.createControl(Context .class); 36 final Context mock = (Context ) mc.getMock(); 37 mock.lookup(name); 38 mc.setReturnValue(o); 39 mock.close(); 40 mc.setVoidCallable(1); 41 mc.replay(); 42 43 JndiTemplate jt = new JndiTemplate() { 44 protected Context createInitialContext() throws NamingException { 45 return mock; 46 } 47 }; 48 49 Object o2 = jt.lookup(name); 50 assertEquals(o, o2); 51 mc.verify(); 52 } 53 54 public void testLookupFails() throws Exception { 55 NamingException ne = new NamingException (); 56 String name = "foo"; 57 MockControl mc = MockControl.createControl(Context .class); 58 final Context mock = (Context ) mc.getMock(); 59 mock.lookup(name); 60 mc.setThrowable(ne); 61 mock.close(); 62 mc.setVoidCallable(1); 63 mc.replay(); 64 65 JndiTemplate jt = new JndiTemplate() { 66 protected Context createInitialContext() throws NamingException { 67 return mock; 68 } 69 }; 70 71 try { 72 jt.lookup(name); 73 fail("Should have thrown NamingException"); 74 } 75 catch (NamingException ex) { 76 } 78 mc.verify(); 79 } 80 81 public void testBind() throws Exception { 82 Object o = new Object (); 83 String name = "foo"; 84 MockControl mc = MockControl.createControl(Context .class); 85 final Context mock = (Context ) mc.getMock(); 86 mock.bind(name, o); 87 mc.setVoidCallable(1); 88 mock.close(); 89 mc.setVoidCallable(1); 90 mc.replay(); 91 92 JndiTemplate jt = new JndiTemplate() { 93 protected Context createInitialContext() throws NamingException { 94 return mock; 95 } 96 }; 97 98 jt.bind(name, o); 99 mc.verify(); 100 } 101 102 public void testRebind() throws Exception { 103 Object o = new Object (); 104 String name = "foo"; 105 MockControl mc = MockControl.createControl(Context .class); 106 final Context mock = (Context ) mc.getMock(); 107 mock.rebind(name, o); 108 mc.setVoidCallable(1); 109 mock.close(); 110 mc.setVoidCallable(1); 111 mc.replay(); 112 113 JndiTemplate jt = new JndiTemplate() { 114 protected Context createInitialContext() throws NamingException { 115 return mock; 116 } 117 }; 118 119 jt.rebind(name, o); 120 mc.verify(); 121 } 122 123 public void testUnbind() throws Exception { 124 String name = "something"; 125 MockControl mc = MockControl.createControl(Context .class); 126 final Context mock = (Context ) mc.getMock(); 127 mock.unbind(name); 128 mc.setVoidCallable(1); 129 mock.close(); 130 mc.setVoidCallable(1); 131 mc.replay(); 132 133 JndiTemplate jt = new JndiTemplate() { 134 protected Context createInitialContext() throws NamingException { 135 return mock; 136 } 137 }; 138 139 jt.unbind(name); 140 mc.verify(); 141 } 142 143 } 144 | Popular Tags |