1   /*
2    * Copyright 2002-2004 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;
17  
18  import javax.swing.JTextField;
19  
20  import org.springframework.richclient.application.support.DefaultApplicationServices;
21  import org.springframework.richclient.test.SpringRichTestCase;
22  import org.springframework.rules.Rules;
23  import org.springframework.rules.support.DefaultRulesSource;
24  
25  /**
26   * @author Peter De Bruycker
27   */
28  public class InputApplicationDialogTests extends SpringRichTestCase {
29      private static final String BUSINESS_FIELD = "name";
30  
31      private BusinessObject businessObject;
32  
33      protected void doSetUp() {
34          // create business object
35          businessObject = new BusinessObject();
36      }
37  
38      public void testInitialEnabledState() {
39          InputApplicationDialog createDialog = createDialog(businessObject, BUSINESS_FIELD);
40  		assertEnabledStateReflectsFormModelState(createDialog);
41  
42          businessObject.setName("test");
43          assertEnabledStateReflectsFormModelState(createDialog);
44      }
45  
46      private void assertEnabledStateReflectsFormModelState(InputApplicationDialog dialog) {
47          assertEquals(dialog.getFormModel().getValidationResults().getHasErrors(), !dialog.isEnabled());
48      }
49  
50      private InputApplicationDialog createDialog(BusinessObject businessObject, String field) {
51          InputApplicationDialog dialog = new InputApplicationDialog(businessObject, BUSINESS_FIELD);
52          dialog.createDialog();
53  
54          return dialog;
55      }
56  
57      public void testConstructor() {
58          InputApplicationDialog dialog = new InputApplicationDialog(businessObject, BUSINESS_FIELD);
59  
60          assertNotNull("No FormModel created", dialog.getFormModel());
61          assertEquals("BusinessObject not set on FormModel", businessObject, dialog.getFormModel().getFormObject());
62  
63          assertNotNull("No inputField created", dialog.getInputField());
64          assertTrue("Default inputField not a JTextField", dialog.getInputField() instanceof JTextField);
65      }
66  
67      public void testEnabledByUserAction() {
68          InputApplicationDialog dialog = createDialog(businessObject, BUSINESS_FIELD);
69  
70          assertFalse(dialog.isEnabled());
71  
72          dialog.getFormModel().getValueModel(BUSINESS_FIELD).setValue("test");
73          assertEnabledStateReflectsFormModelState(dialog);
74  
75          dialog.getFormModel().getValueModel(BUSINESS_FIELD).setValue("");
76          assertEnabledStateReflectsFormModelState(dialog);
77      }
78  
79      /**
80       * May be implemented in subclasses that need to register services with the global
81       * application services instance.
82       */
83      protected void registerAdditionalServices( DefaultApplicationServices applicationServices ) {
84          applicationServices.setRulesSource(new BusinessRulesSource());
85      }
86  
87      private class BusinessRulesSource extends DefaultRulesSource {
88          public BusinessRulesSource() {
89              Rules rules = new Rules(BusinessObject.class);
90              rules.add(BUSINESS_FIELD, rules.required());
91              addRules(rules);
92          }
93      }
94  
95      public class BusinessObject {
96          private String name;
97  
98          public String getName() {
99              return name;
100         }
101 
102         public void setName(String name) {
103             this.name = name;
104         }
105     }
106 }