1 17 package org.apache.geronimo.web.deployment; 18 19 import java.net.URL ; 20 import java.util.List ; 21 import java.util.ArrayList ; 22 23 import javax.xml.namespace.QName ; 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 36 public class GenericToSpecificPlanConverterTest extends TestCase { 37 private static final Log log = LogFactory.getLog(GenericToSpecificPlanConverterTest.class); 38 39 private ClassLoader classLoader = this.getClass().getClassLoader(); 40 41 public void testConvertPlan1() throws Exception { 42 testConvertPlan("plans/tomcat-pre.xml", "plans/tomcat-post.xml"); 43 } 44 public void testConvertPlan2() throws Exception { 45 testConvertPlan("plans/tomcat-pre2.xml", "plans/tomcat-post.xml"); 46 } 47 public void testConvertPlan3() throws Exception { 48 testConvertPlan("plans/tomcat-pre3.xml", "plans/tomcat-post.xml"); 49 } 50 public void testConvertPlanMessageDestination1() throws Exception { 51 testConvertPlan("plans/web-md-pre.xml", "plans/web-md-post.xml"); 52 } 53 54 public void testConvertPlan(String prePlanName, String postPlanName) throws Exception { 55 URL srcXml = classLoader.getResource(prePlanName); 56 URL 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 problems = new ArrayList (); 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 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 actualChars = test.getName(); 86 QName 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 |