1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.util.converter; 27 28 import java.text.DecimalFormat; 29 import java.text.NumberFormat; 30 import java.util.Locale; 31 import static org.junit.Assert.*; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 36 /** 37 */ 38 public class NumberStringConverterTest { 39 private NumberStringConverter converter; 40 41 @Before public void setup() { 42 converter = new NumberStringConverter(); 43 } 44 45 /********************************************************************* 46 * Test constructors 47 ********************************************************************/ 48 49 @Test public void testDefaultConstructor() { 50 NumberStringConverter c = new NumberStringConverter(); 51 assertEquals(Locale.getDefault(), c.locale); 52 assertNull(c.pattern); 53 assertNull(c.numberFormat); 54 } 55 56 @Test public void testConstructor_locale() { 57 NumberStringConverter c = new NumberStringConverter(Locale.CANADA); 58 assertEquals(Locale.CANADA, c.locale); 59 assertNull(c.pattern); 60 assertNull(c.numberFormat); 61 } 62 63 @Test public void testConstructor_pattern() { 64 NumberStringConverter c = new NumberStringConverter("#,##,###,####"); 65 assertEquals(Locale.getDefault(), c.locale); 66 assertEquals("#,##,###,####", c.pattern); 67 assertNull(c.numberFormat); 68 } 69 70 @Test public void testConstructor_locale_pattern() { 71 NumberStringConverter c = new NumberStringConverter(Locale.CANADA, "#,##,###,####"); 72 assertEquals(Locale.CANADA, c.locale); 73 assertEquals("#,##,###,####", c.pattern); 74 assertNull(c.numberFormat); 75 } 76 77 @Test public void testConstructor_numberFormat() { 78 NumberFormat format = NumberFormat.getCurrencyInstance(Locale.JAPAN); 79 NumberStringConverter c = new NumberStringConverter(format); 80 assertNull(c.locale); 81 assertNull(c.pattern); 82 assertEquals(format, c.numberFormat); 83 } 84 85 86 /********************************************************************* 87 * Test methods 88 ********************************************************************/ 89 90 @Test public void getNumberFormat_default() { 91 assertNotNull(converter.getNumberFormat()); 92 } 93 94 @Test public void getNumberFormat_nonNullPattern() { 95 converter = new NumberStringConverter("#,##,###,####"); 96 assertTrue(converter.getNumberFormat() instanceof DecimalFormat); 97 } 98 99 @Test public void getNumberFormat_nonNullNumberFormat() { 100 NumberFormat nf = NumberFormat.getCurrencyInstance(); 101 converter = new NumberStringConverter(nf); 102 assertEquals(nf, converter.getNumberFormat()); 103 } 104 105 106 /********************************************************************* 107 * Test toString / fromString methods 108 ********************************************************************/ 109 110 @Test public void fromString_testValidInput() { 111 assertEquals(10L, converter.fromString("10")); 112 } 113 114 @Test public void fromString_testValidInputWithWhiteSpace() { 115 assertEquals(10L, converter.fromString(" 10 ")); 116 } 117 118 @Test(expected=RuntimeException.class) 119 public void fromString_testInvalidInput() { 120 converter.fromString("abcdefg"); 121 } 122 123 @Test public void toString_validInput() { 124 assertEquals("10", converter.toString(10L)); 125 } 126 }