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;
021:
022: import java.awt.Color;
023: import java.awt.event.FocusEvent;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026: import java.text.DecimalFormat;
027: import java.text.Format;
028: import java.text.MessageFormat;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Date;
032: import javax.swing.text.DateFormatter;
033: import javax.swing.text.DefaultEditorKit;
034: import javax.swing.text.DefaultFormatter;
035: import javax.swing.text.DefaultFormatterFactory;
036: import javax.swing.text.InternationalFormatter;
037: import javax.swing.text.NumberFormatter;
038: import javax.swing.text.TextAction;
039:
040: public class JFormattedTextFieldTest extends SwingTestCase {
041: JFrame jf;
042:
043: DbgFormattedField tf;
044:
045: boolean bWasException;
046:
047: String message;
048:
049: PropertyChangeListenerImpl listener = new PropertyChangeListenerImpl();
050:
051: private FocusEvent FOCUS_LOST;
052:
053: private FocusEvent FOCUS_GAINED;
054:
055: class PropertyChangeListenerImpl implements PropertyChangeListener {
056: String name;
057:
058: Object oldValue;
059:
060: Object newValue;
061:
062: String interestingPropertyName;
063:
064: public void propertyChange(final PropertyChangeEvent e) {
065: String propertyName = e.getPropertyName();
066: if (propertyName.equals(interestingPropertyName)) {
067: name = e.getPropertyName();
068: oldValue = e.getOldValue();
069: newValue = e.getNewValue();
070: }
071: }
072:
073: final void setInterestingPropertyName(final String propertyName) {
074: interestingPropertyName = propertyName;
075: }
076: }
077:
078: class DbgFormattedField extends JFormattedTextField {
079: private static final long serialVersionUID = 1L;
080:
081: private boolean wasCallCommitEdit;
082:
083: @Override
084: public void commitEdit() throws ParseException {
085: wasCallCommitEdit = true;
086: super .commitEdit();
087: }
088:
089: boolean wasCallCommitEdit() {
090: boolean was = wasCallCommitEdit;
091: wasCallCommitEdit = false;
092: return was;
093: }
094: };
095:
096: class DbgFormatter extends DefaultFormatter {
097: private static final long serialVersionUID = 1L;
098:
099: boolean wasCallInstall;
100:
101: boolean wasCallUninstall;
102:
103: @Override
104: public void install(final JFormattedTextField ftf) {
105: wasCallInstall = true;
106: super .install(ftf);
107: }
108:
109: @Override
110: public void uninstall() {
111: wasCallUninstall = true;
112: super .uninstall();
113: }
114:
115: boolean wasCallInstall() {
116: boolean result = wasCallInstall;
117: wasCallInstall = false;
118: return result;
119: }
120:
121: boolean wasCallUninstall() {
122: boolean result = wasCallUninstall;
123: wasCallUninstall = false;
124: return result;
125: }
126: }
127:
128: class FTF extends JFormattedTextField {
129: private static final long serialVersionUID = 1L;
130:
131: boolean hasFocus;
132:
133: public FTF(final boolean hasFocus) {
134: setHasFocus(hasFocus);
135: }
136:
137: @Override
138: public boolean hasFocus() {
139: return hasFocus;
140: }
141:
142: public void setHasFocus(final boolean hasFocus) {
143: this .hasFocus = hasFocus;
144: }
145: }
146:
147: @Override
148: protected void setUp() throws Exception {
149: super .setUp();
150: jf = new JFrame();
151: tf = new DbgFormattedField();
152: tf.addPropertyChangeListener(listener);
153: initFocusEvent();
154: jf.getContentPane().add(tf);
155: jf.setSize(200, 300);
156: jf.pack();
157: bWasException = false;
158: message = null;
159: }
160:
161: private void initFocusEvent() {
162: if (FOCUS_GAINED == null || FOCUS_LOST == null) {
163: FOCUS_GAINED = new FocusEvent(tf, FocusEvent.FOCUS_GAINED);
164: FOCUS_LOST = new FocusEvent(tf, FocusEvent.FOCUS_LOST);
165: }
166: }
167:
168: @Override
169: protected void tearDown() throws Exception {
170: super .tearDown();
171: jf.dispose();
172: }
173:
174: private void checkEvent(final String name, final Object oldValue,
175: final Object newValue) {
176: assertEquals(name, listener.name);
177: assertEquals(oldValue, listener.oldValue);
178: assertEquals(newValue, listener.newValue);
179: }
180:
181: public void testJFormattedTextFieldObject() {
182: Object value = Color.RED;
183: JFormattedTextField tf1 = new JFormattedTextField(value);
184: assertEquals(value, tf1.getValue());
185: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1
186: .getFocusLostBehavior());
187: assertTrue(tf1.getFormatter() instanceof DefaultFormatter);
188: assertTrue(tf1.getFormatterFactory() instanceof DefaultFormatterFactory);
189: DefaultFormatterFactory factory = (DefaultFormatterFactory) tf1
190: .getFormatterFactory();
191: assertTrue(factory.getDefaultFormatter() instanceof DefaultFormatter);
192: assertNull(factory.getEditFormatter());
193: assertNull(factory.getDisplayFormatter());
194: assertNull(factory.getNullFormatter());
195: }
196:
197: public void testJFormattedTextFieldObject_NullToString() {
198: final Object value = new Object() {
199: @Override
200: public String toString() {
201: return null;
202: }
203: };
204: final JFormattedTextField ftf = new JFormattedTextField(value);
205: assertEquals("", ftf.getText());
206: }
207:
208: public void testJFormattedTextFieldAbstractFormatter() {
209: InternationalFormatter formatter = new InternationalFormatter();
210: JFormattedTextField tf1 = new JFormattedTextField(formatter);
211: assertNull(tf1.getValue());
212: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1
213: .getFocusLostBehavior());
214: assertEquals(formatter, tf1.getFormatter());
215: assertTrue(tf1.getFormatterFactory() instanceof DefaultFormatterFactory);
216: DefaultFormatterFactory factory = (DefaultFormatterFactory) tf1
217: .getFormatterFactory();
218: assertNull(factory.getDisplayFormatter());
219: assertNull(factory.getEditFormatter());
220: assertEquals(formatter, factory.getDefaultFormatter());
221: assertNull(factory.getNullFormatter());
222: }
223:
224: public void testJFormattedTextFieldFormat() {
225: Format format = new SimpleDateFormat();
226: InternationalFormatter formatter;
227: DefaultFormatterFactory factory;
228: JFormattedTextField tf1 = new JFormattedTextField(format);
229: assertNull(tf1.getValue());
230: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1
231: .getFocusLostBehavior());
232: assertTrue(tf1.getFormatter() instanceof DateFormatter);
233: formatter = (InternationalFormatter) tf1.getFormatter();
234: assertEquals(format, formatter.getFormat());
235: assertTrue(tf1.getFormatterFactory() instanceof DefaultFormatterFactory);
236: factory = (DefaultFormatterFactory) tf1.getFormatterFactory();
237: assertEquals(formatter, factory.getDefaultFormatter());
238: format = new MessageFormat("");
239: tf1 = new JFormattedTextField(format);
240: assertTrue(tf1.getFormatter() instanceof InternationalFormatter);
241: formatter = (InternationalFormatter) tf1.getFormatter();
242: assertEquals(format, formatter.getFormat());
243: factory = (DefaultFormatterFactory) tf1.getFormatterFactory();
244: assertEquals(formatter, factory.getDefaultFormatter());
245: format = new DecimalFormat();
246: tf1 = new JFormattedTextField(format);
247: assertTrue(tf1.getFormatter() instanceof NumberFormatter);
248: formatter = (InternationalFormatter) tf1.getFormatter();
249: assertEquals(format, formatter.getFormat());
250: factory = (DefaultFormatterFactory) tf1.getFormatterFactory();
251: assertEquals(formatter, factory.getDefaultFormatter());
252: }
253:
254: public void testJFormattedTextFieldAbstractFormatterFactory() {
255: JFormattedTextField.AbstractFormatterFactory factory = new DefaultFormatterFactory();
256: JFormattedTextField tf1 = new JFormattedTextField(factory);
257: assertNull(tf1.getValue());
258: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1
259: .getFocusLostBehavior());
260: assertNull(tf1.getFormatter());
261: assertEquals(factory, tf1.getFormatterFactory());
262: }
263:
264: public void testJFormattedTextFieldAbstractFormatterFactoryObject() {
265: Object value = Color.RED;
266: JFormattedTextField.AbstractFormatterFactory factory = new DefaultFormatterFactory();
267: JFormattedTextField tf1 = new JFormattedTextField(factory,
268: value);
269: assertEquals(value, tf1.getValue());
270: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf1
271: .getFocusLostBehavior());
272: assertNull(tf1.getFormatter());
273: assertEquals(factory, tf1.getFormatterFactory());
274: }
275:
276: public void testSetGetValue() {
277: String propertyName = "value";
278: listener.setInterestingPropertyName(propertyName);
279: assertNull(tf.getValue());
280: Object value = Color.RED;
281: tf.setValue(value);
282: assertEquals(value, tf.getValue());
283: checkEvent(propertyName, null, value);
284: value = "just value";
285: tf.setValue(value);
286: assertEquals(value, tf.getText());
287: assertEquals(value, tf.getValue());
288: checkEvent(propertyName, Color.RED, value);
289: }
290:
291: public void testSetGetFormatter() {
292: String propertyName = "textFormatter";
293: listener.setInterestingPropertyName(propertyName);
294: assertNull(tf.getFormatter());
295: DbgFormatter formatter = new DbgFormatter();
296: tf.setFormatter(formatter);
297: assertTrue(formatter.wasCallInstall());
298: assertEquals(formatter, tf.getFormatter());
299: checkEvent(propertyName, null, formatter);
300: DbgFormatter formatter1 = new DbgFormatter();
301: tf.setFormatter(formatter1);
302: assertTrue(formatter1.wasCallInstall());
303: assertTrue(formatter.wasCallUninstall());
304: assertEquals(formatter1, tf.getFormatter());
305: checkEvent(propertyName, formatter, formatter1);
306: }
307:
308: public void testSetGetFocusLostBehavior() {
309: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf
310: .getFocusLostBehavior());
311: tf.setFocusLostBehavior(JFormattedTextField.COMMIT);
312: assertEquals(JFormattedTextField.COMMIT, tf
313: .getFocusLostBehavior());
314: tf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
315: assertEquals(JFormattedTextField.COMMIT_OR_REVERT, tf
316: .getFocusLostBehavior());
317: tf.setFocusLostBehavior(JFormattedTextField.REVERT);
318: assertEquals(JFormattedTextField.REVERT, tf
319: .getFocusLostBehavior());
320: tf.setFocusLostBehavior(JFormattedTextField.PERSIST);
321: assertEquals(JFormattedTextField.PERSIST, tf
322: .getFocusLostBehavior());
323: try {
324: tf.setFocusLostBehavior(-2);
325: } catch (IllegalArgumentException e) {
326: bWasException = true;
327: message = e.getMessage();
328: }
329: checkException("setFocusLostBehavior must be one of: "
330: + "JFormattedTextField.COMMIT, "
331: + "JFormattedTextField.COMMIT_OR_REVERT, "
332: + "JFormattedTextField.PERSIST "
333: + "or JFormattedTextField.REVERT");
334: try {
335: tf.setFocusLostBehavior(4);
336: } catch (IllegalArgumentException e) {
337: bWasException = true;
338: message = e.getMessage();
339: }
340: checkException("setFocusLostBehavior must be one of: "
341: + "JFormattedTextField.COMMIT, "
342: + "JFormattedTextField.COMMIT_OR_REVERT, "
343: + "JFormattedTextField.PERSIST "
344: + "or JFormattedTextField.REVERT");
345: }
346:
347: public void testSetGetFormatterFactory() {
348: String propertyName = "formatterFactory";
349: listener.setInterestingPropertyName(propertyName);
350: assertNull(tf.getFormatterFactory());
351: DefaultFormatterFactory factory = new DefaultFormatterFactory();
352: tf.setFormatterFactory(factory);
353: assertEquals(factory, tf.getFormatterFactory());
354: checkEvent(propertyName, null, factory);
355: }
356:
357: public void testProcessFocusEventFocusEvent() {
358: Long value = new Long(56);
359: tf.setValue(new Integer(345));
360: tf.setFocusLostBehavior(JFormattedTextField.COMMIT);
361: assertFalse(tf.wasCallCommitEdit());
362: tf.setText("667");
363: tf.processFocusEvent(FOCUS_LOST);
364: assertTrue(tf.wasCallCommitEdit());
365: assertEquals("667", tf.getText());
366: assertEquals(new Long(667), tf.getValue());
367: tf.setText("34ft");
368: tf.processFocusEvent(FOCUS_LOST);
369: assertEquals("34", tf.getText());
370: assertTrue(tf.wasCallCommitEdit());
371: assertEquals(new Long(34), tf.getValue());
372: tf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
373: tf.setText("667");
374: tf.processFocusEvent(FOCUS_LOST);
375: assertTrue(tf.wasCallCommitEdit());
376: assertEquals("667", tf.getText());
377: assertEquals(new Long(667), tf.getValue());
378: tf.setText("56&");
379: tf.processFocusEvent(FOCUS_LOST);
380: assertTrue(tf.wasCallCommitEdit());
381: assertEquals("56", tf.getText());
382: assertEquals(value, tf.getValue());
383: tf.setFocusLostBehavior(JFormattedTextField.REVERT);
384: tf.setText("667");
385: tf.processFocusEvent(FOCUS_LOST);
386: assertFalse(tf.wasCallCommitEdit());
387: assertEquals("56", tf.getText());
388: assertEquals(value, tf.getValue());
389: tf.setText("323rtft");
390: tf.processFocusEvent(FOCUS_LOST);
391: assertFalse(tf.wasCallCommitEdit());
392: assertEquals("56", tf.getText());
393: assertEquals(value, tf.getValue());
394: tf.setFocusLostBehavior(JFormattedTextField.PERSIST);
395: tf.setText("667");
396: tf.processFocusEvent(FOCUS_LOST);
397: assertFalse(tf.wasCallCommitEdit());
398: assertEquals("667", tf.getText());
399: assertEquals(value, tf.getValue());
400: tf.setText("67ft");
401: tf.processFocusEvent(FOCUS_LOST);
402: assertFalse(tf.wasCallCommitEdit());
403: assertEquals("67ft", tf.getText());
404: assertEquals(value, tf.getValue());
405: }
406:
407: public void testSetDocumentDocument() {
408: }
409:
410: public void testGetActions() {
411: Action[] actions = tf.getActions();
412: Action[] defaultActions = new DefaultEditorKit().getActions();
413: assertEquals(defaultActions.length + 2, actions.length);
414: Action cancellAction = null;
415: Action commitAction = null;
416: for (int i = 0; i < actions.length; i++) {
417: Action action = actions[i];
418: String name = (String) action.getValue(Action.NAME);
419: if ("notify-field-accept".equals(name)) {
420: commitAction = action;
421: continue;
422: } else if ("reset-field-edit".equals(name)) {
423: cancellAction = action;
424: continue;
425: } else {
426: boolean fromDefaultAction = false;
427: for (int j = 0; j < defaultActions.length; j++) {
428: if (defaultActions[j].equals(action)) {
429: fromDefaultAction = true;
430: break;
431: }
432: }
433: assertTrue(fromDefaultAction);
434: }
435: }
436: assertTrue(cancellAction instanceof TextAction);
437: assertTrue(commitAction instanceof TextAction);
438: //TODO check commit & cancel actions
439: }
440:
441: public void testCommitEdit() {
442: tf.setFormatter(new NumberFormatter());
443: assertNull(tf.getValue());
444: tf.setText("678");
445: try {
446: tf.commitEdit();
447: } catch (ParseException e) {
448: assertTrue("Unexpected exception: ", false);
449: }
450: assertEquals(new Long(678), tf.getValue());
451: }
452:
453: public void testIsEditValid() {
454: String propertyName = "editValid";
455: listener.setInterestingPropertyName(propertyName);
456: tf.setValue(new Integer(90000000));
457: String text = "5323";
458: tf.setText(text);
459: assertTrue(tf.isEditValid());
460: checkEvent(propertyName, Boolean.FALSE, Boolean.TRUE);
461: text = "valid or invalid?";
462: tf.setText(text);
463: assertFalse(tf.isEditValid());
464: assertEquals(text, tf.getText());
465: checkEvent(propertyName, Boolean.TRUE, Boolean.FALSE);
466: }
467:
468: public void testConstants() {
469: assertEquals(0, JFormattedTextField.COMMIT);
470: assertEquals(1, JFormattedTextField.COMMIT_OR_REVERT);
471: assertEquals(2, JFormattedTextField.REVERT);
472: assertEquals(3, JFormattedTextField.PERSIST);
473: }
474:
475: public void testGetUIClassID() {
476: assertEquals("FormattedTextFieldUI", tf.getUIClassID());
477: }
478:
479: private void checkException(final String text) {
480: assertTrue(bWasException);
481: assertEquals(text, message);
482: bWasException = false;
483: message = null;
484: }
485:
486: private DefaultFormatterFactory getFactoryIfDefault(
487: final JFormattedTextField.AbstractFormatterFactory factory) {
488: assertTrue(factory instanceof DefaultFormatterFactory);
489: return (DefaultFormatterFactory) factory;
490: }
491:
492: private void checkDefaultFormatter(
493: final DefaultFormatterFactory factory) {
494: assertTrue(factory.getDefaultFormatter() instanceof DefaultFormatter);
495: assertNull(factory.getDisplayFormatter());
496: assertNull(factory.getEditFormatter());
497: assertNull(factory.getNullFormatter());
498: }
499:
500: public void testCreateFormattersFactory() {
501: DefaultFormatterFactory factory;
502: tf.setValue(new Integer(34));
503: factory = getFactoryIfDefault(tf.getFormatterFactory());
504: assertTrue(factory.getDefaultFormatter() instanceof NumberFormatter);
505: //TODO: check if factory.getDefaultFormatter() should be same to factory.getDisplayFormatter()
506: // or factory.getEditFormatter().
507: assertNull(factory.getNullFormatter());
508: tf.setFormatterFactory(null);
509: tf.setValue(new Date());
510: factory = getFactoryIfDefault(tf.getFormatterFactory());
511: assertTrue(factory.getDefaultFormatter() instanceof DateFormatter);
512: assertNull(factory.getDisplayFormatter());
513: assertNull(factory.getEditFormatter());
514: assertNull(factory.getNullFormatter());
515: tf.setFormatterFactory(null);
516: tf.setValue("sdffsdf");
517: factory = getFactoryIfDefault(tf.getFormatterFactory());
518: checkDefaultFormatter(factory);
519: tf.setFormatterFactory(null);
520: tf.setValue(Color.RED);
521: factory = getFactoryIfDefault(tf.getFormatterFactory());
522: checkDefaultFormatter(factory);
523: }
524: }
|