KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > web > deployment > GenericToSpecificPlanConverterTest


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 package org.apache.geronimo.web.deployment;
18
19 import java.net.URL JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.apache.xmlbeans.XmlObject;
27 import org.apache.xmlbeans.XmlCursor;
28 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 /**
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  */

36 public class GenericToSpecificPlanConverterTest extends TestCase {
37     private static final Log log = LogFactory.getLog(GenericToSpecificPlanConverterTest.class);
38     
39     private ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
40
41     public void testConvertPlan1() throws Exception JavaDoc {
42         testConvertPlan("plans/tomcat-pre.xml", "plans/tomcat-post.xml");
43     }
44     public void testConvertPlan2() throws Exception JavaDoc {
45         testConvertPlan("plans/tomcat-pre2.xml", "plans/tomcat-post.xml");
46     }
47     public void testConvertPlan3() throws Exception JavaDoc {
48         testConvertPlan("plans/tomcat-pre3.xml", "plans/tomcat-post.xml");
49     }
50     public void testConvertPlanMessageDestination1() throws Exception JavaDoc {
51         testConvertPlan("plans/web-md-pre.xml", "plans/web-md-post.xml");
52     }
53
54     public void testConvertPlan(String JavaDoc prePlanName, String JavaDoc postPlanName) throws Exception JavaDoc {
55         URL JavaDoc srcXml = classLoader.getResource(prePlanName);
56         URL JavaDoc expectedOutputXml = classLoader.getResource(postPlanName);
57         XmlObject rawPlan = XmlBeansUtil.parse(srcXml, getClass().getClassLoader());
58         log.debug("RAW PLAN " + rawPlan.toString());
59         
60         XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
61         XmlObject webPlan = new GenericToSpecificPlanConverter("http://geronimo.apache.org/xml/ns/web/tomcat/config-1.0",
62                 "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-1.2", "tomcat").convertToSpecificPlan(rawPlan);
63
64         log.debug("PROCESSED: " + webPlan.toString());
65         log.debug("EXPECTED: " + expected.toString());
66         
67         List JavaDoc problems = new ArrayList JavaDoc();
68         boolean ok = compareXmlObjects(webPlan, expected, problems);
69         assertTrue("Differences: " + problems, ok);
70     }
71
72
73
74     private boolean compareXmlObjects(XmlObject xmlObject, XmlObject expectedObject, List JavaDoc problems) {
75         XmlCursor test = xmlObject.newCursor();
76         XmlCursor expected = expectedObject.newCursor();
77         boolean similar = true;
78         int elementCount = 0;
79         while (toNextStartToken(test)) {
80             elementCount++;
81             if (!toNextStartToken(expected)) {
82                 problems.add("test longer than expected at element: " + elementCount);
83                 return false;
84             }
85             QName JavaDoc actualChars = test.getName();
86             QName JavaDoc expectedChars = expected.getName();
87             if (!actualChars.equals(expectedChars)) {
88                 problems.add("Different elements at elementCount: " + elementCount + ", test: " + actualChars + ", expected: " + expectedChars);
89                 similar = false;
90             }
91             test.toNextToken();
92             expected.toNextToken();
93         }
94         if (toNextStartToken(expected)) {
95             problems.add("test shorter that expected at element: " + elementCount);
96             similar = false;
97         }
98         return similar;
99     }
100
101     private boolean toNextStartToken(XmlCursor cursor) {
102         while (!cursor.isStart()) {
103             if (!cursor.hasNextToken()) {
104                 return false;
105             }
106             cursor.toNextToken();
107         }
108         return true;
109     }
110 }
111
Popular Tags