jdk/test/javax/management/modelmbean/DescriptorSupportTest.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2003-2004 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4883712 4869006 4894856 5016685
       
    27  * @summary Test that DescriptorSupport correctly validates fields
       
    28  * @author Eamonn McManus
       
    29  * @run clean DescriptorSupportTest
       
    30  * @run build DescriptorSupportTest
       
    31  * @run main DescriptorSupportTest
       
    32  */
       
    33 
       
    34 import java.io.ByteArrayInputStream;
       
    35 import java.io.ByteArrayOutputStream;
       
    36 import java.io.ObjectInputStream;
       
    37 import java.io.ObjectOutputStream;
       
    38 import java.util.ArrayList;
       
    39 import java.util.Arrays;
       
    40 import java.util.List;
       
    41 import javax.management.Descriptor;
       
    42 import javax.management.RuntimeOperationsException;
       
    43 import javax.management.modelmbean.DescriptorSupport;
       
    44 import javax.management.modelmbean.ModelMBeanInfo;
       
    45 import javax.management.modelmbean.ModelMBeanInfoSupport;
       
    46 
       
    47 public class DescriptorSupportTest {
       
    48     private static final Object[] goodFields = {
       
    49         "value", "",
       
    50         "severity", "0",
       
    51         "severity", "6",
       
    52     };
       
    53 
       
    54     private static final Object[] badFields = {
       
    55         "name", null,
       
    56         "name", "",
       
    57         "descriptorType", null,
       
    58         "descriptorType", "",
       
    59         "setMethod", null,
       
    60         "getMethod", null,
       
    61         "role", null,
       
    62         "class", null,
       
    63         "visibility", null,
       
    64         "visibility", new Integer(0),
       
    65         "visibility", "0",
       
    66         "visibility", new Integer(5),
       
    67         "visibility", "5",
       
    68         "severity", null,
       
    69         "severity", new Integer(-1),
       
    70         "severity", "-1",
       
    71         "severity", new Integer(7),
       
    72         "severity", "7",
       
    73         "persistPolicy", null,
       
    74         "persistPolicy", "bogusPersistPolicy",
       
    75         "persistPeriod", null,
       
    76         "persistPeriod", "not a number",
       
    77         "currencyTimeLimit", null,
       
    78         "currencyTimeLimit", "not a number",
       
    79         "lastUpdatedTimeStamp", null,
       
    80         "lastUpdatedTimeStamp", "not a number",
       
    81         "lastReturnedTimeStamp", null,
       
    82         "lastReturnedTimeStamp", "not a number",
       
    83         "log", null,
       
    84         "log", "not T or F or true or false",
       
    85         "log", new Object[0],
       
    86     };
       
    87 
       
    88 
       
    89     public static void main(String[] args) throws Exception {
       
    90         boolean ok = true;
       
    91 
       
    92         System.out.println("Checking that name and descriptorType are " +
       
    93                            "mandatory");
       
    94         // Try omitting name and/or descriptorType
       
    95         for (int i = 0; i < 3; i++) {
       
    96             final boolean addName = ((i & 1) != 0);
       
    97             final boolean addDescriptorType = ((i & 2) != 0);
       
    98             final List fields = new ArrayList();
       
    99             if (addName)
       
   100                 fields.add("name=something");
       
   101             if (addDescriptorType)
       
   102                 fields.add("descriptorType=something-else");
       
   103             final String[] fs = (String[]) fields.toArray(new String[0]);
       
   104             final String what =
       
   105                     "DescriptorSupport with " +
       
   106                     (addName ? "" : "no ") + "name and " +
       
   107                     (addDescriptorType ? "" : "no ") + "descriptorType";
       
   108             DescriptorSupport ds = new DescriptorSupport(fs);
       
   109             if (ds.isValid()) {
       
   110                 System.out.println("INCORRECTLY ACCEPTED: " + what);
       
   111                 ok = false;
       
   112             } else
       
   113                 System.out.println("OK: rejected " + what);
       
   114         }
       
   115 
       
   116         for (int pass = 0; pass < 2; pass++) {
       
   117             boolean shouldAccept = (pass == 0);
       
   118             System.out.println("Trying out " +
       
   119                                (shouldAccept ? "correct" : "bogus") +
       
   120                                " DescriptorSupport fields");
       
   121             Object[] fields = shouldAccept ? goodFields : badFields;
       
   122             for (int i = 0; i < fields.length; i += 2) {
       
   123                 String[] names = {"name", "descriptorType"};
       
   124                 String[] values = {"some-name", "some-type"};
       
   125                 DescriptorSupport d = new DescriptorSupport(names, values);
       
   126                 final String name = (String) fields[i];
       
   127                 final Object value = fields[i + 1];
       
   128                 final String valueS =
       
   129                     (value instanceof String) ? ("\"" + value + "\"") :
       
   130                     (value == null) ? "null" : value.toString();
       
   131                 final String what =
       
   132                     "DescriptorSupport with " + name + " = " + valueS;
       
   133                 try {
       
   134                     d.setField(name, value);
       
   135                     if (shouldAccept)
       
   136                         System.out.println("OK: accepted " + what);
       
   137                     else {
       
   138                         System.out.println("INCORRECTLY ACCEPTED: " + what);
       
   139                         ok = false;
       
   140                     }
       
   141                 } catch (RuntimeOperationsException e) {
       
   142                     if (shouldAccept) {
       
   143                         System.out.println("INCORRECTLY REJECTED: " + what +
       
   144                                            ": " + e);
       
   145                         ok = false;
       
   146                     } else {
       
   147                         System.out.println("OK: rejected " + what);
       
   148                         // OK: this is what should happen
       
   149                     }
       
   150                 } catch (Exception e) {
       
   151                     System.out.println("WRONG EXCEPTION: " + what + ": " + e);
       
   152                     ok = false;
       
   153                 }
       
   154             }
       
   155         }
       
   156 
       
   157         // 4894856: ModelMBeanInfoSupport.setDescriptor(d, "mbean") fails
       
   158         System.out.println("Checking that setDescriptor(d, \"mbean\") works");
       
   159         ModelMBeanInfo mmbi =
       
   160             new ModelMBeanInfoSupport("x", "descr", null, null, null, null);
       
   161         Descriptor d = mmbi.getDescriptor("x", "mbean");
       
   162         try {
       
   163             mmbi.setDescriptor(d, "mbean");
       
   164         } catch (Exception e) {
       
   165             System.out.println("Unexpected exception:");
       
   166             e.printStackTrace(System.out);
       
   167             ok = false;
       
   168         }
       
   169 
       
   170         // 5016685: DescriptorSupport forces field names to lower case
       
   171         System.out.println("Checking that field name case is ignored " +
       
   172                            "but preserved");
       
   173         ok &= caseTest(new DescriptorSupport(new String[] {"NAME=blah"}),
       
   174                        "DescriptorSupport(String[])");
       
   175         ok &= caseTest(new DescriptorSupport(new String[] {"NAME"},
       
   176                                              new String[] {"blah"}),
       
   177                        "DescriptorSupport(String[], Object[])");
       
   178         DescriptorSupport d1 = new DescriptorSupport();
       
   179         d1.setField("NAME", "blah");
       
   180         ok &= caseTest(d1, "DescriptorSupport.setField");
       
   181         d1 = new DescriptorSupport(new String[] {"NAME=blah"});
       
   182         ok &= caseTest(new DescriptorSupport(d1),
       
   183                        "DescriptorSupport(Descriptor)");
       
   184         d1 = new DescriptorSupport(new String[] {"NAME=blah"});
       
   185         ok &= caseTest(new DescriptorSupport(d1.toXMLString()),
       
   186                        "DescriptorSupport(String)");
       
   187         d1 = new DescriptorSupport(new String[] {"NAME=blah"});
       
   188         ByteArrayOutputStream bos = new ByteArrayOutputStream();
       
   189         ObjectOutputStream oos = new ObjectOutputStream(bos);
       
   190         oos.writeObject(d1);
       
   191         oos.close();
       
   192         bos.close();
       
   193         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
       
   194         ObjectInputStream ois = new ObjectInputStream(bis);
       
   195         d1 = (DescriptorSupport) ois.readObject();
       
   196         ok &= caseTest(d1, "serialized DescriptorSupport");
       
   197 
       
   198         if (ok)
       
   199             System.out.println("Test passed");
       
   200         else {
       
   201             System.out.println("TEST FAILED");
       
   202             System.exit(1);
       
   203         }
       
   204     }
       
   205 
       
   206     private static boolean caseTest(Descriptor d, String what) {
       
   207         boolean ok = true;
       
   208 
       
   209         System.out.println("..." + what);
       
   210 
       
   211         String[] names = d.getFieldNames();
       
   212         if (names.length != 1 || !names[0].equals("NAME")) {
       
   213             ok = false;
       
   214             System.out.println("...getFieldNames() fails: " +
       
   215                                Arrays.asList(names));
       
   216         }
       
   217 
       
   218         String[] fields = d.getFields();
       
   219         if (fields.length != 1 || !fields[0].equals("NAME=blah")) {
       
   220             ok = false;
       
   221             System.out.println("...getFields() fails: " +
       
   222                                Arrays.asList(fields));
       
   223         }
       
   224 
       
   225         Object value = d.getFieldValue("namE");
       
   226         if (!"blah".equals(value)) {
       
   227             ok = false;
       
   228             System.out.println("...getFieldValue(\"namE\") fails: " + value);
       
   229         }
       
   230 
       
   231         Object[] values = d.getFieldValues(new String[] {"namE"});
       
   232         if (values.length != 1 || !"blah".equals(values[0])) {
       
   233             ok = false;
       
   234             System.out.println("...getFieldValues({\"namE\"}) fails: " +
       
   235                                Arrays.asList(values));
       
   236         }
       
   237 
       
   238         d.setField("namE", "newblah");
       
   239         Object newblah = d.getFieldValue("Name");
       
   240         if (!"newblah".equals(newblah)) {
       
   241             ok = false;
       
   242             System.out.println("...setField value not returned: " + newblah);
       
   243         }
       
   244 
       
   245         d.setFields(new String[] {"NaMe"}, new Object[] {"newerblah"});
       
   246         Object newerblah = d.getFieldValue("naMe");
       
   247         if (!"newerblah".equals(newerblah)) {
       
   248             ok = false;
       
   249             System.out.println("...setFields value not returned: " +
       
   250                                newerblah);
       
   251         }
       
   252 
       
   253         Descriptor d1 = (Descriptor) d.clone();
       
   254         newerblah = d1.getFieldValue("NAMe");
       
   255         if (!"newerblah".equals(newerblah)) {
       
   256             ok = false;
       
   257             System.out.println("...clone incorrect: " + newerblah);
       
   258         }
       
   259 
       
   260         d.removeField("NAme");
       
   261         names = d.getFieldNames();
       
   262         if (names.length != 0) {
       
   263             ok = false;
       
   264             System.out.println("...removeField failed: " +
       
   265                                Arrays.asList(names));
       
   266         }
       
   267 
       
   268         if (ok)
       
   269             System.out.println("...succeeded");
       
   270 
       
   271         return ok;
       
   272     }
       
   273 }