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 java.util.List;
025: import java.util.Map;
026: import java.util.Properties;
027: import java.net.URL;
028: import java.net.InetAddress;
029: import java.io.InputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.ByteArrayInputStream;
032: import java.io.IOException;
033:
034: import javax.security.auth.login.AppConfigurationEntry;
035: import javax.xml.parsers.DocumentBuilderFactory;
036: import javax.xml.parsers.DocumentBuilder;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.Transformer;
040: import javax.xml.transform.stream.StreamResult;
041:
042: import org.jboss.test.xml.mbeanserver.Services;
043: import org.jboss.test.xml.mbeanserver.MBeanData;
044: import org.jboss.test.xml.mbeanserver.MBeanAttribute;
045: import org.jboss.test.xml.mbeanserver.PolicyConfig;
046: import org.jboss.test.xml.mbeanserver.AuthenticationInfo;
047: import org.jboss.xb.binding.JBossXBRuntimeException;
048: import org.jboss.xb.binding.Unmarshaller;
049: import org.jboss.xb.binding.UnmarshallerFactory;
050: import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
051: import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
052: import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
053: import org.jboss.naming.JNDIBindings;
054: import org.jboss.naming.JNDIBinding;
055: import org.jboss.util.xml.JBossEntityResolver;
056: import org.w3c.dom.Element;
057: import org.w3c.dom.Document;
058: import org.w3c.dom.NodeList;
059: import org.w3c.dom.Node;
060: import org.w3c.dom.ls.LSInput;
061: import org.xml.sax.InputSource;
062: import org.xml.sax.SAXException;
063:
064: import junit.framework.TestCase;
065:
066: /**
067: * Test unmarshalling xml documents conforming to mbean-service_1_0.xsd into
068: * the org.jboss.test.xml.mbeanserver.Services and related objects.
069: *
070: * @author Scott.Stark@jboss.org
071: * @version $Revision: 57211 $
072: */
073: public class MBeanServerUnitTestCase extends TestCase {
074: public void testMbeanService() throws Exception {
075: InputStream is = getResource("xml/mbeanserver/mbean-service_1_0.xsd");
076: SchemaBinding schemaBinding = XsdBinder.bind(is, null);
077: schemaBinding.setIgnoreUnresolvedFieldOrClass(true);
078: schemaBinding.setSchemaResolver(new SchemaBindingResolver() {
079: public String getBaseURI() {
080: throw new UnsupportedOperationException(
081: "getBaseURI is not implemented.");
082: }
083:
084: public void setBaseURI(String baseURI) {
085: throw new UnsupportedOperationException(
086: "setBaseURI is not implemented.");
087: }
088:
089: public SchemaBinding resolve(String nsUri, String baseURI,
090: String schemaLocation) {
091: try {
092: if ("urn:jboss:login-config2".equals(nsUri)) {
093: InputStream is = getResource("xml/mbeanserver/login-config2.xsd");
094: SchemaBinding schemaBinding = XsdBinder.bind(
095: is, null, baseURI);
096: schemaBinding.setSchemaResolver(this );
097: return schemaBinding;
098: } else if ("urn:jboss:user-roles".equals(nsUri)) {
099: InputStream is = getResource("xml/mbeanserver/user-roles.xsd");
100: return XsdBinder.bind(is, null, baseURI);
101: } else {
102: throw new JBossXBRuntimeException(
103: "Unrecognized namespace: " + nsUri);
104: }
105: } catch (IOException e) {
106: throw new JBossXBRuntimeException("IO error", e);
107: }
108: }
109:
110: public LSInput resolveAsLSInput(String nsUri,
111: String baseUri, String schemaLocation) {
112: throw new UnsupportedOperationException(
113: "resolveResource is not implemented.");
114: }
115: });
116:
117: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
118: .newUnmarshaller();
119: InputStream is2 = getResource("xml/mbeanserver/testObjFactory.xml");
120: Services services = (Services) unmarshaller.unmarshal(is2,
121: schemaBinding);
122: List mbeans = services.getMBeans();
123: assertTrue("There is 1 mbean", mbeans.size() == 1);
124:
125: MBeanData mbean = (MBeanData) mbeans.get(0);
126: assertTrue(
127: "ClassName == org.jboss.security.auth.login.DynamicLoginConfig",
128: mbean
129: .getCode()
130: .equals(
131: "org.jboss.security.auth.login.DynamicLoginConfig"));
132: assertTrue(
133: "Name == jboss.security.tests:service=DynamicLoginConfig",
134: mbean
135: .getName()
136: .equals(
137: "jboss.security.tests:service=DynamicLoginConfig"));
138: Map attributes = mbean.getAttributeMap();
139: assertTrue("There are 2 attributes", attributes.size() == 2);
140: MBeanAttribute attr = (MBeanAttribute) attributes
141: .get("PolicyConfig");
142: Object value = attr.getValue();
143: assertTrue("Value isA PolicyConfig",
144: value instanceof PolicyConfig);
145: PolicyConfig pc = (PolicyConfig) value;
146: assertTrue("There 1 AuthenticationInfo", pc.size() == 1);
147: AuthenticationInfo auth = pc.get("conf1");
148: assertTrue("The AuthenticationInfo name ic config1",
149: auth != null);
150: AppConfigurationEntry[] ace = auth.getAppConfigurationEntry();
151: assertTrue("The AppConfigurationEntry has one entry",
152: ace != null && ace.length == 1);
153: assertTrue(
154: "LoginModuleName",
155: ace[0]
156: .getLoginModuleName()
157: .equals(
158: "org.jboss.security.auth.spi.IdentityLoginModule"));
159:
160: attr = (MBeanAttribute) attributes.get("UserHome");
161: assertTrue("Name == UserHome", attr.getName()
162: .equals("UserHome"));
163: assertTrue("Text != null", attr.getText() != null);
164: }
165:
166: /**
167: * A test of unmarshalling an element from a document without any knowledge
168: * of the associated schema.
169: *
170: * @throws Exception
171: */
172: public void testJndiBindings() throws Exception {
173: InputStream is = getResource("xml/mbeanserver/testBinding.xml");
174: // Get the Bindings attribute element
175: DocumentBuilderFactory factory = DocumentBuilderFactory
176: .newInstance();
177: DocumentBuilder builder = factory.newDocumentBuilder();
178: Document doc = builder.parse(is);
179: NodeList attributes = doc.getElementsByTagName("attribute");
180: Element element = (Element) attributes.item(0);
181: NodeList children = element.getChildNodes();
182: Element content = null;
183: for (int n = 0; n < children.getLength(); n++) {
184: Node node = children.item(n);
185: if (node.getNodeType() == Node.ELEMENT_NODE) {
186: content = (Element) node;
187: break;
188: }
189: }
190:
191: // Get a parsable representation of this elements content
192: DOMSource source = new DOMSource(content);
193: TransformerFactory tFactory = TransformerFactory.newInstance();
194: Transformer transformer = tFactory.newTransformer();
195: ByteArrayOutputStream baos = new ByteArrayOutputStream();
196: StreamResult result = new StreamResult(baos);
197: transformer.transform(source, result);
198: baos.close();
199:
200: ByteArrayInputStream is2 = new ByteArrayInputStream(baos
201: .toByteArray());
202:
203: /* Parse the element content using the Unmarshaller starting with an
204: empty schema since we don't know anything about it. This is not quite
205: true as we set the schema baseURI to the resources/xml/naming/ directory
206: so that the jndi-binding-service_1_0.xsd can be found, but this baseURI
207: can be easily specified to the SARDeployer, or the schema can be made
208: available to the entity resolver via some other configuration.
209: */
210: final URL url = Thread.currentThread().getContextClassLoader()
211: .getResource("xml/naming/");
212:
213: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
214: .newUnmarshaller();
215: unmarshaller.setEntityResolver(new JBossEntityResolver() {
216: public InputSource resolveEntity(String publicId,
217: String systemId) throws SAXException, IOException {
218: if (systemId.endsWith("custom-object-binding.xsd")
219: || systemId
220: .endsWith("jndi-binding-service_1_0.xsd")) {
221: String fileName = systemId.substring(systemId
222: .lastIndexOf('/') + 1);
223: URL url = Thread.currentThread()
224: .getContextClassLoader().getResource(
225: "xml/naming/" + fileName);
226: return new InputSource(url.toExternalForm());
227: } else {
228: return super .resolveEntity(publicId, systemId);
229: }
230: }
231: });
232:
233: JNDIBindings bindings = (JNDIBindings) unmarshaller.unmarshal(
234: is2, new SchemaBindingResolver() {
235: public String getBaseURI() {
236: throw new UnsupportedOperationException(
237: "getBaseURI is not implemented.");
238: }
239:
240: public void setBaseURI(String baseURI) {
241: throw new UnsupportedOperationException(
242: "setBaseURI is not implemented.");
243: }
244:
245: public SchemaBinding resolve(String nsUri,
246: String baseURI, String schemaLocation) {
247: return XsdBinder.bind(url.toExternalForm()
248: + schemaLocation, this );
249: }
250:
251: public LSInput resolveAsLSInput(String nsUri,
252: String baseUri, String schemaLocation) {
253: throw new UnsupportedOperationException(
254: "resolveAsLSInput is not implemented.");
255: }
256: });
257:
258: is2.close();
259:
260: // Validate the bindings
261: JNDIBinding[] values = bindings.getBindings();
262: assertTrue("There are 5 bindings(" + values.length + ")",
263: values.length == 5);
264:
265: JNDIBinding key1 = values[0];
266: assertTrue("values[0] name is ctx1/key1", key1.getName()
267: .equals("ctx1/key1"));
268: assertTrue("values[0] is string of value1", key1.getText()
269: .equals("value1"));
270:
271: JNDIBinding userHome = values[1];
272: assertTrue("values[1] name is ctx1/user.home", userHome
273: .getName().equals("ctx1/user.home"));
274: String p = System.getProperty("user.home");
275: assertTrue("values[1] is property ${user.home}", userHome
276: .getText().equals(p));
277:
278: // Test binding from a text to URL based on the type attribute PropertyEditor
279: JNDIBinding jbossHome = values[2];
280: assertTrue("values[2] name is ctx1/key2", jbossHome.getName()
281: .equals("ctx1/key2"));
282: assertTrue("values[2] is http://www.jboss.org", jbossHome
283: .getText().equals("http://www.jboss.org"));
284: assertTrue("values[2] type is java.net.URL", jbossHome
285: .getType().equals("java.net.URL"));
286: Object value2 = jbossHome.getValue();
287: assertTrue("values[2] value is URL(http://www.jboss.org)",
288: value2.equals(new URL("http://www.jboss.org")));
289:
290: // Test a binding from an xml fragment from a foreign namespace.
291: JNDIBinding properties = values[3];
292: Object value = properties.getValue();
293: assertTrue("values[3] name is ctx2/key1", properties.getName()
294: .equals("ctx2/key1"));
295: assertTrue("values[3] is java.util.Properties",
296: value instanceof Properties);
297: Properties props = (Properties) value;
298: assertTrue("Properties(key1) == value1", props.getProperty(
299: "key1").equals("value1"));
300: assertTrue("Properties(key2) == value2", props.getProperty(
301: "key2").equals("value2"));
302:
303: // Test binding from a text to InetAddress based on the editor attribute PropertyEditor
304: JNDIBinding host = values[4];
305: assertTrue("values[4] name is hosts/localhost", host.getName()
306: .equals("hosts/localhost"));
307: assertTrue(host.isTrim());
308: assertTrue("values[4] text is 127.0.0.1", host.getText()
309: .equals("127.0.0.1"));
310: assertTrue(
311: "values[4] editor is org.jboss.util.propertyeditor.InetAddressEditor",
312: host
313: .getEditor()
314: .equals(
315: "org.jboss.util.propertyeditor.InetAddressEditor"));
316: Object value4 = host.getValue();
317: InetAddress hostValue = (InetAddress) value4;
318: InetAddress localhost = InetAddress.getByName("127.0.0.1");
319: assertTrue("values[4] value is InetAddress(127.0.0.1)",
320: hostValue.getHostAddress().equals(
321: localhost.getHostAddress()));
322:
323: }
324:
325: // Private
326:
327: private InputStream getResource(String path) throws IOException {
328: URL url = Thread.currentThread().getContextClassLoader()
329: .getResource(path);
330: if (url == null) {
331: fail("URL not found: " + path);
332: }
333: return url.openStream();
334: }
335: }
|