001/*
002 * Copyright 2002-2019 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.jndi;
018
019import java.util.Map;
020import java.util.concurrent.ConcurrentHashMap;
021
022import javax.naming.NamingException;
023
024import org.springframework.jndi.JndiTemplate;
025
026/**
027 * Simple extension of the JndiTemplate class that always returns a given object.
028 *
029 * <p>Very useful for testing. Effectively a mock object.
030 *
031 * @author Rod Johnson
032 * @author Juergen Hoeller
033 * @deprecated Deprecated as of Spring Framework 5.2 in favor of complete solutions from
034 * third parties such as <a href="https://github.com/h-thurow/Simple-JNDI">Simple-JNDI</a>
035 */
036@Deprecated
037public class ExpectedLookupTemplate extends JndiTemplate {
038
039        private final Map<String, Object> jndiObjects = new ConcurrentHashMap<>(16);
040
041
042        /**
043         * Construct a new JndiTemplate that will always return given objects for
044         * given names. To be populated through {@code addObject} calls.
045         * @see #addObject(String, Object)
046         */
047        public ExpectedLookupTemplate() {
048        }
049
050        /**
051         * Construct a new JndiTemplate that will always return the given object,
052         * but honour only requests for the given name.
053         * @param name the name the client is expected to look up
054         * @param object the object that will be returned
055         */
056        public ExpectedLookupTemplate(String name, Object object) {
057                addObject(name, object);
058        }
059
060
061        /**
062         * Add the given object to the list of JNDI objects that this template will expose.
063         * @param name the name the client is expected to look up
064         * @param object the object that will be returned
065         */
066        public void addObject(String name, Object object) {
067                this.jndiObjects.put(name, object);
068        }
069
070        /**
071         * If the name is the expected name specified in the constructor, return the
072         * object provided in the constructor. If the name is unexpected, a
073         * respective NamingException gets thrown.
074         */
075        @Override
076        public Object lookup(String name) throws NamingException {
077                Object object = this.jndiObjects.get(name);
078                if (object == null) {
079                        throw new NamingException("Unexpected JNDI name '" + name + "': expecting " + this.jndiObjects.keySet());
080                }
081                return object;
082        }
083
084}