KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.axis2.soap.SOAPEnvelope;
19 import org.apache.axis2.soap.SOAPFactory;
20 import org.apache.axis2.soap.impl.llom.builder.StAXSOAPModelBuilder;
21
22 import javax.xml.stream.XMLInputFactory;
23 import javax.xml.stream.XMLStreamReader;
24 import java.io.File JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 /**
29  * This test case tests the basic expectations of the engine from the OM.
30  */

31 public class OMTest extends AbstractTestCase {
32     private SOAPEnvelope envelope;
33     private SOAPFactory fac;
34
35     /**
36      * Constructor.
37      */

38     public OMTest(String JavaDoc testName) {
39         super(testName);
40     }
41
42     protected void setUp() throws Exception JavaDoc {
43         File JavaDoc file = getTestResourceFile("soap/sample1.xml");
44         XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileReader JavaDoc(file));
45         fac = OMAbstractFactory.getSOAP11Factory();
46         OMXMLParserWrapper builder = new StAXSOAPModelBuilder(parser);
47         envelope = (SOAPEnvelope) builder.getDocumentElement();
48     }
49
50     /**
51      * Sometime the hasNext() in the childeren iterator is true yet the next() is null
52      */

53     public void testNullInChilderen() {
54         isNullChildrenThere(envelope);
55     }
56
57     /**
58      * the envelope is completly namesapce qulified so all the OMElements got to have namespace values not null
59      */

60     public void test4MissingNamespaces() {
61         isNameSpacesMissing(envelope);
62     }
63
64     public void isNullChildrenThere(OMElement omeleent) {
65         Iterator JavaDoc it = omeleent.getChildren();
66         while (it.hasNext()) {
67             OMNode node = (OMNode) it.next();
68             assertNotNull(node);
69             if (node.getType() == OMNode.ELEMENT_NODE) {
70                 isNullChildrenThere((OMElement) node);
71             }
72         }
73     }
74
75     public void isNameSpacesMissing(OMElement omeleent) {
76         OMNamespace omns = omeleent.getNamespace();
77         assertNotNull(omns);
78         assertNotNull(omns.getName());
79         Iterator JavaDoc it = omeleent.getChildren();
80         while (it.hasNext()) {
81             OMNode node = (OMNode) it.next();
82             if (node != null && node.getType() == OMNode.ELEMENT_NODE) {
83                 isNameSpacesMissing((OMElement) node);
84             }
85         }
86     }
87
88     public void testRootNotCompleteInPartialBuild() throws Exception JavaDoc {
89         assertFalse("Root should not be complete", envelope.isComplete());
90     }
91
92     /**
93      * Assumption - The fed XML has at least two children under the root element
94      *
95      * @throws Exception
96      */

97     public void testFirstChildDetach() throws Exception JavaDoc {
98         OMElement root = envelope;
99         assertFalse("Root should not be complete", root.isComplete());
100         OMNode oldFirstChild = root.getFirstChild();
101         assertNotNull(oldFirstChild);
102         oldFirstChild.detach();
103         OMNode newFirstChild = root.getFirstChild();
104         assertNotNull(newFirstChild);
105         assertNotSame(oldFirstChild, newFirstChild);
106     }
107
108     //todo this is wrong correct this
109
public void testAdditionOfaCompletelyNewElement() throws Exception JavaDoc {
110
111         // OMElement root= envelope;
112
//
113
// OMNamespace soapenv= root.findNamespace("http://schemas.xmlsoap.org/soap/envelope/", "soapenv");
114
// OMNamespace wsa= root.findNamespace("http://schemas.xmlsoap.org/ws/2004/03/addressing", "wsa");
115
// if (wsa==null)
116
// wsa= root.declareNamespace("http://schemas.xmlsoap.org/ws/2004/03/addressing", "wsa");
117
//
118
// //Assumption - A RelatesTo Element does not exist in the input document
119
// OMElement relatesTo= fac.createOMElement ("RelatesTo", wsa);
120
// relatesTo.addAttribute(fac.createOMAttribute("RelationshipType", null, "wsa:Reply", relatesTo));
121
// relatesTo.addAttribute(fac.createOMAttribute("mustUnderstand", soapenv, "0", relatesTo));
122
// relatesTo.addChild(fac.createText(relatesTo, "uuid:3821F4F0-D020-11D8-A10A-E4EE6425FCB0"));
123
// relatesTo.setComplete(true);
124
//
125
// root.addChild(relatesTo);
126
//
127
// QName name = new QName(wsa.getName(),"RelatesTo",wsa.getPrefix());
128
//
129
// Iterator children = root.getChildrenWithName(name);
130
// //this should contain only one child!
131
// if (children.hasNext()){
132
// OMElement newlyAddedElement = (OMElement)children.next();
133
//
134
// assertNotNull(newlyAddedElement);
135
//
136
// assertEquals(newlyAddedElement.getLocalName(),"RelatesTo");
137
// //todo put the other assert statements here
138
// }else{
139
// assertFalse("New child not added",true);
140
// }
141

142     }
143 }
144
Popular Tags