Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/ObjectName/ComparatorTest.java
38838 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 503668026* @summary Test the ObjectName.compareTo() method.27* @author Luis-Miguel Alventosa28* @run clean ComparatorTest29* @run build ComparatorTest30* @run main ComparatorTest31*/3233import javax.management.*;3435public class ComparatorTest {3637private static final char LT = '<';38private static final char EQ = '=';39private static final char GT = '>';4041private static final String tests[][] = {42//43// domains44//45{ String.valueOf(LT), ":k1=v1", "d:k1=v1" },46{ String.valueOf(EQ), "d:k1=v1", "d:k1=v1" },47{ String.valueOf(GT), "d2:k1=v1", "d1:k1=v1" },48//49// "type=" key property50//51{ String.valueOf(GT), "d:type=a,k1=v1", "d:k1=v1" },52{ String.valueOf(GT), "d:type=a,k1=v1", "d:type=" },53{ String.valueOf(GT), "d:type=a,k1=v1", "d:type=,k1=v1" },54{ String.valueOf(LT), "d:type=a,k1=v1", "d:type=b,k1=v1" },55{ String.valueOf(LT), "d:type=a,k2=v2", "d:type=b,k1=v1" },56//57// canonical form58//59{ String.valueOf(EQ), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1" },60{ String.valueOf(LT), "d:k1=v1,k2=v2", "d:k1=v1,k3=v3" },61{ String.valueOf(LT), "d:k1=v1,k2=v2", "d:k2=v2,k1=v1,k3=v3" },62//63// wildcards64//65{ String.valueOf(LT), "d:k1=v1", "d:k1=v1,*" },66{ String.valueOf(GT), "d:k1=v1,k2=v2", "d:k1=v1,*" },67{ String.valueOf(GT), "domain:k1=v1", "?:k1=v1" },68{ String.valueOf(GT), "domain:k1=v1", "*:k1=v1" },69{ String.valueOf(GT), "domain:k1=v1", "domai?:k1=v1" },70{ String.valueOf(GT), "domain:k1=v1", "domai*:k1=v1" },71{ String.valueOf(GT), "domain:k1=v1", "do?ain:k1=v1" },72{ String.valueOf(GT), "domain:k1=v1", "do*ain:k1=v1" },73};7475private static boolean compare(char comparator, String on1, String on2) {76boolean ok = false;77System.out.println("Test " + on1 + " " + comparator + " " + on2);78try {79ObjectName o1 = ObjectName.getInstance(on1);80ObjectName o2 = ObjectName.getInstance(on2);81int result = o1.compareTo(o2);82switch (comparator) {83case LT:84if (result < 0)85ok = true;86break;87case EQ:88if (result == 0)89ok = true;90break;91case GT:92if (result > 0)93ok = true;94break;95default:96throw new IllegalArgumentException(97"Test incorrect: case: " + comparator);98}99} catch (Exception e) {100ok = false;101System.out.println("Got Unexpected Exception = " + e.toString());102}103return ok;104}105106private static boolean lessThan(String on1, String on2) {107return compare(LT, on1, on2);108}109110private static boolean equalTo(String on1, String on2) {111return compare(EQ, on1, on2);112}113114private static boolean greaterThan(String on1, String on2) {115return compare(GT, on1, on2);116}117118private static int runTest(char comparator, String on1, String on2) {119System.out.println("----------------------------------------------");120boolean ok = false;121boolean lt = lessThan(on1, on2);122boolean eq = equalTo(on1, on2);123boolean gt = greaterThan(on1, on2);124switch (comparator) {125case LT:126ok = lt && !eq && !gt;127System.out.println("Comparison result: LessThan");128break;129case EQ:130ok = !lt && eq && !gt;131System.out.println("Comparison result: EqualTo");132break;133case GT:134ok = !lt && !eq && gt;135System.out.println("Comparison result: GreaterThan");136break;137default:138throw new IllegalArgumentException(139"Test incorrect: case: " + comparator);140}141if (ok)142System.out.println("Test passed!");143else144System.out.println("Test failed!");145System.out.println("----------------------------------------------");146return ok ? 0 : 1;147}148149public static void main(String[] args) throws Exception {150151int error = 0;152153// Check null values154//155System.out.println("----------------------------------------------");156System.out.println("Test ObjectName.compareTo(null)");157try {158new ObjectName("d:k=v").compareTo(null);159error++;160System.out.println("Didn't get expected NullPointerException!");161System.out.println("Test failed!");162} catch (NullPointerException e) {163System.out.println("Got expected exception = " + e.toString());164System.out.println("Test passed!");165} catch (Exception e) {166error++;167System.out.println("Got unexpected exception = " + e.toString());168System.out.println("Test failed!");169}170System.out.println("----------------------------------------------");171172// Compare ObjectNames173//174for (int i = 0; i < tests.length; i++)175error += runTest(tests[i][0].charAt(0), tests[i][1], tests[i][2]);176177if (error > 0) {178final String msg = "Test FAILED! Got " + error + " error(s)";179System.out.println(msg);180throw new IllegalArgumentException(msg);181} else {182System.out.println("Test PASSED!");183}184}185}186187188