1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.support;
19
20import java.io.IOException;
21import java.io.ObjectInputStream;
22import java.io.ObjectOutputStream;
23import java.io.Serializable;
24
25/**
26 * Support class to test the methods of {@code ObjectInputStream.GetField} and
27 * {@code ObjectOutputStream.PutField}. The difference with class
28 * {@code Support_GetPutFields} is that it uses the deprecated method
29 * {@code ObjectOutputStream.PutField.write(ObjectOutput)} to write the fields
30 * to the output stream.
31 */
32public class Support_GetPutFieldsDeprecated implements Serializable {
33
34    private static final long serialVersionUID = 1L;
35
36    public ObjectInputStream.GetField getField;
37    public ObjectOutputStream.PutField putField;
38
39    public boolean booleanValue = false;
40    public byte byteValue = 0;
41    public char charValue = 0;
42    public double doubleValue = 0.0;
43    public float floatValue = 0.0f;
44    public long longValue = 0;
45    public int intValue = 0;
46    public short shortValue = 0;
47    public SimpleClass objectValue = null;
48
49    class SimpleClass implements Serializable {
50
51        private static final long serialVersionUID = 1L;
52        private int a;
53        private String b;
54
55        public SimpleClass(int aValue, String bValue) {
56            a = aValue;
57            b = bValue;
58        }
59
60        public int getA() {
61            return a;
62        }
63
64        public String getB() {
65            return b;
66        }
67
68        public boolean equals(Object obj) {
69            if (obj == null || obj.getClass() != this.getClass()) {
70                return false;
71            }
72
73            SimpleClass other = (SimpleClass) obj;
74            return (a == other.getA() && b.equals(other.getB()));
75        }
76    }
77
78    public void initTestValues() {
79        booleanValue = true;
80        byteValue = (byte) 0xbe;
81        charValue = 'A';
82        doubleValue = 1231.342;
83        floatValue = 43.22f;
84        longValue = 1560732321L;
85        intValue = 33333;
86        objectValue = new SimpleClass(2001, "A Space Odyssey");
87        shortValue = 3078;
88    }
89
90    public boolean equals(Object obj) {
91        if (obj == null || obj.getClass() != this.getClass()) {
92            return false;
93        }
94
95        Support_GetPutFields other = (Support_GetPutFields) obj;
96        return (booleanValue == other.booleanValue &&
97                byteValue == other.byteValue &&
98                charValue == other.charValue &&
99                doubleValue == other.doubleValue &&
100                floatValue == other.floatValue &&
101                longValue == other.longValue &&
102                intValue == other.intValue &&
103                objectValue.equals(other.objectValue) &&
104                shortValue == other.shortValue
105                );
106    }
107
108    private void readObject(ObjectInputStream ois) throws Exception {
109        booleanValue = getField.get("booleanValue", false);
110        byteValue = getField.get("byteValue", (byte) 0);
111        charValue = getField.get("charValue", (char) 0);
112        doubleValue = getField.get("doubleValue", 0.0);
113        floatValue = getField.get("floatValue", 0.0f);
114        longValue = getField.get("longValue", (long) 0);
115        intValue = getField.get("intValue", 0);
116        objectValue = (Support_GetPutFieldsDeprecated.SimpleClass)
117                getField.get("objectValue", (Object) null);
118        shortValue = getField.get("shortValue", (short) 0);
119    }
120
121    private void writeObject(ObjectOutputStream oos) throws IOException {
122        putField = oos.putFields();
123        putField.put("booleanValue", booleanValue);
124        putField.put("byteValue", byteValue);
125        putField.put("charValue", charValue);
126        putField.put("doubleValue", doubleValue);
127        putField.put("floatValue", floatValue);
128        putField.put("longValue", longValue);
129        putField.put("intValue", intValue);
130        putField.put("objectValue", objectValue);
131        putField.put("shortValue", shortValue);
132        putField.write(oos);
133    }
134}
135