jdk/test/javax/management/ObjectName/NullEmptyKeyValueTest.java
author uvangapally
Fri, 19 May 2017 15:27:25 +0530
changeset 45232 1e2ad0809162
parent 30376 2ccf2cf7ea48
child 44423 306c020eb154
permissions -rw-r--r--
8175845: Provide javadoc descriptions for jdk.hotspot.agent module Summary: Added description for jdk.hotspot.agent module Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6229396
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test null/empty key/values in ObjectName constructors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Luis-Miguel Alventosa
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    29
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean NullEmptyKeyValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build NullEmptyKeyValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main NullEmptyKeyValueTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class NullEmptyKeyValueTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static int createObjectName(int i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                                        Class c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                                        String s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                        String d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                        String k,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
                                        String v,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                                        Hashtable<String,String> t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            System.out.println("ObjectName = " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.out.println("ObjectName.Domain = " + d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            System.out.println("ObjectName.Key = " + k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            System.out.println("ObjectName.Value = " + v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            System.out.println("ObjectName.Domain = " + d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.out.println("ObjectName.Hashtable = " + t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            throw new Exception("Test incorrect: case: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ObjectName on = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                on = new ObjectName(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                on = new ObjectName(d, k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                on = new ObjectName(d, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                throw new Exception("Test incorrect: case: " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                System.out.println("Got Unexpected ObjectName = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                           (on == null ? "null" : on.getCanonicalName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                System.out.println("Got Expected ObjectName = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                           (on == null ? "null" : on.getCanonicalName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (c == null || !c.isInstance(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                System.out.println("Got Unexpected Exception = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                   e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                System.out.println("Got Expected Exception = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                   e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static int createObjectName1(Class c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                         String s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return createObjectName(1, c, s, null, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static int createObjectName2(Class c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                         String d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                         String k,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                         String v)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return createObjectName(2, c, null, d, k, v, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private static int createObjectName3(Class c,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                         String d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                         Hashtable<String,String> t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return createObjectName(3, c, null, d, null, null, t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        final Class npec = NullPointerException.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        final Class monec = MalformedObjectNameException.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        error += createObjectName1(npec, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        error += createObjectName1(null, "d:k=v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        error += createObjectName1(null, ":k=v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        error += createObjectName1(monec, "d:=v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        error += createObjectName1(null, "d:k=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        error += createObjectName1(null, "d:k1=,k2=v2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        error += createObjectName1(null, "d:k1=v1,k2=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        error += createObjectName2(npec, null, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        error += createObjectName2(null, "d", "k", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        error += createObjectName2(npec, null, "k", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        error += createObjectName2(null, "", "k", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        error += createObjectName2(npec, "d", null, "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        error += createObjectName2(monec, "d", "", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        error += createObjectName2(npec, "d", "k", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        error += createObjectName2(null, "d", "k", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        Hashtable<String,String> h1 = new Hashtable<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        h1.put("k", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Hashtable<String,String> h2 = new Hashtable<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        h2.put("", "v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Hashtable<String,String> h3 = new Hashtable<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        h3.put("k", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Hashtable<String,String> h4 = new Hashtable<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        h4.put("k1", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        h4.put("k2", "v2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        Hashtable<String,String> h5 = new Hashtable<String,String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        h5.put("k1", "v1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        h5.put("k2", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        error += createObjectName3(npec, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        error += createObjectName3(null, "d", h1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        error += createObjectName3(npec, null, h1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        error += createObjectName3(null, "", h1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        error += createObjectName3(monec, "d", h2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        error += createObjectName3(null, "d", h3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        error += createObjectName3(null, "d", h4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        error += createObjectName3(null, "d", h5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            final String msg = "Test FAILED! Got " + error + " error(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            System.out.println("Test PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}