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