1 /*
2  * Copyright (c) 2005, 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 6213608
27  * @summary Test that AffineTransform can deserialize appropriate versions.
28  * @author flar
29  * @run main TestSerialization
30  */
31 
32 import java.io.FileInputStream;
33 import java.io.ObjectInputStream;
34 import java.io.ByteArrayInputStream;
35 import java.io.FileOutputStream;
36 import java.io.ObjectOutputStream;
37 import java.io.ByteArrayOutputStream;
38 import java.io.IOException;
39 import java.io.InvalidClassException;
40 import java.awt.geom.AffineTransform;
41 
42 public class TestSerialization {
main(String argv[])43     public static void main(String argv[]) {
44         if (argv.length > 0) {
45             System.out.println("Saving from: "+
46                                System.getProperty("java.version"));
47             writeSer(argv[0]);
48             return;
49         }
50         System.out.println("Testing on: "+System.getProperty("java.version"));
51         testReadWrite();
52         readSer("serial.1.2", true);
53     }
54 
55     public static AffineTransform testATs[] = {
56         new AffineTransform(),
57         AffineTransform.getScaleInstance(2.5, -3.0),
58         AffineTransform.getRotateInstance(Math.PI / 4.0),
59         AffineTransform.getShearInstance(1.0, -3.0),
60         AffineTransform.getTranslateInstance(25.0, 12.5),
61         makeComplexAT(),
62     };
63 
makeComplexAT()64     public static AffineTransform makeComplexAT() {
65         AffineTransform at = new AffineTransform();
66         at.scale(2.5, -3.0);
67         at.rotate(Math.PI / 4.0);
68         at.shear(1.0, -3.0);
69         at.translate(25.0, 12.5);
70         return at;
71     };
72 
testReadWrite()73     public static void testReadWrite() {
74         try {
75             ByteArrayOutputStream baos = new ByteArrayOutputStream();
76             ObjectOutputStream oos = new ObjectOutputStream(baos);
77             testWrite(oos);
78             oos.flush();
79             oos.close();
80             byte buf[] = baos.toByteArray();
81             ByteArrayInputStream bais = new ByteArrayInputStream(buf);
82             ObjectInputStream ois = new ObjectInputStream(bais);
83             testRead(ois, true);
84         } catch (InvalidClassException ice) {
85             throw new RuntimeException("Object read failed from loopback");
86         } catch (IOException e) {
87             e.printStackTrace();
88             throw new RuntimeException("IOException testing loopback");
89         }
90     }
91 
resolve(String relfilename)92     public static String resolve(String relfilename) {
93         String dir = System.getProperty("test.src");
94         if (dir == null) {
95             return relfilename;
96         } else {
97             return dir+"/"+relfilename;
98         }
99     }
100 
readSer(String filename, boolean shouldsucceed)101     public static void readSer(String filename, boolean shouldsucceed) {
102         try {
103             FileInputStream fis = new FileInputStream(resolve(filename));
104             ObjectInputStream ois = new ObjectInputStream(fis);
105             testRead(ois, shouldsucceed);
106         } catch (InvalidClassException ice) {
107             throw new RuntimeException("Object read failed from: "+filename);
108         } catch (IOException e) {
109             e.printStackTrace();
110             throw new RuntimeException("IOException reading: "+filename);
111         }
112     }
113 
testRead(ObjectInputStream ois, boolean shouldsucceed)114     public static void testRead(ObjectInputStream ois, boolean shouldsucceed)
115         throws IOException
116     {
117         for (int i = 0; i < testATs.length; i++) {
118             AffineTransform at;
119             try {
120                 at = (AffineTransform) ois.readObject();
121                 if (!shouldsucceed) {
122                     throw new RuntimeException("readObj did not fail");
123                 }
124             } catch (ClassNotFoundException e) {
125                 // Should never happen, but must catch declared exceptions...
126                 throw new RuntimeException("AffineTransform not found!");
127             } catch (InvalidClassException e) {
128                 if (shouldsucceed) {
129                     throw e;
130                 }
131                 continue;
132             }
133             if (!testATs[i].equals(at)) {
134                 throw new RuntimeException("wrong AT read from stream");
135             }
136         }
137     }
138 
writeSer(String filename)139     public static void writeSer(String filename) {
140         try {
141             FileOutputStream fos = new FileOutputStream(filename);
142             ObjectOutputStream oos = new ObjectOutputStream(fos);
143             testWrite(oos);
144             oos.flush();
145             oos.close();
146         } catch (IOException e) {
147             e.printStackTrace();
148             throw new RuntimeException("IOException writing: "+filename);
149         }
150     }
151 
testWrite(ObjectOutputStream oos)152     public static void testWrite(ObjectOutputStream oos)
153         throws IOException
154     {
155         for (int i = 0; i < testATs.length; i++) {
156             oos.writeObject(testATs[i]);
157         }
158     }
159 }
160