KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > xmldb > levelzero > StringTest


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package test.xmldb.levelzero;
8
9 import junit.framework.*;
10 import test.xmldb.*;
11
12 import java.io.BufferedReader JavaDoc;
13 import java.io.FileReader JavaDoc;
14
15 import org.xml.sax.InputSource JavaDoc;
16 import org.apache.xerces.parsers.DOMParser;
17 import org.apache.log4j.Logger;
18
19 import org.xmldb.api.modules.XMLResource;
20 /**
21  * @author Per Nyfelt
22  */

23 public class StringTest extends XMLDBTestCase implements LevelZeroTestConstants {
24
25     Logger logger = Logger.getLogger(StringTest.class);
26
27     /** Creates new StringTest */
28     public StringTest(String JavaDoc name) {
29         super(name);
30     }
31
32     public static Test suite() {
33         return new TestSuite(StringTest.class);
34     }
35
36    /**
37      * test all scenarios for using XML as text (String)
38      */

39     public void testString() {
40         try {
41             logger.debug("\nLevelZeroTest.testString() - started\n");
42
43             // read in the text file so we have something to compare with
44
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(xmlFileName));
45             InputSource JavaDoc source = new InputSource JavaDoc(in);
46             DOMParser parser = new DOMParser();
47             parser.parse(source);
48             String JavaDoc xmlString = toString(parser.getDocument()).trim();
49             XMLResource res = insertStringDocument(id, super.toString(document));
50
51             String JavaDoc result = retrieveTextDocument(id).trim();
52
53             super.assertNotNull("LevelZeroTest.testString() - result", result);
54             super.assertEquals("LevelZeroTest.testString() - length", xmlString.length(), result.length());
55
56             // this fails right now since there is no clear method cleaning out previous
57
// content and append is the standard behavious for XMLContainer
58
updateStringDocument(id);
59
60         } catch (Exception JavaDoc e) {
61             e.printStackTrace();
62             fail( e.getMessage( ) );
63         }
64    }
65
66    private XMLResource insertStringDocument(String JavaDoc id, String JavaDoc document) throws Exception JavaDoc {
67         XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
68         super.assertEquals("LevelZeroTest.testString() - id",res.getId(), id);
69         super.assertSame(res.getParentCollection(),col);
70         res.setContent(document);
71         col.storeResource(res);
72         return res;
73    }
74
75    private String JavaDoc retrieveTextDocument(String JavaDoc id) throws Exception JavaDoc {
76         XMLResource resource = (XMLResource) col.getResource(id);
77         return (String JavaDoc)resource.getContent();
78     }
79
80    private void updateStringDocument(String JavaDoc id) throws Exception JavaDoc {
81         XMLResource resource = (XMLResource) col.getResource(id);
82         String JavaDoc document = (String JavaDoc)resource.getContent();
83
84         //change the XML content
85
document = document.toLowerCase();
86
87         resource.setContent(document);
88         col.storeResource(resource);
89    }
90 }
91
Popular Tags