KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jaxr > scout > JaxrBaseTestCase


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.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 JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.xml.registry.BulkResponse JavaDoc;
33 import javax.xml.registry.BusinessLifeCycleManager JavaDoc;
34 import javax.xml.registry.BusinessQueryManager JavaDoc;
35 import javax.xml.registry.Connection JavaDoc;
36 import javax.xml.registry.ConnectionFactory JavaDoc;
37 import javax.xml.registry.FindQualifier JavaDoc;
38 import javax.xml.registry.JAXRException JavaDoc;
39 import javax.xml.registry.RegistryService JavaDoc;
40 import javax.xml.registry.infomodel.*;
41 import java.net.PasswordAuthentication JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collection JavaDoc;
44 import java.util.HashSet JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Locale JavaDoc;
47 import java.util.Properties JavaDoc;
48 import java.util.Set JavaDoc;
49
50 /**
51  * Acts as the base class for Jaxr Test Cases
52  *
53  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
54  * @since Dec 29, 2004
55  */

56 public class JaxrBaseTestCase extends TestCase
57 {
58
59     protected String JavaDoc userid = "jboss";
60     protected String JavaDoc passwd = "jboss";
61     protected BusinessLifeCycleManager JavaDoc blm = null;
62     protected RegistryService JavaDoc rs = null;
63     protected BusinessQueryManager JavaDoc bqm = null;
64     protected Connection JavaDoc connection = null;
65     protected BulkResponse JavaDoc br = null;
66     protected JBossRMIAdaptorHelper server = null;
67
68     protected ConnectionFactory JavaDoc factory = null;
69
70     protected static final ObjectName JavaDoc OBJECT_NAME = ObjectNameFactory.create("jboss:service=juddi");
71
72     //Debug ID
73
protected static String JavaDoc debugProp = System.getProperty("jaxr.debug", "true");
74
75     /**
76      * Setup of the JUnit test
77      * We create the juddi tables on startup
78      *
79      * @throws Exception
80      */

81     protected void setUp() throws Exception JavaDoc
82     {
83         //Change the createonstart setting for juddi service and restart it
84
server = new JBossRMIAdaptorHelper(this.getClientContext());
85         server.invokeOperation(OBJECT_NAME, "setCreateOnStart",
86                 new Object JavaDoc[]{Boolean.TRUE},
87                 new String JavaDoc[]{Boolean.TYPE.getName()});
88         server.invokeOperation(OBJECT_NAME, "stop",
89                 null, null);
90         server.invokeOperation(OBJECT_NAME, "start",
91                 null, null);
92
93         //Ensure that the Jaxr Connection Factory class is setup
94
String JavaDoc factoryString = "javax.xml.registry.ConnectionFactoryClass";
95         String JavaDoc factoryClass = System.getProperty(factoryString);
96         if(factoryClass == null || factoryClass.length() == 0)
97            System.setProperty(factoryString,"org.apache.ws.scout.registry.ConnectionFactoryImpl");
98
99         String JavaDoc queryurl = System.getProperty("jaxr.query.url",
100               "http://localhost:8080/juddi/inquiry");
101         String JavaDoc puburl = System.getProperty("jaxr.publish.url",
102               "http://localhost:8080/juddi/publish");
103
104         Properties JavaDoc props = new Properties JavaDoc();
105         props.setProperty("javax.xml.registry.queryManagerURL",
106                 queryurl);
107
108         props.setProperty("javax.xml.registry.lifeCycleManagerURL",
109                 puburl);
110
111         String JavaDoc transportClass = System.getProperty("juddi.proxy.transportClass",
112                 "org.jboss.jaxr.juddi.transport.SaajTransport");
113         System.setProperty("juddi.proxy.transportClass", transportClass);
114         try
115         {
116             // Create the connection, passing it the configuration properties
117
factory = ConnectionFactory.newInstance();
118             factory.setProperties(props);
119             connection = factory.createConnection();
120         } catch (JAXRException JavaDoc e)
121         {
122             fail("Setup failed"+e);
123         }
124     }
125
126     /**
127      * Teardown of the junit test
128      * We discard all the tables created by the juddi service
129      *
130      * @throws Exception
131      */

132     protected void tearDown() throws Exception JavaDoc
133     {
134         if (connection != null) connection.close();
135         //stop the juddi service so that all the tables are dropped
136
server.invokeOperation(OBJECT_NAME, "setCreateOnStart",
137                 new Object JavaDoc[]{Boolean.FALSE},
138                 new String JavaDoc[]{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     /**
149      * Does authentication with the uddi registry
150      */

151     protected void login()
152     {
153         PasswordAuthentication JavaDoc passwdAuth = new PasswordAuthentication JavaDoc(userid,
154                 passwd.toCharArray());
155         Set JavaDoc creds = new HashSet JavaDoc();
156         creds.add(passwdAuth);
157
158         try
159         {
160             connection.setCredentials(creds);
161         } catch (JAXRException JavaDoc e)
162         {
163             e.printStackTrace();
164             fail(e.getMessage());
165         }
166     }
167
168     protected void getJAXREssentials() throws JAXRException JavaDoc
169     {
170
171         rs = connection.getRegistryService();
172         blm = rs.getBusinessLifeCycleManager();
173         bqm = rs.getBusinessQueryManager();
174     }
175
176     public InternationalString getIString(String JavaDoc str)
177             throws JAXRException JavaDoc
178     {
179         return blm.createInternationalString(str);
180     }
181      
182     
183     /**
184      * Locale aware Search a business in the registry
185      *
186      * @param bizname
187      * @throws JAXRException
188      */

189     public void searchBusiness(String JavaDoc bizname) throws JAXRException JavaDoc
190     {
191         try
192         {
193             // Get registry service and business query manager
194
this.getJAXREssentials();
195
196             // Define find qualifiers and name patterns
197
Collection JavaDoc findQualifiers = new ArrayList JavaDoc();
198             findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
199             Collection JavaDoc namePatterns = new ArrayList JavaDoc();
200             String JavaDoc pattern = "%" + bizname + "%";
201             LocalizedString ls = blm.createLocalizedString(Locale.getDefault(),
202                   pattern);
203             namePatterns.add(ls);
204
205             // Find based upon qualifier type and values
206
BulkResponse JavaDoc response =
207                     bqm.findOrganizations(findQualifiers,
208                             namePatterns,
209                             null,
210                             null,
211                             null,
212                             null);
213
214             // check how many organisation we have matched
215
Collection JavaDoc 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                 // then step through them
227
for (Iterator JavaDoc 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             }//end else
240
} catch (JAXRException JavaDoc e)
241         {
242             e.printStackTrace();
243             fail(e.getMessage());
244         } finally
245         {
246             connection.close();
247         }
248
249     }
250
251     protected RegistryService JavaDoc getRegistryService() throws JAXRException JavaDoc
252     {
253         assertNotNull(connection);
254         return connection.getRegistryService();
255     }
256
257     protected BusinessQueryManager JavaDoc getBusinessQueryManager() throws JAXRException JavaDoc
258     {
259         assertNotNull(connection);
260         if (rs == null) rs = this.getRegistryService();
261         return rs.getBusinessQueryManager();
262     }
263
264     protected BusinessLifeCycleManager JavaDoc getBusinessLifeCycleManager() throws JAXRException JavaDoc
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 JavaDoc
273     {
274         // Display service and binding information
275
Collection JavaDoc services = org.getServices();
276         for (Iterator JavaDoc svcIter = services.iterator(); svcIter.hasNext();)
277         {
278             Service JavaDoc svc = (Service JavaDoc) 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 JavaDoc serviceBindings = svc.getServiceBindings();
288             for (Iterator JavaDoc 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 JavaDoc
304     {
305         // Display primary contact information
306
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 JavaDoc phNums = pc.getTelephoneNumbers(pc.getType());
313             for (Iterator JavaDoc phIter = phNums.iterator(); phIter.hasNext();)
314             {
315                 TelephoneNumber num = (TelephoneNumber) phIter.next();
316                 System.out.println(" Phone number: " + num.getNumber());
317             }
318             Collection JavaDoc eAddrs = pc.getEmailAddresses();
319             for (Iterator JavaDoc eaIter = eAddrs.iterator(); eaIter.hasNext();)
320             {
321                 System.out.println(" Email Address: " + (EmailAddress) eaIter.next());
322             }
323         }
324     }
325
326     private static String JavaDoc getName(RegistryObject ro) throws JAXRException JavaDoc
327     {
328         if (ro != null && ro.getName() != null)
329         {
330             return ro.getName().getValue();
331         }
332         return "";
333     }
334
335     private static String JavaDoc getDescription(RegistryObject ro) throws JAXRException JavaDoc
336     {
337         if (ro != null && ro.getDescription() != null)
338         {
339             return ro.getDescription().getValue();
340         }
341         return "";
342     }
343
344     private static String JavaDoc getKey(RegistryObject ro) throws JAXRException JavaDoc
345     {
346         if (ro != null && ro.getKey() != null)
347         {
348             return ro.getKey().getId();
349         }
350         return "";
351     }
352
353     /**
354      * Creates a Jaxr Organization with 1 or more services
355      *
356      * @return
357      * @throws JAXRException
358      */

359     protected Organization createOrganization(String JavaDoc orgname)
360             throws JAXRException JavaDoc
361     {
362         Organization org = blm.createOrganization(getIString(orgname));
363         org.setDescription(getIString("JBoss Inc"));
364         Service JavaDoc service = blm.createService(getIString("JBOSS JAXR Service"));
365         service.setDescription(getIString("Services of XML Registry"));
366         //Create serviceBinding
367
ServiceBinding serviceBinding = blm.createServiceBinding();
368         serviceBinding.setDescription(blm.
369                createInternationalString("Test Service Binding"));
370  
371         //Turn validation of URI off
372
serviceBinding.setValidateURI(false);
373         serviceBinding.setAccessURI("http://testjboss.org");
374  
375         // Add the serviceBinding to the service
376
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 JavaDoc postalAddresses = new ArrayList JavaDoc();
389         postalAddresses.add(address);
390         Collection JavaDoc emailAddresses = new ArrayList JavaDoc();
391         EmailAddress emailAddress = blm.createEmailAddress("anil@apache.org");
392         emailAddresses.add(emailAddress);
393
394         Collection JavaDoc numbers = new ArrayList JavaDoc();
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     /**
421      * Delete an Organization with a given key
422      *
423      * @param orgkey
424      * @throws Exception
425      */

426     protected void deleteOrganization(Key orgkey)
427             throws Exception JavaDoc
428     {
429         assertNotNull("Org Key is null?", orgkey);
430         if (blm == null) blm = this.getBusinessLifeCycleManager();
431         Collection JavaDoc keys = new ArrayList JavaDoc();
432         keys.add(orgkey);
433
434         BulkResponse JavaDoc response = blm.deleteOrganizations(keys);
435         Collection JavaDoc exceptions = response.getExceptions();
436         assertNull("Deleting Org with Key=" + orgkey, exceptions);
437     }
438
439     private ClassificationScheme getClassificationScheme(String JavaDoc str1, String JavaDoc str2)
440             throws JAXRException JavaDoc
441     {
442         ClassificationScheme cs = blm.createClassificationScheme(getIString(str1),
443                 getIString(str2));
444         return cs;
445     }
446
447     protected Connection JavaDoc loginSecondUser()
448     {
449         Connection JavaDoc con = null;
450         try
451         {
452             if (factory == null)
453                 throw new IllegalStateException JavaDoc("ConnectionFactory is null");
454             con = factory.createConnection();
455         } catch (JAXRException JavaDoc e)
456         {
457             e.printStackTrace();
458         }
459         PasswordAuthentication JavaDoc passwdAuth = new PasswordAuthentication JavaDoc("jbosscts",
460                 passwd.toCharArray());
461         Set JavaDoc creds = new HashSet JavaDoc();
462         creds.add(passwdAuth);
463
464         try
465         {
466             con.setCredentials(creds);
467         } catch (JAXRException JavaDoc e)
468         {
469             e.printStackTrace();
470             fail(e.getMessage());
471         }
472         return con;
473     }
474
475     protected Concept getAssociationConcept(String JavaDoc associationType)
476     {
477         try
478         {
479             BusinessQueryManager JavaDoc bqm = rs.getBusinessQueryManager();
480             ClassificationScheme associationTypes =
481                     bqm.findClassificationSchemeByName(null, "AssociationType");
482             Collection JavaDoc types = associationTypes.getChildrenConcepts();
483             Iterator JavaDoc 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 JavaDoc e)
494         {
495             e.printStackTrace();
496             return null;
497         }
498         return null;
499
500     }// end of method
501

502     protected InitialContext JavaDoc getClientContext() throws NamingException JavaDoc
503     {
504         String JavaDoc hostname = System.getProperty("host.name", "localhost");
505         if (hostname == null)
506             throw new IllegalStateException JavaDoc("host.name system property not present");
507         Properties JavaDoc env = new Properties JavaDoc();
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 JavaDoc(env);
512     }
513
514 }
515
Popular Tags