alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (JndiDestinationResolverTests.java)

This example Spring Framework source code file (JndiDestinationResolverTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

class, destination, destination, destination_name, destination_name, destinationresolver, exception, jndidestinationresolver, jndidestinationresolver, mockcontrol, naming, namingexception, object, session, session

The Spring Framework JndiDestinationResolverTests.java source code

/*
 * 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.jms.support.destination;

import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.test.AssertThrows;

import javax.jms.Destination;
import javax.jms.Session;
import javax.naming.NamingException;

/**
 * Unit tests for the {@link JndiDestinationResolver} class.
 *
 * @author Rick Evans
 */
public final class JndiDestinationResolverTests extends TestCase {

    private static final String DESTINATION_NAME = "foo";

    private static final Destination DESTINATION = new StubTopic();


    public void testHitsCacheSecondTimeThrough() throws Exception {

        MockControl mockSession = MockControl.createControl(Session.class);
        Session session = (Session) mockSession.getMock();
        mockSession.replay();

        JndiDestinationResolver resolver = new OneTimeLookupJndiDestinationResolver();
        Destination destination = resolver.resolveDestinationName(session, DESTINATION_NAME, true);
        assertNotNull(destination);
        assertSame(DESTINATION, destination);

        mockSession.verify();
    }

    public void testDoesNotUseCacheIfCachingIsTurnedOff() throws Exception {

        MockControl mockSession = MockControl.createControl(Session.class);
        Session session = (Session) mockSession.getMock();
        mockSession.replay();

        CountingCannedJndiDestinationResolver resolver
                = new CountingCannedJndiDestinationResolver();
        resolver.setCache(false);
        Destination destination = resolver.resolveDestinationName(session, DESTINATION_NAME, true);
        assertNotNull(destination);
        assertSame(DESTINATION, destination);
        assertEquals(1, resolver.getCallCount());
        
        destination = resolver.resolveDestinationName(session, DESTINATION_NAME, true);
        assertNotNull(destination);
        assertSame(DESTINATION, destination);
        assertEquals(2, resolver.getCallCount());

        mockSession.verify();
    }

    public void testDelegatesToFallbackIfNotResolvedInJndi() throws Exception {
        MockControl mockSession = MockControl.createControl(Session.class);
        Session session = (Session) mockSession.getMock();
        mockSession.replay();

        MockControl mockDestinationResolver = MockControl.createControl(DestinationResolver.class);
        DestinationResolver dynamicResolver = (DestinationResolver) mockDestinationResolver.getMock();
        dynamicResolver.resolveDestinationName(session, DESTINATION_NAME, true);
        mockDestinationResolver.setReturnValue(DESTINATION);
        mockDestinationResolver.replay();

        JndiDestinationResolver resolver = new JndiDestinationResolver() {
            protected Object lookup(String jndiName, Class requiredClass) throws NamingException {
                throw new NamingException();
            }
        };
        resolver.setFallbackToDynamicDestination(true);
        resolver.setDynamicDestinationResolver(dynamicResolver);
        Destination destination = resolver.resolveDestinationName(session, DESTINATION_NAME, true);

        assertNotNull(destination);
        assertSame(DESTINATION, destination);

        mockSession.verify();
        mockDestinationResolver.verify();
    }

    public void testDoesNotDelegateToFallbackIfNotResolvedInJndi() throws Exception {
        MockControl mockSession = MockControl.createControl(Session.class);
        final Session session = (Session) mockSession.getMock();
        mockSession.replay();

        MockControl mockDestinationResolver = MockControl.createControl(DestinationResolver.class);
        DestinationResolver dynamicResolver = (DestinationResolver) mockDestinationResolver.getMock();
        mockDestinationResolver.replay();

        final JndiDestinationResolver resolver = new JndiDestinationResolver() {
            protected Object lookup(String jndiName, Class requiredClass) throws NamingException {
                throw new NamingException();
            }
        };
        resolver.setDynamicDestinationResolver(dynamicResolver);

        new AssertThrows(DestinationResolutionException.class) {
            public void test() throws Exception {
                resolver.resolveDestinationName(session, DESTINATION_NAME, true);
            }
        }.runTest();

        mockSession.verify();
        mockDestinationResolver.verify();
    }


    private static class OneTimeLookupJndiDestinationResolver extends JndiDestinationResolver {

        private boolean called;

        protected Object lookup(String jndiName, Class requiredType) throws NamingException {
            if (called) {
                fail("Must not be delegating to lookup(..), must be resolving from cache.");
            }
            assertEquals(DESTINATION_NAME, jndiName);
            called = true;
            return DESTINATION;
        }
    }

    private static class CountingCannedJndiDestinationResolver extends JndiDestinationResolver {
        private int callCount;

        public int getCallCount() {
            return this.callCount;
        }

        protected Object lookup(String jndiName, Class requiredType) throws NamingException {
            ++this.callCount;
            return DESTINATION;
        }
    }
}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework JndiDestinationResolverTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.