1   /*
2    * GenericXPathQueryServiceTest.java
3    * JUnit based test
4    *
5    * Created on December 21, 2004, 3:18 PM
6    */
7   
8   package gov.noaa.eds.xapi.generic.modules;
9   
10  import junit.framework.*;
11  import gov.noaa.eds.xapi.generic.*;
12  import org.w3c.dom.Document;
13  import org.w3c.dom.Node;
14  import org.xmldb.api.base.Resource;
15  import org.xmldb.api.base.ResourceSet;
16  import org.xmldb.api.modules.XMLResource;
17  
18  /***
19   *
20   * @author tns
21   */
22  public class GenericXPathQueryServiceTest extends TestCase {
23      
24      GenericXPathQueryService service = null;
25      GenericCollection collection = null;
26      String resourceId = "1";
27      
28      public GenericXPathQueryServiceTest(String testName) {
29          super(testName);
30      }
31  
32      protected void setUp() throws java.lang.Exception {
33          this.service = new GenericXPathQueryService();
34          collection = new GenericCollection();
35          Document resourceDocument = GenericResourceTest.loadTestResource();
36          GenericXmlNodeResource resource = new GenericXmlNodeResource(resourceDocument);
37          resource.setId(resourceId);
38          collection.storeResource(resource);
39          service.setCollection(collection);
40          
41      }
42  
43      protected void tearDown() throws java.lang.Exception {
44      }
45  
46      public static junit.framework.Test suite() {
47          junit.framework.TestSuite suite = new junit.framework.TestSuite(GenericXPathQueryServiceTest.class);
48          
49          return suite;
50      }
51  
52      /***
53       * Test of removeNamespace method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
54       */
55      public void testRemoveNamespace() {
56          System.out.println("testRemoveNamespace");
57          try {
58              service.removeNamespace("");
59              fail("Exception expected");
60          } catch (UnsupportedOperationException expected){
61              //expected
62          }
63          
64          // TODO add your test code below by replacing the default call to fail.
65      }
66  
67      /***
68       * Test of query method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
69       */
70      public void testTitleQuery() throws Exception {
71          System.out.println("testQuery");
72          String titleQuery = "/root/title";
73          ResourceSet resourceSet = service.query(titleQuery);
74          assertNotNull("resource set should not be null",resourceSet);
75          assertEquals("Unexpected number of results",1,resourceSet.getSize());
76          Resource resource = resourceSet.getResource(0);
77          assertNotNull("expected a valid resource",resource);
78          assertTrue("expected an XMLResource", resource instanceof XMLResource);
79          XMLResource xResource = (XMLResource) resource;
80          Node node = xResource.getContentAsDOM();
81          assertNotNull("valid node expected",node);
82          assertEquals("unexpected node name","title",node.getNodeName());
83          Node text = node.getFirstChild();
84          assertNotNull("text expected",text);
85          assertEquals("text type expected","#text",text.getNodeName());
86          String val = text.getNodeValue();
87          assertNotNull("value expected",val);
88          assertEquals("unexpected value","A Test Record",val);
89          
90          // TODO add your test code below by replacing the default call to fail.
91          
92      }
93      
94      public void testTitleToDocumentQuery() throws Exception {
95          //String titleQuery = "/root/title[text() = 'A Test Record']/parent::root/parent::*";
96          String titleQuery = "/root/title[text() = 'A Test Record']/parent::root/parent::node()";
97          ResourceSet resourceSet = service.query(titleQuery);
98          assertNotNull("resource set should not be null",resourceSet);
99          assertEquals("Unexpected number of results",1,resourceSet.getSize());
100         Resource resource = resourceSet.getResource(0);
101         assertNotNull("expected a valid resource",resource);
102         assertTrue("expected an XMLResource", resource instanceof XMLResource);
103         XMLResource xResource = (XMLResource) resource;
104         Node node = xResource.getContentAsDOM();
105         assertNotNull("valid node expected",node);
106         assertEquals("unexpected node type","#document",node.getNodeName());
107         
108     }
109 
110     /***
111      * Test of getNamespace method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
112      */
113     public void testGetNamespace() {
114         System.out.println("testGetNamespace");
115         
116         // TODO add your test code below by replacing the default call to fail.
117         try {
118             service.getNamespace("");
119             fail("Exception expected");
120         } catch (UnsupportedOperationException expected){
121             //expected
122         }
123     }
124 
125     /***
126      * Test of setCollection method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
127      */
128     public void testSetCollection() {
129         System.out.println("testSetCollection");
130         
131         // TODO add your test code below by replacing the default call to fail.
132         GenericXPathQueryService service = new GenericXPathQueryService();
133         service.setCollection(this.collection);
134     }
135 
136     /***
137      * Test of setNamespace method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
138      */
139     public void testSetNamespace() {
140         System.out.println("testSetNamespace");
141         
142         // TODO add your test code below by replacing the default call to fail.
143         try {
144             service.setNamespace("","");
145             fail("Exception expected");
146         } catch (UnsupportedOperationException e){
147             //expected
148         }
149     }
150 
151     /***
152      * Test of queryResource method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
153      */
154     public void testQueryResource() throws Exception {
155         System.out.println("testQueryResource");
156         
157         // TODO add your test code below by replacing the default call to fail.
158         try {
159             service.queryResource("","");
160             fail("Exception expected");
161         } catch (UnsupportedOperationException e){
162             //expected
163         }
164     }
165 
166     /***
167      * Test of getVersion method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
168      */
169     public void testGetVersion() {
170         System.out.println("testGetVersion");
171         
172         // TODO add your test code below by replacing the default call to fail.
173         String v = service.getVersion();
174         assertNotNull("expected valid version",v);
175         assertEquals("unexpected version","1.0",v);
176     }
177 
178     /***
179      * Test of getName method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
180      */
181     public void testGetName() {
182         System.out.println("testGetName");
183         
184         // TODO add your test code below by replacing the default call to fail.
185         String n = service.getName();
186         assertNotNull("expected valid name",n);
187         assertEquals("unexpected name","XPathQueryService",n);
188     }
189 
190     /***
191      * Test of clearNamespaces method, of class gov.noaa.eds.xapi.generic.modules.GenericXPathQueryService.
192      */
193     public void testClearNamespaces() {
194         System.out.println("testClearNamespaces");
195         
196         // TODO add your test code below by replacing the default call to fail.
197         try {
198             service.clearNamespaces();
199             fail("Exception expected");
200         } catch (UnsupportedOperationException e){
201             //expected
202         }
203     }
204     
205     // TODO add test methods here. The name must begin with 'test'. For example:
206     // public void testHello() {}
207     
208 }