1 /*
   2  * Copyright (c) 2015, 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.runtime.test;
  26 
  27 import java.io.ByteArrayInputStream;
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 import jdk.nashorn.internal.objects.NativeSymbol;
  32 import jdk.nashorn.internal.runtime.Symbol;
  33 import org.testng.Assert;
  34 import org.testng.annotations.Test;
  35 
  36 /**
  37  * @bug 8142924
  38  * @summary ES6 symbols created with Symbol.for should deserialize to canonical instances
  39  */
  40 @SuppressWarnings("javadoc")
  41 public class JDK_8142924_Test {
  42     @Test
  43     public static void testNonGlobal() throws Exception {
  44         final String name = "testNonGlobal";
  45         final Symbol symbol1 = (Symbol)NativeSymbol.constructor(false, null, name);
  46         final Symbol symbol2 = serializeRoundTrip(symbol1);
  47         Assert.assertNotSame(symbol1, symbol2);
  48         Assert.assertEquals(symbol2.getName(), name);
  49         Assert.assertNotSame(symbol1, NativeSymbol._for(null, name));
  50     }
  51 
  52     @Test
  53     public static void testGlobal() throws Exception {
  54         final String name = "testGlobal";
  55         final Symbol symbol1 = (Symbol)NativeSymbol._for(null, name);
  56         final Symbol symbol2 = serializeRoundTrip(symbol1);
  57         Assert.assertSame(symbol1, symbol2);
  58     }
  59 
  60     private static Symbol serializeRoundTrip(final Symbol symbol) throws Exception {
  61         final ByteArrayOutputStream bout = new ByteArrayOutputStream();
  62         final ObjectOutputStream out = new ObjectOutputStream(bout);
  63         out.writeObject(symbol);
  64         out.close();
  65         return (Symbol) new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray())).readObject();
  66     }
  67 }