1 /* 2 * Copyright (c) 2005, 2015, 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 6273752 27 * @summary Test ImmutableDescriptor.union 28 * @author Eamonn McManus 29 * @modules java.management 30 * @run clean UnionTest 31 * @run build UnionTest 32 * @run main UnionTest 33 */ 34 35 import java.util.Collections; 36 import javax.management.Descriptor; 37 import javax.management.ImmutableDescriptor; 38 import static javax.management.ImmutableDescriptor.union; 39 import static javax.management.ImmutableDescriptor.EMPTY_DESCRIPTOR; 40 import javax.management.modelmbean.DescriptorSupport; 41 42 public class UnionTest { 43 public static void main(String[] args) throws Exception { 44 ImmutableDescriptor immutableEmpty = new ImmutableDescriptor(); 45 DescriptorSupport mutableEmpty = new DescriptorSupport(); 46 47 checkEmpty(union()); 48 checkEmpty(union(immutableEmpty)); 49 checkEmpty(union(mutableEmpty)); 50 checkEmpty(union(EMPTY_DESCRIPTOR, immutableEmpty, mutableEmpty)); 51 checkEmpty(union(null, immutableEmpty, null)); 52 53 ImmutableDescriptor immutableNumbers = 54 new ImmutableDescriptor(new String[] {"one", "two", "three"}, 55 new Object[] {1, 2, 3}); 56 final String[] noNames = null; 57 DescriptorSupport mutableNumbers = 58 new DescriptorSupport(immutableNumbers.getFieldNames(), 59 immutableNumbers.getFieldValues(noNames)); 60 ImmutableDescriptor immutableOne = 61 new ImmutableDescriptor(Collections.singletonMap("one", 1)); 62 DescriptorSupport mutableOne = 63 new DescriptorSupport(new String[] {"one"}, new Object[] {1}); 64 ImmutableDescriptor immutableTwo = 65 new ImmutableDescriptor(Collections.singletonMap("two", 2)); 66 DescriptorSupport mutableTwo = 67 new DescriptorSupport(new String[] {"two"}, new Object[] {2}); 68 ImmutableDescriptor immutableOneTwo = 69 new ImmutableDescriptor(new String[] {"one", "two"}, 70 new Object[] {1, 2}); 71 72 73 checkEqual(union(immutableNumbers), immutableNumbers); 74 checkEqual(union(immutableNumbers, mutableNumbers), immutableNumbers); 75 checkEqual(union(mutableNumbers, immutableNumbers), immutableNumbers); 76 checkEqual(union(mutableEmpty, immutableEmpty, immutableNumbers, 77 mutableNumbers, immutableOne), immutableNumbers); 78 checkEqual(union(immutableOne, immutableTwo, immutableNumbers), 79 immutableNumbers); 80 checkEquivalent(union(immutableOne, mutableNumbers), immutableNumbers); 81 checkEquivalent(union(immutableOne, immutableTwo), immutableOneTwo); 82 checkEquivalent(union(mutableOne, mutableTwo), immutableOneTwo); 83 84 if (failure != null) 85 throw new Exception("TEST FAILED: " + failure); 86 System.out.println("TEST PASSED"); 87 } 88 89 private static void checkEmpty(ImmutableDescriptor d) { 90 if (d != EMPTY_DESCRIPTOR) { 91 failure = "Union of empty descriptors should be " + 92 "ImmutableDescriptor.EMPTY"; 93 System.err.println("FAILED: " + failure); 94 Thread.dumpStack(); 95 } 96 } 97 98 private static void checkEqual(ImmutableDescriptor d, 99 ImmutableDescriptor e) { 100 if (d != e) { 101 failure = "Union should produce one of its arguments but does not"; 102 System.err.println("FAILED: " + failure); 103 Thread.dumpStack(); 104 } 105 } 106 107 private static void checkEquivalent(ImmutableDescriptor d, 108 ImmutableDescriptor e) { 109 if (!d.equals(e)) { 110 failure = "Union produced this: " + d + "; but should have " + 111 "produced this: " + e; 112 System.err.println("FAILED: " + failure); 113 Thread.dumpStack(); 114 } 115 } 116 117 private static String failure; 118 }