1   /*
2    * Copyright 2002-2006 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.dialog.support;
17  
18  import javax.swing.JComponent;
19  import javax.swing.JLabel;
20  
21  import junit.framework.TestCase;
22  
23  import org.springframework.context.support.ClassPathXmlApplicationContext;
24  import org.springframework.richclient.application.Application;
25  import org.springframework.richclient.application.config.DefaultApplicationLifecycleAdvisor;
26  import org.springframework.richclient.command.ActionCommand;
27  import org.springframework.richclient.core.DefaultMessage;
28  import org.springframework.richclient.core.Guarded;
29  import org.springframework.richclient.core.Message;
30  import org.springframework.richclient.dialog.AbstractDialogPage;
31  import org.springframework.richclient.dialog.DefaultMessageAreaModel;
32  import org.springframework.richclient.dialog.DialogPage;
33  import org.springframework.richclient.dialog.TitlePane;
34  
35  /**
36   * Tests for {@link DialogPageUtils}.
37   * 
38   * @author Larry Streepy
39   */
40  public class DialogPageUtilsTests extends TestCase {
41  
42      public void setUp() {
43          ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
44                  "org/springframework/richclient/dialog/support/generic-application-ctx.xml");
45          Application.load(null);
46          Application app = new Application(new DefaultApplicationLifecycleAdvisor());
47          app.setApplicationContext(applicationContext);
48      }
49  
50      public void testMessageMonitor() {
51          DialogPage page = new TestDialogPage();
52          TestMessagable monitor = new TestMessagable();
53          DialogPageUtils.addMessageMonitor(page, monitor);
54  
55          monitor.resetMessageCount();
56          page.setMessage(new DefaultMessage("a message"));
57          assertEquals("Message text not equal", monitor.getMessage().getMessage(), "a message");
58  
59          page.setMessage(new DefaultMessage("another message"));
60          assertEquals("Message text not equal", monitor.getMessage().getMessage(), "another message");
61  
62          assertEquals("Message count incorrect", 2, monitor.getMessageCount());
63      }
64  
65      public void testPageCompleteAdapter() {
66          TestDialogPage page = new TestDialogPage();
67          Guarded guarded = new TestGuarded();
68  
69          DialogPageUtils.adaptPageCompletetoGuarded(page, guarded);
70  
71          page.setPageComplete(false);
72          assertFalse("guarded should be disabled", guarded.isEnabled());
73  
74          page.setPageComplete(true); // Change it
75          assertTrue("guarded should be enabled", guarded.isEnabled());
76      }
77  
78      public void testStandardViewAdaptsOkCommand() {
79          TestDialogPage page = new TestDialogPage();
80  
81          ActionCommand okCommand = new ActionCommand("okCommand") {
82              protected void doExecuteCommand() {
83              }
84          };
85          ActionCommand cancelCommand = new ActionCommand("cancelCommand") {
86              protected void doExecuteCommand() {
87              }
88          };
89  
90          DialogPageUtils.createStandardView(page, okCommand, cancelCommand);
91  
92          page.setPageComplete(false);
93          assertFalse("okCommand should be disabled", okCommand.isEnabled());
94  
95          page.setPageComplete(true);
96          assertTrue("okCommand should be enabled", okCommand.isEnabled());
97      }
98  
99      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 }