001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.application.support;
017    
018    import junit.framework.Assert;
019    import junit.framework.TestCase;
020    import org.springframework.beans.MutablePropertyValues;
021    import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
022    import org.springframework.context.support.StaticApplicationContext;
023    import org.springframework.richclient.application.ViewDescriptor;
024    
025    import javax.swing.*;
026    
027    
028    /**
029     * Provides a suite of unit tests for the {@link BeanFactoryViewDescriptorRegistry} class.
030     *
031     * @author Kevin Stembridge
032     * @since 0.3
033     *
034     */
035    public class BeanFactoryViewDescriptorRegistryTests extends TestCase {
036    
037        /**
038         * Test method for {@link BeanFactoryViewDescriptorRegistry#getViewDescriptor(java.lang.String)}.
039         */
040        public final void testGetViewDescriptor() {
041            
042            BeanFactoryViewDescriptorRegistry registry = new BeanFactoryViewDescriptorRegistry();
043            StaticApplicationContext appCtx = new StaticApplicationContext();
044            registry.setApplicationContext(appCtx);
045    
046            MutablePropertyValues mpv = new MutablePropertyValues();
047            mpv.addPropertyValue("viewClass", NullView.class);
048            appCtx.registerSingleton("view1", DefaultViewDescriptor.class, mpv);
049            appCtx.registerSingleton("view2", DefaultViewDescriptor.class, mpv);
050            appCtx.registerSingleton("bogusView", String.class);
051            
052            Assert.assertNotNull(registry.getViewDescriptor("view1"));
053            Assert.assertNotNull(registry.getViewDescriptor("view2"));
054            
055            Assert.assertNull("Should return null when viewName not found", registry.getViewDescriptor("bogus"));
056            
057            try {
058                registry.getViewDescriptor("bogusView");
059                Assert.fail("Should have thrown BeanNotOfRequiredTypeException");
060            }
061            catch (BeanNotOfRequiredTypeException e) {
062                //do nothing, test succeeded
063            }
064            
065        }
066    
067        /**
068         * Performs the following assertions on the 
069         * {@link BeanFactoryViewDescriptorRegistry#getViewDescriptors()} method:
070         * 
071         * <ul>
072         * <li>The method does not return null if there are no view descriptors in the underlying 
073         * registry</li>
074         * <li>The correct number of descriptors are returned.</li>
075         * </ul>
076         */
077        public void testGetViewDescriptors() {
078            
079            BeanFactoryViewDescriptorRegistry registry = new BeanFactoryViewDescriptorRegistry();
080            StaticApplicationContext appCtx = new StaticApplicationContext();
081            registry.setApplicationContext(appCtx);
082            
083            ViewDescriptor[] viewDescriptors = registry.getViewDescriptors();
084            
085            Assert.assertNotNull("View descriptor array should never be null", viewDescriptors);
086            Assert.assertEquals("Should be no view descriptors in the array", 0, viewDescriptors.length);
087    
088            MutablePropertyValues mpv = new MutablePropertyValues();
089            mpv.addPropertyValue("viewClass", NullView.class);
090            appCtx.registerSingleton("view1", DefaultViewDescriptor.class, mpv);
091            appCtx.registerSingleton("view2", DefaultViewDescriptor.class, mpv);
092            
093            viewDescriptors = registry.getViewDescriptors();
094            Assert.assertEquals("Should be 2 view descriptors in the array", 2, viewDescriptors.length);
095            
096        }
097    
098        /**
099         * Confirms that an IllegalArgumentException is thrown if a null viewName is passed to the
100         * {@link BeanFactoryViewDescriptorRegistry#getViewDescriptor(String)} method.
101         */
102        public void testForNullViewId() {
103            
104            try {
105                new BeanFactoryViewDescriptorRegistry().getViewDescriptor(null);
106                Assert.fail("Should have thrown an IllegalArgumentException for null view ID");
107            }
108            catch (IllegalArgumentException e) {
109                //do nothing, test succeeded
110            }
111            
112        }
113    
114        private class NullView extends AbstractView
115        {
116    
117            protected JComponent createControl()
118            {
119                return new JPanel();
120            }
121        }
122    
123    }