1 22 package org.jboss.test.xml; 23 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.net.URL ; 28 import java.net.InetAddress ; 29 import java.io.InputStream ; 30 import java.io.ByteArrayOutputStream ; 31 import java.io.ByteArrayInputStream ; 32 import java.io.IOException ; 33 34 import javax.security.auth.login.AppConfigurationEntry ; 35 import javax.xml.parsers.DocumentBuilderFactory ; 36 import javax.xml.parsers.DocumentBuilder ; 37 import javax.xml.transform.dom.DOMSource ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.stream.StreamResult ; 41 42 import org.jboss.test.xml.mbeanserver.Services; 43 import org.jboss.test.xml.mbeanserver.MBeanData; 44 import org.jboss.test.xml.mbeanserver.MBeanAttribute; 45 import org.jboss.test.xml.mbeanserver.PolicyConfig; 46 import org.jboss.test.xml.mbeanserver.AuthenticationInfo; 47 import org.jboss.xb.binding.JBossXBRuntimeException; 48 import org.jboss.xb.binding.Unmarshaller; 49 import org.jboss.xb.binding.UnmarshallerFactory; 50 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding; 51 import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver; 52 import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder; 53 import org.jboss.naming.JNDIBindings; 54 import org.jboss.naming.JNDIBinding; 55 import org.jboss.util.xml.JBossEntityResolver; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.Document ; 58 import org.w3c.dom.NodeList ; 59 import org.w3c.dom.Node ; 60 import org.w3c.dom.ls.LSInput ; 61 import org.xml.sax.InputSource ; 62 import org.xml.sax.SAXException ; 63 64 import junit.framework.TestCase; 65 66 73 public class MBeanServerUnitTestCase 74 extends TestCase 75 { 76 public void testMbeanService() throws Exception 77 { 78 InputStream is = getResource("xml/mbeanserver/mbean-service_1_0.xsd"); 79 SchemaBinding schemaBinding = XsdBinder.bind(is, null); 80 schemaBinding.setIgnoreUnresolvedFieldOrClass(true); 81 schemaBinding.setSchemaResolver(new SchemaBindingResolver() 82 { 83 public String getBaseURI() 84 { 85 throw new UnsupportedOperationException ("getBaseURI is not implemented."); 86 } 87 88 public void setBaseURI(String baseURI) 89 { 90 throw new UnsupportedOperationException ("setBaseURI is not implemented."); 91 } 92 93 public SchemaBinding resolve(String nsUri, String baseURI, String schemaLocation) 94 { 95 try 96 { 97 if("urn:jboss:login-config2".equals(nsUri)) 98 { 99 InputStream is = getResource("xml/mbeanserver/login-config2.xsd"); 100 SchemaBinding schemaBinding = XsdBinder.bind(is, null, baseURI); 101 schemaBinding.setSchemaResolver(this); 102 return schemaBinding; 103 } 104 else if("urn:jboss:user-roles".equals(nsUri)) 105 { 106 InputStream is = getResource("xml/mbeanserver/user-roles.xsd"); 107 return XsdBinder.bind(is, null, baseURI); 108 } 109 else 110 { 111 throw new JBossXBRuntimeException("Unrecognized namespace: " + nsUri); 112 } 113 } 114 catch(IOException e) 115 { 116 throw new JBossXBRuntimeException("IO error", e); 117 } 118 } 119 120 public LSInput resolveAsLSInput(String nsUri, String baseUri, String schemaLocation) 121 { 122 throw new UnsupportedOperationException ("resolveResource is not implemented."); 123 } 124 }); 125 126 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 127 InputStream is2 = getResource("xml/mbeanserver/testObjFactory.xml"); 128 Services services = (Services)unmarshaller.unmarshal(is2, schemaBinding); 129 List mbeans = services.getMBeans(); 130 assertEquals("There is 1 mbean", 1, mbeans.size()); 131 132 MBeanData mbean = (MBeanData) mbeans.get(0); 133 assertTrue("ClassName == org.jboss.security.auth.login.DynamicLoginConfig", 134 mbean.getCode().equals("org.jboss.security.auth.login.DynamicLoginConfig")); 135 assertTrue("Name == jboss.security.tests:service=DynamicLoginConfig", 136 mbean.getName().equals("jboss.security.tests:service=DynamicLoginConfig")); 137 Map attributes = mbean.getAttributeMap(); 138 assertTrue("There are 2 attributes", 139 attributes.size() == 2); 140 MBeanAttribute attr = (MBeanAttribute) attributes.get("PolicyConfig"); 141 Object value = attr.getValue(); 142 assertTrue("Value isA PolicyConfig", 143 value instanceof PolicyConfig ); 144 PolicyConfig pc = (PolicyConfig) value; 145 assertTrue("There 1 AuthenticationInfo", 146 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("LoginModuleName", 154 ace[0].getLoginModuleName().equals("org.jboss.security.auth.spi.IdentityLoginModule")); 155 156 attr = (MBeanAttribute) attributes.get("UserHome"); 157 assertTrue("Name == UserHome", 158 attr.getName().equals("UserHome")); 159 assertTrue("Text != null", 160 attr.getText() != null); 161 } 162 163 169 public void testJndiBindings() throws Exception 170 { 171 InputStream is = getResource("xml/mbeanserver/testBinding.xml"); 172 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 174 DocumentBuilder builder = factory.newDocumentBuilder(); 175 Document doc = builder.parse(is); 176 NodeList attributes = doc.getElementsByTagName("attribute"); 177 Element element = (Element ) attributes.item(0); 178 NodeList children = element.getChildNodes(); 179 Element content = null; 180 for(int n = 0; n < children.getLength(); n ++) 181 { 182 Node node = children.item(n); 183 if( node.getNodeType() == Node.ELEMENT_NODE ) 184 { 185 content = (Element ) node; 186 break; 187 } 188 } 189 190 DOMSource source = new DOMSource (content); 192 TransformerFactory tFactory = TransformerFactory.newInstance(); 193 Transformer transformer = tFactory.newTransformer(); 194 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 195 StreamResult result = new StreamResult (baos); 196 transformer.transform(source, result); 197 baos.close(); 198 199 ByteArrayInputStream is2 = new ByteArrayInputStream (baos.toByteArray()); 200 201 208 final URL url = Thread.currentThread().getContextClassLoader().getResource("xml/naming/"); 209 210 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller(); 211 unmarshaller.setEntityResolver(new JBossEntityResolver(){ 212 public InputSource resolveEntity(String publicId, String systemId) throws SAXException , IOException 213 { 214 if(systemId.endsWith("custom-object-binding.xsd") || 215 systemId.endsWith("jndi-binding-service_1_0.xsd")) 216 { 217 String fileName = systemId.substring(systemId.lastIndexOf('/') + 1); 218 URL url = Thread.currentThread().getContextClassLoader(). 219 getResource("xml/naming/" + fileName); 220 return new InputSource (url.toExternalForm()); 221 } 222 else 223 { 224 return super.resolveEntity(publicId, systemId); 225 } 226 } 227 }); 228 229 JNDIBindings bindings = (JNDIBindings) unmarshaller.unmarshal(is2, 230 new SchemaBindingResolver(){ 231 public String getBaseURI() 232 { 233 throw new UnsupportedOperationException ("getBaseURI is not implemented."); 234 } 235 236 public void setBaseURI(String baseURI) 237 { 238 throw new UnsupportedOperationException ("setBaseURI is not implemented."); 239 } 240 241 public SchemaBinding resolve(String nsUri, String baseURI, String schemaLocation) 242 { 243 return XsdBinder.bind(url.toExternalForm() + schemaLocation, this); 244 } 245 246 public LSInput resolveAsLSInput(String nsUri, String baseUri, String schemaLocation) 247 { 248 throw new UnsupportedOperationException ("resolveAsLSInput is not implemented."); 249 } 250 }); 251 252 is2.close(); 253 254 JNDIBinding[] values = bindings.getBindings(); 256 assertTrue("There are 5 bindings("+values.length+")", values.length == 5); 257 258 JNDIBinding key1 = values[0]; 259 assertTrue("values[0] name is ctx1/key1", key1.getName().equals("ctx1/key1")); 260 assertTrue("values[0] is string of value1", key1.getText().equals("value1")); 261 262 JNDIBinding userHome = values[1]; 263 assertTrue("values[1] name is ctx1/user.home", userHome.getName().equals("ctx1/user.home")); 264 String p = System.getProperty("user.home"); 265 assertTrue("values[1] is property ${user.home}", userHome.getText().equals(p)); 266 267 JNDIBinding jbossHome = values[2]; 269 assertTrue("values[2] name is ctx1/key2", jbossHome.getName().equals("ctx1/key2")); 270 assertTrue("values[2] is http://www.jboss.org", 271 jbossHome.getText().equals("http://www.jboss.org")); 272 assertTrue("values[2] type is java.net.URL", 273 jbossHome.getType().equals("java.net.URL")); 274 Object value2 = jbossHome.getValue(); 275 assertTrue("values[2] value is URL(http://www.jboss.org)", 276 value2.equals(new URL ("http://www.jboss.org"))); 277 278 JNDIBinding properties = values[3]; 280 Object value = properties.getValue(); 281 assertTrue("values[3] name is ctx2/key1", properties.getName().equals("ctx2/key1")); 282 assertTrue("values[3] is java.util.Properties", value instanceof Properties ); 283 Properties props = (Properties ) value; 284 assertTrue("Properties(key1) == value1", props.getProperty("key1").equals("value1")); 285 assertTrue("Properties(key2) == value2", props.getProperty("key2").equals("value2")); 286 287 JNDIBinding host = values[4]; 289 assertTrue("values[4] name is hosts/localhost", host.getName().equals("hosts/localhost")); 290 assertTrue(host.isTrim()); 291 assertTrue("values[4] text is 127.0.0.1", 292 host.getText().equals("127.0.0.1")); 293 assertTrue("values[4] editor is org.jboss.util.propertyeditor.InetAddressEditor", 294 host.getEditor().equals("org.jboss.util.propertyeditor.InetAddressEditor")); 295 Object value4 = host.getValue(); 296 InetAddress hostValue = (InetAddress ) value4; 297 InetAddress localhost = InetAddress.getByName("127.0.0.1"); 298 assertTrue("values[4] value is InetAddress(127.0.0.1)", 299 hostValue.getHostAddress().equals(localhost.getHostAddress())); 300 301 } 302 303 305 private InputStream getResource(String path) 306 throws IOException 307 { 308 URL url = Thread.currentThread().getContextClassLoader().getResource(path); 309 if(url == null) 310 { 311 fail("URL not found: " + path); 312 } 313 return url.openStream(); 314 } 315 } 316 | Popular Tags |