001    /*
002     * Copyright 2002-2004 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;
017    
018    import javax.swing.JTextField;
019    
020    import org.springframework.richclient.application.support.DefaultApplicationServices;
021    import org.springframework.richclient.test.SpringRichTestCase;
022    import org.springframework.rules.Rules;
023    import org.springframework.rules.support.DefaultRulesSource;
024    
025    /**
026     * @author Peter De Bruycker
027     */
028    public class InputApplicationDialogTests extends SpringRichTestCase {
029        private static final String BUSINESS_FIELD = "name";
030    
031        private BusinessObject businessObject;
032    
033        protected void doSetUp() {
034            // create business object
035            businessObject = new BusinessObject();
036        }
037    
038        public void testInitialEnabledState() {
039            InputApplicationDialog createDialog = createDialog(businessObject, BUSINESS_FIELD);
040                    assertEnabledStateReflectsFormModelState(createDialog);
041    
042            businessObject.setName("test");
043            assertEnabledStateReflectsFormModelState(createDialog);
044        }
045    
046        private void assertEnabledStateReflectsFormModelState(InputApplicationDialog dialog) {
047            assertEquals(dialog.getFormModel().getValidationResults().getHasErrors(), !dialog.isEnabled());
048        }
049    
050        private InputApplicationDialog createDialog(BusinessObject businessObject, String field) {
051            InputApplicationDialog dialog = new InputApplicationDialog(businessObject, BUSINESS_FIELD);
052            dialog.createDialog();
053    
054            return dialog;
055        }
056    
057        public void testConstructor() {
058            InputApplicationDialog dialog = new InputApplicationDialog(businessObject, BUSINESS_FIELD);
059    
060            assertNotNull("No FormModel created", dialog.getFormModel());
061            assertEquals("BusinessObject not set on FormModel", businessObject, dialog.getFormModel().getFormObject());
062    
063            assertNotNull("No inputField created", dialog.getInputField());
064            assertTrue("Default inputField not a JTextField", dialog.getInputField() instanceof JTextField);
065        }
066    
067        public void testEnabledByUserAction() {
068            InputApplicationDialog dialog = createDialog(businessObject, BUSINESS_FIELD);
069    
070            assertFalse(dialog.isEnabled());
071    
072            dialog.getFormModel().getValueModel(BUSINESS_FIELD).setValue("test");
073            assertEnabledStateReflectsFormModelState(dialog);
074    
075            dialog.getFormModel().getValueModel(BUSINESS_FIELD).setValue("");
076            assertEnabledStateReflectsFormModelState(dialog);
077        }
078    
079        /**
080         * May be implemented in subclasses that need to register services with the global
081         * application services instance.
082         */
083        protected void registerAdditionalServices( DefaultApplicationServices applicationServices ) {
084            applicationServices.setRulesSource(new BusinessRulesSource());
085        }
086    
087        private class BusinessRulesSource extends DefaultRulesSource {
088            public BusinessRulesSource() {
089                Rules rules = new Rules(BusinessObject.class);
090                rules.add(BUSINESS_FIELD, rules.required());
091                addRules(rules);
092            }
093        }
094    
095        public class BusinessObject {
096            private String name;
097    
098            public String getName() {
099                return name;
100            }
101    
102            public void setName(String name) {
103                this.name = name;
104            }
105        }
106    }