Path: blob/master/test/jdk/java/util/LinkedList/Clone.java
41149 views
/*1* Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 421699726* @summary Cloning a subclass of LinkedList results in an object that isn't27* an instance of the subclass. The same applies to TreeSet and28* TreeMap.29*/3031import java.util.LinkedList;32import java.util.TreeMap;33import java.util.TreeSet;3435public class Clone {36public static void main(String[] args) {37LinkedList2 l = new LinkedList2();38LinkedList2 lClone = (LinkedList2) l.clone();39if (!(l.equals(lClone) && lClone.equals(l)))40throw new RuntimeException("LinkedList.clone() is broken 1.");41l.add("a");42lClone = (LinkedList2) l.clone();43if (!(l.equals(lClone) && lClone.equals(l)))44throw new RuntimeException("LinkedList.clone() is broken 2.");45l.add("b");46lClone = (LinkedList2) l.clone();47if (!(l.equals(lClone) && lClone.equals(l)))48throw new RuntimeException("LinkedList.clone() is broken 2.");495051TreeSet2 s = new TreeSet2();52TreeSet2 sClone = (TreeSet2) s.clone();53if (!(s.equals(sClone) && sClone.equals(s)))54throw new RuntimeException("TreeSet.clone() is broken.");55s.add("a");56sClone = (TreeSet2) s.clone();57if (!(s.equals(sClone) && sClone.equals(s)))58throw new RuntimeException("TreeSet.clone() is broken.");59s.add("b");60sClone = (TreeSet2) s.clone();61if (!(s.equals(sClone) && sClone.equals(s)))62throw new RuntimeException("TreeSet.clone() is broken.");6364TreeMap2 m = new TreeMap2();65TreeMap2 mClone = (TreeMap2) m.clone();66if (!(m.equals(mClone) && mClone.equals(m)))67throw new RuntimeException("TreeMap.clone() is broken.");68m.put("a", "b");69mClone = (TreeMap2) m.clone();70if (!(m.equals(mClone) && mClone.equals(m)))71throw new RuntimeException("TreeMap.clone() is broken.");72m.put("c", "d");73mClone = (TreeMap2) m.clone();74if (!(m.equals(mClone) && mClone.equals(m)))75throw new RuntimeException("TreeMap.clone() is broken.");76}7778private static class LinkedList2 extends LinkedList {}79private static class TreeSet2 extends TreeSet {}80private static class TreeMap2 extends TreeMap {}81}828384