1 22 23 package org.jboss.test.xml.mbeanserver; 24 25 import java.net.URL ; 26 import java.net.InetAddress ; 27 import java.util.Arrays ; 28 import java.util.Properties ; 29 import java.util.HashMap ; 30 31 import junit.framework.TestCase; 32 import org.jboss.xb.binding.Unmarshaller; 33 import org.jboss.xb.binding.UnmarshallerFactory; 34 import org.jboss.xb.binding.sunday.unmarshalling.DefaultSchemaResolver; 35 import org.jboss.test.xml.AbstractJBossXBTest; 36 import org.jboss.test.xml.mbeanserver.interceptors.SomeBeanInterceptor; 37 import org.jboss.mx.metadata.xb.ModelMBeanInfoSupportWrapper; 38 import org.jboss.mx.interceptor.Interceptor; 39 import org.jboss.mx.interceptor.PersistenceInterceptor2; 40 import org.jboss.mx.interceptor.ModelMBeanInterceptor; 41 import org.jboss.mx.interceptor.ObjectReferenceInterceptor; 42 43 import javax.management.Descriptor ; 44 import javax.management.MBeanAttributeInfo ; 45 import javax.management.MBeanConstructorInfo ; 46 import javax.management.MBeanNotificationInfo ; 47 import javax.management.MBeanOperationInfo ; 48 import javax.management.modelmbean.ModelMBeanInfo ; 49 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 50 51 57 public class XMBean2UnitTestCase 58 extends AbstractJBossXBTest 59 { 60 public XMBean2UnitTestCase(String name) 61 { 62 super(name); 63 } 64 65 public void testJavaBeanSchemaInitializerInterceptor() throws Exception 66 { 67 DefaultSchemaResolver resolver = new DefaultSchemaResolver(); 68 JavaBeanSchemaInitializer si = new JavaBeanSchemaInitializer(); 69 resolver.addSchemaInitializer("urn:jboss:simplejavabean:1.0", si); 70 resolver.addSchemaLocation("urn:jboss-test:xmbean:2.0", "xml/mbeanserver/jboss_xmbean_2_0.xsd"); 71 resolver.addSchemaLocation("urn:jboss:simplejavabean:1.0", "xml/mbeanserver/simplejavabean_1_0.xsd"); 72 73 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 74 String xmlPath = getResourcePath("xml/mbeanserver/testXMBean2.xml"); 75 Object root = unmarshaller.unmarshal(xmlPath, resolver); 76 System.out.println(root); 77 assertTrue("", root instanceof ModelMBeanInfoSupportWrapper); 78 ModelMBeanInfoSupportWrapper mbean = (ModelMBeanInfoSupportWrapper) root; 79 Descriptor descriptor = mbean.getDescriptors(); 80 Object i = descriptor.getFieldValue("interceptors"); 81 Interceptor[] interceptors = (Interceptor[]) i; 82 SomeBeanInterceptor sbi = (SomeBeanInterceptor) interceptors[0]; 83 assertTrue(sbi.isFlag()); 84 assertTrue("aClass == Integer.class", sbi.getaClass() == Integer .class); 85 URL homePage = new URL ("http://www.jboss.org/"); 86 assertTrue("homePage == URL(http://www.jboss.org/)", sbi.getHomePage().equals(homePage)); 87 Long l = new Long (123456789); 88 assertTrue("aLong == 123456789", sbi.getaLong().equals(l)); 89 assertTrue("aString == string1", sbi.getaString().equals("string1")); 90 assertTrue("anInt == 1234", sbi.getAnInt() == 1234); 91 InetAddress localhost = InetAddress.getByName("127.0.0.1"); 92 assertTrue("address == 127.0.0.1", sbi.getAddress().equals(localhost)); 93 String [] strings = {"string1", "string2", "string3"}; 94 assertTrue("someStrings == {string1, string2, string3}", 95 Arrays.equals(strings, sbi.getSomeStrings())); 96 Properties someProperties = new Properties (); 97 someProperties.setProperty("prop1", "value1"); 98 someProperties.setProperty("prop2", "value2"); 99 someProperties.setProperty("prop3", "value3"); 100 assertTrue("someProperties == {string1, string2, string3}", 101 sbi.getSomeProperties().equals(someProperties)); 102 103 Object i1 = interceptors[1]; 104 assertTrue(i1 instanceof PersistenceInterceptor2); 105 Object i2 = interceptors[2]; 106 assertTrue(i2 instanceof ModelMBeanInterceptor); 107 Object i3 = interceptors[3]; 108 assertTrue(i3 instanceof ObjectReferenceInterceptor); 109 110 String clazz = mbean.getClassName(); 111 assertTrue("class is org.jboss.naming.JNDIBindingService", 112 clazz.equals("org.jboss.naming.JNDIBindingService")); 113 114 ModelMBeanInfo info = mbean.getMBeanInfo(); 115 MBeanAttributeInfo [] attrs = info.getAttributes(); 116 assertTrue("There are 2 attributes", attrs.length == 2); 117 MBeanAttributeInfo rn = info.getAttribute("RootName"); 118 assertNotNull(rn); 119 assertEquals("RootName.name", rn.getName(), "RootName"); 120 assertEquals("RootName.type", rn.getType(), "java.lang.String"); 121 MBeanAttributeInfo bindings = info.getAttribute("Bindings"); 122 assertNotNull(bindings); 123 assertEquals("Bindings.name", bindings.getName(), "Bindings"); 124 assertEquals("Bindings.type", bindings.getType(), "org.jboss.naming.JNDIBindings"); 125 126 MBeanConstructorInfo [] ctors = info.getConstructors(); 127 assertEquals("ctors length", ctors.length, 1); 128 String description = info.getDescription(); 129 assertEquals("description", description, 130 "An xmbean description with custom interceptors that are handled by the JavaBeanSchemaInitializer"); 131 MBeanNotificationInfo [] notices = info.getNotifications(); 132 assertEquals("notices length", notices.length, 1); 133 assertEquals("notices[0].name", notices[0].getName(), "bindEvent"); 134 assertEquals("notices[0].description", 135 notices[0].getDescription(), "The bind event notification"); 136 String [] types = {"org.jboss.naming.JNDIBindingService.bindEvent"}; 137 assertEquals("notices[0].types", 138 notices[0].getNotifTypes(), types); 139 MBeanOperationInfo [] ops = info.getOperations(); 140 assertEquals("ops length", ops.length, 2); 141 assertEquals("ops[0].name", ops[0].getName(), "start"); 142 assertEquals("ops[1].name", ops[1].getName(), "stop"); 143 } 144 145 147 private String getResourcePath(String path) 148 { 149 URL url = Thread.currentThread().getContextClassLoader().getResource(path); 150 if(url == null) 151 { 152 fail("URL not found: " + path); 153 } 154 return url.toString(); 155 } 156 } 157 | Popular Tags |