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.Serializable;
21import java.io.ObjectInputStream;
22import java.io.ObjectOutputStream;
23import java.io.IOException;
24
25/**
26 * Support class to test the methods of {@code ObjectInputStream.GetField} and
27 * {@code ObjectOutputStream.PutField} to write fields to an output stream and
28 * read them back from an input stream.
29 */
30public class Support_GetPutFields implements Serializable {
31
32    private static final long serialVersionUID = 1L;
33
34    public ObjectInputStream.GetField getField;
35    public ObjectOutputStream.PutField putField;
36
37    public boolean booleanValue = false;
38    public byte byteValue = 0;
39    public char charValue = 0;
40    public double doubleValue = 0.0;
41    public float floatValue = 0.0f;
42    public long longValue = 0;
43    public int intValue = 0;
44    public short shortValue = 0;
45    public SimpleClass objectValue = null;
46
47    class SimpleClass implements Serializable {
48
49        private static final long serialVersionUID = 1L;
50        private int a;
51        private String b;
52
53        public SimpleClass(int aValue, String bValue) {
54            a = aValue;
55            b = bValue;
56        }
57
58        public int getA() {
59            return a;
60        }
61
62        public String getB() {
63            return b;
64        }
65
66        public boolean equals(Object obj) {
67            if (obj == null || obj.getClass() != this.getClass()) {
68                return false;
69            }
70
71            SimpleClass other = (SimpleClass) obj;
72            return (a == other.getA() && b.equals(other.getB()));
73        }
74    }
75
76    public void initTestValues() {
77        booleanValue = true;
78        byteValue = (byte) 0xbe;
79        charValue = 'B';
80        doubleValue = 424242.42;
81        floatValue = 24.12f;
82        longValue = 6700654321l;
83        intValue = 77777;
84        objectValue = new SimpleClass(1965, "Hello Mars");
85        shortValue = 1234;
86    }
87
88    public boolean equals(Object obj) {
89        if (obj == null || obj.getClass() != this.getClass()) {
90            return false;
91        }
92
93        Support_GetPutFields other = (Support_GetPutFields) obj;
94        return (booleanValue == other.booleanValue &&
95                byteValue == other.byteValue &&
96                charValue == other.charValue &&
97                doubleValue == other.doubleValue &&
98                floatValue == other.floatValue &&
99                longValue == other.longValue &&
100                intValue == other.intValue &&
101                objectValue.equals(other.objectValue) &&
102                shortValue == other.shortValue
103                );
104    }
105
106    private void readObject(ObjectInputStream ois) throws Exception {
107        getField = ois.readFields();
108        booleanValue = getField.get("booleanValue", false);
109        byteValue = getField.get("byteValue", (byte) 0);
110        charValue = getField.get("charValue", (char) 0);
111        doubleValue = getField.get("doubleValue", 0.0);
112        floatValue = getField.get("floatValue", 0.0f);
113        longValue = getField.get("longValue", (long) 0);
114        intValue = getField.get("intValue", 0);
115        objectValue = (Support_GetPutFields.SimpleClass)
116                getField.get("objectValue", (Object) null);
117        shortValue = getField.get("shortValue", (short) 0);
118    }
119
120    private void writeObject(ObjectOutputStream oos) throws IOException {
121        putField = oos.putFields();
122        putField.put("booleanValue", booleanValue);
123        putField.put("byteValue", byteValue);
124        putField.put("charValue", charValue);
125        putField.put("doubleValue", doubleValue);
126        putField.put("floatValue", floatValue);
127        putField.put("longValue", longValue);
128        putField.put("intValue", intValue);
129        putField.put("objectValue", objectValue);
130        putField.put("shortValue", shortValue);
131        oos.writeFields();
132    }
133}
134