1 /*
   2  * Copyright (c) 2008, 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 6336968
  27  * @summary Test adding non-Attribute values to an AttributeList.
  28  * @author Eamonn McManus
  29  * @modules java.management
  30  */
  31 
  32 import java.util.Collections;
  33 import java.util.List;
  34 import javax.management.Attribute;
  35 import javax.management.AttributeList;
  36 
  37 public class AttributeListTypeSafeTest {
  38 
  39     private static String failure;
  40 
  41     public static void main(String[] args) throws Exception {
  42         // Test calling asList after adding non-Attribute by various means
  43         for (Op op : Op.values()) {
  44             AttributeList alist = new AttributeList();
  45             alist.add(new Attribute("foo", "bar"));
  46             doOp(alist, op);
  47             String what = "asList() after calling " + op + " with non-Attribute";
  48             try {
  49                 List<Attribute> lista = alist.asList();
  50                 fail(what + ": succeeded but should not have");
  51             } catch (IllegalArgumentException e) {
  52                 System.out.println("OK: " + what + ": got IllegalArgumentException");
  53             }
  54         }
  55 
  56         // Test adding non-Attribute by various means after calling asList
  57         for (Op op : Op.values()) {
  58             AttributeList alist = new AttributeList();
  59             List<Attribute> lista = alist.asList();
  60             lista.add(new Attribute("foo", "bar"));
  61             String what = op + " with non-Attribute after calling asList()";
  62             try {
  63                 doOp(alist, op);
  64                 fail(what + ": succeeded but should not have");
  65             } catch (IllegalArgumentException e) {
  66                 System.out.println("OK: " + what + ": got IllegalArgumentException");
  67             }
  68         }
  69 
  70         if (failure == null)
  71             System.out.println("TEST PASSED");
  72         else
  73             throw new Exception("TEST FAILED: " + failure);
  74     }
  75 
  76     private static enum Op {
  77         ADD("add(Object)"), ADD_AT("add(int, Object)"),
  78         ADD_ALL("add(Collection)"), ADD_ALL_AT("add(int, Collection)"),
  79         SET("set(int, Object)");
  80 
  81         private Op(String what) {
  82             this.what = what;
  83         }
  84 
  85         @Override
  86         public String toString() {
  87             return what;
  88         }
  89 
  90         private final String what;
  91     }
  92 
  93     private static void doOp(AttributeList alist, Op op) {
  94         Object x = "oops";
  95         switch (op) {
  96             case ADD: alist.add(x); break;
  97             case ADD_AT: alist.add(0, x); break;
  98             case ADD_ALL: alist.add(Collections.singleton(x)); break;
  99             case ADD_ALL_AT: alist.add(0, Collections.singleton(x)); break;
 100             case SET: alist.set(0, x); break;
 101             default: throw new AssertionError("Case not covered");
 102         }
 103     }
 104 
 105     private static void fail(String why) {
 106         System.out.println("FAIL: " + why);
 107         failure = why;
 108     }
 109 
 110 }