1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.application.support;
17  
18  import junit.framework.Assert;
19  import junit.framework.TestCase;
20  import org.springframework.beans.MutablePropertyValues;
21  import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
22  import org.springframework.context.support.StaticApplicationContext;
23  import org.springframework.richclient.application.ViewDescriptor;
24  
25  import javax.swing.*;
26  
27  
28  /**
29   * Provides a suite of unit tests for the {@link BeanFactoryViewDescriptorRegistry} class.
30   *
31   * @author Kevin Stembridge
32   * @since 0.3
33   *
34   */
35  public class BeanFactoryViewDescriptorRegistryTests extends TestCase {
36  
37      /**
38       * Test method for {@link BeanFactoryViewDescriptorRegistry#getViewDescriptor(java.lang.String)}.
39       */
40      public final void testGetViewDescriptor() {
41          
42          BeanFactoryViewDescriptorRegistry registry = new BeanFactoryViewDescriptorRegistry();
43          StaticApplicationContext appCtx = new StaticApplicationContext();
44          registry.setApplicationContext(appCtx);
45  
46          MutablePropertyValues mpv = new MutablePropertyValues();
47          mpv.addPropertyValue("viewClass", NullView.class);
48          appCtx.registerSingleton("view1", DefaultViewDescriptor.class, mpv);
49          appCtx.registerSingleton("view2", DefaultViewDescriptor.class, mpv);
50          appCtx.registerSingleton("bogusView", String.class);
51          
52          Assert.assertNotNull(registry.getViewDescriptor("view1"));
53          Assert.assertNotNull(registry.getViewDescriptor("view2"));
54          
55          Assert.assertNull("Should return null when viewName not found", registry.getViewDescriptor("bogus"));
56          
57          try {
58              registry.getViewDescriptor("bogusView");
59              Assert.fail("Should have thrown BeanNotOfRequiredTypeException");
60          }
61          catch (BeanNotOfRequiredTypeException e) {
62              //do nothing, test succeeded
63          }
64          
65      }
66  
67      /**
68       * Performs the following assertions on the 
69       * {@link BeanFactoryViewDescriptorRegistry#getViewDescriptors()} method:
70       * 
71       * <ul>
72       * <li>The method does not return null if there are no view descriptors in the underlying 
73       * registry</li>
74       * <li>The correct number of descriptors are returned.</li>
75       * </ul>
76       */
77      public void testGetViewDescriptors() {
78          
79          BeanFactoryViewDescriptorRegistry registry = new BeanFactoryViewDescriptorRegistry();
80          StaticApplicationContext appCtx = new StaticApplicationContext();
81          registry.setApplicationContext(appCtx);
82          
83          ViewDescriptor[] viewDescriptors = registry.getViewDescriptors();
84          
85          Assert.assertNotNull("View descriptor array should never be null", viewDescriptors);
86          Assert.assertEquals("Should be no view descriptors in the array", 0, viewDescriptors.length);
87  
88          MutablePropertyValues mpv = new MutablePropertyValues();
89          mpv.addPropertyValue("viewClass", NullView.class);
90          appCtx.registerSingleton("view1", DefaultViewDescriptor.class, mpv);
91          appCtx.registerSingleton("view2", DefaultViewDescriptor.class, mpv);
92          
93          viewDescriptors = registry.getViewDescriptors();
94          Assert.assertEquals("Should be 2 view descriptors in the array", 2, viewDescriptors.length);
95          
96      }
97  
98      /**
99       * 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 }