1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @bug 8888888
  26  * @compile FailureAtomicity.java Expose.java foo/foo/HomeAddress.java bar/bar/HomeAddress.java
  27  * @run main failureAtomicity.FailureAtomicity
  28  * @summary Best effort Failure Atomicity for default read object.
  29  */
  30 package failureAtomicity;
  31 
  32 import java.io.ByteArrayInputStream;
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.ObjectInputStream;
  35 import java.io.ObjectOutputStream;
  36 
  37 public class FailureAtomicity {
  38     public static void main(String[] args) throws Exception {
  39         failureAtomicity.bar.bar.HomeAddress barHomeAddress =
  40                 new failureAtomicity.bar.bar.HomeAddress(5, "Dublin", "Dublin",
  41                                                  "Ireland", new byte[] { 0x01 });
  42 
  43         try { toFooHomeAddress(barHomeAddress); }
  44         catch (ClassCastException e) { /* Expected */ }
  45 
  46         failureAtomicity.foo.foo.HomeAddress fooHomeAddress =
  47                 (failureAtomicity.foo.foo.HomeAddress)Expose.obj;
  48 
  49         System.out.println("fooHomeAddress: " + fooHomeAddress);
  50 
  51         if (fooHomeAddress.houseNumber != 0 /*|| fooHomeAddress.city != null*/
  52             || fooHomeAddress.county != null || fooHomeAddress.country != null)
  53             throw new RuntimeException("Some fields are being set");
  54     }
  55 
  56     static failureAtomicity.foo.foo.HomeAddress
  57         toFooHomeAddress(failureAtomicity.bar.bar.HomeAddress barHomeAddress)
  58         throws Exception
  59     {
  60         byte[] bytes = serialize(barHomeAddress);
  61         bytes = barToFoo(bytes);
  62         return (failureAtomicity.foo.foo.HomeAddress)deserialize(bytes);
  63     }
  64 
  65     @SuppressWarnings("deprecation")
  66     static byte[] barToFoo(byte[] bytes) {
  67         String str = new String(bytes, 0);
  68         str = str.replaceAll("bar.bar", "foo.foo");
  69         str.getBytes(0, bytes.length, bytes, 0);
  70         return bytes;
  71     }
  72 
  73     static byte[] serialize(Object obj) throws Exception {
  74         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  75              ObjectOutputStream out = new ObjectOutputStream(baos);) {
  76             out.writeObject(obj);
  77             return baos.toByteArray();
  78         }
  79     }
  80 
  81     static Object deserialize(byte[] data) throws Exception {
  82         return new ObjectInputStream(new ByteArrayInputStream(data))
  83                 .readObject();
  84     }
  85 }