KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jaxr > scout > query > infomodel > JaxrFindAssociationsTestCase


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.query.infomodel;
23
24 import org.jboss.test.jaxr.scout.JaxrBaseTestCase;
25 import org.jboss.test.jaxr.scout.util.ScoutUtil;
26
27 import javax.xml.registry.BulkResponse JavaDoc;
28 import javax.xml.registry.BusinessLifeCycleManager JavaDoc;
29 import javax.xml.registry.BusinessQueryManager JavaDoc;
30 import javax.xml.registry.Connection JavaDoc;
31 import javax.xml.registry.JAXRException JavaDoc;
32 import javax.xml.registry.LifeCycleManager JavaDoc;
33 import javax.xml.registry.RegistryService JavaDoc;
34 import javax.xml.registry.infomodel.Association JavaDoc;
35 import javax.xml.registry.infomodel.Concept JavaDoc;
36 import javax.xml.registry.infomodel.Key JavaDoc;
37 import javax.xml.registry.infomodel.Organization JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 /**
43  * Tests FindAssociation using JAXR API
44  * @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
45  * @since Mar 9, 2005
46  */

47 public class JaxrFindAssociationsTestCase extends JaxrBaseTestCase
48 {
49    public void testFindAssociations() throws JAXRException JavaDoc
50    {
51
52       String JavaDoc orgSource = "Source Organization";
53       String JavaDoc type = "Implements";
54
55       Key JavaDoc savekey = null;
56       Key JavaDoc assockey = null;
57
58       BusinessQueryManager JavaDoc bqm2 = null;
59       BusinessLifeCycleManager JavaDoc blm2 = null;
60       Collection JavaDoc associationKeys = null;
61       Collection JavaDoc sourceKeys = null;
62       Collection JavaDoc targetKeys = null;
63       String JavaDoc targetId = null;
64       String JavaDoc sourceId = null;
65       String JavaDoc orgTarget = "Target Organization";
66
67       try
68       {
69          login();
70          getJAXREssentials();
71          // Authenticate second user
72
Connection JavaDoc con2 = loginSecondUser();
73          RegistryService JavaDoc rs2 = con2.getRegistryService();
74          blm2 = rs2.getBusinessLifeCycleManager();
75          bqm2 = rs2.getBusinessQueryManager();
76
77          Organization JavaDoc source = blm.createOrganization(blm.createInternationalString(orgSource));
78          Organization JavaDoc target = blm2.createOrganization(blm.createInternationalString(orgTarget));
79
80          // publish the source organization
81
Collection JavaDoc orgs = new ArrayList JavaDoc();
82          orgs.add(source);
83          br = blm.saveOrganizations(orgs);
84          if (br.getExceptions() != null)
85          {
86             fail(" Source:Save Orgs failed");
87          }
88
89          sourceKeys = br.getCollection();
90          Iterator JavaDoc iter = sourceKeys.iterator();
91          while (iter.hasNext())
92          {
93             savekey = (Key JavaDoc)iter.next();
94          }
95          sourceId = savekey.getId();
96          String JavaDoc objectType = LifeCycleManager.ORGANIZATION;
97          Organization JavaDoc pubSource = (Organization JavaDoc)bqm.getRegistryObject(sourceId, objectType);
98          assertNotNull(pubSource.getName().getValue());
99
100          // publish the target
101
orgs.clear();
102          orgs.add(target);
103          br = blm2.saveOrganizations(orgs);
104          if (br.getExceptions() != null)
105          {
106             fail(" Target:Save Orgs failed");
107          }
108          targetKeys = br.getCollection();
109          iter = targetKeys.iterator();
110          while (iter.hasNext())
111          {
112             savekey = (Key JavaDoc)iter.next();
113          }
114          targetId = savekey.getId();
115          Organization JavaDoc pubTarget = (Organization JavaDoc)bqm2.getRegistryObject(targetId, objectType);
116
117          Concept JavaDoc associationType = getAssociationConcept(type);
118          if (associationType == null)
119             fail(" getAssociationConcept returned null associationType");
120
121          Association JavaDoc a = blm.createAssociation(pubTarget, associationType);
122          a.setSourceObject(pubSource);
123
124          blm.confirmAssociation(a); //First user
125
blm2.confirmAssociation(a); //Second user
126

127          // publish Association
128
Collection JavaDoc associations = new ArrayList JavaDoc();
129          associations.add(a);
130          // Second user saves the association.
131
br = blm2.saveAssociations(associations, false);
132
133          if (br.getExceptions() != null)
134          {
135             fail("Error:Save Associations failed \n");
136          }
137          BulkResponse JavaDoc targetAssoc = bqm.findCallerAssociations(null,
138                  new Boolean JavaDoc(true),
139                  new Boolean JavaDoc(true),
140                  null);
141
142          if (targetAssoc.getExceptions() == null)
143          {
144             Collection JavaDoc targetCol = targetAssoc.getCollection();
145             if (targetCol.size() > 0)
146             {
147                iter = targetCol.iterator();
148                while (iter.hasNext())
149                {
150                   Association JavaDoc a1 = (Association JavaDoc)iter.next();
151                   Organization JavaDoc o = (Organization JavaDoc)a1.getSourceObject();
152                   o = (Organization JavaDoc)a1.getTargetObject();
153                   Concept JavaDoc atype = a1.getAssociationType();
154                   assertNotNull("Concept Type stored in Association", atype);
155                }
156             }
157          }
158
159
160          br = null;
161          Collection JavaDoc associationTypes = new ArrayList JavaDoc();
162          associationTypes.add(type);
163          br = bqm.findAssociations(null, sourceId, targetId, null);
164          if (br.getExceptions() != null)
165          {
166             fail("Error: findAssociations failed ");
167          }
168          associations = null;
169          associations = br.getCollection();
170          if (associations.size() > 0)
171          {
172             iter = associations.iterator();
173             while (iter.hasNext())
174             {
175                Association JavaDoc a1 = (Association JavaDoc)iter.next();
176                assockey = a1.getKey();
177                ScoutUtil.validateAssociation(a1,orgSource);
178             }
179          }
180       }
181       catch (Exception JavaDoc e)
182       {
183          e.printStackTrace();
184          fail(" Test failed ");
185       }
186       finally
187       {
188          //Clean Up
189
try
190          {
191             if (assockey != null)
192             {
193                associationKeys = new ArrayList JavaDoc();
194                associationKeys.add(assockey);
195                blm.deleteAssociations(associationKeys);
196             }
197             blm2.deleteOrganizations(targetKeys);
198             blm.deleteOrganizations(sourceKeys);
199          }
200          catch (JAXRException JavaDoc ex)
201          {
202             ex.printStackTrace();
203             fail("Error: Cleanup failed");
204          }
205       }
206
207    } // end of method
208

209
210 }
211
Popular Tags