KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > upgrade > Upgrade1_0To1_1Test


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

17
18 package org.apache.geronimo.upgrade;
19
20
21 import java.net.URL JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.StringWriter JavaDoc;
27 import java.io.Writer JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 import junit.framework.TestCase;
32 import org.apache.xmlbeans.XmlObject;
33 import org.apache.xmlbeans.XmlCursor;
34 import org.apache.xmlbeans.XmlException;
35
36 /**
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class Upgrade1_0To1_1Test extends TestCase {
40
41     private final ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
42
43     public void test1() throws Exception JavaDoc {
44         test("appclient_ejb_1");
45     }
46
47     public void test2() throws Exception JavaDoc {
48         test("appclient_dep_1");
49     }
50
51     public void test3() throws Exception JavaDoc {
52         test("transport_1");
53     }
54
55     public void test4() throws Exception JavaDoc {
56         test("transport_2");
57     }
58
59     public void test5() throws Exception JavaDoc {
60         test("assembly_1");
61     }
62
63     public void test6() throws Exception JavaDoc {
64         try {
65             test("ejb_pkgen_1");
66             fail();
67         } catch (XmlException e) {
68
69         }
70     }
71
72     public void test7() throws Exception JavaDoc {
73         test("servlet_1");
74     }
75
76     public void test8() throws Exception JavaDoc {
77         test("gbean_1");
78     }
79
80     private void test(String JavaDoc testName) throws Exception JavaDoc {
81         InputStream JavaDoc srcXml = classLoader.getResourceAsStream(testName + ".xml");
82         try {
83             Writer JavaDoc targetXml = new StringWriter JavaDoc();
84             new Upgrade1_0To1_1().upgrade(srcXml, targetXml);
85         
86             String JavaDoc targetString = targetXml.toString();
87             XmlObject targetXmlObject = XmlObject.Factory.parse(targetString);
88             URL JavaDoc expectedOutputXml = classLoader.getResource(testName + "_result.xml");
89             XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
90             List JavaDoc problems = new ArrayList JavaDoc();
91             boolean ok = compareXmlObjects(targetXmlObject, expected, problems);
92             if (!ok) {
93                 System.out.println(targetString);
94             }
95             assertTrue("Differences: " + problems, ok);
96         } finally {
97             if (srcXml != null)
98             {
99                 try {
100                     srcXml.close();
101                 } catch (IOException JavaDoc ignored) {
102                     // ignored
103
}
104             }
105         }
106     }
107
108     private boolean compareXmlObjects(XmlObject xmlObject, XmlObject expectedObject, List JavaDoc problems) {
109         XmlCursor test = xmlObject.newCursor();
110         XmlCursor expected = expectedObject.newCursor();
111         boolean similar = true;
112         int elementCount = 0;
113         while (toNextStartToken(test)) {
114             elementCount++;
115             if (!toNextStartToken(expected)) {
116                 problems.add("test longer than expected at element: " + elementCount);
117                 return false;
118             }
119             QName JavaDoc actualQName = test.getName();
120             QName JavaDoc expectedQName = expected.getName();
121             if (!actualQName.equals(expectedQName)) {
122                 problems.add("Different elements at elementCount: " + elementCount + ", test: " + actualQName + ", expected: " + expectedQName);
123                 similar = false;
124             }
125             test.toNextToken();
126             expected.toNextToken();
127         }
128         if (toNextStartToken(expected)) {
129             problems.add("test shorter that expected at element: " + elementCount);
130             similar = false;
131         }
132         return similar;
133     }
134
135     private boolean toNextStartToken(XmlCursor cursor) {
136         while (!cursor.isStart()) {
137             if (!cursor.hasNextToken()) {
138                 return false;
139             }
140             cursor.toNextToken();
141         }
142         return true;
143     }
144
145 }
146
Popular Tags