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.ParseException;
023: import javax.swing.SwingTestCase;
024: import javax.swing.JFormattedTextField;
025:
026: public class MaskFormatterTest extends SwingTestCase {
027: MaskFormatter formatter;
028:
029: boolean bWasException;
030:
031: @Override
032: protected void setUp() throws Exception {
033: super .setUp();
034: formatter = new MaskFormatter();
035: bWasException = false;
036: }
037:
038: @Override
039: protected void tearDown() throws Exception {
040: super .tearDown();
041: }
042:
043: public void testMaskFormatter() {
044: assertNull(formatter.getMask());
045: checkMainProperties();
046: }
047:
048: private void checkMainProperties() {
049: assertEquals(' ', formatter.getPlaceholderCharacter());
050: assertNull(formatter.getInvalidCharacters());
051: assertNull(formatter.getValidCharacters());
052: assertNull(formatter.getPlaceholder());
053: assertTrue(formatter.getValueContainsLiteralCharacters());
054: assertFalse(formatter.getAllowsInvalid());
055: assertTrue(formatter.getOverwriteMode());
056: assertFalse(formatter.getCommitsOnValidEdit());
057: assertNull(formatter.getValueClass());
058: }
059:
060: public void testMaskFormatterString() {
061: String mask = "#`ULA?*H";
062: try {
063: formatter = new MaskFormatter(mask);
064: assertEquals(mask, formatter.getMask());
065: checkMainProperties();
066: mask = "#`ula?*h";
067: formatter = new MaskFormatter(mask);
068: assertEquals(mask, formatter.getMask());
069: checkMainProperties();
070: } catch (ParseException e) {
071: assertFalse("Unexpected exception: " + e.getMessage(), true);
072: }
073: //API says that's it's possible ParseException in this constructor.
074: //I don't know when it's possible.
075: }
076:
077: public void testSetGetMask() {
078: String mask = "0xH-H";
079: try {
080: formatter.setMask(mask);
081: assertEquals(mask, formatter.getMask());
082: assertEquals("0xa-B", formatter.stringToValue("0xa-B"));
083: } catch (ParseException e) {
084: assertFalse("Unexpected exception: " + e.getMessage(), true);
085: }
086: try {
087: formatter.stringToValue("ddd");
088: } catch (ParseException e) {
089: bWasException = true;
090: }
091: assertTrue(bWasException);
092: }
093:
094: public void testSetGetInvalidCharacters() {
095: try {
096: formatter.setMask("*****");
097: formatter.setInvalidCharacters("#2");
098: assertEquals("#2", formatter.getInvalidCharacters());
099: formatter.stringToValue("rrrr5");
100: } catch (ParseException e) {
101: assertFalse("Unexpected exception: " + e.getMessage(), true);
102: }
103: try {
104: formatter.stringToValue("#rra");
105: } catch (ParseException e) {
106: bWasException = true;
107: }
108: assertTrue(bWasException);
109: }
110:
111: public void testSetGetPlaceholder() {
112: try {
113: formatter.setMask("HHH$%$HHH^^^HHH");
114: formatter.setPlaceholder("fff$%$fff^^^fff");
115: assertEquals("fff$%$fff^^^fff", formatter.getPlaceholder());
116: assertEquals("111$%$fff^^^fff", formatter
117: .valueToString("111"));
118: formatter.setPlaceholderCharacter('*');
119: assertEquals("111$%$fff^^^fff", formatter
120: .valueToString("111"));
121: formatter.setPlaceholder("123$%$abc");
122: assertEquals("123$%$abc", formatter.getPlaceholder());
123: assertEquals("AAA$%$abc^^^***", formatter
124: .valueToString("AAA"));
125: } catch (ParseException e) {
126: assertFalse("Unexpected exception: " + e.getMessage(), true);
127: }
128: }
129:
130: public void testSetGetPlaceholderCharacter() {
131: try {
132: formatter.setMask("HHH$%$HHH^^^HHH");
133: formatter.setPlaceholderCharacter('&');
134: assertEquals('&', formatter.getPlaceholderCharacter());
135: assertEquals("456$%$6&&^^^&&&", formatter
136: .valueToString("456$%$6"));
137: formatter.setPlaceholder("123$%$abc");
138: assertEquals("456$%$6bc^^^&&&", formatter
139: .valueToString("456$%$6"));
140: } catch (ParseException e) {
141: assertFalse("Unexpected exception: " + e.getMessage(), true);
142: }
143: }
144:
145: public void testSetGetValidCharacters() {
146: try {
147: formatter.setMask("****");
148: formatter.setValidCharacters("012345678abc");
149: assertEquals("012345678abc", formatter.getValidCharacters());
150: formatter.stringToValue("acb4");
151: } catch (ParseException e) {
152: assertFalse("Unexpected exception: " + e.getMessage(), true);
153: }
154: try {
155: formatter.stringToValue("rra");
156: } catch (ParseException e) {
157: bWasException = true;
158: }
159: assertTrue(bWasException);
160: }
161:
162: public void testSetGetValidInvalidCharacters() {
163: try {
164: formatter.setMask("****");
165: formatter.setInvalidCharacters("a");
166: formatter.setValidCharacters("a");
167: formatter.stringToValue("a");
168: } catch (ParseException e) {
169: bWasException = true;
170: }
171: assertTrue(bWasException);
172: }
173:
174: public void testSetGetValueContainsLiteralCharacters() {
175: try {
176: formatter.setMask("(###) ###-####");
177: String text = "(415) 555-1212";
178: assertEquals(text, formatter.stringToValue(text));
179: formatter.setValueContainsLiteralCharacters(false);
180: assertEquals("4155551212", formatter.stringToValue(text));
181: } catch (ParseException e) {
182: assertFalse("Unexpected exception: " + e.getMessage(), true);
183: }
184: }
185:
186: public void testSetIncorrectPlaceHolder() {
187: try {
188: formatter.setMask("##-##-##");
189: String placeholder = "asdkahsdkjahs";
190: formatter.setPlaceholder(placeholder);
191: assertEquals(placeholder, formatter.getPlaceholder());
192: assertEquals("26-ka-sd", formatter.valueToString("26"));
193: } catch (ParseException e) {
194: assertFalse("Unexpected exception: " + e.getMessage(), true);
195: }
196: }
197:
198: public void testSetPlaceHolderWithInvalidCharacters() {
199: try {
200: formatter.setMask("##-##-##");
201: formatter.setValidCharacters("12");
202: String placeholder = "asdkahsdkjahs";
203: formatter.setPlaceholder(placeholder);
204: assertEquals(placeholder, formatter.getPlaceholder());
205: assertEquals("21-ka-sd", formatter.valueToString("21"));
206: } catch (ParseException e) {
207: assertFalse("Unexpected exception: " + e.getMessage(), true);
208: }
209: }
210:
211: public void testValueToString_InvalidValue() {
212: try {
213: formatter.setMask("-^##-##-##");
214: formatter.valueToString("23");
215: } catch (ParseException e) {
216: bWasException = true;
217: }
218: checkException();
219: }
220:
221: public void testValueToString_ValueWithoutLiterals() {
222: try {
223: formatter.setMask("-^##-##-##");
224: formatter.setValueContainsLiteralCharacters(false);
225: formatter.setPlaceholderCharacter('*');
226: assertEquals("-^23-**-**", formatter.valueToString("23"));
227: } catch (ParseException e) {
228: assertFalse("Unexpected exception: " + e.getMessage(), true);
229: }
230: }
231:
232: public void testSetMask_Escape_StringToValue() {
233: try {
234: formatter.setMask("'Uw''et'67H'H");
235: formatter.stringToValue("Uw'et678H");
236: } catch (ParseException e) {
237: assertFalse("Unexpected exception: " + e.getMessage(), true);
238: }
239: }
240:
241: public void testSetMask_Escape_AtTheEnd() {
242: try {
243: formatter.setMask("567'");
244: assertEquals("567'", formatter.getMask());
245: assertEquals("567", formatter.stringToValue("567"));
246: } catch (ParseException e) {
247: assertFalse("Unexpected exception: " + e.getMessage(), true);
248: }
249: try {
250: formatter.stringToValue("567'");
251: } catch (ParseException e) {
252: bWasException = true;
253: }
254: checkException();
255: }
256:
257: public void testValueToString_ESCAPE() {
258: try {
259: formatter.setMask("'U456H'''H");
260: assertEquals("U4567'H", formatter.valueToString("U4567'H"));
261: formatter.setValueContainsLiteralCharacters(false);
262: assertEquals("U4567'H", formatter.valueToString("7"));
263: } catch (ParseException e) {
264: assertFalse("Unexpected exception: " + e.getMessage(), true);
265: }
266: }
267:
268: public void testValueToString_ESCAPE_ParseException() {
269: try {
270: formatter.setMask("'U456H'''H");
271: formatter.valueToString("4567'H");
272: } catch (ParseException e) {
273: bWasException = true;
274: }
275: checkException();
276: try {
277: formatter.setValueContainsLiteralCharacters(false);
278: formatter.valueToString("y3");
279: } catch (ParseException e) {
280: bWasException = true;
281: }
282: checkException();
283: }
284:
285: private void checkException() {
286: assertTrue(bWasException);
287: bWasException = false;
288: }
289:
290: public void testGetNavigationFilter() {
291: assertNotNull(formatter.getNavigationFilter());
292: }
293:
294: public void testStringToValue_InvalidPlaceHolderChar() {
295: try {
296: formatter.setMask("UUU-UUU");
297: formatter.setPlaceholder("aaaaa");
298: formatter.setPlaceholderCharacter('x');
299: } catch (ParseException e) {
300: assertTrue("Unexpected exception: " + e.getMessage(), false);
301: }
302: }
303:
304: public void testValueToString_Object() throws ParseException {
305: // Regression for HARMONY-1742
306: MaskFormatter obj = new MaskFormatter();
307: obj.valueToString(new Object());
308: }
309:
310: public void testInstall_JFormattedTextField() {
311: // Regression for HARMONY-1742
312: MaskFormatter obj = new MaskFormatter();
313: obj.install(new JFormattedTextField());
314: }
315: }
|