1   /*
2    * Copyright 2008 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 java.util.HashMap;
19  import java.util.Map;
20  
21  import javax.swing.JComponent;
22  import javax.swing.JLabel;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * testcase for {@link DefaultViewDescriptor}
28   * 
29   * @author Peter De Bruycker
30   */
31  public class DefaultViewDescriptorTests extends TestCase {
32      public void testConstructor() {
33          DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class);
34  
35          assertEquals("theView", descriptor.getId());
36          assertEquals(TestView.class, descriptor.getViewClass());
37      }
38  
39      public void testViewCreation() {
40          DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class);
41  
42          TestView view = (TestView) descriptor.createPageComponent();
43          assertNotNull(view);
44      }
45  
46      public void testViewCreationWithProperties() {
47          Map<String, Object> viewProperties = new HashMap<String, Object>();
48          viewProperties.put("stringProperty", "test value");
49  
50          DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class, viewProperties);
51  
52          TestView view = (TestView) descriptor.createPageComponent();
53          assertNotNull(view);
54  
55          assertEquals("test value", view.getStringProperty());
56      }
57  
58      public void testSetViewClass() throws Exception {
59          DefaultViewDescriptor descriptor = new DefaultViewDescriptor();
60  
61          descriptor.setId("viewId");
62  
63          Class notAViewClass = String.class;
64  
65          try {
66              descriptor.setViewClass(notAViewClass);
67              fail("Must throw exception");
68          } catch (IllegalArgumentException e) {
69              // test passes
70          }
71  
72      }
73  
74      public static class TestView extends AbstractView {
75  
76          private String stringProperty;
77  
78          @Override
79          protected JComponent createControl() {
80              return new JLabel("test");
81          }
82  
83          public void setStringProperty(String stringProperty) {
84              this.stringProperty = stringProperty;
85          }
86  
87          public String getStringProperty() {
88              return stringProperty;
89          }
90  
91      }
92  }