KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > xml > MultispacedUnitTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.xml;
8
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.Reader JavaDoc;
12 import java.io.StringReader JavaDoc;
13 import java.io.StringWriter JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.List JavaDoc;
16 import junit.framework.TestCase;
17 import org.jboss.logging.Logger;
18 import org.jboss.test.xml.multispaced.XMBeanAttributeMetaData;
19 import org.jboss.test.xml.multispaced.XMBeanConstructorMetaData;
20 import org.jboss.test.xml.multispaced.XMBeanMetaData;
21 import org.jboss.test.xml.multispaced.XMBeanMetaDataFactory;
22 import org.jboss.test.xml.multispaced.XMBeanMetaDataProvider;
23 import org.jboss.test.xml.multispaced.XMBeanNotificationMetaData;
24 import org.jboss.test.xml.multispaced.XMBeanOperationMetaData;
25 import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPm;
26 import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPmMetaDataFactory;
27 import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPmMetaDataProvider;
28 import org.jboss.xb.binding.Marshaller;
29 import org.jboss.xb.binding.ObjectModelProvider;
30 import org.jboss.xb.binding.Unmarshaller;
31 import org.jboss.xb.binding.UnmarshallerFactory;
32 import org.jboss.xb.binding.XercesXsMarshaller;
33 import org.jboss.xb.binding.XsMarshaller;
34
35 /**
36  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
37  * @version <tt>$Revision: 1.3.2.4 $</tt>
38  */

39 public class MultispacedUnitTestCase
40    extends TestCase
41 {
42    private static final Logger log = Logger.getLogger(MultispacedUnitTestCase.class);
43
44    public MultispacedUnitTestCase()
45    {
46    }
47
48    public MultispacedUnitTestCase(String JavaDoc name)
49    {
50       super(name);
51    }
52
53    public void testMultispacedUnmarshalling() throws Exception JavaDoc
54    {
55       log.debug("<multispaced-unmarshalling>");
56
57       InputStream JavaDoc xmlIs = getResource("xml/multispaced/xmbean.xml");
58       InputStreamReader JavaDoc xmlReader = new InputStreamReader JavaDoc(xmlIs);
59
60       XMBeanMetaData xmbean = unmarshalXMBean(xmlReader);
61
62       xmlReader.close();
63
64       checkUnmarshalledXMBean(xmbean);
65
66       log.debug("</multispaced-unmarshalling>");
67    }
68
69    public void testJaxMeMarshalling() throws Exception JavaDoc
70    {
71       log.debug("<multispaced-marshalling>");
72
73       System.setProperty(Marshaller.PROP_MARSHALLER, XsMarshaller.class.getName());
74       marshallingTest();
75
76       log.debug("</multispaced-marshalling>");
77    }
78
79    public void testXercesMarshalling() throws Exception JavaDoc
80    {
81       log.debug("<multispaced-marshalling>");
82
83       System.setProperty(Marshaller.PROP_MARSHALLER, XercesXsMarshaller.class.getName());
84       marshallingTest();
85
86       log.debug("</multispaced-marshalling>");
87    }
88
89    // Private
90

91    private void marshallingTest()
92       throws Exception JavaDoc
93    {
94       StringWriter JavaDoc strWriter = new StringWriter JavaDoc();
95
96       Marshaller marshaller = Marshaller.FACTORY.getInstance();
97       marshaller.addRootElement("http://jboss.org/xmbean", "xmbean", "mbean");
98
99       // todo something with this
100
if(marshaller instanceof XsMarshaller)
101       {
102          ((XsMarshaller)marshaller).declareNamespace("xmbean", "http://jboss.org/xmbean");
103          ((XsMarshaller)marshaller).declareNamespace("jdbcpm", "http://jboss.org/xmbean/persistence/jdbc");
104       }
105       else if(marshaller instanceof XercesXsMarshaller)
106       {
107          ((XercesXsMarshaller)marshaller).declareNamespace("xmbean", "http://jboss.org/xmbean");
108          ((XercesXsMarshaller)marshaller).declareNamespace("jdbcpm", "http://jboss.org/xmbean/persistence/jdbc");
109       }
110
111       XMBeanMetaData xmbean = createXMBeanMetaData();
112       ObjectModelProvider provider = XMBeanMetaDataProvider.INSTANCE;
113
114       ObjectModelProvider jdbcPmProvider = new JDBCPmMetaDataProvider((JDBCPm)xmbean.getPersistenceManager());
115       marshaller.mapClassToNamespace(JDBCPm.class,
116          "persistence-manager",
117          "http://jboss.org/xmbean/persistence/jdbc",
118          getResourceUrl("xml/multispaced/jdbcpm.xsd").toString(),
119          jdbcPmProvider
120       );
121
122       marshaller.marshal(getResourceUrl("xml/multispaced/xmbean.xsd").toString(), provider, xmbean, strWriter);
123
124       final String JavaDoc xml = strWriter.getBuffer().toString();
125       log.debug("marshalled with " + marshaller.getClass().getName() + ": " + xml);
126
127       StringReader JavaDoc xmlReader = new StringReader JavaDoc(xml);
128       XMBeanMetaData unmarshalled = unmarshalXMBean(xmlReader);
129
130       assertEquals(xmbean, unmarshalled);
131    }
132
133    private XMBeanMetaData unmarshalXMBean(Reader JavaDoc xmlReader)
134       throws Exception JavaDoc
135    {
136       XMBeanMetaData xmbean = new XMBeanMetaData();
137
138       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
139             .newUnmarshaller();
140       unmarshaller.mapFactoryToNamespace(JDBCPmMetaDataFactory.INSTANCE, "http://jboss.org/xmbean/persistence/jdbc");
141       unmarshaller.unmarshal(xmlReader, XMBeanMetaDataFactory.INSTANCE, xmbean);
142
143       return xmbean;
144    }
145
146    private void checkUnmarshalledXMBean(XMBeanMetaData xmbean)
147    {
148       log.debug("xmbean: " + xmbean);
149
150       assertEquals("The JBoss XMBean version of the monitor server", xmbean.getDescription());
151       assertEquals("monitor.MonitorPOJO", xmbean.getMbeanClass());
152
153       final List JavaDoc constructors = xmbean.getConstructors();
154       assertEquals(1, constructors.size());
155       XMBeanConstructorMetaData constructor = (XMBeanConstructorMetaData)constructors.get(0);
156       assertEquals("The no-arg constructor", constructor.getDescription());
157       assertEquals("monitor.MonitorPOJO", constructor.getName());
158
159       final List JavaDoc attributes = xmbean.getAttributes();
160       assertEquals(1, attributes.size());
161       XMBeanAttributeMetaData attribute = (XMBeanAttributeMetaData)attributes.get(0);
162       assertEquals("read-write", attribute.getAccess());
163       assertEquals("getInterval", attribute.getGetMethod());
164       assertEquals("setInterval", attribute.getSetMethod());
165       assertEquals("The interval in milliseconds between checks of VM memory and threads", attribute.getDescription());
166       assertEquals("Interval", attribute.getName());
167       assertEquals("int", attribute.getType());
168
169       final List JavaDoc operations = xmbean.getOperations();
170       assertEquals(1, operations.size());
171       XMBeanOperationMetaData operation = (XMBeanOperationMetaData)operations.get(0);
172       assertEquals("Access the last HistoryLength monitor reports", operation.getDescription());
173       assertEquals("history", operation.getName());
174       assertEquals("java.lang.String", operation.getReturnType());
175
176       final List JavaDoc notifications = xmbean.getNotifications();
177       assertEquals(1, notifications.size());
178       XMBeanNotificationMetaData notification = (XMBeanNotificationMetaData)notifications.get(0);
179       assertEquals("A notification sent when the monitor interval expires", notification.getDescription());
180       assertEquals("javax.management.Notification", notification.getName());
181       assertEquals("monitor.IntervalElapsed", notification.getNotificationType());
182
183       final JDBCPm pm = (JDBCPm)xmbean.getPersistenceManager();
184       if(pm == null)
185       {
186          fail("persistence-manager is null.");
187       }
188
189       assertEquals("java:/DefaultDS", pm.getDatasource());
190       assertEquals("xmbeans", pm.getTable());
191    }
192
193    private XMBeanMetaData createXMBeanMetaData()
194    {
195       XMBeanMetaData xmbean = new XMBeanMetaData();
196       xmbean.setDescription("The JBoss XMBean version of the monitor server");
197       xmbean.setMbeanClass("monitor.MonitorPOJO");
198
199       XMBeanConstructorMetaData constructor = new XMBeanConstructorMetaData();
200       constructor.setDescription("The no-arg constructor");
201       constructor.setName("monitor.MonitorPOJO");
202       xmbean.addConstructor(constructor);
203
204       XMBeanAttributeMetaData attribute = new XMBeanAttributeMetaData();
205       attribute.setAccess("read-write");
206       attribute.setGetMethod("getInterval");
207       attribute.setSetMethod("setInterval");
208       attribute.setDescription("The interval in milliseconds between checks of VM memory and threads");
209       attribute.setName("Interval");
210       attribute.setType("int");
211       xmbean.addAttribute(attribute);
212
213       XMBeanOperationMetaData operation = new XMBeanOperationMetaData();
214       operation.setDescription("Access the last HistoryLength monitor reports");
215       operation.setName("history");
216       operation.setReturnType("java.lang.String");
217       xmbean.addOperation(operation);
218
219       XMBeanNotificationMetaData notification = new XMBeanNotificationMetaData();
220       notification.setDescription("A notification sent when the monitor interval expires");
221       notification.setName("javax.management.Notification");
222       notification.setNotificationType("monitor.IntervalElapsed");
223       xmbean.addNotification(notification);
224
225       JDBCPm pm = new JDBCPm();
226       pm.setDatasource("java:/DefaultDS");
227       pm.setTable("xmbeans");
228       xmbean.setPersistenceManager(pm);
229
230       return xmbean;
231    }
232
233    private static InputStream JavaDoc getResource(String JavaDoc name)
234    {
235       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
236       if(is == null)
237       {
238          throw new IllegalStateException JavaDoc("Resource not found: " + name);
239       }
240       return is;
241    }
242
243    private static URL JavaDoc getResourceUrl(String JavaDoc name)
244    {
245       URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(name);
246       if(url == null)
247       {
248          throw new IllegalStateException JavaDoc("Resource not found: " + name);
249       }
250       return url;
251    }
252 }
253
Popular Tags