1 /*
2  * Copyright (c) 2005, 2016, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 4068067
27  * @library /java/text/testlib
28  * @summary test NumberFormat with exponential separator symbols. It also tests the new
29  *          public methods in DecimalFormatSymbols, setExponentSeparator() and
30  *          getExponentSeparator()
31  */
32 
33 package test.java.text.Format.NumberFormat;
34 
35 import java.util.*;
36 import java.text.*;
37 
38 import test.java.text.testlib.IntlTest;
39 
40 public class DFSExponential extends IntlTest
41 {
42 
main(String[] args)43     public static void main(String[] args) throws Exception {
44         new DFSExponential().run(args);
45     }
46 
47 
DFSExponenTest()48    public void DFSExponenTest() throws Exception {
49         DecimalFormatSymbols sym = new DecimalFormatSymbols(Locale.US);
50         String pat[] = { "0.####E0", "00.000E00", "##0.####E000", "0.###E0;[0.###E0]"  };
51         double val[] = { 0.01234, 123456789, 1.23e300, -3.141592653e-271 };
52         long lval[] = { 0, -1, 1, 123456789 };
53         String valFormat[][] = {
54                 {"1.234x10^-2", "1.2346x10^8", "1.23x10^300", "-3.1416x10^-271"},
55                 {"12.340x10^-03", "12.346x10^07", "12.300x10^299", "-31.416x10^-272"},
56                 {"12.34x10^-003", "123.4568x10^006", "1.23x10^300", "-314.1593x10^-273"},
57                 {"1.234x10^-2", "1.235x10^8", "1.23x10^300", "[3.142x10^-271]"},
58         };
59 
60 
61         int ival = 0, ilval = 0;
62         logln("Default exponent separator: "+sym.getExponentSeparator());
63         try {
64             sym.setExponentSeparator("x10^");
65         } catch (NullPointerException e){
66             errln("null String was passed to set an exponent separator symbol");
67             throw new RuntimeException("Test Malfunction: null String was passed to set an exponent separator symbol" );
68         }
69         logln("Current exponent separator: "+sym.getExponentSeparator());
70 
71         for (int p=0; p<pat.length; ++p){
72             DecimalFormat fmt = new DecimalFormat(pat[p], sym);
73             logln("     Pattern: " + fmt.toPattern());
74             String locPattern = fmt.toLocalizedPattern();
75             logln("     Localized pattern: "+locPattern);
76             //fmt.applyLocalizedPattern(locPattern);
77             //System.out.println("      fmt.applyLocalizedPattern(): "+fmt.toLocalizedPattern());
78 
79             for (int v=0; v<val.length; ++v) {
80                 String s = fmt.format(val[v]);
81                 logln("         " + val[v]+" --> "+s);
82                 if(valFormat[p][v].equals(s)){
83                     logln(": Passed");
84                 }else{
85                    errln(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);
86                    throw new RuntimeException(" Failed: Should be formatted as "+valFormat[p][v]+ "but got "+s);
87                 }
88            }
89          } //end of the first for loop
90    }
91 }
92