1 22 package org.jboss.test.jbossmx.compliance.server; 23 24 import org.jboss.test.jbossmx.compliance.TestCase; 25 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.ObjectName ; 29 import javax.management.Attribute ; 30 import javax.management.InstanceNotFoundException ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.MBeanException ; 33 import javax.management.RuntimeMBeanException ; 34 import javax.management.RuntimeErrorException ; 35 import javax.management.InvalidAttributeValueException ; 36 37 import org.jboss.test.jbossmx.compliance.server.support.Test; 38 import org.jboss.test.jbossmx.compliance.server.support.TestMBean; 39 import org.jboss.test.jbossmx.compliance.server.support.MyScreamingException; 40 import org.jboss.test.jbossmx.compliance.server.support.ExceptionOnTheRun; 41 import org.jboss.test.jbossmx.compliance.server.support.BabarError; 42 43 44 public class MBeanServerTestCase 45 extends TestCase 46 { 47 public MBeanServerTestCase(String s) 48 { 49 super(s); 50 } 51 52 public void testInvokeWithNonExistantMBean() 53 { 54 try 55 { 56 MBeanServer server = MBeanServerFactory.createMBeanServer(); 57 server.invoke(new ObjectName (":mbean=doesnotexist"), "noMethod", null, null); 58 59 fail("InstanceNotFoundException was not thrown from an invoke operation on a non-existant MBean."); 61 } 62 catch (InstanceNotFoundException e) 63 { 64 } 66 catch (Throwable t) 67 { 68 log.debug("failed", t); 69 fail("Unexpected error on server.invoke(NonExistantMBean): " + t.toString()); 70 } 71 } 72 73 public void testInvokeWithBusinessException() 74 { 75 try 76 { 77 MBeanServer server = MBeanServerFactory.createMBeanServer(); 78 ObjectName name = new ObjectName ("test:test=test"); 79 server.registerMBean(new Test(), name); 80 81 server.invoke(name, "operationWithException", null, null); 82 83 fail("MBeanException was not thrown."); 85 } 86 catch (MBeanException e) 87 { 88 assertTrue(e.getTargetException() instanceof MyScreamingException); 90 } 91 catch (Throwable t) 92 { 93 fail("Unexpected error: " + t.toString()); 94 } 95 } 96 97 98 public void testGetAttributeWithNonExistingAttribute() 99 { 100 try 101 { 102 MBeanServer server = MBeanServerFactory.createMBeanServer(); 103 Object foo = server.getAttribute(new ObjectName (MBEAN_SERVER_DELEGATE), "Foo"); 104 105 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 107 } 108 catch (AttributeNotFoundException e) 109 { 110 } 112 catch (Throwable t) 113 { 114 fail("Unexpected error: " + t.toString()); 115 } 116 } 117 118 public void testGetAttributeWithBusinessException() 119 { 120 try 121 { 122 MBeanServer server = MBeanServerFactory.createMBeanServer(); 123 ObjectName name = new ObjectName ("test:test=test"); 124 server.registerMBean(new Test(), name); 125 126 Object foo = server.getAttribute(name, "ThisWillScream"); 127 128 fail("Did not throw the screaming exception"); 130 } 131 catch (MBeanException e) 132 { 133 assertTrue(e.getTargetException() instanceof MyScreamingException); 137 } 138 catch (Throwable t) 139 { 140 fail("Unexpected error: " + t.toString()); 141 } 142 } 143 144 public void testGetAttributeWithNonExistingMBean() 145 { 146 try 147 { 148 MBeanServer server = MBeanServerFactory.createMBeanServer(); 149 ObjectName name = new ObjectName ("test:name=DoesNotExist"); 150 151 server.getAttribute(name, "Whatever"); 152 153 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 155 } 156 catch (InstanceNotFoundException e) 157 { 158 } 160 catch (Throwable t) 161 { 162 fail("Unexpected error: " + t.toString()); 163 } 164 } 165 166 public void testGetAttributeWithUncheckedException() 167 { 168 try 169 { 170 MBeanServer server = MBeanServerFactory.createMBeanServer(); 171 ObjectName name = new ObjectName ("test:test=test"); 172 server.registerMBean(new Test(), name); 173 174 server.getAttribute(name, "ThrowUncheckedException"); 175 176 fail("RuntimeMBeanException was not thrown"); 178 } 179 catch (RuntimeMBeanException e) 180 { 181 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 183 } 184 catch (Throwable t) 185 { 186 fail("Unexpected err0r: " + t.toString()); 187 } 188 } 189 190 public void testGetAttributeWithError() 191 { 192 try 193 { 194 MBeanServer server = MBeanServerFactory.createMBeanServer(); 195 ObjectName name = new ObjectName ("test:test=test"); 196 server.registerMBean(new Test(), name); 197 198 server.getAttribute(name, "Error"); 199 200 fail("Error was not thrown"); 202 } 203 catch (RuntimeErrorException e) 204 { 205 assertTrue(e.getTargetError() instanceof BabarError); 207 } 208 catch (Throwable t) 209 { 210 fail("Unexpected error: " + t.toString()); 211 } 212 } 213 214 public void testSetAttributeWithNonExistingAttribute() 215 { 216 try 217 { 218 MBeanServer server = MBeanServerFactory.createMBeanServer(); 219 server.setAttribute(new ObjectName (MBEAN_SERVER_DELEGATE), new Attribute ("Foo", "value")); 220 221 fail("AttributeNotFoundexception was not thrown when invoking getAttribute() call on a non-existant attribute."); 223 } 224 catch (AttributeNotFoundException e) 225 { 226 } 228 catch (Throwable t) 229 { 230 fail("Unexpected error: " + t.toString()); 231 } 232 } 233 234 public void testSetAttributeWithBusinessException() 235 { 236 try 237 { 238 MBeanServer server = MBeanServerFactory.createMBeanServer(); 239 ObjectName name = new ObjectName ("test:test=test"); 240 server.registerMBean(new Test(), name); 241 242 server.setAttribute(name, new Attribute ("ThisWillScream", "value")); 243 244 fail("Did not throw the screaming exception"); 246 } 247 catch (MBeanException e) 248 { 249 assertTrue(e.getTargetException() instanceof MyScreamingException); 253 } 254 catch (Throwable t) 255 { 256 fail("Unexpected error: " + t.toString()); 257 } 258 } 259 260 public void testSetAttributeWithNonExistingMBean() 261 { 262 try 263 { 264 MBeanServer server = MBeanServerFactory.createMBeanServer(); 265 ObjectName name = new ObjectName ("test:name=DoesNotExist"); 266 267 server.setAttribute(name, new Attribute ("Whatever", "nothing")); 268 269 fail("InstanceNotFoundException was not thrown on a nonexistant MBean."); 271 } 272 catch (InstanceNotFoundException e) 273 { 274 } 276 catch (Throwable t) 277 { 278 fail("Unexpected error: " + t.toString()); 279 } 280 } 281 282 public void testSetAttributeWithUncheckedException() 283 { 284 try 285 { 286 MBeanServer server = MBeanServerFactory.createMBeanServer(); 287 ObjectName name = new ObjectName ("test:test=test"); 288 server.registerMBean(new Test(), name); 289 290 server.setAttribute(name, new Attribute ("ThrowUncheckedException", "value")); 291 292 fail("RuntimeMBeanException was not thrown"); 294 } 295 catch (RuntimeMBeanException e) 296 { 297 assertTrue(e.getTargetException() instanceof ExceptionOnTheRun); 299 } 300 catch (Throwable t) 301 { 302 fail("Unexpected err0r: " + t.toString()); 303 } 304 } 305 306 public void testSetAttributeWithError() 307 { 308 try 309 { 310 MBeanServer server = MBeanServerFactory.createMBeanServer(); 311 ObjectName name = new ObjectName ("test:test=test"); 312 server.registerMBean(new Test(), name); 313 314 server.setAttribute(name, new Attribute ("Error", "value")); 315 316 fail("Error was not thrown"); 318 } 319 catch (RuntimeErrorException e) 320 { 321 assertTrue(e.getTargetError() instanceof BabarError); 323 } 324 catch (Throwable t) 325 { 326 fail("Unexpected error: " + t.toString()); 327 } 328 } 329 330 } 331 | Popular Tags |