KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > xmldb > other > CollectionTest


1 /*
2  * The XML:DB Initiative Software License, Version 1.0
3  *
4  *
5  * Copyright (c) 2000-2001 The XML:DB Initiative. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * XML:DB Initiative (http://www.xmldb.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The name "XML:DB Initiative" must not be used to endorse or
28  * promote products derived from this software without prior written
29  * permission. For written permission, please contact info@xmldb.org.
30  *
31  * 5. Products derived from this software may not be called "XML:DB",
32  * nor may "XML:DB" appear in their name, without prior written
33  * permission of the XML:DB Initiative.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the XML:DB Initiative. For more information
51  * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
52  */

53 package test.xmldb.other;
54
55 import test.xmldb.*;
56 import junit.framework.*;
57
58 import org.xmldb.api.base.*;
59 import org.xmldb.api.modules.*;
60
61 public class CollectionTest extends XMLDBTestCase {
62    public CollectionTest(String JavaDoc name) {
63       super(name);
64    }
65
66    public static Test suite() {
67       return new TestSuite(CollectionTest.class);
68    }
69
70    public void testGetName( ) {
71       try {
72          assertTrue(col.getName().equals("child1"));
73       } catch (Exception JavaDoc e) {
74          fail( e.getMessage( ) );
75       }
76    }
77
78    public void testGetParentCollection() {
79       try {
80          Collection parent = col.getParentCollection();
81          assertTrue(parent.getName().equals("data"));
82          assertTrue(parent.getParentCollection() == null);
83       } catch (Exception JavaDoc e) {
84          fail( e.getMessage( ) );
85       }
86    }
87
88    public void testGetChildCollectionCount() {
89       try {
90          assertTrue(col.getChildCollectionCount() == 2);
91       } catch (Exception JavaDoc e) {
92          fail( e.getMessage( ) );
93       }
94    }
95
96    public void testListChildCollections() {
97       try {
98          String JavaDoc[] children = col.listChildCollections();
99
100          // We don't want to assume order here
101
boolean match1 = false;
102          boolean match2 = false;
103          for ( int i = 0; i < children.length; i++ ) {
104             String JavaDoc child = children[i];
105             if ( child.equals("subchild1") )
106                match1 = true;
107             if ( child.equals("subchild2") )
108                match2 = true;
109          }
110
111          assertTrue(match1 && match2);
112
113          try {
114             assertTrue(children[2] == null);
115          }
116          catch (ArrayIndexOutOfBoundsException JavaDoc e) {
117             // we should pass if we get this exception
118
}
119       } catch (Exception JavaDoc e) {
120          fail( e.getMessage( ) );
121       }
122    }
123
124
125    public void testGetChildCollection() {
126       try {
127          Collection child = col.getChildCollection("subchild1");
128          assertTrue(child.getName().equals("subchild1"));
129       } catch (Exception JavaDoc e) {
130          fail( e.getMessage( ) );
131       }
132    }
133
134    public void testGetResourceCount() {
135       try {
136          // TODO: should account for the fact the binary resources are
137
// optional
138
assertTrue(col.getResourceCount() == 4);
139       } catch (Exception JavaDoc e) {
140          fail( e.getMessage( ) );
141       }
142    }
143
144    public void testListResources() {
145       try {
146          String JavaDoc[] resources = col.listResources();
147          // We don't want to assume order here
148
boolean match1 = false;
149          boolean match2 = false;
150          boolean match3 = false;
151          for ( int i = 0; i < resources.length; i++ ) {
152             String JavaDoc res = resources[i];
153             if ( res.equals("test1.xml") )
154                match1 = true;
155             if ( res.equals("test2.xml") )
156                match2 = true;
157             if ( res.equals("test3.xml") )
158                match3 = true;
159          }
160
161          assertTrue(match1 && match2 && match3);
162
163          try {
164             // TODO: should account for the fact the binary resources are
165
// optional
166
assertTrue(resources[4] == null);
167          }
168          catch (ArrayIndexOutOfBoundsException JavaDoc e) {
169             // we should pass if we get this exception
170
}
171       } catch (Exception JavaDoc e) {
172          fail( e.getMessage( ) );
173       }
174    }
175
176    public void testCreateResource() {
177       try {
178          // Test for XMLResource and explicit id
179
XMLResource res = (XMLResource) col.createResource("test4.xml",
180             XMLResource.RESOURCE_TYPE);
181          assertTrue(res.getResourceType().equals(XMLResource.RESOURCE_TYPE));
182          assertTrue(res.getId().equals("test4.xml"));
183
184          // Test for BinaryResource and auto generate id
185
// TODO: should account for the fact the binary resources are
186
// optional
187
BinaryResource res2 = (BinaryResource) col.createResource("",
188             BinaryResource.RESOURCE_TYPE);
189          assertTrue(res2.getResourceType().equals(BinaryResource.RESOURCE_TYPE));
190          assertTrue(! res2.getId().equals(""));
191
192          // Test for unknown resource type
193
try {
194             Resource res3 = col.createResource("", "GoogleResource");
195             assertTrue(false);
196          }
197          catch (XMLDBException e) {
198             assertTrue(e.errorCode == ErrorCodes.UNKNOWN_RESOURCE_TYPE);
199          }
200       } catch (Exception JavaDoc e) {
201          fail( e.getMessage( ) );
202       }
203    }
204
205
206    public void testStoreResource() {
207       String JavaDoc content = "<?xml version=\"1.0\"?><tag1><tag2>value</tag2></tag1>";
208       try {
209          XMLResource res = (XMLResource) col.createResource("", XMLResource.RESOURCE_TYPE);
210          res.setContent(content);
211          col.storeResource(res);
212
213          XMLResource res2 = (XMLResource) col.getResource(res.getId());
214          assertTrue(res2 != null);
215          assertTrue(res2.getId().equals(res.getId()));
216          assertTrue(res2.getContent() != null);
217
218          col.removeResource(res);
219       } catch (Exception JavaDoc e) {
220          fail( e.getMessage( ) );
221       }
222    }
223
224    public void testRemoveResource() {
225       String JavaDoc content = "<?xml version=\"1.0\"?><tag1><tag2>value</tag2></tag1>";
226       try {
227          // Create a new resource
228
XMLResource res = (XMLResource) col.createResource("", XMLResource.RESOURCE_TYPE);
229          res.setContent(content);
230          col.storeResource(res);
231
232          // Verify the resource exists
233
XMLResource res2 = (XMLResource) col.getResource(res.getId());
234          assertTrue(res2 != null);
235          assertTrue(res2.getId().equals(res.getId()));
236          assertTrue(res2.getContent() != null);
237
238          // Remove the resource
239
col.removeResource(res2);
240          Resource res3 = col.getResource(res.getId());
241          assertTrue(res3 == null);
242
243          // Make sure the resource is gone.
244
try {
245             col.removeResource(res);
246          }
247          catch (XMLDBException e) {
248             assertTrue(e.errorCode == ErrorCodes.NO_SUCH_RESOURCE);
249          }
250       } catch (Exception JavaDoc e) {
251          fail( e.getMessage( ) );
252       }
253    }
254
255    public void testGetResource() {
256       try {
257          // Check for an XML resource
258
Resource res = col.getResource("test1.xml");
259          assertTrue(res != null);
260          assertTrue(res.getResourceType().equals(XMLResource.RESOURCE_TYPE));
261          assertTrue(res.getContent() != null);
262
263          // Check for a binary resource
264
// TODO: should account for the fact the binary resources are
265
// optional
266
res = col.getResource("image.gif");
267          assertTrue(res != null);
268          assertTrue(res.getResourceType().equals(BinaryResource.RESOURCE_TYPE));
269          assertTrue(res.getContent() != null);
270
271          // Check for a missing resource
272
res = col.getResource("missing.xml");
273          assertTrue(res == null);
274
275       } catch (Exception JavaDoc e) {
276          fail( e.getMessage( ) );
277       }
278    }
279
280    public void testXPathQueryService() {
281       try {
282          XPathQueryService serv =
283             (XPathQueryService) col.getService("XPathQueryService", "1.0");
284          assertTrue(serv != null);
285
286          ResourceIterator result = serv.query("/data");
287          int i = 0;
288          while (result.hasMoreResources()) {
289             result.nextResource();
290             i++;
291          }
292          assertTrue(i == 3);
293
294          result = serv.query("/data/child/subchild[@name='subchild1']");
295          i = 0;
296          while (result.hasMoreResources()) {
297             result.nextResource();
298             i++;
299          }
300          assertTrue(i == 6);
301
302          result = serv.query("/data/child/subchild/subsubchild[@name='subsubchild1']");
303          i = 0;
304          while (result.hasMoreResources()) {
305             result.nextResource();
306             i++;
307          }
308          assertTrue(i == 1);
309       } catch (Exception JavaDoc e) {
310          fail( e.getMessage( ) );
311       }
312    }
313
314    /*
315    public void testStub() {
316       try {
317
318       } catch (Exception e) {
319          fail( e.getUserName( ) );
320       }
321    }
322    */

323 }
324
Popular Tags