1   /*
2    * XMLResourceTest.java
3    * JUnit based test
4    *
5    * Created on May 6, 2004, 4:20 PM
6    */
7   
8   package gov.noaa.gdsg.xmldbremote.xmldbClient;
9   
10  import junit.framework.*;
11  import org.xmldb.api.modules.XMLResource;
12  import org.xmldb.api.base.XMLDBException;
13  import org.xmldb.api.base.Collection;
14  import org.w3c.dom.Node;
15  import org.w3c.dom.Document;
16  import org.w3c.dom.Element;
17  import org.xml.sax.InputSource;
18  import javax.xml.parsers.DocumentBuilderFactory;
19  
20  import org.apache.commons.digester.Digester;
21  import org.apache.commons.digester.SetPropertiesRule;
22  
23  
24  
25  /***
26   *
27   * @author tns
28   */
29  public class XmldbClientXMLResourceTest extends TestCase {
30      
31      private XMLResource resource = null;
32      private String endPointUrl = "http://localhost:8080/mm/services/nmmrdb";
33      
34      
35      /*** Holds value of property collectionName. */
36      private String collectionName;
37      
38      /*** Holds value of property resourceId. */
39      private String resourceId;
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 documentElementName. */
51      private String documentElementName;
52      
53      public XmldbClientXMLResourceTest(java.lang.String testName) throws Exception {
54          super(testName);
55          this.gatherParameters();
56          this.loadResource();
57      }
58      
59      private void loadResource() throws XMLDBException {
60          assertNotNull("Collection URI is null", this.collectionUri);
61          assertNotNull("Collection user name is null", this.collectionUserName);
62          assertNotNull("Collectioin password is null", this.collectionPassword);
63          XmldbClientDatabase db = new XmldbClientDatabase();
64          db.setEndPointUrl(endPointUrl);
65          db.init();
66          Collection col = db.getCollection(collectionUri, collectionUserName, collectionPassword);
67          assertNotNull("Resource id is null",this.resourceId);
68          this.resource = (XMLResource) col.getResource(this.resourceId);
69          assertNotNull("Requested resource " + this.resourceId + " was not returned",this.resource);
70          
71      }
72      
73      /***Reads the testConfi.xml file for properties.*/
74      private void gatherParameters() throws Exception {
75          
76          //lets load the document.
77          java.io.InputStream inputStream = null;
78          try {
79              inputStream = this.getClass().getResourceAsStream("testConfig.xml");
80              
81              //set up the Digester
82              Digester digester = new Digester();
83              digester.push(this);
84              digester.addRule("test-config/xmlresource-test", new SetPropertiesRule());
85              digester.parse(inputStream);
86              digester.pop();
87          } finally {
88              if (inputStream != null){
89                  inputStream.close();
90              }
91          }
92      }
93      
94      public static Test suite() {
95          TestSuite suite = new TestSuite(XmldbClientXMLResourceTest.class);
96          return suite;
97      }
98      
99      /*** Test of getContentAsDOM method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
100     public void testGetContentAsDOM()  throws Exception{
101         System.out.println("testGetContentAsDOM");
102         
103         // Add your test code below by replacing the default call to fail.
104         Node node = this.resource.getContentAsDOM();
105         assertNotNull("Request for DOM returned null",node);
106         
107         assertTrue("Node is not and instance of Document",  node instanceof Document);
108         Document document = (Document) node;
109         
110         Element rootElement = document.getDocumentElement();
111         assertNotNull("Root element is null", rootElement);
112         
113         String requestedRootNodeName = rootElement.getNodeName();
114         assertNotNull("Root node name is null",requestedRootNodeName);
115         
116         assertEquals("Wrong document element name ",requestedRootNodeName, this.documentElementName);
117     }
118     
119     /*** Test of getContentAsSAX method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
120     public void testGetContentAsSAX()  throws Exception{
121         System.out.println("testGetContentAsSAX");
122         
123         // Add your test code below by replacing the default call to fail.
124         this.resource.getContentAsSAX(new org.xml.sax.helpers.DefaultHandler());
125     }
126     
127     /*** Test of getDocumentId method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
128     public void testGetDocumentId()  throws Exception{
129         System.out.println("testGetDocumentId");
130         
131         // Add your test code below by replacing the default call to fail.
132         String documentId = this.resource.getDocumentId();
133     }
134     
135     /*** Test of getResourceType method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
136     public void testGetResourceType() throws Exception {
137         System.out.println("testGetResourceType");
138         
139         // Add your test code below by replacing the default call to fail.
140         String resourceType = this.resource.getResourceType();
141         assertNotNull("Request for resource type is null",resourceType);
142         
143         assertEquals("Expected resource type of 'XMLResource'","XMLResource", resourceType);
144     }
145     
146     /*** Test of setContentAsDOM method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
147     public void testSetContentAsDOM() throws Exception {
148         System.out.println("testSetContentAsDOM");
149         
150         // Add your test code below by replacing the default call to fail.
151         //load testSetContentAsDOM.xml
152         java.io.InputStream inputStream = null;
153         try {
154             inputStream = this.getClass().getResourceAsStream("testSetContentAsDOM.xml");
155             InputSource inputSource = new InputSource(inputStream);
156             Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
157             this.resource.setContentAsDOM(document);
158         } finally {
159             if (inputStream != null){
160                 inputStream.close();
161             }
162         }
163     }
164     
165     /*** Test of setContentAsSAX method, of class gov.noaa.mm.xmldbImpl.BMSXMLResource. */
166     public void testSetContentAsSAX() throws Exception {
167         System.out.println("testSetContentAsSAX");
168         
169         // Add your test code below by replacing the default call to fail.
170         this.resource.setContentAsSAX();
171     }
172     /*** Getter for property collectionName.
173      * @return Value of property collectionName.
174      *
175      */
176     public String getCollectionName() {
177         return this.collectionName;
178     }
179     
180     /*** Setter for property collectionName.
181      * @param collectionName New value of property collectionName.
182      *
183      */
184     public void setCollectionName(String collectionName) {
185         this.collectionName = collectionName;
186     }
187     
188     /*** Getter for property resourceId.
189      * @return Value of property resourceId.
190      *
191      */
192     public String getResourceId() {
193         return this.resourceId;
194     }
195     
196     /*** Setter for property resourceId.
197      * @param resourceId New value of property resourceId.
198      *
199      */
200     public void setResourceId(String resourceId) {
201         this.resourceId = resourceId;
202     }
203     
204     /*** Getter for property resourcePassword.
205      * @return Value of property resourcePassword.
206      *
207      */
208     public String getCollectionPassword() {
209         return this.collectionPassword;
210     }
211     
212     /*** Setter for property resourcePassword.
213      * @param resourcePassword New value of property resourcePassword.
214      *
215      */
216     public void setCollectionPassword(String collectionPassword) {
217         this.collectionPassword = collectionPassword;
218     }
219     
220     /*** Getter for property resourceUserName.
221      * @return Value of property resourceUserName.
222      *
223      */
224     public String getCollectionUserName() {
225         return this.collectionUserName;
226     }
227     
228     /*** Setter for property resourceUserName.
229      * @param resourceUserName New value of property resourceUserName.
230      *
231      */
232     public void setCollectionUserName(String collectionUserName) {
233         this.collectionUserName = collectionUserName;
234     }
235     
236     /*** Getter for property collectionUri.
237      * @return Value of property collectionUri.
238      *
239      */
240     public String getCollectionUri() {
241         return this.collectionUri;
242     }
243     
244     /*** Setter for property collectionUri.
245      * @param collectionUri New value of property collectionUri.
246      *
247      */
248     public void setCollectionUri(String collectionUri) {
249         this.collectionUri = collectionUri;
250     }
251     
252     /*** Getter for property rootNodeName.
253      * @return Value of property rootNodeName.
254      *
255      */
256     public String getDocumentElementName() {
257         return this.documentElementName;
258     }
259     
260     /*** Setter for property rootNodeName.
261      * @param rootNodeName New value of property rootNodeName.
262      *
263      */
264     public void setDocumentElementName(String documentElementName) {
265         this.documentElementName = documentElementName;
266     }
267     
268     // Add test methods here, they have to start with 'test' name.
269     // for example:
270     // public void testHello() {}
271     
272     
273 }