KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > mbeanserver > XMBean2UnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.xml.mbeanserver;
24
25 import java.net.URL JavaDoc;
26 import java.net.InetAddress JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.HashMap JavaDoc;
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 JavaDoc;
44 import javax.management.MBeanAttributeInfo JavaDoc;
45 import javax.management.MBeanConstructorInfo JavaDoc;
46 import javax.management.MBeanNotificationInfo JavaDoc;
47 import javax.management.MBeanOperationInfo JavaDoc;
48 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
49 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
50
51 /**
52  * Test unmarshalling xml documents conforming to jboss_xmbean_2_0.xsd
53  *
54  * @author Scott.Stark@jboss.org
55  * @version $Revision: 42816 $
56  */

57 public class XMBean2UnitTestCase
58    extends AbstractJBossXBTest
59 {
60    public XMBean2UnitTestCase(String JavaDoc name)
61    {
62       super(name);
63    }
64
65    public void testJavaBeanSchemaInitializerInterceptor() throws Exception JavaDoc
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 JavaDoc xmlPath = getResourcePath("xml/mbeanserver/testXMBean2.xml");
75       Object JavaDoc root = unmarshaller.unmarshal(xmlPath, resolver);
76       System.out.println(root);
77       assertTrue("", root instanceof ModelMBeanInfoSupportWrapper);
78       ModelMBeanInfoSupportWrapper mbean = (ModelMBeanInfoSupportWrapper) root;
79       Descriptor JavaDoc descriptor = mbean.getDescriptors();
80       Object JavaDoc 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 JavaDoc.class);
85       URL JavaDoc homePage = new URL JavaDoc("http://www.jboss.org/");
86       assertTrue("homePage == URL(http://www.jboss.org/)", sbi.getHomePage().equals(homePage));
87       Long JavaDoc l = new Long JavaDoc(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 JavaDoc localhost = InetAddress.getByName("127.0.0.1");
92       assertTrue("address == 127.0.0.1", sbi.getAddress().equals(localhost));
93       String JavaDoc[] strings = {"string1", "string2", "string3"};
94       assertTrue("someStrings == {string1, string2, string3}",
95          Arrays.equals(strings, sbi.getSomeStrings()));
96       Properties JavaDoc someProperties = new Properties JavaDoc();
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 JavaDoc i1 = interceptors[1];
104       assertTrue(i1 instanceof PersistenceInterceptor2);
105       Object JavaDoc i2 = interceptors[2];
106       assertTrue(i2 instanceof ModelMBeanInterceptor);
107       Object JavaDoc i3 = interceptors[3];
108       assertTrue(i3 instanceof ObjectReferenceInterceptor);
109
110       String JavaDoc clazz = mbean.getClassName();
111       assertTrue("class is org.jboss.naming.JNDIBindingService",
112          clazz.equals("org.jboss.naming.JNDIBindingService"));
113
114       ModelMBeanInfo JavaDoc info = mbean.getMBeanInfo();
115       MBeanAttributeInfo JavaDoc[] attrs = info.getAttributes();
116       assertTrue("There are 2 attributes", attrs.length == 2);
117       MBeanAttributeInfo JavaDoc 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 JavaDoc 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 JavaDoc[] ctors = info.getConstructors();
127       assertEquals("ctors length", ctors.length, 1);
128       String JavaDoc description = info.getDescription();
129       assertEquals("description", description,
130          "An xmbean description with custom interceptors that are handled by the JavaBeanSchemaInitializer");
131       MBeanNotificationInfo JavaDoc[] 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 JavaDoc[] types = {"org.jboss.naming.JNDIBindingService.bindEvent"};
137       assertEquals("notices[0].types",
138          notices[0].getNotifTypes(), types);
139       MBeanOperationInfo JavaDoc[] 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    // Private
146

147    private String JavaDoc getResourcePath(String JavaDoc path)
148    {
149       URL JavaDoc 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