001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.web.deployment;
017:
018: import java.net.URL;
019: import java.util.List;
020: import java.util.ArrayList;
021:
022: import javax.xml.namespace.QName;
023:
024: import junit.framework.TestCase;
025: import org.apache.xmlbeans.XmlObject;
026: import org.apache.xmlbeans.XmlCursor;
027: import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * @version $Rev: 577890 $ $Date: 2007-09-20 12:37:26 -0700 (Thu, 20 Sep 2007) $
034: */
035: public class GenericToSpecificPlanConverterTest extends TestCase {
036: private static final Log log = LogFactory
037: .getLog(GenericToSpecificPlanConverterTest.class);
038:
039: private ClassLoader classLoader = this .getClass().getClassLoader();
040:
041: public void testConvertPlan1() throws Exception {
042: testConvertPlan("plans/tomcat-pre.xml", "plans/tomcat-post.xml");
043: }
044:
045: public void testConvertPlan2() throws Exception {
046: testConvertPlan("plans/tomcat-pre2.xml",
047: "plans/tomcat-post.xml");
048: }
049:
050: public void testConvertPlan3() throws Exception {
051: testConvertPlan("plans/tomcat-pre3.xml",
052: "plans/tomcat-post.xml");
053: }
054:
055: public void testConvertPlanMessageDestination1() throws Exception {
056: testConvertPlan("plans/web-md-pre.xml", "plans/web-md-post.xml");
057: }
058:
059: public void testConvertPlan(String prePlanName, String postPlanName)
060: throws Exception {
061: URL srcXml = classLoader.getResource(prePlanName);
062: URL expectedOutputXml = classLoader.getResource(postPlanName);
063: XmlObject rawPlan = XmlBeansUtil.parse(srcXml, getClass()
064: .getClassLoader());
065: log.debug("RAW PLAN " + rawPlan.toString());
066:
067: XmlObject expected = XmlObject.Factory.parse(expectedOutputXml);
068: XmlObject webPlan = new GenericToSpecificPlanConverter(
069: "http://geronimo.apache.org/xml/ns/web/tomcat/config-1.0",
070: "http://geronimo.apache.org/xml/ns/j2ee/web/tomcat-2.0.1",
071: "tomcat").convertToSpecificPlan(rawPlan);
072:
073: log.debug("PROCESSED: " + webPlan.toString());
074: log.debug("EXPECTED: " + expected.toString());
075:
076: List problems = new ArrayList();
077: boolean ok = compareXmlObjects(webPlan, expected, problems);
078: assertTrue("Differences: " + problems, ok);
079: }
080:
081: private boolean compareXmlObjects(XmlObject xmlObject,
082: XmlObject expectedObject, List problems) {
083: XmlCursor test = xmlObject.newCursor();
084: XmlCursor expected = expectedObject.newCursor();
085: boolean similar = true;
086: int elementCount = 0;
087: while (toNextStartToken(test)) {
088: elementCount++;
089: if (!toNextStartToken(expected)) {
090: problems.add("test longer than expected at element: "
091: + elementCount);
092: return false;
093: }
094: QName actualChars = test.getName();
095: QName expectedChars = expected.getName();
096: if (!actualChars.equals(expectedChars)) {
097: problems.add("Different elements at elementCount: "
098: + elementCount + ", test: " + actualChars
099: + ", expected: " + expectedChars);
100: similar = false;
101: }
102: test.toNextToken();
103: expected.toNextToken();
104: }
105: if (toNextStartToken(expected)) {
106: problems.add("test shorter that expected at element: "
107: + elementCount);
108: similar = false;
109: }
110: return similar;
111: }
112:
113: private boolean toNextStartToken(XmlCursor cursor) {
114: while (!cursor.isStart()) {
115: if (!cursor.hasNextToken()) {
116: return false;
117: }
118: cursor.toNextToken();
119: }
120: return true;
121: }
122: }
|