1   /*
2    * XPathQueryServiceTest.java
3    * JUnit based test
4    *
5    * Created on May 6, 2004, 5:08 PM
6    */
7   
8   package gov.noaa.gdsg.xmldbremote.xmldbClient;
9   
10  import junit.framework.*;
11  import org.xmldb.api.modules.XPathQueryService;
12  import org.xmldb.api.base.Collection;
13  import org.xmldb.api.base.ResourceSet;
14  import org.xmldb.api.base.Service;
15  import org.xmldb.api.base.XMLDBException;
16  
17  import org.apache.commons.digester.Digester;
18  import org.apache.commons.digester.SetPropertiesRule;
19  import org.w3c.dom.Node;
20  import org.xmldb.api.base.Resource;
21  import org.xmldb.api.modules.XMLResource;
22  
23  
24  /***
25   *
26   * @author tns
27   * @version $Id: XmldbClientXPathQueryServiceTest.java,v 1.1 2004/10/29 17:47:14 mrxtravis Exp $
28   */
29  public class XmldbClientXPathQueryServiceTest extends TestCase {
30      
31      private XPathQueryService service = null;
32      
33      private String endPointUrl = "http://localhost:8080/mm/services/nmmrdb";
34      
35      
36      /*** Holds value of property collectionName. */
37      private String collectionName;
38      
39      /*** Holds value of property collectionPassword. */
40      private String collectionPassword;
41      
42      /*** Holds value of property collectionUserName. */
43      private String collectionUserName;
44      
45      /*** Holds value of property collectionUri. */
46      private String collectionUri;
47      
48      /*** Holds value of property xPathQueryServiceVersion. */
49      private String xPathQueryServiceVersion = "1.0";
50      
51      /*** Holds value of property numQueryResults. */
52      private long numQueryResults = -1;
53      
54      /*** Holds value of property query. */
55      private String query;
56      
57      public XmldbClientXPathQueryServiceTest(java.lang.String testName) throws Exception {
58          super(testName);
59          this.gatherParameters();
60          this.loadXPathQueryService();
61      }
62      
63      public void setUp() throws Exception { 
64          
65      }
66      
67      private void loadXPathQueryService() throws XMLDBException {
68          assertNotNull("Collection URI is null", this.collectionUri);
69          assertNotNull("Collection user name is null", this.collectionUserName);
70          assertNotNull("Collectioin password is null", this.collectionPassword);
71          XmldbClientDatabase db = new XmldbClientDatabase();
72          db.setEndPointUrl(endPointUrl);
73          db.init();
74          Collection col = db.getCollection(collectionUri, collectionUserName, collectionPassword);
75          assertNotNull("Request collection was not returned",col);
76          assertNotNull("Param xPathQueryServiceVersion is null",xPathQueryServiceVersion);
77          Service aService = col.getService("XPathQueryService", this.xPathQueryServiceVersion);
78          assertNotNull("Service returned is null, requested 'XPathQueryService' version:" + this.xPathQueryServiceVersion, aService);
79          assertTrue("The returned service is not of type 'XPathQueryService' it is " 
80              + aService.getClass().getName(), aService instanceof XPathQueryService);
81          this.service = (XPathQueryService) aService;
82          assertNotNull("Request service 'XPathQueryService' version:" + this.xPathQueryServiceVersion
83              + " was not returned when requested", this.service);
84      }
85      
86      
87      /***Reads the testConfi.xml file for properties.*/
88      private void gatherParameters() throws Exception {
89          
90          //lets load the document.
91          java.io.InputStream inputStream = null;
92          try {
93              inputStream = this.getClass().getResourceAsStream("testConfig.xml");
94              
95              //set up the Digester
96              Digester digester = new Digester();
97              digester.push(this);
98              digester.addRule("test-config/XPathQueryService", new SetPropertiesRule());
99              digester.parse(inputStream);
100             digester.pop();
101         } finally {
102             if (inputStream != null){
103                 inputStream.close();
104             }
105         }
106     }
107     
108     
109     public static Test suite() {
110         TestSuite suite = new TestSuite(XmldbClientXPathQueryServiceTest.class);
111         return suite;
112     }
113     
114     /*** Test of clearNamespaces method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
115     public void testClearNamespaces() throws Exception {
116         System.out.println("testClearNamespaces");
117         
118         // Add your test code below by replacing the default call to fail.
119         this.service.clearNamespaces();
120     }
121     
122     /*** Test of getName method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
123     public void testGetName() throws Exception {
124         System.out.println("testGetName");
125         
126         // Add your test code below by replacing the default call to fail.
127         String name = this.service.getName();
128         assertNotNull("Service name is null", name);
129         assertEquals("Service name did not match 'XPathQueryService'", "XPathQueryService", name);
130     }
131     
132     /*** Test of getNamespace method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
133     public void testGetNamespace() throws Exception {
134         System.out.println("testGetNamespace");
135         
136         // Add your test code below by replacing the default call to fail.
137         this.service.getNamespace("");
138     }
139     
140     /*** Test of getProperty method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
141     public void testGetProperty() throws Exception {
142         System.out.println("testGetProperty");
143         
144         // Add your test code below by replacing the default call to fail.
145         this.service.getProperty("");
146     }
147     
148     /*** Test of getVersion method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
149     public void testGetVersion() throws Exception {
150         System.out.println("testGetVersion");
151         
152         // Add your test code below by replacing the default call to fail.
153         String version = this.service.getVersion();
154         assertNotNull("Version returned null",version);
155         assertEquals("Incorrect version number",this.xPathQueryServiceVersion, version);
156     }
157     
158     /*** Test of query method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
159     public void testQuery() throws Exception {
160         System.out.println("testQuery");
161         
162         // Add your test code below by replacing the default call to fail.
163         assertNotNull("Query is unspecified",this.query);
164         this.assertTrue("numQueryResults is unspecified",this.numQueryResults >= 0);
165         
166         ResourceSet resourceSet = this.service.query(this.query);
167         assertNotNull("Query returned null resource set", resourceSet);
168         
169         assertEquals("Number of resulting records unexpected from query:" + this.query,
170             resourceSet.getSize(), this.numQueryResults);
171     }
172 
173     /*** Test query method with a query */
174     public void testElementQuery() throws Exception {
175         String query = "/metadata/idinfo/citation/citeinfo/title[ contains( text(), 'NGDC')]";
176         ResourceSet resourceSet = this.service.query(query);
177         assertNotNull("Query returned null resource set", resourceSet);
178         
179         assertEquals("Number of resulting records unexpected from query:" + this.query,
180             12, resourceSet.getSize());
181         Resource resource = resourceSet.getResource(0);
182         assertNotNull("Resource expected",resource);
183         assertTrue("resource expected to be XMLResource",resource instanceof XMLResource);
184         XMLResource xResource = (XMLResource) resource;
185         Node node = xResource.getContentAsDOM();
186         assertNotNull("node expected to exist",node);
187         assertEquals("unexpected node name " + node.getNodeName() ,"title",node.getNodeName());
188         Node textNode = node.getFirstChild();
189         assertNotNull("text node expected",textNode);
190         String val = textNode.getNodeValue();
191         assertNotNull("node value expected",val);
192         assertTrue("unexpected title '" + val + "'",val.indexOf("NGDC") >= 0);
193         
194         
195         
196     }
197     
198     /*** Test of queryResource method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
199 //    public void testQueryResource() throws Exception {
200 //        System.out.println("testQueryResource");
201 //        
202 //        // Add your test code below by replacing the default call to fail.
203 //        fail("No tests");
204 //    }
205     
206     /*** Test of removeNamespace method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
207 //    public void testRemoveNamespace() throws Exception {
208 //        System.out.println("testRemoveNamespace");
209 //        
210 //        // Add your test code below by replacing the default call to fail.
211 //        fail("The test case is empty.");
212 //    }
213     
214     /*** Test of setCollection method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
215 //    public void testSetCollection() throws Exception {
216 //        System.out.println("testSetCollection");
217 //        
218 //        // Add your test code below by replacing the default call to fail.
219 //        fail("The test case is empty.");
220 //    }
221     
222     /*** Test of setNamespace method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
223     public void testSetNamespace() throws Exception {
224         System.out.println("testSetNamespace");
225         
226         // Add your test code below by replacing the default call to fail.
227         fail("The test case is empty.");
228     }
229     
230     /*** Test of setProperty method, of class gov.noaa.mm.xmldbImpl.BMSXPathQueryService. */
231     public void testSetProperty() throws Exception {
232         System.out.println("testSetProperty");
233         
234         // Add your test code below by replacing the default call to fail.
235         this.service.setProperty("","");
236     }
237     
238     /*** Getter for property collectionName.
239      * @return Value of property collectionName.
240      *
241      */
242     public String getCollectionName() {
243         return this.collectionName;
244     }
245     
246     /*** Setter for property collectionName.
247      * @param collectionName New value of property collectionName.
248      *
249      */
250     public void setCollectionName(String collectionName) {
251         this.collectionName = collectionName;
252     }
253     
254     /*** Getter for property collectionPassword.
255      * @return Value of property collectionPassword.
256      *
257      */
258     public String getCollectionPassword() {
259         return this.collectionPassword;
260     }
261     
262     /*** Setter for property collectionPassword.
263      * @param collectionPassword New value of property collectionPassword.
264      *
265      */
266     public void setCollectionPassword(String collectionPassword) {
267         this.collectionPassword = collectionPassword;
268     }
269     
270     /*** Getter for property collectionUserName.
271      * @return Value of property collectionUserName.
272      *
273      */
274     public String getCollectionUserName() {
275         return this.collectionUserName;
276     }
277     
278     /*** Setter for property collectionUserName.
279      * @param collectionUserName New value of property collectionUserName.
280      *
281      */
282     public void setCollectionUserName(String collectionUserName) {
283         this.collectionUserName = collectionUserName;
284     }
285     
286     /*** Getter for property collectionUri.
287      * @return Value of property collectionUri.
288      *
289      */
290     public String getCollectionUri() {
291         return this.collectionUri;
292     }
293     
294     /*** Setter for property collectionUri.
295      * @param collectionUri New value of property collectionUri.
296      *
297      */
298     public void setCollectionUri(String collectionUri) {
299         this.collectionUri = collectionUri;
300     }
301     
302     /*** Getter for property xPathQueryServiceVersion.
303      * @return Value of property xPathQueryServiceVersion.
304      *
305      */
306     public String getXPathQueryServiceVersion() {
307         return this.xPathQueryServiceVersion;
308     }
309     
310     /*** Setter for property xPathQueryServiceVersion.
311      * @param xPathQueryServiceVersion New value of property xPathQueryServiceVersion.
312      *
313      */
314     public void setXPathQueryServiceVersion(String xPathQueryServiceVersion) {
315         this.xPathQueryServiceVersion = xPathQueryServiceVersion;
316     }
317     
318     /*** Getter for property numQueryResults.
319      * @return Value of property numQueryResults.
320      *
321      */
322     public long getNumQueryResults() {
323         return this.numQueryResults;
324     }
325     
326     /*** Setter for property numQueryResults.
327      * @param numQueryResults New value of property numQueryResults.
328      *
329      */
330     public void setNumQueryResults(long numQueryResults) {
331         this.numQueryResults = numQueryResults;
332     }
333     
334     /*** Getter for property query.
335      * @return Value of property query.
336      *
337      */
338     public String getQuery() {
339         return this.query;
340     }
341     
342     /*** Setter for property query.
343      * @param query New value of property query.
344      *
345      */
346     public void setQuery(String query) {
347         this.query = query;
348     }
349     
350     // Add test methods here, they have to start with 'test' name.
351     // for example:
352     // public void testHello() {}
353     
354     
355 }