001    /*
002     * Copyright 2002-2006 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.dialog.support;
017    
018    import javax.swing.JComponent;
019    import javax.swing.JLabel;
020    
021    import junit.framework.TestCase;
022    
023    import org.springframework.context.support.ClassPathXmlApplicationContext;
024    import org.springframework.richclient.application.Application;
025    import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
026    import org.springframework.richclient.command.ActionCommand;
027    import org.springframework.richclient.core.DefaultMessage;
028    import org.springframework.richclient.core.Guarded;
029    import org.springframework.richclient.core.Message;
030    import org.springframework.richclient.dialog.AbstractDialogPage;
031    import org.springframework.richclient.dialog.DefaultMessageAreaModel;
032    import org.springframework.richclient.dialog.DialogPage;
033    import org.springframework.richclient.dialog.TitlePane;
034    
035    /**
036     * Tests for {@link DialogPageUtils}.
037     * 
038     * @author Larry Streepy
039     */
040    public class DialogPageUtilsTests extends TestCase {
041    
042        public void setUp() {
043            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
044                    "org/springframework/richclient/dialog/support/generic-application-ctx.xml");
045            Application.load(null);
046            Application app = new Application(new DefaultApplicationLifecycleAdvisor());
047            app.setApplicationContext(applicationContext);
048        }
049    
050        public void testMessageMonitor() {
051            DialogPage page = new TestDialogPage();
052            TestMessagable monitor = new TestMessagable();
053            DialogPageUtils.addMessageMonitor(page, monitor);
054    
055            monitor.resetMessageCount();
056            page.setMessage(new DefaultMessage("a message"));
057            assertEquals("Message text not equal", monitor.getMessage().getMessage(), "a message");
058    
059            page.setMessage(new DefaultMessage("another message"));
060            assertEquals("Message text not equal", monitor.getMessage().getMessage(), "another message");
061    
062            assertEquals("Message count incorrect", 2, monitor.getMessageCount());
063        }
064    
065        public void testPageCompleteAdapter() {
066            TestDialogPage page = new TestDialogPage();
067            Guarded guarded = new TestGuarded();
068    
069            DialogPageUtils.adaptPageCompletetoGuarded(page, guarded);
070    
071            page.setPageComplete(false);
072            assertFalse("guarded should be disabled", guarded.isEnabled());
073    
074            page.setPageComplete(true); // Change it
075            assertTrue("guarded should be enabled", guarded.isEnabled());
076        }
077    
078        public void testStandardViewAdaptsOkCommand() {
079            TestDialogPage page = new TestDialogPage();
080    
081            ActionCommand okCommand = new ActionCommand("okCommand") {
082                protected void doExecuteCommand() {
083                }
084            };
085            ActionCommand cancelCommand = new ActionCommand("cancelCommand") {
086                protected void doExecuteCommand() {
087                }
088            };
089    
090            DialogPageUtils.createStandardView(page, okCommand, cancelCommand);
091    
092            page.setPageComplete(false);
093            assertFalse("okCommand should be disabled", okCommand.isEnabled());
094    
095            page.setPageComplete(true);
096            assertTrue("okCommand should be enabled", okCommand.isEnabled());
097        }
098    
099        public void testCreateTitlePane() {
100            TestDialogPage page = new TestDialogPage();
101    
102            TitlePane titlePane = DialogPageUtils.createTitlePane(page);
103    
104            page.setMessage(new DefaultMessage("test message"));
105            assertEquals("Message text not equal", titlePane.getMessage().getMessage(), "test message");
106        }
107    
108        /**
109         * Class to provide a concrete DialogPage for testing.
110         */
111        private class TestDialogPage extends AbstractDialogPage {
112    
113            public TestDialogPage() {
114                super("MyPageId");
115            }
116    
117            protected JComponent createControl() {
118                return new JLabel();
119            }
120        }
121    
122        /**
123         * Class to inspect Messagable operations.
124         */
125        private class TestMessagable extends DefaultMessageAreaModel {
126    
127            private int msgCount = 0;
128    
129            public void setMessage( Message message ) {
130                msgCount += 1;
131                super.setMessage(message);
132            }
133    
134            public int getMessageCount() {
135                return msgCount;
136            }
137    
138            public void resetMessageCount() {
139                msgCount = 0;
140            }
141        }
142    
143        /**
144         * Class to inspect Guarded operations.
145         */
146        private class TestGuarded implements Guarded {
147            private boolean enabled = false;
148    
149            public boolean isEnabled() {
150                return enabled;
151            }
152    
153            public void setEnabled( boolean enabled ) {
154                this.enabled = enabled;
155            }
156        }
157    }