001/*
002 * Copyright 2002-2018 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;
021import javax.naming.NamingException;
022
023import org.springframework.jndi.JndiTemplate;
024
025/**
026 * Simple extension of the JndiTemplate class that always returns a given object.
027 *
028 * <p>Very useful for testing. Effectively a mock object.
029 *
030 * @author Rod Johnson
031 * @author Juergen Hoeller
032 */
033public class ExpectedLookupTemplate extends JndiTemplate {
034
035        private final Map<String, Object> jndiObjects = new ConcurrentHashMap<String, Object>(16);
036
037
038        /**
039         * Construct a new JndiTemplate that will always return given objects for
040         * given names. To be populated through {@code addObject} calls.
041         * @see #addObject(String, Object)
042         */
043        public ExpectedLookupTemplate() {
044        }
045
046        /**
047         * Construct a new JndiTemplate that will always return the given object,
048         * but honour only requests for the given name.
049         * @param name the name the client is expected to look up
050         * @param object the object that will be returned
051         */
052        public ExpectedLookupTemplate(String name, Object object) {
053                addObject(name, object);
054        }
055
056
057        /**
058         * Add the given object to the list of JNDI objects that this template will expose.
059         * @param name the name the client is expected to look up
060         * @param object the object that will be returned
061         */
062        public void addObject(String name, Object object) {
063                this.jndiObjects.put(name, object);
064        }
065
066        /**
067         * If the name is the expected name specified in the constructor, return the
068         * object provided in the constructor. If the name is unexpected, a
069         * respective NamingException gets thrown.
070         */
071        @Override
072        public Object lookup(String name) throws NamingException {
073                Object object = this.jndiObjects.get(name);
074                if (object == null) {
075                        throw new NamingException("Unexpected JNDI name '" + name + "': expecting " + this.jndiObjects.keySet());
076                }
077                return object;
078        }
079
080}