aboutsummaryrefslogtreecommitdiff
path: root/test/java/text/Format/NumberFormat/DFSSerialization.java
blob: 40b40b39b8719be346d7d5a807140125533ee0dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4068067
 * @library /java/text/testlib
 * @build DFSSerialization IntlTest HexDumpReader
 * @run main DFSSerialization
 * @summary Three different tests are done. 1.read from the object created using jdk1.4.2  2.create a valid DecimalFormatSymbols object with current JDK, then read the object 3.Try to create an valid DecimalFormatSymbols object by passing null to set null for the exponent separator symbol. Expect the NullPointerException.
 */

import java.awt.*;
import java.text.*;
import java.util.*;
import java.io.*;

public class DFSSerialization extends IntlTest{
    public static void main(String[] args) throws Exception {
        new DFSSerialization().run(args);
    }
    public void TestDFSSerialization(){
        /*
         * 1. read from the object created using jdk1.4.2
         */
        File oldFile = new File(System.getProperty("test.src", "."), "DecimalFormatSymbols.142.txt");
        DecimalFormatSymbols dfs142 = readTestObject(oldFile);
        if (dfs142 != null){
            if (dfs142.getExponentSeparator().equals("E") && dfs142.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){
                System.out.println("\n  Deserialization of JDK1.4.2 Object from the current JDK: Passed.");
                logln(" Deserialization of JDK1.4.2 Object from the current JDK: Passed.");
            } else {
                errln(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"
                      +dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());
                /*
                 * logically should not throw this exception as errln throws exception
                 * if not thrown yet - but in case errln got changed
                 */
                throw new RuntimeException(" Deserialization of JDK1.4.2 Object from the current JDK was Failed:"
                                           +dfs142.getCurrencySymbol()+" "+dfs142.getExponentSeparator());
            }
        }
        /*
         * 2. create a valid DecimalFormatSymbols object with current JDK, then read the object
         */
        String validObject = "DecimalFormatSymbols.current";
        File currentFile = createTestObject(validObject, "*SpecialExponentSeparator*");

        DecimalFormatSymbols dfsValid = readTestObject(currentFile);
        if (dfsValid != null){
            if (dfsValid.getExponentSeparator().equals("*SpecialExponentSeparator*") &&
                dfsValid.getCurrencySymbol().equals("*SpecialCurrencySymbol*")){
                System.out.println("  Deserialization of current JDK Object from the current JDK: Passed.");
                logln(" Deserialization of current JDK Object from the current JDK: Passed.");
            } else {
                errln(" Deserialization of current JDK Object from the current JDK was Failed:"
                      +dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());
                /*
                 * logically should not throw this exception as errln throws exception
                 * if not thrown yet - but in case errln got changed
                 */
                throw new RuntimeException(" Deserialization of current Object from the current JDK was Failed:"
                                           +dfsValid.getCurrencySymbol()+" "+dfsValid.getExponentSeparator());
            }
        }
        /*
         * 3. Try to create an valid DecimalFormatSymbols object by passing null
         *    to set null for the exponent separator symbol. Expect the NullPointerException.
         */
        DecimalFormatSymbols symNPE = new DecimalFormatSymbols(Locale.US);
        boolean npePassed = false;
        try {
            symNPE.setExponentSeparator(null);
        } catch (NullPointerException npe){
            npePassed = true;
            System.out.println("  Trying to set exponent separator with null: Passed.");
            logln(" Trying to set exponent separator with null: Passed.");
        }
        if (!npePassed){
            System.out.println(" Trying to set exponent separator with null:Failed.");
            errln("  Trying to set exponent separator with null:Failed.");
            /*
             * logically should not throw this exception as errln throws exception
             * if not thrown yet - but in case errln got changed
             */
            throw new RuntimeException(" Trying to set exponent separator with null:Failed.");
        }

    }

    private DecimalFormatSymbols readTestObject(File inputFile){
        try (InputStream istream = inputFile.getName().endsWith(".txt") ?
                                       HexDumpReader.getStreamFromHexDump(inputFile) :
                                       new FileInputStream(inputFile)) {
            ObjectInputStream p = new ObjectInputStream(istream);
            DecimalFormatSymbols dfs = (DecimalFormatSymbols)p.readObject();
            return dfs;
        } catch (Exception e) {
            errln("Test Malfunction in DFSSerialization: Exception while reading the object");
            /*
             * logically should not throw this exception as errln throws exception
             * if not thrown yet - but in case errln got changed
             */
            throw new RuntimeException("Test Malfunction: re-throwing the exception", e);
        }
    }

    private File createTestObject(String objectName, String expString){
        DecimalFormatSymbols dfs= new DecimalFormatSymbols();
        dfs.setExponentSeparator(expString);
        dfs.setCurrencySymbol("*SpecialCurrencySymbol*");
        logln(" The special exponent separator is set : "  + dfs.getExponentSeparator());
        logln(" The special currency symbol is set : "  + dfs.getCurrencySymbol());

        // 6345659: create a test object in the test.class dir where test user has a write permission.
        File file = new File(System.getProperty("test.class", "."), objectName);
        try (FileOutputStream ostream = new FileOutputStream(file)) {
            ObjectOutputStream p = new ObjectOutputStream(ostream);
            p.writeObject(dfs);
            //System.out.println(" The special currency symbol is set : "  + dfs.getCurrencySymbol());
            return file;
        } catch (Exception e){
            errln("Test Malfunction in DFSSerialization: Exception while creating an object");
            /*
             * logically should not throw this exception as errln throws exception
             * if not thrown yet - but in case errln got changed
             */
            throw new RuntimeException("Test Malfunction: re-throwing the exception", e);
        }
    }
}