Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/descriptor/ImmutableArrayFieldTest.java
38838 views
/*1* Copyright (c) 2005, 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 629918026* @summary Test that immutability of ImmutableDescriptor cannot be27* compromised by modifying field values that are arrays.28* @author Eamonn McManus29* @run clean ImmutableArrayFieldTest30* @run build ImmutableArrayFieldTest31* @run main ImmutableArrayFieldTest32*/3334import java.util.Arrays;35import javax.management.ImmutableDescriptor;3637public class ImmutableArrayFieldTest {38public static void main(String[] args) throws Exception {39boolean ok = true;40ImmutableDescriptor d = new ImmutableDescriptor(41new String[] {42"strings", "ints", "booleans",43},44new Object[] {45new String[] {"foo"},46new int[] {5},47new boolean[] {false},48});4950String[] strings = (String[]) d.getFieldValue("strings");51strings[0] = "bar";52strings = (String[]) d.getFieldValue("strings");53if (!strings[0].equals("foo")) {54System.out.println("FAILED: modified string array field");55ok = false;56}5758int[] ints = (int[]) d.getFieldValue("ints");59ints[0] = 0;60ints = (int[]) d.getFieldValue("ints");61if (ints[0] != 5) {62System.out.println("FAILED: modified int array field");63ok = false;64}6566boolean[] bools = (boolean[]) d.getFieldValue("booleans");67bools[0] = true;68bools = (boolean[]) d.getFieldValue("booleans");69if (bools[0]) {70System.out.println("FAILED: modified boolean array field");71ok = false;72}7374Object[] values = d.getFieldValues("strings", "ints", "booleans");75((String[]) values[0])[0] = "bar";76((int[]) values[1])[0] = 0;77((boolean[]) values[2])[0] = true;78values = d.getFieldValues("strings", "ints", "booleans");79if (!((String[]) values[0])[0].equals("foo") ||80((int[]) values[1])[0] != 5 ||81((boolean[]) values[2])[0]) {82System.out.println("FAILED: getFieldValues modifiable: " +83Arrays.deepToString(values));84}8586if (ok)87System.out.println("TEST PASSED");88else89throw new Exception("Array field values were modifiable");90}91}929394