1 17 18 package org.apache.geronimo.upgrade; 19 20 21 import java.net.URL ; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.StringWriter ; 27 import java.io.Writer ; 28 29 import javax.xml.namespace.QName ; 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 39 public class Upgrade1_0To1_1Test extends TestCase { 40 41 private final ClassLoader classLoader = this.getClass().getClassLoader(); 42 43 public void test1() throws Exception { 44 test("appclient_ejb_1"); 45 } 46 47 public void test2() throws Exception { 48 test("appclient_dep_1"); 49 } 50 51 public void test3() throws Exception { 52 test("transport_1"); 53 } 54 55 public void test4() throws Exception { 56 test("transport_2"); 57 } 58 59 public void test5() throws Exception { 60 test("assembly_1"); 61 } 62 63 public void test6() throws Exception { 64 try { 65 test("ejb_pkgen_1"); 66 fail(); 67 } catch (XmlException e) { 68 69 } 70 } 71 72 public void test7() throws Exception { 73 test("servlet_1"); 74 } 75 76 public void test8() throws Exception { 77 test("gbean_1"); 78 } 79 80 private void test(String testName) throws Exception { 81 InputStream srcXml = classLoader.getResourceAsStream(testName + ".xml"); 82 try { 83 Writer targetXml = new StringWriter (); 84 new Upgrade1_0To1_1().upgrade(srcXml, targetXml); 85 86 String targetString = targetXml.toString(); 87 XmlObject targetXmlObject = XmlObject.Factory.parse(targetString); 88 URL expectedOutputXml = classLoader.getResource(testName + "_result.xml"); 89 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 90 List problems = new ArrayList (); 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 ignored) { 102 } 104 } 105 } 106 } 107 108 private boolean compareXmlObjects(XmlObject xmlObject, XmlObject expectedObject, List 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 actualQName = test.getName(); 120 QName 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 |