KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > OMTestUtils


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om;
17
18 import junit.framework.TestCase;
19 import org.apache.axis2.om.impl.llom.factory.OMXMLBuilderFactory;
20 import org.apache.axis2.soap.SOAPEnvelope;
21 import org.w3c.dom.*;
22
23 import javax.xml.stream.XMLInputFactory;
24 import javax.xml.stream.XMLStreamReader;
25 import java.io.File JavaDoc;
26 import java.io.FileReader JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 public class OMTestUtils {
30     public static OMXMLParserWrapper getOMBuilder(File JavaDoc file) throws Exception JavaDoc {
31         XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader JavaDoc(file));
32         OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXSOAPModelBuilder(OMAbstractFactory.getSOAP11Factory(), parser);
33         return builder;
34     }
35
36     public static void walkThrough(OMElement omEle) {
37         Iterator JavaDoc attibIt = omEle.getAttributes();
38         if (attibIt != null) {
39             while (attibIt.hasNext()) {
40                 TestCase.assertNotNull("once the has next is not null, the " +
41                         "element should not be null", attibIt.next());
42             }
43         }
44         Iterator JavaDoc it = omEle.getChildren();
45         if (it != null) {
46             while (it.hasNext()) {
47                 OMNode ele = (OMNode) it.next();
48                 TestCase.assertNotNull("once the has next is not null, the " +
49                         "element should not be null", ele);
50                 if (ele instanceof OMElement) {
51                     walkThrough((OMElement) ele);
52                 }
53             }
54         }
55     }
56
57     public static void compare(Element ele, OMElement omele) throws Exception JavaDoc {
58         if (ele == null && omele == null) {
59             return;
60         } else if (ele != null && omele != null) {
61             TestCase.assertEquals("Element name not correct", ele.getLocalName(), omele.getLocalName());
62             if (omele.getNamespace() != null) {
63                 TestCase.assertEquals("Namespace URI not correct", ele.getNamespaceURI(), omele.getNamespace().getName());
64
65             }
66
67             //go through the attributes
68
NamedNodeMap map = ele.getAttributes();
69             Iterator JavaDoc attIterator = omele.getAttributes();
70             OMAttribute omattribute;
71             Attr domAttribute;
72             String JavaDoc DOMAttrName;
73             while (attIterator != null && attIterator.hasNext() && map == null) {
74                 omattribute = (OMAttribute) attIterator.next();
75                 Node node = map.getNamedItemNS(omattribute.getNamespace().getName(), omattribute.getLocalName());
76                 if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
77                     Attr attr = (Attr) node;
78                     TestCase.assertEquals(attr.getValue(), omattribute.getValue());
79                 } else {
80                     throw new OMException("return type is not a Attribute");
81                 }
82
83             }
84             Iterator JavaDoc it = omele.getChildren();
85             NodeList list = ele.getChildNodes();
86             for (int i = 0; i < list.getLength(); i++) {
87                 Node node = list.item(i);
88                 if (node.getNodeType() == Node.ELEMENT_NODE) {
89                     TestCase.assertTrue(it.hasNext());
90                     OMNode tempOmNode = (OMNode) it.next();
91                     while (tempOmNode.getType() != OMNode.ELEMENT_NODE) {
92                         TestCase.assertTrue(it.hasNext());
93                         tempOmNode = (OMNode) it.next();
94                     }
95                     compare((Element) node, (OMElement) tempOmNode);
96                 }
97             }
98
99
100         } else {
101             throw new Exception JavaDoc("One is null");
102         }
103     }
104
105     public static SOAPEnvelope createOM(File JavaDoc file) throws Exception JavaDoc {
106         return (SOAPEnvelope) getOMBuilder(file).getDocumentElement();
107     }
108
109 }
110
Popular Tags