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.*;
12  import org.xmldb.api.DatabaseManager;
13  import org.xmldb.api.modules.*;
14  import java.util.Iterator;
15  
16  import org.apache.commons.digester.Digester;
17  import org.apache.commons.digester.SetPropertiesRule;
18  
19  
20  /***
21   *
22   * @author tns
23   */
24  public class XmldbClientResourceIteratorTest extends TestCase {
25      
26      private ResourceIterator resouceIterator = null;
27      
28      private String endPointUrl = "http://localhost:8080/mm/services/nmmrdb";
29      
30      
31      /*** Holds value of property collectionName. */
32      private String collectionName;
33      
34      /*** Holds value of property collectionPassword. */
35      private String collectionPassword;
36      
37      /*** Holds value of property collectionUserName. */
38      private String collectionUserName;
39      
40      /*** Holds value of property collectionUri. */
41      private String collectionUri;
42      
43      /*** Holds value of property xPathQueryServiceVersion. */
44      private String xPathQueryServiceVersion = "1.0";
45      
46      /*** Holds value of property numQueryResults. */
47      private long numQueryResults;
48      
49      /*** Holds value of property query. */
50      private String query;
51      
52      
53      
54      public XmldbClientResourceIteratorTest(java.lang.String testName) {
55          super(testName);
56      }
57      
58      public void setUp() throws Exception{
59          this.gatherParameters();
60          this.loadResourceIterator();
61      }
62      
63      /***Reads the testConfi.xml file for properties.*/
64      private void gatherParameters() throws Exception {
65          
66          
67          java.io.InputStream inputStream = null;
68          try {
69              inputStream = this.getClass().getResourceAsStream("testConfig.xml");
70              
71              
72              Digester digester = new Digester();
73              digester.push(this);
74              digester.addRule("test-config/resourceIterator", new SetPropertiesRule());
75              digester.parse(inputStream);
76              digester.pop();
77          } finally {
78              if (inputStream != null){
79                  inputStream.close();
80              }
81          }
82      }
83      
84      private void loadResourceIterator() throws XMLDBException {
85          assertNotNull("Collection URI is null", this.collectionUri);
86          assertNotNull("Collection user name is null", this.collectionUserName);
87          assertNotNull("Collectioin password is null", this.collectionPassword);
88          
89          XmldbClientDatabase db = new XmldbClientDatabase();
90          db.setEndPointUrl(endPointUrl);
91          db.init();
92          Collection col = db.getCollection(collectionUri, collectionUserName, collectionPassword);
93          assertNotNull("Request collection was not returned",col);
94          assertNotNull("Param xPathQueryServiceVersion is null",xPathQueryServiceVersion);
95          Service aService = col.getService("XPathQueryService", this.xPathQueryServiceVersion);
96          assertNotNull("Service returned is null, requested 'XPathQueryService' version:" + this.xPathQueryServiceVersion, aService);
97          assertTrue("The returned service is not of type 'XPathQueryService' it is "
98          + aService.getClass().getName(), aService instanceof XPathQueryService);
99          XPathQueryService service = (XPathQueryService) aService;
100         assertNotNull("Query unspecified", this.query);
101         ResourceSet resourceSet = service.query(this.query);
102         assertNotNull("Resource set is null",resourceSet);
103         
104         this.resouceIterator = resourceSet.getIterator();
105         assertNotNull("Resource iterator is null",this.resouceIterator);
106     }
107     
108     
109     public static Test suite() {
110         TestSuite suite = new TestSuite(XmldbClientResourceIteratorTest.class);
111         return suite;
112     }
113     
114     /*** Test of hasMoreResources method, of class gov.noaa.mm.xmldbImpl.BMSResourceIterator. */
115     public void testHasMoreResources() throws Exception {
116         System.out.println("testHasMoreResources");
117         
118         
119         for (int i = 0; i < this.numQueryResults; i++){
120             assertTrue("iterator should have more resources at index:" + i, this.resouceIterator.hasMoreResources());
121             this.resouceIterator.nextResource();
122         }
123         assertFalse("iterator should be out of resources", this.resouceIterator.hasMoreResources());
124     }
125     
126     /*** Test of nextResource method, of class gov.noaa.mm.xmldbImpl.BMSResourceIterator. */
127     public void testNextResource() throws Exception {
128         System.out.println("testNextResource");
129         
130         
131         for (int i = 0; i < this.numQueryResults; i++){
132             Resource resource = this.resouceIterator.nextResource();
133             assertNotNull("Iterator returned null resource at index:" + i, resource);
134         }
135         try {
136             assertNull("iterator should be out of resources", this.resouceIterator.nextResource());
137             fail("error not thrown when requesting an elemenent iteration that does not exist");
138         } catch (Exception e){
139             assertTrue("Did not throw appropriate error when requesting an element iteratio that does not exist"
140             , e instanceof XMLDBException);
141             
142             XMLDBException dbE = (XMLDBException) e;
143             assertEquals("Wrong error code when requesting an element iteration that does not exist",
144             ErrorCodes.NO_SUCH_RESOURCE, dbE.errorCode);
145         }
146     }
147     
148     
149     
150     
151     
152     public String getCollectionName() {
153         return this.collectionName;
154     }
155     
156     /*** Setter for property collectionName.
157      * @param collectionName New value of property collectionName.
158      *
159      */
160     public void setCollectionName(String collectionName) {
161         this.collectionName = collectionName;
162     }
163     
164     /*** Getter for property collectionPassword.
165      * @return Value of property collectionPassword.
166      *
167      */
168     public String getCollectionPassword() {
169         return this.collectionPassword;
170     }
171     
172     /*** Setter for property collectionPassword.
173      * @param collectionPassword New value of property collectionPassword.
174      *
175      */
176     public void setCollectionPassword(String collectionPassword) {
177         this.collectionPassword = collectionPassword;
178     }
179     
180     /*** Getter for property collectionUserName.
181      * @return Value of property collectionUserName.
182      *
183      */
184     public String getCollectionUserName() {
185         return this.collectionUserName;
186     }
187     
188     /*** Setter for property collectionUserName.
189      * @param collectionUserName New value of property collectionUserName.
190      *
191      */
192     public void setCollectionUserName(String collectionUserName) {
193         this.collectionUserName = collectionUserName;
194     }
195     
196     /*** Getter for property collectionUri.
197      * @return Value of property collectionUri.
198      *
199      */
200     public String getCollectionUri() {
201         return this.collectionUri;
202     }
203     
204     /*** Setter for property collectionUri.
205      * @param collectionUri New value of property collectionUri.
206      *
207      */
208     public void setCollectionUri(String collectionUri) {
209         this.collectionUri = collectionUri;
210     }
211     
212     /*** Getter for property xPathQueryServiceVersion.
213      * @return Value of property xPathQueryServiceVersion.
214      *
215      */
216     public String getXPathQueryServiceVersion() {
217         return this.xPathQueryServiceVersion;
218     }
219     
220     /*** Setter for property xPathQueryServiceVersion.
221      * @param xPathQueryServiceVersion New value of property xPathQueryServiceVersion.
222      *
223      */
224     public void setXPathQueryServiceVersion(String xPathQueryServiceVersion) {
225         this.xPathQueryServiceVersion = xPathQueryServiceVersion;
226     }
227     
228     /*** Getter for property numQueryResults.
229      * @return Value of property numQueryResults.
230      *
231      */
232     public long getNumQueryResults() {
233         return this.numQueryResults;
234     }
235     
236     /*** Setter for property numQueryResults.
237      * @param numQueryResults New value of property numQueryResults.
238      *
239      */
240     public void setNumQueryResults(long numQueryResults) {
241         this.numQueryResults = numQueryResults;
242     }
243     
244     /*** Getter for property query.
245      * @return Value of property query.
246      *
247      */
248     public String getQuery() {
249         return this.query;
250     }
251     
252     /*** Setter for property query.
253      * @param query New value of property query.
254      *
255      */
256     public void setQuery(String query) {
257         this.query = query;
258     }
259     
260 }