001    /*
002     * Copyright 2008 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 java.util.HashMap;
019    import java.util.Map;
020    
021    import javax.swing.JComponent;
022    import javax.swing.JLabel;
023    
024    import junit.framework.TestCase;
025    
026    /**
027     * testcase for {@link DefaultViewDescriptor}
028     * 
029     * @author Peter De Bruycker
030     */
031    public class DefaultViewDescriptorTests extends TestCase {
032        public void testConstructor() {
033            DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class);
034    
035            assertEquals("theView", descriptor.getId());
036            assertEquals(TestView.class, descriptor.getViewClass());
037        }
038    
039        public void testViewCreation() {
040            DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class);
041    
042            TestView view = (TestView) descriptor.createPageComponent();
043            assertNotNull(view);
044        }
045    
046        public void testViewCreationWithProperties() {
047            Map<String, Object> viewProperties = new HashMap<String, Object>();
048            viewProperties.put("stringProperty", "test value");
049    
050            DefaultViewDescriptor descriptor = new DefaultViewDescriptor("theView", TestView.class, viewProperties);
051    
052            TestView view = (TestView) descriptor.createPageComponent();
053            assertNotNull(view);
054    
055            assertEquals("test value", view.getStringProperty());
056        }
057    
058        public void testSetViewClass() throws Exception {
059            DefaultViewDescriptor descriptor = new DefaultViewDescriptor();
060    
061            descriptor.setId("viewId");
062    
063            Class notAViewClass = String.class;
064    
065            try {
066                descriptor.setViewClass(notAViewClass);
067                fail("Must throw exception");
068            } catch (IllegalArgumentException e) {
069                // test passes
070            }
071    
072        }
073    
074        public static class TestView extends AbstractView {
075    
076            private String stringProperty;
077    
078            @Override
079            protected JComponent createControl() {
080                return new JLabel("test");
081            }
082    
083            public void setStringProperty(String stringProperty) {
084                this.stringProperty = stringProperty;
085            }
086    
087            public String getStringProperty() {
088                return stringProperty;
089            }
090    
091        }
092    }