1 /*
   2  * Copyright (c) 2006, 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 6434298
  27  * @summary Test IAE is thrown when typeName or description parameter is null for TabularType and CompositeType constructors
  28  * @author Joel FERAUD
  29  * @modules java.management
  30  */
  31 
  32 import javax.management.openmbean.*;
  33 
  34 public class NullConstructorParamsTest {
  35 
  36     /**
  37      * Print message
  38      */
  39     private static void echo(String message) {
  40         System.out.println(message);
  41     }
  42 
  43     /**
  44      * Main
  45      */
  46     public static void main(String[] args) throws Exception {
  47 
  48         echo("SUMMARY: Test IAE is thrown when typeName or description parameter is null " +
  49              "for TabularType and CompositeType constructors");
  50 
  51         echo(">>> Create CompositeType with non null params: should be ok");
  52         CompositeType ctok =
  53             new CompositeType(
  54                               "typeNameOK",
  55                               "for test",
  56                               new String[] {"a", "b"},
  57                               new String[] {"a_desc", "b_desc"},
  58                               new OpenType[] {SimpleType.BOOLEAN,SimpleType.STRING});
  59         echo("+++ CompositeType created ok");
  60         echo("");
  61 
  62         echo(">>> Create TabularType with non null params: should be ok");
  63         TabularType tabok =
  64             new TabularType(
  65                             "typeNameOK",
  66                             "for test",
  67                             ctok,
  68                             new String[] {"a"});
  69         echo("+++ TabularType created ok");
  70         echo("");
  71 
  72 
  73         int IAENotThrown = 0;
  74 
  75         try {
  76             echo(">>> Create CompositeType with null typeName: expect IAE");
  77             CompositeType ctnull1 =
  78                 new CompositeType(
  79                                   null,
  80                                   "for test",
  81                                   new String[] {"a", "b"},
  82                                   new String[] {"a_desc", "b_desc"},
  83                                   new OpenType[] {SimpleType.BOOLEAN, SimpleType.STRING});
  84             IAENotThrown++;
  85             echo("*** IAE not thrown as expected ***");
  86             echo("*** Test will FAIL ***");
  87             echo("");
  88         } catch (IllegalArgumentException iae) {
  89             echo("+++ IAE thrown as expected");
  90             echo("");
  91         } catch (Exception e) {
  92             IAENotThrown++;
  93             echo("*** Did not get IAE as expected, but instead: ");
  94             e.printStackTrace();
  95             echo("*** Test will FAIL ***");
  96             echo("");
  97         }
  98 
  99         try {
 100             echo(">>> Create TabularType with null typeName: expect IAE");
 101             TabularType tabnull1 =
 102                 new TabularType(
 103                                 null,
 104                                 "for test",
 105                                 ctok,
 106                                 new String[] {"a"});
 107             IAENotThrown++;
 108             echo("*** IAE not thrown as expected ***");
 109             echo("*** Test will FAIL ***");
 110             echo("");
 111         } catch (IllegalArgumentException iae) {
 112             echo("+++ IAE thrown as expected");
 113             echo("");
 114         } catch (Exception e) {
 115             IAENotThrown++;
 116             echo("*** Did not get IAE as expected, but instead: ");
 117             e.printStackTrace();
 118             echo("*** Test will FAIL ***");
 119             echo("");
 120         }
 121 
 122         try {
 123             echo(">>> Create CompositeType with null description: expect IAE");
 124             CompositeType ctnull2 =
 125                 new CompositeType(
 126                                   "test",
 127                                   null,
 128                                   new String[] {"a", "b"},
 129                                   new String[] {"a_desc", "b_desc"},
 130                                   new OpenType[] {SimpleType.BOOLEAN, SimpleType.STRING});
 131             IAENotThrown++;
 132             echo("*** IAE not thrown as expected ***");
 133             echo("*** Test will FAIL ***");
 134             echo("");
 135         } catch (IllegalArgumentException iae) {
 136             echo("+++ IAE thrown as expected");
 137             echo("");
 138         } catch (Exception e) {
 139             IAENotThrown++;
 140             echo("*** Did not get IAE as expected, but instead: ");
 141             e.printStackTrace();
 142             echo("*** Test will FAIL ***");
 143             echo("");
 144         }
 145 
 146         try {
 147             echo(">>> Create TabularType with null description: expect IAE");
 148             TabularType tabnull2 =
 149                 new TabularType(
 150                                 "test",
 151                                 null,
 152                                 ctok,
 153                                 new String[] {"a"});
 154             IAENotThrown++;
 155             echo("*** IAE not thrown as expected ***");
 156             echo("*** Test will FAIL ***");
 157             echo("");
 158         } catch (IllegalArgumentException iae) {
 159             echo("+++ IAE thrown as expected");
 160             echo("");
 161         } catch (Exception e) {
 162             IAENotThrown++;
 163             echo("*** Did not get IAE as expected, but instead: ");
 164             e.printStackTrace();
 165             echo("*** Test will FAIL ***");
 166             echo("");
 167         }
 168 
 169         if (IAENotThrown != 0 ) {
 170             echo("*** Test FAILED: IAE not thrown as expected ***");
 171             echo("");
 172             throw new RuntimeException("IAE not thrown as expected");
 173         }
 174         echo("+++ Test PASSED");
 175         echo("");
 176 
 177     }
 178 }