1 22 package org.jboss.test.xml; 23 24 import java.util.Properties ; 25 import java.net.URL ; 26 import java.net.InetAddress ; 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 44 public class JNDIBindingUnitTestCase 45 extends TestCase 46 { 47 public void testMain() throws Exception 48 { 49 URL 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 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 p = System.getProperty("user.home"); 67 assertTrue("values[1] is property ${user.home}", userHome.getText().equals(p)); 68 69 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 value2 = jbossHome.getValue(); 77 assertTrue("values[2] value is URL(http://www.jboss.org)", 78 value2.equals(new URL ("http://www.jboss.org"))); 79 80 JNDIBinding properties = values[3]; 82 Object 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 ); 85 Properties props = (Properties ) value; 86 assertTrue("Properties(key1) == value1", props.getProperty("key1").equals("value1")); 87 assertTrue("Properties(key2) == value2", props.getProperty("key2").equals("value2")); 88 89 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 value4 = host.getValue(); 98 InetAddress hostValue = (InetAddress ) value4; 99 InetAddress 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 106 private static URL getResource(String path) 107 { 108 java.net.URL 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 |