001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.xml;
023:
024: import junit.framework.TestCase;
025:
026: import java.io.InputStream;
027: import java.io.Reader;
028: import java.io.InputStreamReader;
029: import java.io.StringWriter;
030: import java.io.StringReader;
031: import java.util.List;
032: import java.net.URL;
033:
034: import org.jboss.xb.binding.Marshaller;
035: import org.jboss.xb.binding.ObjectModelProvider;
036: import org.jboss.xb.binding.Unmarshaller;
037: import org.jboss.xb.binding.UnmarshallerFactory;
038: import org.jboss.xb.binding.XercesXsMarshaller;
039: import org.jboss.xb.binding.AbstractMarshaller;
040: import org.jboss.xb.binding.sunday.MarshallerImpl;
041: import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPmMetaDataFactory;
042: import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPm;
043: import org.jboss.test.xml.multispaced.pm.jdbc.JDBCPmMetaDataProvider;
044: import org.jboss.test.xml.multispaced.XMBeanMetaData;
045: import org.jboss.test.xml.multispaced.XMBeanMetaDataFactory;
046: import org.jboss.test.xml.multispaced.XMBeanMetaDataProvider;
047: import org.jboss.test.xml.multispaced.XMBeanOperationMetaData;
048: import org.jboss.test.xml.multispaced.XMBeanAttributeMetaData;
049: import org.jboss.test.xml.multispaced.XMBeanConstructorMetaData;
050: import org.jboss.test.xml.multispaced.XMBeanNotificationMetaData;
051: import org.jboss.logging.Logger;
052:
053: /**
054: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
055: * @version <tt>$Revision: 57211 $</tt>
056: */
057: public class MultispacedUnitTestCase extends TestCase {
058: private static final Logger log = Logger
059: .getLogger(MultispacedUnitTestCase.class);
060:
061: public MultispacedUnitTestCase() {
062: }
063:
064: public MultispacedUnitTestCase(String name) {
065: super (name);
066: }
067:
068: public void testMultispacedUnmarshalling() throws Exception {
069: log.debug("--- " + getName());
070:
071: InputStream xmlIs = getResource("xml/multispaced/xmbean.xml");
072: InputStreamReader xmlReader = new InputStreamReader(xmlIs);
073:
074: XMBeanMetaData xmbean = unmarshalXMBean(xmlReader);
075:
076: xmlReader.close();
077:
078: checkUnmarshalledXMBean(xmbean);
079: }
080:
081: public void testMultispacedUnmarshalling2() throws Exception {
082: log.debug("--- " + getName());
083:
084: InputStream xmlIs = getResource("xml/multispaced/xmbean-localns.xml");
085: InputStreamReader xmlReader = new InputStreamReader(xmlIs);
086:
087: XMBeanMetaData xmbean = unmarshalXMBean(xmlReader);
088:
089: xmlReader.close();
090:
091: checkUnmarshalledXMBean(xmbean);
092: }
093:
094: public void testMarshallingXerces() throws Exception {
095: log.debug("--- " + getName());
096:
097: System.setProperty(Marshaller.PROP_MARSHALLER,
098: XercesXsMarshaller.class.getName());
099: marshallingTest();
100: }
101:
102: public void testMarshallingSunday() throws Exception {
103: log.debug("--- " + getName());
104:
105: System.setProperty(Marshaller.PROP_MARSHALLER,
106: MarshallerImpl.class.getName());
107: marshallingTest();
108: }
109:
110: // Private
111:
112: private void marshallingTest() throws Exception {
113: StringWriter strWriter = new StringWriter();
114:
115: AbstractMarshaller marshaller = (AbstractMarshaller) Marshaller.FACTORY
116: .getInstance();
117: marshaller.addRootElement("http://jboss.org/xmbean", "xmbean",
118: "mbean");
119: marshaller
120: .declareNamespace("xmbean", "http://jboss.org/xmbean");
121: marshaller.declareNamespace("jdbcpm",
122: "http://jboss.org/xmbean/persistence/jdbc");
123:
124: XMBeanMetaData xmbean = createXMBeanMetaData();
125: ObjectModelProvider provider = XMBeanMetaDataProvider.INSTANCE;
126:
127: ObjectModelProvider jdbcPmProvider = new JDBCPmMetaDataProvider(
128: (JDBCPm) xmbean.getPersistenceManager());
129: marshaller
130: .mapClassToGlobalElement(JDBCPm.class,
131: "persistence-manager",
132: "http://jboss.org/xmbean/persistence/jdbc",
133: getResourceUrl("xml/multispaced/jdbcpm.xsd")
134: .toString(), jdbcPmProvider);
135:
136: marshaller.marshal(getResourceUrl("xml/multispaced/xmbean.xsd")
137: .toString(), provider, xmbean, strWriter);
138:
139: final String xml = strWriter.getBuffer().toString();
140: log.debug("marshalled with " + marshaller.getClass().getName()
141: + ": " + xml);
142:
143: StringReader xmlReader = new StringReader(xml);
144: XMBeanMetaData unmarshalled = unmarshalXMBean(xmlReader);
145:
146: assertEquals(xmbean, unmarshalled);
147: }
148:
149: private XMBeanMetaData unmarshalXMBean(Reader xmlReader)
150: throws Exception {
151: XMBeanMetaData xmbean = new XMBeanMetaData();
152:
153: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
154: .newUnmarshaller();
155: unmarshaller.mapFactoryToNamespace(
156: JDBCPmMetaDataFactory.INSTANCE,
157: "http://jboss.org/xmbean/persistence/jdbc");
158: unmarshaller.unmarshal(xmlReader,
159: XMBeanMetaDataFactory.INSTANCE, xmbean);
160:
161: return xmbean;
162: }
163:
164: private void checkUnmarshalledXMBean(XMBeanMetaData xmbean) {
165: log.debug("xmbean: " + xmbean);
166:
167: assertEquals("The JBoss XMBean version of the monitor server",
168: xmbean.getDescription());
169: assertEquals("monitor.MonitorPOJO", xmbean.getMbeanClass());
170:
171: final List constructors = xmbean.getConstructors();
172: assertEquals(1, constructors.size());
173: XMBeanConstructorMetaData constructor = (XMBeanConstructorMetaData) constructors
174: .get(0);
175: assertEquals("The no-arg constructor", constructor
176: .getDescription());
177: assertEquals("monitor.MonitorPOJO", constructor.getName());
178:
179: final List attributes = xmbean.getAttributes();
180: assertEquals(1, attributes.size());
181: XMBeanAttributeMetaData attribute = (XMBeanAttributeMetaData) attributes
182: .get(0);
183: assertEquals("read-write", attribute.getAccess());
184: assertEquals("getInterval", attribute.getGetMethod());
185: assertEquals("setInterval", attribute.getSetMethod());
186: assertEquals(
187: "The interval in milliseconds between checks of VM memory and threads",
188: attribute.getDescription());
189: assertEquals("Interval", attribute.getName());
190: assertEquals("int", attribute.getType());
191:
192: final List operations = xmbean.getOperations();
193: assertEquals(1, operations.size());
194: XMBeanOperationMetaData operation = (XMBeanOperationMetaData) operations
195: .get(0);
196: assertEquals("Access the last HistoryLength monitor reports",
197: operation.getDescription());
198: assertEquals("history", operation.getName());
199: assertEquals("java.lang.String", operation.getReturnType());
200:
201: final List notifications = xmbean.getNotifications();
202: assertEquals(1, notifications.size());
203: XMBeanNotificationMetaData notification = (XMBeanNotificationMetaData) notifications
204: .get(0);
205: assertEquals(
206: "A notification sent when the monitor interval expires",
207: notification.getDescription());
208: assertEquals("javax.management.Notification", notification
209: .getName());
210: assertEquals("monitor.IntervalElapsed", notification
211: .getNotificationType());
212:
213: final JDBCPm pm = (JDBCPm) xmbean.getPersistenceManager();
214: if (pm == null) {
215: fail("persistence-manager is null.");
216: }
217:
218: assertEquals("java:/DefaultDS", pm.getDatasource());
219: assertEquals("xmbeans", pm.getTable());
220: }
221:
222: private XMBeanMetaData createXMBeanMetaData() {
223: XMBeanMetaData xmbean = new XMBeanMetaData();
224: xmbean
225: .setDescription("The JBoss XMBean version of the monitor server");
226: xmbean.setMbeanClass("monitor.MonitorPOJO");
227:
228: XMBeanConstructorMetaData constructor = new XMBeanConstructorMetaData();
229: constructor.setDescription("The no-arg constructor");
230: constructor.setName("monitor.MonitorPOJO");
231: xmbean.addConstructor(constructor);
232:
233: XMBeanAttributeMetaData attribute = new XMBeanAttributeMetaData();
234: attribute.setAccess("read-write");
235: attribute.setGetMethod("getInterval");
236: attribute.setSetMethod("setInterval");
237: attribute
238: .setDescription("The interval in milliseconds between checks of VM memory and threads");
239: attribute.setName("Interval");
240: attribute.setType("int");
241: xmbean.addAttribute(attribute);
242:
243: XMBeanOperationMetaData operation = new XMBeanOperationMetaData();
244: operation
245: .setDescription("Access the last HistoryLength monitor reports");
246: operation.setName("history");
247: operation.setReturnType("java.lang.String");
248: xmbean.addOperation(operation);
249:
250: XMBeanNotificationMetaData notification = new XMBeanNotificationMetaData();
251: notification
252: .setDescription("A notification sent when the monitor interval expires");
253: notification.setName("javax.management.Notification");
254: notification.setNotificationType("monitor.IntervalElapsed");
255: xmbean.addNotification(notification);
256:
257: JDBCPm pm = new JDBCPm();
258: pm.setDatasource("java:/DefaultDS");
259: pm.setTable("xmbeans");
260: xmbean.setPersistenceManager(pm);
261:
262: return xmbean;
263: }
264:
265: private static InputStream getResource(String name) {
266: InputStream is = Thread.currentThread().getContextClassLoader()
267: .getResourceAsStream(name);
268: if (is == null) {
269: throw new IllegalStateException("Resource not found: "
270: + name);
271: }
272: return is;
273: }
274:
275: private static URL getResourceUrl(String name) {
276: URL url = Thread.currentThread().getContextClassLoader()
277: .getResource(name);
278: if (url == null) {
279: throw new IllegalStateException("Resource not found: "
280: + name);
281: }
282: return url;
283: }
284: }
|