/*
 * Copyright 2002-2005 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.mock.jndi;

import javax.naming.NamingException;

import org.springframework.jndi.JndiTemplate;

/**
 * Simple implementation of JndiTemplate interface that always returns
 * a given object. Very useful for testing. Effectively a mock object.
 *
 * @author Rod Johnson
 * @see org.springframework.jdbc.datasource.DriverManagerDataSource
 */
public class ExpectedLookupTemplate extends JndiTemplate {

	private final String name;

	private final Object object;

	/**
	 * Construct a new JndiTemplate that will always return the
	 * given object, but honour only requests for the given name.
	 * @param name the name the client is expected to look up
	 * @param object the object that will be returned
	 */
	public ExpectedLookupTemplate(String name, Object object) {
		this.name = name;
		this.object = object;
	}

	/**
	 * If the name is the expected name specified in the constructor,
	 * return the object provided in the constructor. If the name is
	 * unexpected, a respective NamingException gets thrown.
	 */
	public Object lookup(String name) throws NamingException {
		if (!name.equals(this.name)) {
			throw new NamingException("Unexpected JNDI name '" + name + "' - expecting '" + this.name + "'");
		}
		return this.object;
	}

}

@@ -658,16 +658,95 @@
 .jndi;%0A%0A
+import java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 import j
@@ -1002,71 +1002,30 @@
  * @
-see org.springframework.jdbc.datasource.DriverManagerDataSource
+author Juergen Hoeller
 %0A */
@@ -1104,51 +1104,301 @@
 nal 
-String name;%0A%0A%09private final Object object;
+Map jndiObjects = Collections.synchronizedMap(new HashMap());%0A%0A%0A%09/**%0A%09 * Construct a new JndiTemplate that will always return given objects%0A%09 * for given names. To be populated through %3Ccode%3EaddObject%3C/code%3E calls.%0A%09 * @see #addObject(String, Object)%0A%09 */%0A%09public ExpectedLookupTemplate() %7B%0A%09%7D
 %0A%0A%09/
@@ -1706,54 +1706,342 @@
 %7B%0A%09%09
-this.name = name;%0A%09%09this.object =
+addObject(name, object);%0A%09%7D%0A%0A%09/**%0A%09 * Add the given object to the list of JNDI objects that this%0A%09 * template will expose.%0A%09 * @param name the name the client is expected to look up%0A%09 * @param object the object that will be returned%0A%09 */%0A%09public void addObject(String name, Object object) %7B%0A%09%09this.jndiObjects.put(name,
  object
+)
 ;%0A%09%7D%0A%0A
+%0A
 %09/**
@@ -2303,35 +2303,72 @@
 %7B%0A%09%09
-if (!name.equals(this.name)
+Object object = this.jndiObjects.get(name);%0A%09%09if (object == null
 ) %7B%0A
@@ -2436,10 +2436,9 @@
 + %22'
- -
+:
  exp
@@ -2444,17 +2444,16 @@
 pecting 
-'
 %22 + this
@@ -2457,18 +2457,28 @@
 his.
-name + %22'%22
+jndiObjects.keySet()
 );%0A%09
@@ -2489,21 +2489,16 @@
 %09return 
-this.
 object;%0A

support multiple JNDI name/object pairs