001: /*
002: * The XML:DB Initiative Software License, Version 1.0
003: *
004: *
005: * Copyright (c) 2000-2001 The XML:DB Initiative. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * XML:DB Initiative (http://www.xmldb.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The name "XML:DB Initiative" must not be used to endorse or
028: * promote products derived from this software without prior written
029: * permission. For written permission, please contact info@xmldb.org.
030: *
031: * 5. Products derived from this software may not be called "XML:DB",
032: * nor may "XML:DB" appear in their name, without prior written
033: * permission of the XML:DB Initiative.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the XML:DB Initiative. For more information
051: * on the XML:DB Initiative, please see <http://www.xmldb.org/>.
052: */
053: package test.xmldb.other;
054:
055: import test.xmldb.*;
056: import java.io.*;
057: import junit.framework.*;
058:
059: import org.xmldb.api.*;
060: import org.xmldb.api.base.*;
061: import org.xmldb.api.modules.*;
062:
063: public class CollectionTest extends XMLDBTestCase {
064: public CollectionTest(String name) {
065: super (name);
066: }
067:
068: public static Test suite() {
069: return new TestSuite(CollectionTest.class);
070: }
071:
072: public void testGetName() {
073: try {
074: assert (col.getName().equals("child1"));
075: } catch (Exception e) {
076: fail(e.getMessage());
077: }
078: }
079:
080: public void testGetParentCollection() {
081: try {
082: Collection parent = col.getParentCollection();
083: assert (parent.getName().equals("data"));
084: assert (parent.getParentCollection() == null);
085: } catch (Exception e) {
086: fail(e.getMessage());
087: }
088: }
089:
090: public void testGetChildCollectionCount() {
091: try {
092: assert (col.getChildCollectionCount() == 2);
093: } catch (Exception e) {
094: fail(e.getMessage());
095: }
096: }
097:
098: public void testListChildCollections() {
099: try {
100: String[] children = col.listChildCollections();
101:
102: // We don't want to assume order here
103: boolean match1 = false;
104: boolean match2 = false;
105: for (int i = 0; i < children.length; i++) {
106: String child = children[i];
107: if (child.equals("subchild1"))
108: match1 = true;
109: if (child.equals("subchild2"))
110: match2 = true;
111: }
112:
113: assert (match1 && match2);
114:
115: try {
116: assert (children[2] == null);
117: } catch (ArrayIndexOutOfBoundsException e) {
118: // we should pass if we get this exception
119: }
120: } catch (Exception e) {
121: fail(e.getMessage());
122: }
123: }
124:
125: public void testGetChildCollection() {
126: try {
127: Collection child = col.getChildCollection("subchild1");
128: assert (child.getName().equals("subchild1"));
129: } catch (Exception 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: assert (col.getResourceCount() == 4);
139: } catch (Exception e) {
140: fail(e.getMessage());
141: }
142: }
143:
144: public void testListResources() {
145: try {
146: String[] 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 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: assert (match1 && match2 && match3);
162:
163: try {
164: // TODO: should account for the fact the binary resources are
165: // optional
166: assert (resources[4] == null);
167: } catch (ArrayIndexOutOfBoundsException e) {
168: // we should pass if we get this exception
169: }
170: } catch (Exception e) {
171: fail(e.getMessage());
172: }
173: }
174:
175: public void testCreateResource() {
176: try {
177: // Test for XMLResource and explicit id
178: XMLResource res = (XMLResource) col.createResource(
179: "test4.xml", XMLResource.RESOURCE_TYPE);
180: assert (res.getResourceType()
181: .equals(XMLResource.RESOURCE_TYPE));
182: assert (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: assert (res2.getResourceType()
190: .equals(BinaryResource.RESOURCE_TYPE));
191: assert (!res2.getId().equals(""));
192:
193: // Test for unknown resource type
194: try {
195: Resource res3 = col
196: .createResource("", "GoogleResource");
197: assert (false);
198: } catch (XMLDBException e) {
199: assert (e.errorCode == ErrorCodes.UNKNOWN_RESOURCE_TYPE);
200: }
201: } catch (Exception e) {
202: fail(e.getMessage());
203: }
204: }
205:
206: public void testStoreResource() {
207: String content = "<?xml version=\"1.0\"?><tag1><tag2>value</tag2></tag1>";
208: try {
209: XMLResource res = (XMLResource) col.createResource("",
210: XMLResource.RESOURCE_TYPE);
211: res.setContent(content);
212: col.storeResource(res);
213:
214: XMLResource res2 = (XMLResource) col.getResource(res
215: .getId());
216: assert (res2 != null);
217: assert (res2.getId().equals(res.getId()));
218: assert (res2.getContent() != null);
219:
220: col.removeResource(res);
221: } catch (Exception e) {
222: fail(e.getMessage());
223: }
224: }
225:
226: public void testRemoveResource() {
227: String content = "<?xml version=\"1.0\"?><tag1><tag2>value</tag2></tag1>";
228: try {
229: // Create a new resource
230: XMLResource res = (XMLResource) col.createResource("",
231: XMLResource.RESOURCE_TYPE);
232: res.setContent(content);
233: col.storeResource(res);
234:
235: // Verify the resource exists
236: XMLResource res2 = (XMLResource) col.getResource(res
237: .getId());
238: assert (res2 != null);
239: assert (res2.getId().equals(res.getId()));
240: assert (res2.getContent() != null);
241:
242: // Remove the resource
243: col.removeResource(res2);
244: Resource res3 = col.getResource(res.getId());
245: assert (res3 == null);
246:
247: // Make sure the resource is gone.
248: try {
249: col.removeResource(res);
250: } catch (XMLDBException e) {
251: assert (e.errorCode == ErrorCodes.NO_SUCH_RESOURCE);
252: }
253: } catch (Exception e) {
254: fail(e.getMessage());
255: }
256: }
257:
258: public void testGetResource() {
259: try {
260: // Check for an XML resource
261: Resource res = col.getResource("test1.xml");
262: assert (res != null);
263: assert (res.getResourceType()
264: .equals(XMLResource.RESOURCE_TYPE));
265: assert (res.getContent() != null);
266:
267: // Check for a binary resource
268: // TODO: should account for the fact the binary resources are
269: // optional
270: res = col.getResource("image.gif");
271: assert (res != null);
272: assert (res.getResourceType()
273: .equals(BinaryResource.RESOURCE_TYPE));
274: assert (res.getContent() != null);
275:
276: // Check for a missing resource
277: res = col.getResource("missing.xml");
278: assert (res == null);
279:
280: } catch (Exception e) {
281: fail(e.getMessage());
282: }
283: }
284:
285: public void testXPathQueryService() {
286: try {
287: XPathQueryService serv = (XPathQueryService) col
288: .getService("XPathQueryService", "1.0");
289: assert (serv != null);
290:
291: ResourceIterator result = serv.query("/data");
292: int i = 0;
293: while (result.hasMoreResources()) {
294: result.nextResource();
295: i++;
296: }
297: assert (i == 3);
298:
299: result = serv
300: .query("/data/child/subchild[@name='subchild1']");
301: i = 0;
302: while (result.hasMoreResources()) {
303: result.nextResource();
304: i++;
305: }
306: assert (i == 6);
307:
308: result = serv
309: .query("/data/child/subchild/subsubchild[@name='subsubchild1']");
310: i = 0;
311: while (result.hasMoreResources()) {
312: result.nextResource();
313: i++;
314: }
315: assert (i == 1);
316: } catch (Exception e) {
317: fail(e.getMessage());
318: }
319: }
320:
321: /*
322: public void testStub() {
323: try {
324:
325: } catch (Exception e) {
326: fail( e.getMessage( ) );
327: }
328: }
329: */
330: }
|