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.binding.value.swing;
17  
18  import java.text.ParseException;
19  
20  import javax.swing.JFormattedTextField;
21  import javax.swing.JFormattedTextField.AbstractFormatter;
22  import javax.swing.JFormattedTextField.AbstractFormatterFactory;
23  
24  import org.springframework.binding.support.TestPropertyChangeListener;
25  import org.springframework.binding.value.ValueModel;
26  import org.springframework.binding.value.support.ValueHolder;
27  import org.springframework.richclient.test.SpringRichTestCase;
28  
29  /**
30   * Test cases for {@link FormattedTextFieldAdapter}
31   * 
32   * @author Oliver Hutchison
33   */
34  public class FormattedTextFieldAdapterTests extends SpringRichTestCase {
35  
36      private ValueModel valueModel;
37  
38      private TestPropertyChangeListener valueListener;
39  
40      private TestableJTextComponent comp;
41  
42      public void doSetUp() throws Exception {
43          valueModel = new ValueHolder("");
44          valueListener = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
45          valueModel.addValueChangeListener(valueListener);
46          comp = new TestableJTextComponent();
47          comp.setFormatterFactory(new OnlyAlowLowerCaseFormatterFactory());
48          new FormattedTextFieldAdapter(comp, valueModel, ValueCommitPolicy.AS_YOU_TYPE);
49      }
50  
51      public void testComponentChangeUpdatesValueModel() {
52          comp.setValue("newvalue"); 
53          assertEquals("newvalue", valueModel.getValue());
54          assertEquals(1, valueListener.eventCount());
55  
56          comp.setText("newervalue");
57          assertEquals("newervalue", valueModel.getValue());
58          assertEquals(3, valueListener.eventCount());
59      }
60  
61      public void testValueModelChangeUpdatesComponent() {
62          valueModel.setValue("newvalue");
63          assertEquals("newvalue", comp.getValue());
64          assertEquals(1, valueListener.eventCount());
65      }
66  
67      public void testTypingUpdatesValueModel() {
68          comp.typeText("a");
69          assertEquals("a", valueModel.getValue());
70          assertEquals(1, valueListener.eventCount());
71  
72          valueListener.reset();
73          comp.typeText("bc");
74          assertEquals("abc", valueModel.getValue());
75          assertEquals(2, valueListener.eventCount());
76  
77          valueListener.reset();
78          comp.setCaretPosition(1);
79          comp.typeText("d");
80          assertEquals("adbc", valueModel.getValue());
81          assertEquals(1, valueListener.eventCount());
82          
83          // type an invalid char
84          valueListener.reset();
85          comp.setCaretPosition(1);
86          comp.typeText("E");
87          assertEquals("adbc", valueModel.getValue());
88          assertEquals(0, valueListener.eventCount());
89  
90          valueListener.reset();
91          comp.setCaretPosition(2);
92          comp.typeBackSpace();
93          comp.typeText("f");
94          assertEquals("afdbc", valueModel.getValue());
95          assertEquals(1, valueListener.eventCount());
96      }
97      
98      private static final class OnlyAlowLowerCaseFormatterFactory extends AbstractFormatterFactory {
99          public AbstractFormatter getFormatter(JFormattedTextField tf) {
100             return new AbstractFormatter() {
101 
102                 public Object stringToValue(String text) throws ParseException {
103                     if (text != null && !text.equals(text.toLowerCase())) {
104                         throw new ParseException(text, 0);
105                     }
106                     return text;
107                 }
108 
109                 public String valueToString(Object value) throws ParseException {
110                     return (String) value;
111                 }
112             };
113         }
114     }
115 }