Path: blob/master/test/jdk/javax/management/descriptor/UnionTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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 627375226* @summary Test ImmutableDescriptor.union27* @author Eamonn McManus28*29* @run clean UnionTest30* @run build UnionTest31* @run main UnionTest32*/3334import java.util.Collections;35import javax.management.Descriptor;36import javax.management.ImmutableDescriptor;37import static javax.management.ImmutableDescriptor.union;38import static javax.management.ImmutableDescriptor.EMPTY_DESCRIPTOR;39import javax.management.modelmbean.DescriptorSupport;4041public class UnionTest {42public static void main(String[] args) throws Exception {43ImmutableDescriptor immutableEmpty = new ImmutableDescriptor();44DescriptorSupport mutableEmpty = new DescriptorSupport();4546checkEmpty(union());47checkEmpty(union(immutableEmpty));48checkEmpty(union(mutableEmpty));49checkEmpty(union(EMPTY_DESCRIPTOR, immutableEmpty, mutableEmpty));50checkEmpty(union(null, immutableEmpty, null));5152ImmutableDescriptor immutableNumbers =53new ImmutableDescriptor(new String[] {"one", "two", "three"},54new Object[] {1, 2, 3});55final String[] noNames = null;56DescriptorSupport mutableNumbers =57new DescriptorSupport(immutableNumbers.getFieldNames(),58immutableNumbers.getFieldValues(noNames));59ImmutableDescriptor immutableOne =60new ImmutableDescriptor(Collections.singletonMap("one", 1));61DescriptorSupport mutableOne =62new DescriptorSupport(new String[] {"one"}, new Object[] {1});63ImmutableDescriptor immutableTwo =64new ImmutableDescriptor(Collections.singletonMap("two", 2));65DescriptorSupport mutableTwo =66new DescriptorSupport(new String[] {"two"}, new Object[] {2});67ImmutableDescriptor immutableOneTwo =68new ImmutableDescriptor(new String[] {"one", "two"},69new Object[] {1, 2});707172checkEqual(union(immutableNumbers), immutableNumbers);73checkEqual(union(immutableNumbers, mutableNumbers), immutableNumbers);74checkEqual(union(mutableNumbers, immutableNumbers), immutableNumbers);75checkEqual(union(mutableEmpty, immutableEmpty, immutableNumbers,76mutableNumbers, immutableOne), immutableNumbers);77checkEqual(union(immutableOne, immutableTwo, immutableNumbers),78immutableNumbers);79checkEquivalent(union(immutableOne, mutableNumbers), immutableNumbers);80checkEquivalent(union(immutableOne, immutableTwo), immutableOneTwo);81checkEquivalent(union(mutableOne, mutableTwo), immutableOneTwo);8283if (failure != null)84throw new Exception("TEST FAILED: " + failure);85System.out.println("TEST PASSED");86}8788private static void checkEmpty(ImmutableDescriptor d) {89if (d != EMPTY_DESCRIPTOR) {90failure = "Union of empty descriptors should be " +91"ImmutableDescriptor.EMPTY";92System.err.println("FAILED: " + failure);93Thread.dumpStack();94}95}9697private static void checkEqual(ImmutableDescriptor d,98ImmutableDescriptor e) {99if (d != e) {100failure = "Union should produce one of its arguments but does not";101System.err.println("FAILED: " + failure);102Thread.dumpStack();103}104}105106private static void checkEquivalent(ImmutableDescriptor d,107ImmutableDescriptor e) {108if (!d.equals(e)) {109failure = "Union produced this: " + d + "; but should have " +110"produced this: " + e;111System.err.println("FAILED: " + failure);112Thread.dumpStack();113}114}115116private static String failure;117}118119120