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.text.AttributedCharacterIterator;
023: import java.text.Format;
024: import java.text.NumberFormat;
025: import java.text.ParseException;
026: import javax.swing.JFormattedTextField;
027: import javax.swing.SwingTestCase;
028:
029: public class InternationalFormatterTest extends SwingTestCase {
030: InternationalFormatter formatter;
031:
032: JFormattedTextField ftf;
033:
034: boolean bWasException;
035:
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: formatter = new InternationalFormatter() {
040: private static final long serialVersionUID = 1L;
041: // boolean getSupportsIncrement() {
042: // return super.getStrue;
043: // }
044: };
045: ftf = new JFormattedTextField();
046: bWasException = false;
047: }
048:
049: @Override
050: protected void tearDown() throws Exception {
051: super .tearDown();
052: }
053:
054: private void checkException() {
055: assertTrue(bWasException);
056: bWasException = false;
057: }
058:
059: private void checkMainProperties() {
060: assertTrue(formatter.getAllowsInvalid());
061: assertFalse(formatter.getCommitsOnValidEdit());
062: assertFalse(formatter.getOverwriteMode());
063: //assertNull(formatter.getActions());
064: //System.out.println(formatter.getActions()[1].getValue(Action.NAME));
065: assertNull(formatter.getMaximum());
066: assertNull(formatter.getMinimum());
067: }
068:
069: public void testInternationalFormatter() {
070: assertNull(formatter.getFormat());
071: checkMainProperties();
072: }
073:
074: public void testInternationalFormatterFormat() {
075: Format format = NumberFormat.getNumberInstance();
076: formatter = new InternationalFormatter(format);
077: assertEquals(format, formatter.getFormat());
078: checkMainProperties();
079: }
080:
081: public void testClone() {
082: Object clone = null;
083: formatter.install(ftf);
084: formatter.setValueClass(Integer.class);
085: formatter.setAllowsInvalid(false);
086: formatter.setCommitsOnValidEdit(true);
087: formatter.setOverwriteMode(true);
088: Format format = NumberFormat.getCurrencyInstance();
089: formatter.setFormat(format);
090: Comparable<?> max = new Integer(23);
091: Comparable<?> min = new Integer(24);
092: formatter.setMaximum(max);
093: formatter.setMinimum(min);
094: try {
095: clone = formatter.clone();
096: } catch (CloneNotSupportedException e) {
097: assertFalse("Unexpected exception: " + e.getMessage(), true);
098: }
099: assertTrue(clone instanceof InternationalFormatter);
100: InternationalFormatter form = (InternationalFormatter) clone;
101: assertFalse(form.getAllowsInvalid());
102: assertTrue(form.getCommitsOnValidEdit());
103: assertTrue(form.getOverwriteMode());
104: assertEquals(format, form.getFormat());
105: assertEquals(max, form.getMaximum());
106: assertEquals(min, form.getMinimum());
107: }
108:
109: public void testGetActions() {
110: }
111:
112: public void testStringToValue() {
113: assertNull(formatter.getFormat());
114: assertNull(formatter.getValueClass());
115: try {
116: assertNull(formatter.stringToValue(null));
117: assertEquals("234", formatter.stringToValue("234"));
118: formatter.setFormat(NumberFormat.getNumberInstance());
119: assertEquals(new Long(723), formatter.stringToValue("723"));
120: formatter.setMaximum(new Long(23)); //if Integer
121: formatter.setMinimum(new Long(20));
122: assertEquals(new Long(21), formatter.stringToValue("21"));
123: } catch (ParseException e) {
124: assertTrue("Unexpected exception: ", false);
125: }
126: try {
127: formatter.stringToValue("-27");
128: } catch (ParseException e) {
129: bWasException = true;
130: }
131: checkException();
132: try {
133: formatter.stringToValue("abc12");
134: } catch (ParseException e) {
135: bWasException = true;
136: }
137: checkException();
138: try {
139: formatter.stringToValue("true");
140: } catch (ParseException e) {
141: bWasException = true;
142: }
143: checkException();
144: try {
145: formatter.setFormat(null);
146: formatter.setValueClass(Boolean.class);
147: formatter.setMaximum(null);
148: formatter.setMinimum(null);
149: assertEquals(Boolean.TRUE, formatter.stringToValue("true"));
150: } catch (ParseException e) {
151: assertTrue("Unexpected exception: ", false);
152: }
153: }
154:
155: public void testValueToString() {
156: try {
157: assertEquals("", formatter.valueToString(null));
158: Object value;
159: assertNull(formatter.getFormat());
160: assertEquals("234", formatter
161: .valueToString(new Integer(234)));
162: value = new DefaultCaret();
163: assertEquals(value.toString(), formatter
164: .valueToString(value));
165: Format format = NumberFormat.getPercentInstance();
166: formatter.setFormat(format);
167: value = new Integer(456);
168: assertEquals(format.format(value), formatter
169: .valueToString(value));
170: format = NumberFormat.getCurrencyInstance();
171: formatter.setFormat(format);
172: value = new Integer(345);
173: assertEquals(format.format(value), formatter
174: .valueToString(value));
175: } catch (ParseException e) {
176: assertTrue("Unexpected exception: ", false);
177: }
178: }
179:
180: public void testGetFields() {
181: ftf.setValue(new Integer(345));
182: formatter = (InternationalFormatter) ftf.getFormatter();
183: Format format = formatter.getFormat();
184: Format.Field[] fields = formatter.getFields(0);
185: assertEquals(1, fields.length);
186: AttributedCharacterIterator iter = format
187: .formatToCharacterIterator(new Integer(345));
188: assertTrue(iter.getAttributes().containsKey(fields[0]));
189: assertEquals(0, formatter.getFields(-7).length);
190: //TODO
191: //formatter.setFormat(null);
192: //assertEquals(0, formatter.getFields(0).length);
193: }
194:
195: public void testSetGetFormat() {
196: Format format = NumberFormat.getCurrencyInstance();
197: formatter.setFormat(format);
198: assertEquals(format, formatter.getFormat());
199: }
200:
201: public void testSetGetMaximum() {
202: Integer max = new Integer(35);
203: Integer min = new Integer(40);
204: formatter.setMaximum(max);
205: assertEquals(max, formatter.getMaximum());
206: formatter.setMinimum(min);
207: assertEquals(min, formatter.getMinimum());
208: }
209:
210: public void testSetGetMinimum() {
211: Integer max = new Integer(10);
212: Integer min = new Integer(20);
213: formatter.setMinimum(min);
214: assertEquals(min, formatter.getMinimum());
215: formatter.setMaximum(max);
216: assertEquals(max, formatter.getMaximum());
217: }
218: }
|