KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > BasicTests


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: BasicTests.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.lazydom;
25
26 import java.io.File JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import junit.framework.Test;
30
31 import org.enhydra.xml.driver.TestDomOps;
32 import org.enhydra.xml.driver.TestException;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 /**
37  * Basic tests of Lazy DOM. The XMLC tests do good job of testing a lot
38  * of the LazyDOM. This concentrates on corner cases and bug regression.
39  * There really needsto be a lot more here.
40  */

41 public class BasicTests extends LazyDOMTestCaseBase {
42     /** Input files */
43     private static final String JavaDoc INSULIN3_XML_DIR = "../xmlc/xml";
44     private static final String JavaDoc INSULIN3_XML = "bioml/insulin3.bioml";
45
46     /** DOM builder and parser */
47     private LazyDOMBuilder fBuilder
48         = new LazyDOMBuilder(getMsgWriter(),
49                              org.enhydra.xml.xmlc.dom.lazydom.LazyDomFactory.class);
50
51     /** Factory method to create suite of these tests */
52     public static Test suite() {
53         return createSuite(BasicTests.class, null);
54     }
55     
56     /** Constructor */
57     public BasicTests(Method JavaDoc method) {
58         super(method);
59     }
60
61     /** Create an unexpanded document from insulin3.bioml */
62     private LazyDocument parseInsulin3() {
63         File JavaDoc insulin3 = getOtherInputFile(INSULIN3_XML_DIR, INSULIN3_XML);
64         return fBuilder.parseUnexpanded(insulin3);
65     }
66
67     /**
68      * Test 1: Simple test on insulin3.bioml
69      */

70     public void test1() {
71         LazyDocument lazyDoc = parseInsulin3();
72         printDiffDom("Unexpanded Lazy DOM", lazyDoc, "unexpanded.dom");
73         TestDomOps.expandTree(lazyDoc);
74         printDiffDom("Expanded Lazy DOM", lazyDoc, "expanded.dom");
75     }
76
77     /**
78      * Basic modifications on insulin3.bioml,
79      */

80     public void test2() {
81         LazyDocument lazyDoc = parseInsulin3();
82         DocInfo docInfo = new DocInfo(lazyDoc);
83         
84         // FIXME: this was disabled
85
// Add a text node to an element.
86
Element JavaDoc lazyElem = docInfo.getByAttrValue(lazyDoc, "label",
87                                                   "HUMINS locus");
88         lazyElem.appendChild(lazyDoc.createTextNode("@ new node #1 @"));
89
90         // Add a attribute
91
lazyElem = docInfo.getByAttrValue(lazyDoc, "end", "4992");
92         lazyElem.setAttribute("newAttr", "new value");
93
94         // Expand the parent
95
lazyElem = docInfo.getByAttrValue(lazyDoc, "format", "PIR");
96
97         Node JavaDoc parent = (Element JavaDoc)lazyElem.getParentNode();
98         String JavaDoc name = parent.getNodeName(); // Make sure we got something
99

100         printDiffDom("Modified, unexpanded Lazy DOM", lazyDoc,
101                      "unexpanded.dom");
102         TestDomOps.expandTree(lazyDoc);
103         printDiffDom("Modified, expanded Lazy DOM", lazyDoc,
104                      "expanded.dom");
105     }
106
107     /**
108      * Test 3: cloning of attribute on shallow clone. This failed at one
109      * time.
110      */

111     public void test3() {
112         String JavaDoc tagName = "organism";
113         LazyDocument lazyDoc = parseInsulin3();
114         DocInfo docInfo = new DocInfo(lazyDoc);
115
116         // Find an Element, without attributes expanded
117
LazyElement elem = (LazyElement)getElementByTagName(lazyDoc, lazyDoc, tagName);
118         if (elem == null) {
119             throw new TestException("element null for tag: " + tagName);
120         }
121         if (elem.areAttributesExpanded()) {
122             throw new TestException("attributes should not be expanded");
123         }
124         LazyElement cloneElem = (LazyElement)elem.cloneNode(false);
125         if (!elem.areAttributesExpanded()) {
126             new TestException("attributes should be expanded");
127         }
128         String JavaDoc labelValue = cloneElem.getAttribute("label");
129         if ((labelValue == null) || !labelValue.equals("Homo sapiens (human)")) {
130             throw new TestException("attribute \"label\" has wrong value: \""
131                                     + labelValue + "\"");
132         }
133     }
134 }
135
Popular Tags