jdk/test/javax/management/ObjectName/ComparatorTest.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 5036680
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test the ObjectName.compareTo() method.
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 ComparatorTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build ComparatorTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main ComparatorTest
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 javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ComparatorTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final char LT = '<';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final char EQ = '=';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final char GT = '>';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final String tests[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        // domains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        { String.valueOf(LT), ":k1=v1", "d:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        { String.valueOf(EQ), "d:k1=v1", "d:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        { String.valueOf(GT), "d2:k1=v1", "d1:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // "type=" key property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        { String.valueOf(GT), "d:type=a,k1=v1", "d:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        { String.valueOf(GT), "d:type=a,k1=v1", "d:type=" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        { String.valueOf(GT), "d:type=a,k1=v1", "d:type=,k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        { String.valueOf(LT), "d:type=a,k1=v1", "d:type=b,k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        { String.valueOf(LT), "d:type=a,k2=v2", "d:type=b,k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // canonical form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        { String.valueOf(EQ), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        { String.valueOf(LT), "d:k1=v1,k2=v2", "d:k1=v1,k3=v3" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        { String.valueOf(LT), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1,k3=v3" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // wildcards
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        { String.valueOf(LT), "d:k1=v1", "d:k1=v1,*" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        { String.valueOf(GT), "d:k1=v1,k2=v2", "d:k1=v1,*" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        { String.valueOf(GT), "domain:k1=v1", "?:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        { String.valueOf(GT), "domain:k1=v1", "*:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        { String.valueOf(GT), "domain:k1=v1", "domai?:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        { String.valueOf(GT), "domain:k1=v1", "domai*:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        { String.valueOf(GT), "domain:k1=v1", "do?ain:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        { String.valueOf(GT), "domain:k1=v1", "do*ain:k1=v1" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static boolean compare(char comparator, String on1, String on2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        System.out.println("Test " + on1 + " " + comparator + " " + on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            ObjectName o1 = ObjectName.getInstance(on1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            ObjectName o2 = ObjectName.getInstance(on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int result = o1.compareTo(o2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            switch (comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                case LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                case EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    if (result == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                case GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    if (result > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                              "Test incorrect: case: " + comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.out.println("Got Unexpected Exception = " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static boolean lessThan(String on1, String on2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return compare(LT, on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private static boolean equalTo(String on1, String on2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return compare(EQ, on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private static boolean greaterThan(String on1, String on2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return compare(GT, on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static int runTest(char comparator, String on1, String on2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        boolean ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        boolean lt = lessThan(on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        boolean eq = equalTo(on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        boolean gt = greaterThan(on1, on2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        switch (comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            case LT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                ok = lt && !eq && !gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.println("Comparison result: LessThan");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            case EQ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                ok = !lt && eq && !gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                System.out.println("Comparison result: EqualTo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            case GT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                ok = !lt && !eq && gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                System.out.println("Comparison result: GreaterThan");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                          "Test incorrect: case: " + comparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.out.println("Test passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.out.println("Test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return ok ? 0 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // Check null values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        System.out.println("Test ObjectName.compareTo(null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            new ObjectName("d:k=v").compareTo(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            System.out.println("Didn't get expected NullPointerException!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            System.out.println("Test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("Got expected exception = " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            System.out.println("Test passed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            System.out.println("Got unexpected exception = " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println("Test failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        System.out.println("----------------------------------------------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // Compare ObjectNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        for (int i = 0; i < tests.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            error += runTest(tests[i][0].charAt(0), tests[i][1], tests[i][2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            final String msg = "Test FAILED! Got " + error + " error(s)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            System.out.println("Test PASSED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
}