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.event.ActionEvent;
023: import java.text.DecimalFormat;
024: import java.text.ParseException;
025: import java.text.SimpleDateFormat;
026: import java.util.Date;
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.JFormattedTextField;
029: import javax.swing.SwingTestCase;
030:
031: public class InternationalFormatterRTest extends SwingTestCase {
032: InternationalFormatter formatter;
033:
034: boolean bWasException;
035:
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: formatter = new InternationalFormatter(new DecimalFormat());
040: bWasException = false;
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: super .tearDown();
046: }
047:
048: public void testStringToValue_Min() {
049: formatter.setMinimum(new Integer(10));
050: try {
051: formatter.stringToValue("55");
052: } catch (ParseException e) {
053: bWasException = true;
054: }
055: assertFalse(bWasException);
056: }
057:
058: public void testStringToValue_Value() {
059: formatter.setValueClass(Integer.class);
060: try {
061: assertTrue(formatter.stringToValue("55") instanceof Integer);
062: } catch (ParseException e) {
063: bWasException = true;
064: }
065: assertFalse(bWasException);
066: }
067:
068: public void testSetMin() {
069: assertNull(formatter.getValueClass());
070: formatter.setMinimum(new Integer(10));
071: assertEquals(Integer.class, formatter.getValueClass());
072: }
073:
074: public void testSetMax() {
075: assertNull(formatter.getValueClass());
076: formatter.setMaximum(new Integer(10));
077: assertEquals(Integer.class, formatter.getValueClass());
078: }
079:
080: public void testIncrementDecrement() {
081: if (!BasicSwingTestCase.isHarmony()) {
082: return;
083: }
084: JFormattedTextField ftf = new JFormattedTextField();
085: ftf.setFormatterFactory(new DefaultFormatterFactory(
086: new DateFormatter(new SimpleDateFormat("dd.MM.yyyy"))));
087: ftf.setValue(new Date());
088: ftf.setText("31.01.2006");
089: ftf.setCaretPosition(0);
090: TextAction action = new InternationalFormatter.IncrementAction(
091: "inc", 1);
092: action.actionPerformed(new ActionEvent(ftf, 0, null));
093: assertEquals("01.02.2006", ftf.getText());
094: assertEquals(2, ftf.getCaretPosition());
095: assertEquals("01", ftf.getSelectedText());
096: action = new InternationalFormatter.IncrementAction("inc", -1);
097: action.actionPerformed(new ActionEvent(ftf, 0, null));
098: assertEquals("31.01.2006", ftf.getText());
099: assertEquals(2, ftf.getCaretPosition());
100: assertEquals("31", ftf.getSelectedText());
101: }
102: }
|