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 javax.swing.JFormattedTextField;
023: import javax.swing.SwingTestCase;
024:
025: public class DefaultFormatterFactoryTest extends SwingTestCase {
026: class FTF extends JFormattedTextField {
027: private static final long serialVersionUID = 1L;
028:
029: boolean hasFocus;
030:
031: public FTF(final Object value, final boolean hasFocus) {
032: super (value);
033: setHasFocus(hasFocus);
034: }
035:
036: @Override
037: public boolean hasFocus() {
038: return hasFocus;
039: }
040:
041: public void setHasFocus(final boolean hasFocus) {
042: this .hasFocus = hasFocus;
043: }
044: }
045:
046: DefaultFormatterFactory factory;
047:
048: DefaultFormatter formatter;
049:
050: FTF ftf;
051:
052: DefaultFormatter defaultDormatter;
053:
054: DefaultFormatter editFormatter;
055:
056: DefaultFormatter displayFormatter;
057:
058: DefaultFormatter nullFormatter;
059:
060: @Override
061: protected void setUp() throws Exception {
062: super .setUp();
063: factory = new DefaultFormatterFactory();
064: formatter = new DefaultFormatter();
065: }
066:
067: private void checkFormatter(
068: final JFormattedTextField.AbstractFormatter formatter) {
069: assertEquals(formatter, factory.getFormatter(ftf));
070: }
071:
072: private void init(final Object value, final boolean hasFocus) {
073: ftf = new FTF(value, hasFocus);
074: initFormatters();
075: }
076:
077: private void initFormatters() {
078: defaultDormatter = new DefaultFormatter();
079: editFormatter = new DefaultFormatter();
080: displayFormatter = new DefaultFormatter();
081: nullFormatter = new DefaultFormatter();
082: }
083:
084: public void testGetFormatterNull() {
085: init(null, false);
086: assertNull(factory.getFormatter(ftf));
087: factory.setDefaultFormatter(defaultDormatter);
088: checkFormatter(defaultDormatter);
089: factory.setEditFormatter(editFormatter);
090: checkFormatter(defaultDormatter);
091: factory.setDisplayFormatter(displayFormatter);
092: checkFormatter(displayFormatter);
093: factory.setNullFormatter(nullFormatter);
094: checkFormatter(nullFormatter);
095: }
096:
097: public void testGetFormatterNullHasFocus() {
098: init(null, true);
099: assertNull(factory.getFormatter(ftf));
100: factory.setDefaultFormatter(defaultDormatter);
101: checkFormatter(defaultDormatter);
102: factory.setDisplayFormatter(displayFormatter);
103: checkFormatter(defaultDormatter);
104: factory.setEditFormatter(editFormatter);
105: checkFormatter(editFormatter);
106: factory.setNullFormatter(nullFormatter);
107: checkFormatter(nullFormatter);
108: }
109:
110: public void testGetFormatterNotNull_Number() {
111: init(new Integer(333), false);
112: assertNull(factory.getFormatter(ftf));
113: factory.setNullFormatter(nullFormatter);
114: assertNull(factory.getFormatter(ftf));
115: factory.setEditFormatter(editFormatter);
116: assertNull(factory.getFormatter(ftf));
117: factory.setDefaultFormatter(defaultDormatter);
118: checkFormatter(defaultDormatter);
119: factory.setDisplayFormatter(displayFormatter);
120: checkFormatter(displayFormatter);
121: }
122:
123: public void testGetFormatterNotNullHasFocus() {
124: init(new Integer(333), true);
125: assertNull(factory.getFormatter(ftf));
126: factory.setNullFormatter(nullFormatter);
127: assertNull(factory.getFormatter(ftf));
128: factory.setDisplayFormatter(displayFormatter);
129: assertNull(factory.getFormatter(ftf));
130: factory.setDefaultFormatter(defaultDormatter);
131: checkFormatter(defaultDormatter);
132: factory.setEditFormatter(editFormatter);
133: checkFormatter(editFormatter);
134: }
135:
136: public void testDefaultFormatterFactory() {
137: assertNull(factory.getNullFormatter());
138: assertNull(factory.getEditFormatter());
139: assertNull(factory.getDisplayFormatter());
140: assertNull(factory.getDefaultFormatter());
141: }
142:
143: public void testDefaultFormatterFactoryDefault() {
144: factory = new DefaultFormatterFactory(formatter);
145: assertNull(factory.getNullFormatter());
146: assertNull(factory.getEditFormatter());
147: assertNull(factory.getDisplayFormatter());
148: assertEquals(formatter, factory.getDefaultFormatter());
149: }
150:
151: public void testDefaultFormatterFactoryDefaultDisplay() {
152: DefaultFormatter formatter1 = new DefaultFormatter();
153: factory = new DefaultFormatterFactory(formatter, formatter1);
154: assertNull(factory.getNullFormatter());
155: assertNull(factory.getEditFormatter());
156: assertEquals(formatter1, factory.getDisplayFormatter());
157: assertEquals(formatter, factory.getDefaultFormatter());
158: }
159:
160: public void testDefaultFormatterFactoryDefaultDisplayEdit() {
161: DefaultFormatter formatter1 = new DefaultFormatter();
162: DefaultFormatter formatter2 = new DefaultFormatter();
163: factory = new DefaultFormatterFactory(formatter, formatter1,
164: formatter2);
165: assertNull(factory.getNullFormatter());
166: assertEquals(formatter2, factory.getEditFormatter());
167: assertEquals(formatter1, factory.getDisplayFormatter());
168: assertEquals(formatter, factory.getDefaultFormatter());
169: }
170:
171: public void testDefaultFormatterFactoryDefaultDisplayEditNull() {
172: DefaultFormatter formatter1 = new DefaultFormatter();
173: DefaultFormatter formatter2 = new DefaultFormatter();
174: DefaultFormatter formatter3 = new DefaultFormatter();
175: factory = new DefaultFormatterFactory(formatter, formatter1,
176: formatter2, formatter3);
177: assertEquals(formatter3, factory.getNullFormatter());
178: assertEquals(formatter2, factory.getEditFormatter());
179: assertEquals(formatter1, factory.getDisplayFormatter());
180: assertEquals(formatter, factory.getDefaultFormatter());
181: }
182:
183: public void testSetGetDefaultFormatter() {
184: assertNull(factory.getDefaultFormatter());
185: factory.setDefaultFormatter(formatter);
186: assertEquals(formatter, factory.getDefaultFormatter());
187: }
188:
189: public void testSetGetDisplayFormatter() {
190: assertNull(factory.getDisplayFormatter());
191: factory.setDisplayFormatter(formatter);
192: assertEquals(formatter, factory.getDisplayFormatter());
193: }
194:
195: public void testSetGetNullFormatter() {
196: assertNull(factory.getNullFormatter());
197: factory.setNullFormatter(formatter);
198: assertEquals(formatter, factory.getNullFormatter());
199: }
200:
201: public void testSetGetEditFormatter() {
202: assertNull(factory.getEditFormatter());
203: factory.setEditFormatter(formatter);
204: assertEquals(formatter, factory.getEditFormatter());
205: }
206: }
|