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.Properties;
025: import java.net.URL;
026: import java.net.InetAddress;
027: import java.io.IOException;
028:
029: import org.jboss.xb.binding.Unmarshaller;
030: import org.jboss.xb.binding.UnmarshallerFactory;
031: import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
032: import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
033: import org.jboss.naming.JNDIBindings;
034: import org.jboss.naming.JNDIBinding;
035: import org.jboss.util.xml.JBossEntityResolver;
036: import org.xml.sax.InputSource;
037: import org.xml.sax.SAXException;
038:
039: import junit.framework.TestCase;
040:
041: /**
042: * Test unmarshalling xml documents conforming to xml/naming/jndi_binding_service_1_0.xsd
043: * the org.jboss.naming.JNDIBindings and related objects.
044: *
045: * @author Scott.Stark@jboss.org
046: * @version $Revision: 57211 $
047: */
048: public class JNDIBindingUnitTestCase extends TestCase {
049: public void testMain() throws Exception {
050: URL url = getResource("xml/naming/jndi-binding-service_1_0.xsd");
051: SchemaBinding schemaBinding = XsdBinder.bind(url.toString());
052: schemaBinding.setIgnoreUnresolvedFieldOrClass(false);
053:
054: Unmarshaller unmarshaller = UnmarshallerFactory.newInstance()
055: .newUnmarshaller();
056: unmarshaller.setEntityResolver(new JBossEntityResolver() {
057: public InputSource resolveEntity(String publicId,
058: String systemId) throws SAXException, IOException {
059: if (systemId.endsWith("jndi-binding-service_1_0.xsd")) {
060: URL url = Thread
061: .currentThread()
062: .getContextClassLoader()
063: .getResource(
064: "xml/naming/jndi-binding-service_1_0.xsd");
065: return new InputSource(url.toExternalForm());
066: } else if (systemId
067: .endsWith("custom-object-binding.xsd")) {
068: URL url = Thread
069: .currentThread()
070: .getContextClassLoader()
071: .getResource(
072: "xml/naming/custom-object-binding.xsd");
073: return new InputSource(url.toExternalForm());
074: } else {
075: return super .resolveEntity(publicId, systemId);
076: }
077: }
078: });
079:
080: URL xml = getResource("xml/naming/testBindings.xml");
081: JNDIBindings bindings = (JNDIBindings) unmarshaller.unmarshal(
082: xml.openStream(), schemaBinding);
083: JNDIBinding[] values = bindings.getBindings();
084: assertTrue("There are 5 bindings(" + values.length + ")",
085: values.length == 5);
086:
087: JNDIBinding key1 = values[0];
088: assertTrue("values[0] name is ctx1/key1", key1.getName()
089: .equals("ctx1/key1"));
090: assertTrue("values[0] is string of value1", key1.getText()
091: .equals("value1"));
092:
093: JNDIBinding userHome = values[1];
094: assertTrue("values[1] name is ctx1/user.home", userHome
095: .getName().equals("ctx1/user.home"));
096: String p = System.getProperty("user.home");
097: assertTrue("values[1] is property ${user.home}", userHome
098: .getText().equals(p));
099:
100: // Test binding from a text to URL based on the type attribute PropertyEditor
101: JNDIBinding jbossHome = values[2];
102: assertTrue("values[2] name is ctx1/key2", jbossHome.getName()
103: .equals("ctx1/key2"));
104: assertTrue("values[2] is http://www.jboss.org", jbossHome
105: .getText().equals("http://www.jboss.org"));
106: assertTrue("values[2] type is java.net.URL", jbossHome
107: .getType().equals("java.net.URL"));
108: Object value2 = jbossHome.getValue();
109: assertTrue("values[2] value is URL(http://www.jboss.org)",
110: value2.equals(new URL("http://www.jboss.org")));
111:
112: // Test a binding from an xml fragment from a foreign namespace.
113: JNDIBinding properties = values[3];
114: Object value = properties.getValue();
115: assertTrue("values[3] name is ctx2/key1", properties.getName()
116: .equals("ctx2/key1"));
117: assertTrue("values[3] is java.util.Properties",
118: value instanceof Properties);
119: Properties props = (Properties) value;
120: assertTrue("Properties(key1) == value1", props.getProperty(
121: "key1").equals("value1"));
122: assertTrue("Properties(key2) == value2", props.getProperty(
123: "key2").equals("value2"));
124:
125: // Test binding from a text to InetAddress based on the editor attribute PropertyEditor
126: JNDIBinding host = values[4];
127: assertTrue("values[4] name is hosts/localhost", host.getName()
128: .equals("hosts/localhost"));
129: assertTrue(host.isTrim());
130: assertTrue("values[4] text is 127.0.0.1", host.getText()
131: .equals("127.0.0.1"));
132: assertTrue(
133: "values[4] editor is org.jboss.util.propertyeditor.InetAddressEditor",
134: host
135: .getEditor()
136: .equals(
137: "org.jboss.util.propertyeditor.InetAddressEditor"));
138: Object value4 = host.getValue();
139: InetAddress hostValue = (InetAddress) value4;
140: InetAddress localhost = InetAddress.getByName("127.0.0.1");
141: assertTrue("values[4] value is InetAddress(127.0.0.1)",
142: hostValue.getHostAddress().equals(
143: localhost.getHostAddress()));
144: }
145:
146: // Private
147:
148: private static URL getResource(String path) {
149: java.net.URL url = Thread.currentThread()
150: .getContextClassLoader().getResource(path);
151: if (url == null) {
152: fail("URL not found: " + path);
153: }
154: return url;
155: }
156: }
|