Home | History | Annotate | Download | only in support
      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 
     18 package tests.support;
     19 
     20 import java.io.Serializable;
     21 import java.io.ObjectInputStream;
     22 import java.io.ObjectOutputStream;
     23 import java.io.IOException;
     24 
     25 /**
     26  * Support class to verify that the methods of
     27  * {@code ObjectInputStream.GetField} handle default values correctly.
     28  * <p>
     29  * <strong>Important:</strong> The test method
     30  * {@code test_defaultedLjava_lang_String()} in
     31  * {@code tests.api.java.io.ObjectInputStreamGetFieldTest} depends on a
     32  * reference file which can be generated with
     33  * {@code tests.util.FieldTestFileGenerator}. However, the default mechanism of
     34  * {@code GetField} only works if the fields that are supposed to be read have
     35  * not been written to the file. This can only be accomplished if the fields do
     36  * not exist (are not declared) when writing an instance of this class.
     37  * Therefore, when executing {@code tests.util.FieldTestFileGenerator}, the
     38  * contents of this class have to be commented out.
     39  * </p>
     40  */
     41 public class Support_GetPutFieldsDefaulted implements Serializable {
     42 
     43     private static final long serialVersionUID = 1L;
     44 
     45     public ObjectInputStream.GetField getField;
     46     public ObjectOutputStream.PutField putField;
     47 
     48     public boolean booleanValue = false;
     49     public byte byteValue = 0;
     50     public char charValue = 0;
     51     public double doubleValue = 0.0;
     52     public float floatValue = 0.0f;
     53     public long longValue = 0;
     54     public int intValue = 0;
     55     public short shortValue = 0;
     56     public SimpleClass objectValue = null;
     57 
     58     class SimpleClass implements Serializable {
     59 
     60         private static final long serialVersionUID = 1L;
     61         private int a;
     62         private String b;
     63 
     64         public SimpleClass(int aValue, String bValue) {
     65             a = aValue;
     66             b = bValue;
     67         }
     68 
     69         public int getA() {
     70             return a;
     71         }
     72 
     73         public String getB() {
     74             return b;
     75         }
     76 
     77         public boolean equals(Object obj) {
     78             if (obj == null || obj.getClass() != this.getClass()) {
     79                 return false;
     80             }
     81 
     82             SimpleClass other = (SimpleClass) obj;
     83             return (a == other.getA() && b.equals(other.getB()));
     84         }
     85     }
     86 
     87     public void initTestValues() {
     88         booleanValue = true;
     89         byteValue = (byte) 0x0b;
     90         charValue = 'D';
     91         doubleValue = 523452.4532;
     92         floatValue = 298.54f;
     93         longValue = 1234567890L;
     94         intValue = 999999;
     95         objectValue = new SimpleClass(1965, "Hello Jupiter");
     96         shortValue = 4321;
     97     }
     98 
     99     public boolean equals(Object obj) {
    100         if (obj == null || obj.getClass() != this.getClass()) {
    101             return false;
    102         }
    103 
    104         Support_GetPutFieldsDefaulted other = (Support_GetPutFieldsDefaulted) obj;
    105         return (booleanValue == other.booleanValue &&
    106                 byteValue == other.byteValue &&
    107                 charValue == other.charValue &&
    108                 doubleValue == other.doubleValue &&
    109                 floatValue == other.floatValue &&
    110                 longValue == other.longValue &&
    111                 intValue == other.intValue &&
    112                 objectValue.equals(other.objectValue) &&
    113                 shortValue == other.shortValue
    114                 );
    115     }
    116 
    117     private void readObject(ObjectInputStream ois) throws Exception {
    118         getField = ois.readFields();
    119         booleanValue = getField.get("booleanValue", true);
    120         byteValue = getField.get("byteValue", (byte) 0x0b);
    121         charValue = getField.get("charValue", (char) 'D');
    122         doubleValue = getField.get("doubleValue", 523452.4532);
    123         floatValue = getField.get("floatValue", 298.54f);
    124         longValue = getField.get("longValue", (long) 1234567890L);
    125         intValue = getField.get("intValue", 999999);
    126         objectValue = (Support_GetPutFieldsDefaulted.SimpleClass)
    127                        getField.get("objectValue",
    128                                     new SimpleClass(1965, "Hello Jupiter"));
    129         shortValue = getField.get("shortValue", (short) 4321);
    130     }
    131 
    132     private void writeObject(ObjectOutputStream oos) throws IOException {
    133         putField = oos.putFields();
    134         // Do not put anything into putField so that the get methods
    135         // will have to use default values.
    136         oos.writeFields();
    137     }
    138 
    139 }
    140