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