001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.Rectangle;
024: import java.text.NumberFormat;
025: import java.text.ParseException;
026: import javax.swing.JFormattedTextField;
027: import javax.swing.SwingTestCase;
028: import javax.swing.SwingUtilities;
029: import junit.framework.AssertionFailedError;
030:
031: public class DefaultFormatterTest extends SwingTestCase {
032: DefaultFormatter formatter;
033:
034: JFormattedTextField ftf;
035:
036: boolean bWasException;
037:
038: String message;
039:
040: AssertionFailedError assertion;
041:
042: @Override
043: protected void setUp() throws Exception {
044: super .setUp();
045: formatter = new DefaultFormatter();
046: ftf = new JFormattedTextField();
047: bWasException = false;
048: message = null;
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: super .tearDown();
054: }
055:
056: public void testClone() {
057: Object clone = null;
058: formatter.install(ftf);
059: formatter.setValueClass(Integer.class);
060: formatter.setAllowsInvalid(false);
061: formatter.setCommitsOnValidEdit(true);
062: formatter.setOverwriteMode(false);
063: try {
064: clone = formatter.clone();
065: } catch (CloneNotSupportedException e) {
066: assertFalse("Unexpected exception: " + e.getMessage(), true);
067: }
068: assertTrue(clone instanceof DefaultFormatter);
069: DefaultFormatter form = (DefaultFormatter) clone;
070: assertEquals(Integer.class, form.getValueClass());
071: assertFalse(form.getAllowsInvalid());
072: assertTrue(form.getCommitsOnValidEdit());
073: assertFalse(form.getOverwriteMode());
074: }
075:
076: public void testGetDocumentFilter() {
077: assertNotNull(formatter.getDocumentFilter());
078: }
079:
080: public void testValueToString() {
081: Object value = Color.RED;
082: try {
083: assertEquals(value.toString(), formatter
084: .valueToString(value));
085: value = "just value";
086: assertEquals(value.toString(), formatter
087: .valueToString(value));
088: value = new Integer(123);
089: assertEquals(value.toString(), formatter
090: .valueToString(value));
091: } catch (ParseException e) {
092: assertTrue("Unexpected exception: " + e.getMessage(), false);
093: }
094: }
095:
096: public void testDefaultFormatter() {
097: assertNull(formatter.getValueClass());
098: assertTrue(formatter.getAllowsInvalid());
099: assertFalse(formatter.getCommitsOnValidEdit());
100: assertTrue(formatter.getOverwriteMode());
101: }
102:
103: public void testSetGetAllowsInvalid_NumberFormatter() {
104: String text = "444555666";
105: ftf.setValue(new Integer(334580));
106: NumberFormatter numFormatter = (NumberFormatter) ftf
107: .getFormatter();
108: ftf.setText(text);
109: assertEquals(text, ftf.getText());
110: text = "111222333";
111: numFormatter.setAllowsInvalid(false);
112: ftf.setText(text);
113: SwingUtilities.invokeLater(new Runnable() {
114: public void run() {
115: try {
116: assertEquals(NumberFormat.getNumberInstance()
117: .format(new Integer(111222333)), ftf
118: .getText());
119: } catch (AssertionFailedError e) {
120: assertion = e;
121: }
122: }
123: });
124: if (assertion != null) {
125: throw assertion;
126: }
127: }
128:
129: public void testSetGetCommitsOnValidEdit_NumberFormatter() {
130: ftf.setValue(new Integer(334580));
131: formatter = (DefaultFormatter) ftf.getFormatter();
132: ftf.setText("567");
133: assertEquals(new Integer(334580), ftf.getValue());
134: formatter.setCommitsOnValidEdit(true);
135: assertTrue(formatter.getCommitsOnValidEdit());
136: ftf.setText("123456");
137: SwingUtilities.invokeLater(new Runnable() {
138: public void run() {
139: try {
140: assertEquals(new Long(123456), ftf.getValue());
141: } catch (AssertionFailedError e) {
142: assertion = e;
143: }
144: }
145: });
146: if (assertion != null) {
147: throw assertion;
148: }
149: }
150:
151: public void testSetGetCommitsOnValidEdit() {
152: ftf.setValue(Boolean.TRUE);
153: formatter = (DefaultFormatter) ftf.getFormatter();
154: ftf.setText("false");
155: assertEquals(Boolean.TRUE, ftf.getValue());
156: formatter.setCommitsOnValidEdit(true);
157: assertTrue(formatter.getCommitsOnValidEdit());
158: ftf.setText("false");
159: SwingUtilities.invokeLater(new Runnable() {
160: public void run() {
161: try {
162: assertEquals(Boolean.FALSE, ftf.getValue());
163: } catch (AssertionFailedError e) {
164: assertion = e;
165: }
166: }
167: });
168: if (assertion != null) {
169: throw assertion;
170: }
171: }
172:
173: public void testSetGetOverwriteMode_NumberFormatter() {
174: ftf.setValue(new Integer(334580));
175: NumberFormatter numFormatter = (NumberFormatter) ftf
176: .getFormatter();
177: try {
178: AbstractDocument doc = (AbstractDocument) ftf.getDocument();
179: ftf.setText("00");
180: doc.insertString(0, "123", null);
181: assertEquals("12300", ftf.getText());
182: doc.insertString(0, "456", null);
183: assertEquals("45612300", ftf.getText());
184: numFormatter.setOverwriteMode(true);
185: doc.insertString(0, "789", null);
186: assertEquals("78912300", ftf.getText());
187: doc.insertString(3, "xxx", null);
188: assertEquals("789xxx00", ftf.getText());
189: assertEquals(numFormatter, ftf.getFormatter());
190: } catch (BadLocationException e) {
191: assertTrue("Unexpected exception: " + e.getMessage(), false);
192: }
193: }
194:
195: public void testSetGetOverwriteMode() {
196: ftf.setValue(Boolean.FALSE);
197: DefaultFormatter formatter = (DefaultFormatter) ftf
198: .getFormatter();
199: try {
200: AbstractDocument doc = (AbstractDocument) ftf.getDocument();
201: ftf.setText("00");
202: doc.insertString(0, "123", null);
203: assertEquals("123", ftf.getText());
204: doc.insertString(0, "456", null);
205: assertEquals("456", ftf.getText());
206: formatter.setOverwriteMode(false);
207: doc.insertString(0, "789", null);
208: assertEquals("789456", ftf.getText());
209: doc.insertString(3, "xxx", null);
210: assertEquals("789xxx456", ftf.getText());
211: assertEquals(formatter, ftf.getFormatter());
212: } catch (BadLocationException e) {
213: assertTrue("Unexpected exception: " + e.getMessage(), false);
214: }
215: }
216:
217: public void testStringToValue() {
218: formatter.install(ftf);
219: assertNull(ftf.getValue());
220: assertNull(formatter.getValueClass());
221: try {
222: assertEquals("java.lang.String", formatter.stringToValue(
223: "546").getClass().getName());
224: ftf.setValue(Boolean.TRUE);
225: assertEquals("java.lang.Boolean", formatter.stringToValue(
226: "true").getClass().getName());
227: formatter.setValueClass(Float.class);
228: assertEquals("java.lang.Float", formatter.stringToValue(
229: "546").getClass().getName());
230: formatter.setValueClass(Rectangle.class);
231: assertEquals("java.lang.String", formatter.stringToValue(
232: "546").getClass().getName());
233: } catch (ParseException e) {
234: assertTrue("Unexpected exception: " + e.getMessage(), false);
235: }
236: try {
237: formatter.setValueClass(Integer.class);
238: formatter.stringToValue("ttt");
239: } catch (ParseException e) {
240: bWasException = true;
241: message = e.getMessage();
242: }
243: assertTrue(bWasException);
244: assertEquals("Error creating instance", message);
245: }
246:
247: public void testSetGetValueClass() {
248: assertNull(formatter.getValueClass());
249: formatter.setValueClass(Rectangle.class);
250: assertEquals(Rectangle.class, formatter.getValueClass());
251: }
252: }
|