KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jboss.test.xml;
23
24 import java.util.Properties JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.InetAddress JavaDoc;
27
28 import org.jboss.xb.binding.Unmarshaller;
29 import org.jboss.xb.binding.UnmarshallerFactory;
30 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
31 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
32 import org.jboss.naming.JNDIBindings;
33 import org.jboss.naming.JNDIBinding;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Test unmarshalling xml documents conforming to xml/naming/jndi_binding_service_1_0.xsd
39  * the org.jboss.naming.JNDIBindings and related objects.
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 57517 $
43  */

44 public class JNDIBindingUnitTestCase
45    extends TestCase
46 {
47    public void testMain() throws Exception JavaDoc
48    {
49       URL JavaDoc url = getResource("xml/naming/jndi-binding-service_1_0.xsd");
50       SchemaBinding schemaBinding = XsdBinder.bind(url.toString());
51       schemaBinding.setIgnoreUnresolvedFieldOrClass(false);
52
53       Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
54
55       URL JavaDoc xml = getResource("xml/naming/testBindings.xml");
56       JNDIBindings bindings = (JNDIBindings)unmarshaller.unmarshal(xml.openStream(), schemaBinding);
57       JNDIBinding[] values = bindings.getBindings();
58       assertTrue("There are 5 bindings("+values.length+")", values.length == 5);
59
60       JNDIBinding key1 = values[0];
61       assertTrue("values[0] name is ctx1/key1", key1.getName().equals("ctx1/key1"));
62       assertTrue("values[0] is string of value1", key1.getText().equals("value1"));
63
64       JNDIBinding userHome = values[1];
65       assertTrue("values[1] name is ctx1/user.home", userHome.getName().equals("ctx1/user.home"));
66       String JavaDoc p = System.getProperty("user.home");
67       assertTrue("values[1] is property ${user.home}", userHome.getText().equals(p));
68
69       // Test binding from a text to URL based on the type attribute PropertyEditor
70
JNDIBinding jbossHome = values[2];
71       assertTrue("values[2] name is ctx1/key2", jbossHome.getName().equals("ctx1/key2"));
72       assertTrue("values[2] is http://www.jboss.org",
73          jbossHome.getText().equals("http://www.jboss.org"));
74       assertTrue("values[2] type is java.net.URL",
75          jbossHome.getType().equals("java.net.URL"));
76       Object JavaDoc value2 = jbossHome.getValue();
77       assertTrue("values[2] value is URL(http://www.jboss.org)",
78          value2.equals(new URL JavaDoc("http://www.jboss.org")));
79
80       // Test a binding from an xml fragment from a foreign namespace.
81
JNDIBinding properties = values[3];
82       Object JavaDoc value = properties.getValue();
83       assertTrue("values[3] name is ctx2/key1", properties.getName().equals("ctx2/key1"));
84       assertTrue("values[3] is java.util.Properties", value instanceof Properties JavaDoc);
85       Properties JavaDoc props = (Properties JavaDoc) value;
86       assertTrue("Properties(key1) == value1", props.getProperty("key1").equals("value1"));
87       assertTrue("Properties(key2) == value2", props.getProperty("key2").equals("value2"));
88
89       // Test binding from a text to InetAddress based on the editor attribute PropertyEditor
90
JNDIBinding host = values[4];
91       assertTrue("values[4] name is hosts/localhost", host.getName().equals("hosts/localhost"));
92       assertTrue(host.isTrim());
93       assertTrue("values[4] text is 127.0.0.1",
94          host.getText().equals("127.0.0.1"));
95       assertTrue("values[4] editor is org.jboss.util.propertyeditor.InetAddressEditor",
96          host.getEditor().equals("org.jboss.util.propertyeditor.InetAddressEditor"));
97       Object JavaDoc value4 = host.getValue();
98       InetAddress JavaDoc hostValue = (InetAddress JavaDoc) value4;
99       InetAddress JavaDoc localhost = InetAddress.getByName("127.0.0.1");
100       assertTrue("values[4] value is InetAddress(127.0.0.1)",
101          hostValue.getHostAddress().equals(localhost.getHostAddress()));
102    }
103
104    // Private
105

106    private static URL JavaDoc getResource(String JavaDoc path)
107    {
108       java.net.URL JavaDoc url = Thread.currentThread().getContextClassLoader().getResource(path);
109       if(url == null)
110       {
111          fail("URL not found: " + path);
112       }
113       return url;
114    }
115 }
116
Popular Tags