1 22 package org.jboss.test.jaxr.scout; 23 24 import junit.framework.TestCase; 25 import org.jboss.mx.util.ObjectNameFactory; 26 import org.jboss.test.JBossRMIAdaptorHelper; 27 28 import javax.management.ObjectName ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.NamingException ; 32 import javax.xml.registry.BulkResponse ; 33 import javax.xml.registry.BusinessLifeCycleManager ; 34 import javax.xml.registry.BusinessQueryManager ; 35 import javax.xml.registry.Connection ; 36 import javax.xml.registry.ConnectionFactory ; 37 import javax.xml.registry.FindQualifier ; 38 import javax.xml.registry.JAXRException ; 39 import javax.xml.registry.RegistryService ; 40 import javax.xml.registry.infomodel.*; 41 import java.net.PasswordAuthentication ; 42 import java.util.ArrayList ; 43 import java.util.Collection ; 44 import java.util.HashSet ; 45 import java.util.Iterator ; 46 import java.util.Locale ; 47 import java.util.Properties ; 48 import java.util.Set ; 49 50 56 public class JaxrBaseTestCase extends TestCase 57 { 58 59 protected String userid = "jboss"; 60 protected String passwd = "jboss"; 61 protected BusinessLifeCycleManager blm = null; 62 protected RegistryService rs = null; 63 protected BusinessQueryManager bqm = null; 64 protected Connection connection = null; 65 protected BulkResponse br = null; 66 protected JBossRMIAdaptorHelper server = null; 67 68 protected ConnectionFactory factory = null; 69 70 protected static final ObjectName OBJECT_NAME = ObjectNameFactory.create("jboss:service=juddi"); 71 72 protected static String debugProp = System.getProperty("jaxr.debug", "true"); 74 75 81 protected void setUp() throws Exception 82 { 83 server = new JBossRMIAdaptorHelper(this.getClientContext()); 85 server.invokeOperation(OBJECT_NAME, "setCreateOnStart", 86 new Object []{Boolean.TRUE}, 87 new String []{Boolean.TYPE.getName()}); 88 server.invokeOperation(OBJECT_NAME, "stop", 89 null, null); 90 server.invokeOperation(OBJECT_NAME, "start", 91 null, null); 92 93 String factoryString = "javax.xml.registry.ConnectionFactoryClass"; 95 String factoryClass = System.getProperty(factoryString); 96 if(factoryClass == null || factoryClass.length() == 0) 97 System.setProperty(factoryString,"org.apache.ws.scout.registry.ConnectionFactoryImpl"); 98 99 String queryurl = System.getProperty("jaxr.query.url", 100 "http://localhost:8080/juddi/inquiry"); 101 String puburl = System.getProperty("jaxr.publish.url", 102 "http://localhost:8080/juddi/publish"); 103 104 Properties props = new Properties (); 105 props.setProperty("javax.xml.registry.queryManagerURL", 106 queryurl); 107 108 props.setProperty("javax.xml.registry.lifeCycleManagerURL", 109 puburl); 110 111 String transportClass = System.getProperty("juddi.proxy.transportClass", 112 "org.jboss.jaxr.juddi.transport.SaajTransport"); 113 System.setProperty("juddi.proxy.transportClass", transportClass); 114 try 115 { 116 factory = ConnectionFactory.newInstance(); 118 factory.setProperties(props); 119 connection = factory.createConnection(); 120 } catch (JAXRException e) 121 { 122 fail("Setup failed"+e); 123 } 124 } 125 126 132 protected void tearDown() throws Exception 133 { 134 if (connection != null) connection.close(); 135 server.invokeOperation(OBJECT_NAME, "setCreateOnStart", 137 new Object []{Boolean.FALSE}, 138 new String []{Boolean.TYPE.getName()}); 139 server.invokeOperation(OBJECT_NAME, "stop", 140 null, null); 141 } 142 143 public void testJaxrEssentials() 144 { 145 assertNotNull(connection); 146 } 147 148 151 protected void login() 152 { 153 PasswordAuthentication passwdAuth = new PasswordAuthentication (userid, 154 passwd.toCharArray()); 155 Set creds = new HashSet (); 156 creds.add(passwdAuth); 157 158 try 159 { 160 connection.setCredentials(creds); 161 } catch (JAXRException e) 162 { 163 e.printStackTrace(); 164 fail(e.getMessage()); 165 } 166 } 167 168 protected void getJAXREssentials() throws JAXRException 169 { 170 171 rs = connection.getRegistryService(); 172 blm = rs.getBusinessLifeCycleManager(); 173 bqm = rs.getBusinessQueryManager(); 174 } 175 176 public InternationalString getIString(String str) 177 throws JAXRException 178 { 179 return blm.createInternationalString(str); 180 } 181 182 183 189 public void searchBusiness(String bizname) throws JAXRException 190 { 191 try 192 { 193 this.getJAXREssentials(); 195 196 Collection findQualifiers = new ArrayList (); 198 findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC); 199 Collection namePatterns = new ArrayList (); 200 String pattern = "%" + bizname + "%"; 201 LocalizedString ls = blm.createLocalizedString(Locale.getDefault(), 202 pattern); 203 namePatterns.add(ls); 204 205 BulkResponse response = 207 bqm.findOrganizations(findQualifiers, 208 namePatterns, 209 null, 210 null, 211 null, 212 null); 213 214 Collection orgs = response.getCollection(); 216 if (orgs == null) 217 { 218 if ("true".equalsIgnoreCase(debugProp)) 219 System.out.println(" -- Matched 0 orgs"); 220 221 } else 222 { 223 if ("true".equalsIgnoreCase(debugProp)) 224 System.out.println(" -- Matched " + orgs.size() + " organizations -- "); 225 226 for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();) 228 { 229 Organization org = (Organization) orgIter.next(); 230 if ("true".equalsIgnoreCase(debugProp)) 231 { 232 System.out.println("Org name: " + getName(org)); 233 System.out.println("Org description: " + getDescription(org)); 234 System.out.println("Org key id: " + getKey(org)); 235 } 236 checkUser(org); 237 checkServices(org); 238 } 239 } } catch (JAXRException e) 241 { 242 e.printStackTrace(); 243 fail(e.getMessage()); 244 } finally 245 { 246 connection.close(); 247 } 248 249 } 250 251 protected RegistryService getRegistryService() throws JAXRException 252 { 253 assertNotNull(connection); 254 return connection.getRegistryService(); 255 } 256 257 protected BusinessQueryManager getBusinessQueryManager() throws JAXRException 258 { 259 assertNotNull(connection); 260 if (rs == null) rs = this.getRegistryService(); 261 return rs.getBusinessQueryManager(); 262 } 263 264 protected BusinessLifeCycleManager getBusinessLifeCycleManager() throws JAXRException 265 { 266 assertNotNull(connection); 267 if (rs == null) rs = this.getRegistryService(); 268 return rs.getBusinessLifeCycleManager(); 269 } 270 271 private static void checkServices(Organization org) 272 throws JAXRException 273 { 274 Collection services = org.getServices(); 276 for (Iterator svcIter = services.iterator(); svcIter.hasNext();) 277 { 278 Service svc = (Service ) svcIter.next(); 279 if ("true".equalsIgnoreCase(debugProp)) 280 { 281 System.out.println(" Service name: " + getName(svc)); 282 System.out.println(" Service description: " + getDescription(svc)); 283 } 284 assertEquals("JBOSS JAXR Service",getName(svc)); 285 assertEquals("Services of XML Registry",getDescription(svc)); 286 287 Collection serviceBindings = svc.getServiceBindings(); 288 for (Iterator sbIter = serviceBindings.iterator(); sbIter.hasNext();) 289 { 290 ServiceBinding sb = (ServiceBinding) sbIter.next(); 291 if ("true".equalsIgnoreCase(debugProp)) 292 { 293 System.out.println(" Binding Description: " + getDescription(sb)); 294 System.out.println(" Access URI: " + sb.getAccessURI()); 295 } 296 assertEquals("http://testjboss.org", sb.getAccessURI()); 297 assertEquals("Test Service Binding", getDescription(sb)); 298 } 299 } 300 } 301 302 private static void checkUser(Organization org) 303 throws JAXRException 304 { 305 User pc = org.getPrimaryContact(); 307 if (pc != null) 308 { 309 PersonName pcName = pc.getPersonName(); 310 System.out.println(" Contact name: " + pcName.getFullName()); 311 assertEquals("Anil S",pcName.getFullName()); 312 Collection phNums = pc.getTelephoneNumbers(pc.getType()); 313 for (Iterator phIter = phNums.iterator(); phIter.hasNext();) 314 { 315 TelephoneNumber num = (TelephoneNumber) phIter.next(); 316 System.out.println(" Phone number: " + num.getNumber()); 317 } 318 Collection eAddrs = pc.getEmailAddresses(); 319 for (Iterator eaIter = eAddrs.iterator(); eaIter.hasNext();) 320 { 321 System.out.println(" Email Address: " + (EmailAddress) eaIter.next()); 322 } 323 } 324 } 325 326 private static String getName(RegistryObject ro) throws JAXRException 327 { 328 if (ro != null && ro.getName() != null) 329 { 330 return ro.getName().getValue(); 331 } 332 return ""; 333 } 334 335 private static String getDescription(RegistryObject ro) throws JAXRException 336 { 337 if (ro != null && ro.getDescription() != null) 338 { 339 return ro.getDescription().getValue(); 340 } 341 return ""; 342 } 343 344 private static String getKey(RegistryObject ro) throws JAXRException 345 { 346 if (ro != null && ro.getKey() != null) 347 { 348 return ro.getKey().getId(); 349 } 350 return ""; 351 } 352 353 359 protected Organization createOrganization(String orgname) 360 throws JAXRException 361 { 362 Organization org = blm.createOrganization(getIString(orgname)); 363 org.setDescription(getIString("JBoss Inc")); 364 Service service = blm.createService(getIString("JBOSS JAXR Service")); 365 service.setDescription(getIString("Services of XML Registry")); 366 ServiceBinding serviceBinding = blm.createServiceBinding(); 368 serviceBinding.setDescription(blm. 369 createInternationalString("Test Service Binding")); 370 371 serviceBinding.setValidateURI(false); 373 serviceBinding.setAccessURI("http://testjboss.org"); 374 375 service.addServiceBinding(serviceBinding); 377 378 User user = blm.createUser(); 379 org.setPrimaryContact(user); 380 PersonName personName = blm.createPersonName("Anil S"); 381 TelephoneNumber telephoneNumber = blm.createTelephoneNumber(); 382 telephoneNumber.setNumber("111-111-7777"); 383 telephoneNumber.setType(null); 384 PostalAddress address 385 = blm.createPostalAddress("111", 386 "My Drive", "BuckHead", 387 "GA", "USA", "1111-111", ""); 388 Collection postalAddresses = new ArrayList (); 389 postalAddresses.add(address); 390 Collection emailAddresses = new ArrayList (); 391 EmailAddress emailAddress = blm.createEmailAddress("anil@apache.org"); 392 emailAddresses.add(emailAddress); 393 394 Collection numbers = new ArrayList (); 395 numbers.add(telephoneNumber); 396 user.setPersonName(personName); 397 user.setPostalAddresses(postalAddresses); 398 user.setEmailAddresses(emailAddresses); 399 user.setTelephoneNumbers(numbers); 400 401 ClassificationScheme cScheme = getClassificationScheme("ntis-gov:naics", ""); 402 Key cKey = blm.createKey("uuid:C0B9FE13-324F-413D-5A5B-2004DB8E5CC2"); 403 cScheme.setKey(cKey); 404 Classification classification = blm.createClassification(cScheme, 405 "Computer Systems Design and Related Services", 406 "5415"); 407 org.addClassification(classification); 408 ClassificationScheme cScheme1 = getClassificationScheme("D-U-N-S", ""); 409 Key cKey1 = blm.createKey("uuid:3367C81E-FF1F-4D5A-B202-3EB13AD02423"); 410 cScheme1.setKey(cKey1); 411 ExternalIdentifier ei = 412 blm.createExternalIdentifier(cScheme1, "D-U-N-S number", 413 "08-146-6849"); 414 org.addExternalIdentifier(ei); 415 org.addService(service); 416 return org; 417 } 418 419 420 426 protected void deleteOrganization(Key orgkey) 427 throws Exception 428 { 429 assertNotNull("Org Key is null?", orgkey); 430 if (blm == null) blm = this.getBusinessLifeCycleManager(); 431 Collection keys = new ArrayList (); 432 keys.add(orgkey); 433 434 BulkResponse response = blm.deleteOrganizations(keys); 435 Collection exceptions = response.getExceptions(); 436 assertNull("Deleting Org with Key=" + orgkey, exceptions); 437 } 438 439 private ClassificationScheme getClassificationScheme(String str1, String str2) 440 throws JAXRException 441 { 442 ClassificationScheme cs = blm.createClassificationScheme(getIString(str1), 443 getIString(str2)); 444 return cs; 445 } 446 447 protected Connection loginSecondUser() 448 { 449 Connection con = null; 450 try 451 { 452 if (factory == null) 453 throw new IllegalStateException ("ConnectionFactory is null"); 454 con = factory.createConnection(); 455 } catch (JAXRException e) 456 { 457 e.printStackTrace(); 458 } 459 PasswordAuthentication passwdAuth = new PasswordAuthentication ("jbosscts", 460 passwd.toCharArray()); 461 Set creds = new HashSet (); 462 creds.add(passwdAuth); 463 464 try 465 { 466 con.setCredentials(creds); 467 } catch (JAXRException e) 468 { 469 e.printStackTrace(); 470 fail(e.getMessage()); 471 } 472 return con; 473 } 474 475 protected Concept getAssociationConcept(String associationType) 476 { 477 try 478 { 479 BusinessQueryManager bqm = rs.getBusinessQueryManager(); 480 ClassificationScheme associationTypes = 481 bqm.findClassificationSchemeByName(null, "AssociationType"); 482 Collection types = associationTypes.getChildrenConcepts(); 483 Iterator iter = types.iterator(); 484 Concept concept = null; 485 while (iter.hasNext()) 486 { 487 concept = (Concept) iter.next(); 488 if (concept.getName().getValue().equals(associationType)) 489 { 490 return concept; 491 } 492 } 493 } catch (Exception e) 494 { 495 e.printStackTrace(); 496 return null; 497 } 498 return null; 499 500 } 502 protected InitialContext getClientContext() throws NamingException 503 { 504 String hostname = System.getProperty("host.name", "localhost"); 505 if (hostname == null) 506 throw new IllegalStateException ("host.name system property not present"); 507 Properties env = new Properties (); 508 env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"); 509 env.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces"); 510 env.setProperty(Context.PROVIDER_URL, "jnp://" + hostname + ":1099"); 511 return new InitialContext (env); 512 } 513 514 } 515 | Popular Tags |