1   
2   
3   
4   
5   
6   
7   
8   package gov.noaa.gdsg.xmldbremote.xmldbClient;
9   
10  import junit.framework.*;
11  import org.xmldb.api.base.ResourceSet;
12  import org.xmldb.api.base.Resource;
13  import org.xmldb.api.base.ResourceIterator;
14  import org.xmldb.api.base.XMLDBException;
15  import org.xmldb.api.base.ErrorCodes;
16  import org.xmldb.api.base.Service;
17  import org.xmldb.api.modules.XPathQueryService;
18  import org.xmldb.api.DatabaseManager;
19  import java.util.LinkedList;
20  import java.util.List;
21  import org.xmldb.api.base.Collection;
22  
23  import org.apache.commons.digester.Digester;
24  import org.apache.commons.digester.SetPropertiesRule;
25  
26  
27  /***
28   *
29   * @author tns
30   */
31  public class XmldbClientResourceSetTest extends TestCase {
32      
33      private ResourceSet resourceSet = null;
34      
35      private String endPointUrl = "http://localhost:8080/mm/services/nmmrdb";
36      
37      
38      /*** Holds value of property collectionName. */
39      private String collectionName;
40      
41      /*** Holds value of property collectionPassword. */
42      private String collectionPassword;
43      
44      /*** Holds value of property collectionUserName. */
45      private String collectionUserName;
46      
47      /*** Holds value of property collectionUri. */
48      private String collectionUri;
49      
50      /*** Holds value of property xPathQueryServiceVersion. */
51      private String xPathQueryServiceVersion = "1.0";
52      
53      /*** Holds value of property numQueryResults. */
54      private long numQueryResults;
55      
56      /*** Holds value of property query. */
57      private String query;
58      
59      
60      
61      public XmldbClientResourceSetTest(java.lang.String testName) {
62          super(testName);
63      }
64      
65      public static Test suite() {
66          TestSuite suite = new TestSuite(XmldbClientResourceSetTest.class);
67          return suite;
68      }
69      
70      public void setUp() throws Exception {
71          this.gatherParameters();
72          this.loadResourceSet();
73          
74      }
75      
76      /***Reads the testConfi.xml file for properties.*/
77      private void gatherParameters() throws Exception {
78          
79          
80          java.io.InputStream inputStream = null;
81          try {
82              inputStream = this.getClass().getResourceAsStream("testConfig.xml");
83              
84              
85              Digester digester = new Digester();
86              digester.push(this);
87              digester.addRule("test-config/resourceSet", new SetPropertiesRule());
88              digester.parse(inputStream);
89              digester.pop();
90          } finally {
91              if (inputStream != null){
92                  inputStream.close();
93              }
94          }
95      }    
96      
97      private void loadResourceSet() throws XMLDBException {
98          assertNotNull("Collection URI is null", this.collectionUri);
99          assertNotNull("Collection user name is null", this.collectionUserName);
100         assertNotNull("Collectioin password is null", this.collectionPassword);
101         XmldbClientDatabase db = new XmldbClientDatabase();
102         db.setEndPointUrl(endPointUrl);
103         db.init();
104         Collection col = db.getCollection(collectionUri, collectionUserName, collectionPassword);
105         assertNotNull("Request collection was not returned",col);
106         assertNotNull("Param xPathQueryServiceVersion is null",xPathQueryServiceVersion);
107         Service aService = col.getService("XPathQueryService", this.xPathQueryServiceVersion);
108         assertNotNull("Service returned is null, requested 'XPathQueryService' version:" + this.xPathQueryServiceVersion, aService);
109         assertTrue("The returned service is not of type 'XPathQueryService' it is " 
110             + aService.getClass().getName(), aService instanceof XPathQueryService);
111         XPathQueryService service = (XPathQueryService) aService;
112         assertNotNull("Query unspecified", this.query);
113         this.resourceSet = service.query(this.query);
114         assertNotNull("Resource set is null",this.resourceSet);
115     }
116     
117     
118     /*** Test of addResource method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
119     public void testAddResource() throws Exception {
120         System.out.println("testAddResource");
121         
122         
123         fail("The test case is empty.");
124     }
125     
126     /*** Test of clear method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
127     public void testClear() throws Exception {
128         System.out.println("testClear");
129         
130         
131         this.resourceSet.clear();
132         assertTrue("Cleared resource set, but still has elements", this.resourceSet.getSize() == 0);
133     }
134     
135     /*** Test of getIterator method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
136     public void testGetIterator() throws Exception {
137         System.out.println("testGetIterator");
138         
139         
140         ResourceIterator it = this.resourceSet.getIterator();
141         
142     }
143     
144     /*** Test of getMembersAsResource method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
145     public void testGetMembersAsResource() throws Exception {
146         System.out.println("testGetMembersAsResource");
147         
148         
149         Resource resource = this.resourceSet.getMembersAsResource();
150     }
151     
152     /*** Test of getResource method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
153     public void testGetResource() throws Exception {
154         System.out.println("testGetResource");
155         
156         
157         assertTrue("No resources to get", this.resourceSet.getSize() > 0);
158         Resource resource = this.resourceSet.getResource(0);
159     }
160     
161     /*** Test of getSize method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
162     public void testGetSize() throws Exception {
163         System.out.println("testGetSize");
164         
165         
166         long size = this.resourceSet.getSize();
167         assertTrue("Returned size was not expected", this.numQueryResults == size);
168     }
169     
170     /*** Test of removeResource method, of class gov.noaa.mm.xmldbImpl.BMSResourceSet. */
171     public void testRemoveResource() throws Exception {
172         System.out.println("testRemoveResource");
173         
174         
175         long size = this.resourceSet.getSize();
176         assertTrue("No resources to get", this.resourceSet.getSize() > 0);
177         this.resourceSet.removeResource(0);
178         assertTrue("Removal of resource should result in one less record", size == this.resourceSet.getSize() + 1);
179     }
180     
181     
182     
183     
184     
185    /*** Getter for property collectionName.
186      * @return Value of property collectionName.
187      *
188      */
189     public String getCollectionName() {
190         return this.collectionName;
191     }
192     
193     /*** Setter for property collectionName.
194      * @param collectionName New value of property collectionName.
195      *
196      */
197     public void setCollectionName(String collectionName) {
198         this.collectionName = collectionName;
199     }
200     
201     /*** Getter for property collectionPassword.
202      * @return Value of property collectionPassword.
203      *
204      */
205     public String getCollectionPassword() {
206         return this.collectionPassword;
207     }
208     
209     /*** Setter for property collectionPassword.
210      * @param collectionPassword New value of property collectionPassword.
211      *
212      */
213     public void setCollectionPassword(String collectionPassword) {
214         this.collectionPassword = collectionPassword;
215     }
216     
217     /*** Getter for property collectionUserName.
218      * @return Value of property collectionUserName.
219      *
220      */
221     public String getCollectionUserName() {
222         return this.collectionUserName;
223     }
224     
225     /*** Setter for property collectionUserName.
226      * @param collectionUserName New value of property collectionUserName.
227      *
228      */
229     public void setCollectionUserName(String collectionUserName) {
230         this.collectionUserName = collectionUserName;
231     }
232     
233     /*** Getter for property collectionUri.
234      * @return Value of property collectionUri.
235      *
236      */
237     public String getCollectionUri() {
238         return this.collectionUri;
239     }
240     
241     /*** Setter for property collectionUri.
242      * @param collectionUri New value of property collectionUri.
243      *
244      */
245     public void setCollectionUri(String collectionUri) {
246         this.collectionUri = collectionUri;
247     }
248     
249     /*** Getter for property xPathQueryServiceVersion.
250      * @return Value of property xPathQueryServiceVersion.
251      *
252      */
253     public String getXPathQueryServiceVersion() {
254         return this.xPathQueryServiceVersion;
255     }
256     
257     /*** Setter for property xPathQueryServiceVersion.
258      * @param xPathQueryServiceVersion New value of property xPathQueryServiceVersion.
259      *
260      */
261     public void setXPathQueryServiceVersion(String xPathQueryServiceVersion) {
262         this.xPathQueryServiceVersion = xPathQueryServiceVersion;
263     }
264     
265     /*** Getter for property numQueryResults.
266      * @return Value of property numQueryResults.
267      *
268      */
269     public long getNumQueryResults() {
270         return this.numQueryResults;
271     }
272     
273     /*** Setter for property numQueryResults.
274      * @param numQueryResults New value of property numQueryResults.
275      *
276      */
277     public void setNumQueryResults(long numQueryResults) {
278         this.numQueryResults = numQueryResults;
279     }
280     
281     /*** Getter for property query.
282      * @return Value of property query.
283      *
284      */
285     public String getQuery() {
286         return this.query;
287     }
288     
289     /*** Setter for property query.
290      * @param query New value of property query.
291      *
292      */
293     public void setQuery(String query) {
294         this.query = query;
295     }
296     
297 }