JndiTemplateTests.java 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jndi;

19 20 21
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;

22 23 24
import javax.naming.Context;
import javax.naming.NameNotFoundException;

25
import org.junit.Test;
26 27 28 29

/**
 * @author Rod Johnson
 * @author Juergen Hoeller
30
 * @author Chris Beams
31 32
 * @since 08.07.2003
 */
33
public class JndiTemplateTests {
34

35
    @Test
36 37 38
	public void testLookupSucceeds() throws Exception {
		Object o = new Object();
		String name = "foo";
39 40 41 42
		final Context context = createMock(Context.class);
		expect(context.lookup(name)).andReturn(o);
		context.close();
		replay(context);
43 44 45

		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
46
				return context;
47 48 49 50 51
			}
		};

		Object o2 = jt.lookup(name);
		assertEquals(o, o2);
52
		verify(context);
53 54
	}
	
55
    @Test
56 57 58
	public void testLookupFails() throws Exception {
		NameNotFoundException ne = new NameNotFoundException();
		String name = "foo";
59 60 61 62
		final Context context = createMock(Context.class);
		expect(context.lookup(name)).andThrow(ne);
		context.close();
		replay(context);
63 64 65

		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
66
				return context;
67 68 69 70 71 72 73 74 75 76
			}
		};

		try {
			jt.lookup(name);
			fail("Should have thrown NamingException");
		}
		catch (NameNotFoundException ex) {
			// Ok
		}
77
		verify(context);
78 79
	}
	
80
    @Test
81 82
	public void testLookupReturnsNull() throws Exception {
		String name = "foo";
83 84 85 86
		final Context context = createMock(Context.class);
		expect(context.lookup(name)).andReturn(null);
		context.close();
		replay(context);
87 88 89

		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
90
				return context;
91 92 93 94 95 96 97 98 99 100
			}
		};

		try {
			jt.lookup(name);
			fail("Should have thrown NamingException");
		}
		catch (NameNotFoundException ex) {
			// Ok
		}
101
		verify(context);
102 103
	}

104
    @Test
105 106 107
	public void testLookupFailsWithTypeMismatch() throws Exception {
		Object o = new Object();
		String name = "foo";
108 109 110 111
		final Context context = createMock(Context.class);
		expect(context.lookup(name)).andReturn(o);
		context.close();
		replay(context);
112 113 114

		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
115
				return context;
116 117 118 119 120 121 122 123 124 125
			}
		};

		try {
			jt.lookup(name, String.class);
			fail("Should have thrown TypeMismatchNamingException");
		}
		catch (TypeMismatchNamingException ex) {
			// Ok
		}
126
		verify(context);
127 128
	}

129
    @Test
130 131 132
	public void testBind() throws Exception {
		Object o = new Object();
		String name = "foo";
133 134 135 136
		final Context context = createMock(Context.class);
		context.bind(name, o);
		context.close();
		replay(context);
137 138 139
		
		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
140
				return context;
141 142 143 144
			}
		};
		
		jt.bind(name, o);
145
		verify(context);
146 147
	}
	
148
    @Test
149 150 151
	public void testRebind() throws Exception {
		Object o = new Object();
		String name = "foo";
152 153 154 155
		final Context context = createMock(Context.class);
		context.rebind(name, o);
		context.close();
		replay(context);
156 157 158

		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
159
				return context;
160 161 162 163
			}
		};

		jt.rebind(name, o);
164
		verify(context);
165 166
	}

167
    @Test
168 169
	public void testUnbind() throws Exception {
		String name = "something";
170 171 172 173
		final Context context = createMock(Context.class);
		context.unbind(name);
		context.close();
		replay(context);
174 175 176
	
		JndiTemplate jt = new JndiTemplate() {
			protected Context createInitialContext() {
177
				return context;
178 179 180 181
			}
		};
	
		jt.unbind(name);
182
		verify(context);
183 184 185
	}

}