1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35 public class BeanFactoryViewDescriptorRegistryTests extends TestCase {
36
37
38
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
63 }
64
65 }
66
67
68
69
70
71
72
73
74
75
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
100
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
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 }