File: SerialTest.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (285 lines) | stat: -rw-r--r-- 9,935 bytes parent folder | download | duplicates (16)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (c) 2005, 2018, 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 4263142 4172661
 * @summary First run verifies that objects serialize and deserialize
 *          correctly against the current release.
 *          Second run verifies that objects from previous releases
 *          still deserialize correctly.  The serial_1_6.out file was
 *          created using the "write" option under release 1.6.
 *          The test was modified after fixing 4172661 to add testing
 *          of Path2D serialization (and to recut the test file with
 *          the new serialVersionUID of GeneralPath).
 * @run main SerialTest
 * @run main SerialTest read serial_1_6.out
 */

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class SerialTest {
    public static Object testobjects[] = {
        // non-shapes...
        new Point2D.Float(37, 42),
        new Point2D.Double(85, 63),
        new AffineTransform(10, 20, 30, 40, 50, 60),

        // shapes...
        new QuadCurve2D.Float(10f, 10f, 50f, 50f, 100f, 10f),
        new QuadCurve2D.Double(20f, 20f, 50f, 50f, 100f, 20f),
        new CubicCurve2D.Float(10f, 10f, 50f, 10f, 10f, 50f, 50f, 50f),
        new CubicCurve2D.Double(0.0, 0.0, 50.0, 0.0, 0.0, 50.0, 50.0, 50.0),
        new GeneralPath(),
        new GeneralPath(PathIterator.WIND_NON_ZERO),
        new GeneralPath(PathIterator.WIND_EVEN_ODD),
        makeGeneralPath(PathIterator.WIND_NON_ZERO, 5f),
        makeGeneralPath(PathIterator.WIND_EVEN_ODD, 23f),
        new Line2D.Float(20f, 20f, 25f, 50f),
        new Line2D.Double(20.0, 20.0, 35.0, 50.0),
        new Rectangle2D.Float(100f, 100f, 50f, 25f),
        new Rectangle2D.Double(200.0, 200.0, 75.0, 35.0),
        new RoundRectangle2D.Float(120f, 120f, 50f, 35f, 5f, 7f),
        new RoundRectangle2D.Double(220.0, 220.0, 85.0, 45.0, 3.0, 9.0),
        new Ellipse2D.Float(110f, 110f, 50f, 55f),
        new Ellipse2D.Double(210.0, 210.0, 75.0, 45.0),
        new Arc2D.Float(10f, 10f, 50f, 40f, 45f, 72f, Arc2D.OPEN),
        new Arc2D.Float(10f, 10f, 40f, 50f, 135f, 72f, Arc2D.PIE),
        new Arc2D.Float(10f, 10f, 40f, 60f, 225f, 72f, Arc2D.CHORD),
        new Arc2D.Double(10.0, 20.0, 50.0, 40.0, 45.0, 72.0, Arc2D.OPEN),
        new Arc2D.Double(10.0, 20.0, 40.0, 50.0, 135.0, 72.0, Arc2D.PIE),
        new Arc2D.Double(10.0, 20.0, 40.0, 60.0, 225.0, 72.0, Arc2D.CHORD),

        // Paths
        new Path2D.Float(),
        new Path2D.Float(PathIterator.WIND_NON_ZERO),
        new Path2D.Float(PathIterator.WIND_EVEN_ODD),
        makePath2DFloat(PathIterator.WIND_NON_ZERO, 5f),
        makePath2DFloat(PathIterator.WIND_EVEN_ODD, 23f),
        new Path2D.Double(),
        new Path2D.Double(PathIterator.WIND_NON_ZERO),
        new Path2D.Double(PathIterator.WIND_EVEN_ODD),
        makePath2DDouble(PathIterator.WIND_NON_ZERO, 5f),
        makePath2DDouble(PathIterator.WIND_EVEN_ODD, 23f),
    };

    public static Shape makeGeneralPath(int winding, float off) {
        return fill(new GeneralPath(winding), off);
    }

    public static Shape makePath2DFloat(int winding, float off) {
        return fill(new Path2D.Float(winding), off);
    }

    public static Shape makePath2DDouble(int winding, float off) {
        return fill(new Path2D.Double(winding), off);
    }

    public static Path2D fill(Path2D p2d, float off) {
        p2d.moveTo(off+10, off+10);
        p2d.lineTo(off+100, off+50);
        p2d.quadTo(off+50, off+100, off+200, off+100);
        p2d.curveTo(off+400, off+20, off+20, off+400, off+100, off+100);
        p2d.closePath();
        return p2d;
    }

    static int numerrors;

    public static void error(Object o1, Object o2, String reason) {
        System.err.println("Failed comparing: "+o1+" to "+o2);
        System.err.println(reason);
        numerrors++;
    }

    public static void usage(int exitcode) {
        System.err.println("usage: java SerialTest [read|write] <filename>");
        System.exit(exitcode);
    }

    public static void main(String argv[]) {
        if (argv.length > 0) {
            if (argv.length < 2) {
                usage(1);
            }

            String arg = argv[0].toLowerCase();
            if (arg.equals("write")) {
                serializeTo(argv[1]);
            } else if (arg.equals("read")) {
                testFrom(argv[1]);
            } else {
                usage(1);
            }
        } else {
            testSerial();
        }
    }

    public static File makeFile(String filename) {
        return new File(System.getProperty("test.src", "."), filename);
    }

    public static void serializeTo(String filename) {
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(makeFile(filename));
        } catch (IOException ioe) {
            throw new InternalError("bad output filename: "+filename);
        }
        serializeTo(fos);
    }

    public static void testFrom(String filename) {
        FileInputStream fis;
        try {
            fis = new FileInputStream(makeFile(filename));
        } catch (IOException ioe) {
            throw new InternalError("bad input filename: "+filename);
        }
        testFrom(fis);
    }

    public static void testSerial() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        serializeTo(baos);

        byte buf[] = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);

        testFrom(bais);
    }

    public static void serializeTo(OutputStream os) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(os);

            for (Object o1: testobjects) {
                oos.writeObject(o1);
            }

            oos.close();
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    public static void testFrom(InputStream is) {
        try {
            ObjectInputStream ois = new ObjectInputStream(is);

            for (Object o1: testobjects) {
                Object o2 = ois.readObject();
                if (o1 instanceof Shape) {
                    compareShapes((Shape) o1, (Shape) o2);
                } else {
                    if (!o1.equals(o2)) {
                        error(o1, o2, "objects not equal");
                    }
                }
            }

            try {
                ois.readObject();
                throw new RuntimeException("extra data in stream");
            } catch (IOException ioe2) {
            }
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        } catch (ClassNotFoundException cnfe) {
            throw new RuntimeException(cnfe);
        }
    }

    public static void compareShapes(Shape s1, Shape s2) {
        PathIterator pi1 = s1.getPathIterator(null);
        PathIterator pi2 = s2.getPathIterator(null);

        if (pi1.getWindingRule() != pi2.getWindingRule()) {
            error(s1, s2, "winding rules are different");
        }

        double coords1[] = new double[6];
        double coords2[] = new double[6];

        while (!pi1.isDone()) {
            if (pi2.isDone()) {
                error(s1, s2, "Shape 2 ended prematurely");
                return;
            }

            int t1 = pi1.currentSegment(coords1);
            int t2 = pi2.currentSegment(coords2);

            if (t1 != t2) {
                error(s1, s2, "different segment types");
            }

            int ncoords;
            switch (t1) {
            case PathIterator.SEG_MOVETO:  ncoords = 2; break;
            case PathIterator.SEG_LINETO:  ncoords = 2; break;
            case PathIterator.SEG_QUADTO:  ncoords = 4; break;
            case PathIterator.SEG_CUBICTO: ncoords = 6; break;
            case PathIterator.SEG_CLOSE:   ncoords = 0; break;

            default:
                throw new RuntimeException("unknown segment type");
            }

            for (int i = 0; i < ncoords; i++) {
                if (coords1[i] != coords2[i]) {
                    error(s1, s2, "coordinates differ");
                }
            }
            pi1.next();
            pi2.next();
        }

        if (!pi2.isDone()) {
            error(s1, s2, "Shape 1 ended prematurely");
        }
    }
}