1 /*
   2  * Copyright (c) 2005, 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 /*
  25  * @test
  26  * @bug 6299180
  27  * @summary Test that immutability of ImmutableDescriptor cannot be
  28  * compromised by modifying field values that are arrays.
  29  * @author Eamonn McManus
  30  * @run clean ImmutableArrayFieldTest
  31  * @run build ImmutableArrayFieldTest
  32  * @run main ImmutableArrayFieldTest
  33  */
  34 
  35 import java.util.Arrays;
  36 import javax.management.ImmutableDescriptor;
  37 
  38 public class ImmutableArrayFieldTest {
  39     public static void main(String[] args) throws Exception {
  40         boolean ok = true;
  41         ImmutableDescriptor d = new ImmutableDescriptor(
  42                 new String[] {
  43                     "strings", "ints", "booleans",
  44                 },
  45                 new Object[] {
  46                     new String[] {"foo"},
  47                     new int[] {5},
  48                     new boolean[] {false},
  49                 });
  50 
  51         String[] strings = (String[]) d.getFieldValue("strings");
  52         strings[0] = "bar";
  53         strings = (String[]) d.getFieldValue("strings");
  54         if (!strings[0].equals("foo")) {
  55             System.out.println("FAILED: modified string array field");
  56             ok = false;
  57         }
  58 
  59         int[] ints = (int[]) d.getFieldValue("ints");
  60         ints[0] = 0;
  61         ints = (int[]) d.getFieldValue("ints");
  62         if (ints[0] != 5) {
  63             System.out.println("FAILED: modified int array field");
  64             ok = false;
  65         }
  66 
  67         boolean[] bools = (boolean[]) d.getFieldValue("booleans");
  68         bools[0] = true;
  69         bools = (boolean[]) d.getFieldValue("booleans");
  70         if (bools[0]) {
  71             System.out.println("FAILED: modified boolean array field");
  72             ok = false;
  73         }
  74 
  75         Object[] values = d.getFieldValues("strings", "ints", "booleans");
  76         ((String[]) values[0])[0] = "bar";
  77         ((int[]) values[1])[0] = 0;
  78         ((boolean[]) values[2])[0] = true;
  79         values = d.getFieldValues("strings", "ints", "booleans");
  80         if (!((String[]) values[0])[0].equals("foo") ||
  81                 ((int[]) values[1])[0] != 5 ||
  82                 ((boolean[]) values[2])[0]) {
  83             System.out.println("FAILED: getFieldValues modifiable: " +
  84                     Arrays.deepToString(values));
  85         }
  86 
  87         if (ok)
  88             System.out.println("TEST PASSED");
  89         else
  90             throw new Exception("Array field values were modifiable");
  91     }
  92 }