01: // You can redistribute this software and/or modify it under the terms of
02: // the Ozone Library License version 1 published by ozone-db.org.
03: //
04: // The original code and portions created by SMB are
05: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
06: //
07: package test.xmldb.levelzero;
08:
09: import junit.framework.*;
10: import test.xmldb.*;
11:
12: import java.io.BufferedReader;
13: import java.io.FileReader;
14:
15: import org.xml.sax.InputSource;
16: import org.apache.xerces.parsers.DOMParser;
17:
18: import org.xmldb.api.modules.XMLResource;
19:
20: /**
21: * @author Per Nyfelt
22: */
23: public class StringTest extends XMLDBTestCase implements
24: LevelZeroTestConstants {
25:
26: /** Creates new StringTest */
27: public StringTest(String name) {
28: super (name);
29: }
30:
31: public static Test suite() {
32: return new TestSuite(StringTest.class);
33: }
34:
35: /**
36: * test all scenarios for using XML as text (String)
37: */
38: public void testString() {
39: try {
40: System.out
41: .println("\nLevelZeroTest.testString() - started\n");
42:
43: // read in the text file so we have something to compare with
44: BufferedReader in = new BufferedReader(new FileReader(
45: xmlFileName));
46: InputSource source = new InputSource(in);
47: DOMParser parser = new DOMParser();
48: parser.parse(source);
49: String xmlString = toString(parser.getDocument()).trim();
50: XMLResource res = insertStringDocument(id, super
51: .toString(document));
52:
53: String result = retrieveTextDocument(id).trim();
54:
55: super .assertNotNull("LevelZeroTest.testString() - result",
56: result);
57: super .assertEquals("LevelZeroTest.testString() - length",
58: xmlString.length(), result.length());
59:
60: // this fails right now since there is no clear method cleaning out previous
61: // content and append is the standard behavious for XMLContainer
62: System.out
63: .println("StringTest.testString() - updateStringDocument uncommented");
64: // updateStringDocument(id);
65:
66: } catch (Exception e) {
67: e.printStackTrace();
68: fail(e.getMessage());
69: }
70: }
71:
72: private XMLResource insertStringDocument(String id, String document)
73: throws Exception {
74: XMLResource res = (XMLResource) col.createResource(id,
75: XMLResource.RESOURCE_TYPE);
76: super .assertEquals("LevelZeroTest.testString() - id", res
77: .getId(), id);
78: super .assertSame(res.getParentCollection(), col);
79: res.setContent(document);
80: col.storeResource(res);
81: return res;
82: }
83:
84: private String retrieveTextDocument(String id) throws Exception {
85: XMLResource resource = (XMLResource) col.getResource(id);
86: return (String) resource.getContent();
87: }
88:
89: private void updateStringDocument(String id) throws Exception {
90: XMLResource resource = (XMLResource) col.getResource(id);
91: String document = (String) resource.getContent();
92:
93: //change the XML content
94: document = document.toLowerCase();
95:
96: resource.setContent(document);
97: col.storeResource(resource);
98: }
99: }
|