jdk/test/java/util/EnumMap/ProperEntrySetOnClone.java
author jlahoda
Wed, 12 Dec 2012 20:26:56 +0100
changeset 14803 88347e495d34
parent 12879 59547faaa927
permissions -rw-r--r--
8004504: ListBuffer could reuse List.nil() as the sentinel element Summary: ListBuffer.last now points to the last elements with client data, or null if none. Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12879
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     1
/*
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     4
 *
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     8
 *
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    13
 * accompanied this code).
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    14
 *
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    18
 *
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    21
 * questions.
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    22
 */
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    23
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    24
/*
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    25
 * @test
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    26
 * @bug     7164256
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    27
 * @summary EnumMap.entrySet() returns an entrySet referencing to the cloned instance
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    28
 * @author  Diego Belfer
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    29
 */
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    30
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    31
import java.util.EnumMap;
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    32
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    33
public class ProperEntrySetOnClone {
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    34
    public enum Test {
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    35
        ONE, TWO
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    36
    }
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    37
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    38
    public static void main(String[] args) {
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    39
        EnumMap<Test, String> map1 = new EnumMap<Test, String>(Test.class);
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    40
        map1.put(Test.ONE, "1");
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    41
        map1.put(Test.TWO, "2");
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    42
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    43
        // We need to force creation of the map1.entrySet
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    44
        int size = map1.entrySet().size();
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    45
        if (size != 2) {
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    46
            throw new RuntimeException(
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    47
                    "Invalid size in original map. Expected: 2 was: " + size);
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    48
        }
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    49
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    50
        EnumMap<Test, String> map2 = map1.clone();
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    51
        map2.remove(Test.ONE);
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    52
        size = map2.entrySet().size();
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    53
        if (size != 1) {
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    54
            throw new RuntimeException(
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    55
                    "Invalid size in cloned instance. Expected: 1 was: " + size);
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    56
        }
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    57
    }
59547faaa927 7164256: EnumMap clone doesn't clear the entrySet keeping a reference to the original Map
alanb
parents:
diff changeset
    58
}