J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 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 4716807 |
| 27 | * @summary Test if the repository supports correctly the use of |
| 28 | * wildcards in the ObjectName key properties value part. |
| 29 | * @author Luis-Miguel Alventosa |
| 30 | * @run clean RepositoryWildcardTest |
| 31 | * @run build RepositoryWildcardTest |
| 32 | * @run main RepositoryWildcardTest |
| 33 | */ |
| 34 | |
| 35 | import java.util.HashSet; |
| 36 | import java.util.Set; |
| 37 | import java.util.TreeSet; |
| 38 | import javax.management.InstanceNotFoundException; |
| 39 | import javax.management.MBeanServer; |
| 40 | import javax.management.MBeanServerDelegate; |
| 41 | import javax.management.MBeanServerFactory; |
| 42 | import javax.management.ObjectName; |
| 43 | import javax.management.RuntimeOperationsException; |
| 44 | |
| 45 | public class RepositoryWildcardTest { |
| 46 | |
| 47 | private static final String classname = |
| 48 | "javax.management.monitor.StringMonitor"; |
| 49 | |
| 50 | private static int mbeanCreation(MBeanServer mbs, String name) |
| 51 | throws Exception { |
| 52 | int error = 0; |
| 53 | try { |
| 54 | System.out.println("Test: createMBean(" + name + ")"); |
| 55 | mbs.createMBean(classname, ObjectName.getInstance(name)); |
| 56 | error++; |
| 57 | System.out.println("Didn't get expected exception!"); |
| 58 | System.out.println("Test failed!"); |
| 59 | } catch (RuntimeOperationsException e) { |
| 60 | System.out.println("Got expected exception = " + |
| 61 | e.getCause().toString()); |
| 62 | System.out.println("Test passed!"); |
| 63 | } |
| 64 | return error; |
| 65 | } |
| 66 | |
| 67 | private static int mbeanDeletion(MBeanServer mbs, String name) |
| 68 | throws Exception { |
| 69 | int error = 0; |
| 70 | try { |
| 71 | System.out.println("Test: unregisterMBean(" + name + ")"); |
| 72 | mbs.unregisterMBean(ObjectName.getInstance(name)); |
| 73 | error++; |
| 74 | System.out.println("Didn't get expected exception!"); |
| 75 | System.out.println("Test failed!"); |
| 76 | } catch (InstanceNotFoundException e) { |
| 77 | System.out.println("Got expected exception = " + e.toString()); |
| 78 | System.out.println("Test passed!"); |
| 79 | } |
| 80 | return error; |
| 81 | } |
| 82 | |
| 83 | private static int mbeanQuery(MBeanServer mbs, |
| 84 | String name, |
| 85 | Set<ObjectName> expectedSet) |
| 86 | throws Exception { |
| 87 | int error = 0; |
| 88 | System.out.println("Test: queryNames(" + name + ")"); |
| 89 | Set<ObjectName> returnedSet = |
| 90 | mbs.queryNames(ObjectName.getInstance(name), null); |
| 91 | System.out.println("ReturnedSet = " + new TreeSet(returnedSet)); |
| 92 | System.out.println("ExpectedSet = " + new TreeSet(expectedSet)); |
| 93 | if (returnedSet.equals(expectedSet)) { |
| 94 | System.out.println("Test passed!"); |
| 95 | } else { |
| 96 | error++; |
| 97 | System.out.println("Test failed!"); |
| 98 | } |
| 99 | return error; |
| 100 | } |
| 101 | |
| 102 | public static void main(String[] args) throws Exception { |
| 103 | |
| 104 | int error = 0; |
| 105 | |
| 106 | MBeanServer mbs = MBeanServerFactory.newMBeanServer(); |
| 107 | |
| 108 | ObjectName[] namesArray = { |
| 109 | ObjectName.getInstance("d:k=abc"), |
| 110 | ObjectName.getInstance("d:k=abcd"), |
| 111 | ObjectName.getInstance("d:k=abcde"), |
| 112 | ObjectName.getInstance("d:k=abc,k2=v2"), |
| 113 | ObjectName.getInstance("d:k=abcd,k2=v2"), |
| 114 | ObjectName.getInstance("d:k=abcde,k2=v2"), |
| 115 | ObjectName.getInstance("d:k=\"abc\""), |
| 116 | ObjectName.getInstance("d:k=\"abc\",k2=v2"), |
| 117 | ObjectName.getInstance("d:p1=v1,p2=v2,p3=v3,p4=v4,p5=v5,p6=v6," + |
| 118 | "p7=v7,p8=v8,p9=v9,p10=v10,p11=v11,p12=v12") |
| 119 | }; |
| 120 | |
| 121 | for (ObjectName name : namesArray) |
| 122 | mbs.createMBean(classname, name); |
| 123 | |
| 124 | System.out.println("----------------------------------------------"); |
| 125 | System.out.println("TEST createMBean WITH PATTERNS"); |
| 126 | System.out.println("----------------------------------------------"); |
| 127 | |
| 128 | error += mbeanCreation(mbs, "d:k=v,*"); |
| 129 | error += mbeanCreation(mbs, "d:k=*"); |
| 130 | error += mbeanCreation(mbs, "d:k=a?b"); |
| 131 | error += mbeanCreation(mbs, "d:k=\"?\""); |
| 132 | error += mbeanCreation(mbs, "d:k=\"a*b\""); |
| 133 | |
| 134 | System.out.println("----------------------------------------------"); |
| 135 | System.out.println("TEST queryNames WITH PATTERNS"); |
| 136 | System.out.println("----------------------------------------------"); |
| 137 | |
| 138 | Set<ObjectName> expectedSet = new HashSet<ObjectName>(); |
| 139 | for (ObjectName name : namesArray) |
| 140 | expectedSet.add(name); |
| 141 | expectedSet.add(MBeanServerDelegate.DELEGATE_NAME); |
| 142 | Set<ObjectName> returnedSet = |
| 143 | mbs.queryNames(ObjectName.getInstance("*:*"), null); |
| 144 | error += mbeanQuery(mbs, "*:*", expectedSet); |
| 145 | |
| 146 | expectedSet = new HashSet<ObjectName>(); |
| 147 | for (ObjectName name : namesArray) |
| 148 | expectedSet.add(name); |
| 149 | error += mbeanQuery(mbs, "d:*", expectedSet); |
| 150 | |
| 151 | expectedSet = new HashSet<ObjectName>(); |
| 152 | expectedSet.add(namesArray[0]); |
| 153 | expectedSet.add(namesArray[1]); |
| 154 | expectedSet.add(namesArray[2]); |
| 155 | expectedSet.add(namesArray[6]); |
| 156 | error += mbeanQuery(mbs, "d:k=*", expectedSet); |
| 157 | |
| 158 | expectedSet = new HashSet<ObjectName>(); |
| 159 | expectedSet.add(namesArray[3]); |
| 160 | expectedSet.add(namesArray[4]); |
| 161 | expectedSet.add(namesArray[5]); |
| 162 | expectedSet.add(namesArray[7]); |
| 163 | error += mbeanQuery(mbs, "d:k=*,k2=v2", expectedSet); |
| 164 | |
| 165 | expectedSet = new HashSet<ObjectName>(); |
| 166 | expectedSet.add(namesArray[3]); |
| 167 | expectedSet.add(namesArray[4]); |
| 168 | expectedSet.add(namesArray[5]); |
| 169 | expectedSet.add(namesArray[7]); |
| 170 | error += mbeanQuery(mbs, "d:k=*,k2=v?", expectedSet); |
| 171 | |
| 172 | expectedSet = new HashSet<ObjectName>(); |
| 173 | expectedSet.add(namesArray[3]); |
| 174 | expectedSet.add(namesArray[4]); |
| 175 | expectedSet.add(namesArray[5]); |
| 176 | expectedSet.add(namesArray[7]); |
| 177 | error += mbeanQuery(mbs, "d:*,k2=v2", expectedSet); |
| 178 | |
| 179 | expectedSet = new HashSet<ObjectName>(); |
| 180 | expectedSet.add(namesArray[1]); |
| 181 | error += mbeanQuery(mbs, "d:k=ab?d", expectedSet); |
| 182 | |
| 183 | expectedSet = new HashSet<ObjectName>(); |
| 184 | expectedSet.add(namesArray[1]); |
| 185 | expectedSet.add(namesArray[4]); |
| 186 | error += mbeanQuery(mbs, "d:k=ab?d,*", expectedSet); |
| 187 | |
| 188 | expectedSet = new HashSet<ObjectName>(); |
| 189 | expectedSet.add(namesArray[6]); |
| 190 | error += mbeanQuery(mbs, "d:k=\"*\"", expectedSet); |
| 191 | |
| 192 | expectedSet = new HashSet<ObjectName>(); |
| 193 | expectedSet.add(namesArray[7]); |
| 194 | error += mbeanQuery(mbs, "d:k=\"*\",k2=v?", expectedSet); |
| 195 | |
| 196 | expectedSet = new HashSet<ObjectName>(); |
| 197 | expectedSet.add(namesArray[8]); |
| 198 | error += mbeanQuery(mbs, |
| 199 | "d:p1=v?,p2=v?,p3=v?,p4=v?,p5=v?,p6=v?," + |
| 200 | "p7=v?,p8=v?,p9=v?,p10=v??,p11=v??,p12=v??", |
| 201 | expectedSet); |
| 202 | |
| 203 | System.out.println("----------------------------------------------"); |
| 204 | System.out.println("TEST unregisterMBean WITH PATTERNS"); |
| 205 | System.out.println("----------------------------------------------"); |
| 206 | |
| 207 | error += mbeanDeletion(mbs, "d:k=*"); |
| 208 | error += mbeanDeletion(mbs, "d:k=\"*\""); |
| 209 | |
| 210 | if (error > 0) { |
| 211 | final String msg = "Test FAILED! Got " + error + " error(s)"; |
| 212 | System.out.println(msg); |
| 213 | throw new IllegalArgumentException(msg); |
| 214 | } else { |
| 215 | System.out.println("Test PASSED!"); |
| 216 | } |
| 217 | } |
| 218 | } |