jdk/test/java/beans/XMLEncoder/AbstractTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.beans.ExceptionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.beans.XMLDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.beans.XMLEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
abstract class AbstractTest<T> implements ExceptionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private final BeanValidator validator = new BeanValidator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public final void exceptionThrown(Exception exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        throw new Error("unexpected exception", exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Returns an object to test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * This object will be encoded and decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * and the object creation will be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * @return an object to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected abstract T getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Returns a different object to test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * If this object is not {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * it will be encoded and decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * and the object updating will be tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @return a different object to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected T getAnotherObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * This method should be overriden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * if specified encoder should be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param encoder  the XML encoder to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected void initialize(XMLEncoder encoder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * This method should be overriden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * if specified decoder should be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param decoder  the XML decoder to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected void initialize(XMLDecoder decoder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * This method should be overriden
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * for test-specific comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param before  the object before encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param after   the object after decoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected void validate(T before, T after) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this.validator.validate(before, after);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * This is entry point to start testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param security  use {@code true} to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *                  second pass in secure context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    final void test(boolean security) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Bean.DEFAULT = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        T object = getObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("Test object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        validate(object, testObject(object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("Test object creating");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        validate(object, testBean(object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Bean.DEFAULT = object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        object = getAnotherObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (object != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            System.out.println("Test another object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            validate(object, testObject(object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Test object updating");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            validate(object, testBean(object));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (security) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.setSecurityManager(new SecurityManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            test(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private T testBean(T object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Bean bean = new Bean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        bean.setValue(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        bean = testObject(bean);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return (T) bean.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private <Z> Z testObject(Z object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        byte[] array = writeObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.out.print(new String(array, Charset.forName("UTF-8")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return (Z) readObject(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private byte[] writeObject(Object object) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ByteArrayOutputStream output = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        XMLEncoder encoder = new XMLEncoder(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        initialize(encoder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        encoder.writeObject(object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        encoder.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return output.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private Object readObject(byte[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ByteArrayInputStream input = new ByteArrayInputStream(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        XMLDecoder decoder = new XMLDecoder(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        initialize(decoder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Object object = decoder.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        decoder.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
}