1 /** 2 * 3 * Copyright 2004 The Apache Software Foundation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * 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.schema; 19 20 import java.io.File; 21 import java.util.ArrayList; 22 import java.util.List; 23 24 import junit.framework.TestCase; 25 import org.apache.xmlbeans.XmlCursor; 26 import org.apache.xmlbeans.XmlObject; 27 import org.apache.xmlbeans.XmlException; 28 import org.apache.geronimo.xbeans.j2ee.EjbJarType; 29 30 /** 31 * ejb 1.1 dtd appears to be a subset of ejb 2.0 dtd so the same xsl should 32 * work for both. 33 * 34 * @version $Rev: 151327 $ $Date: 2005-02-03 22:59:48 -0800 (Thu, 03 Feb 2005) $ 35 */ 36 public class SchemaConversionUtilsTest extends TestCase { 37 private static final File basedir = new File(System.getProperty("basedir", System.getProperty("user.dir"))); 38 39 //comment on validity of j2ee 1.4 schemas: validation doesn't work... 40 // From: "Radu Preotiuc-Pietro" <radup@bea.com> 41 // Date: Tue Jun 15, 2004 3:37:50 PM US/Pacific 42 // To: <xmlbeans-user@xml.apache.org> 43 // Subject: RE: Problem with validate -- xsb schema file missing/wrong name 44 // Reply-To: xmlbeans-user@xml.apache.org 45 // 46 // Unfortunately, there is an issue in XmlBeans v1 having to do with duplicate id constraints definitions. 47 // XmlBeans v2 does not have this issue. 48 // Also, these ejb Schemas are techically incorrect because they violate the id constraint uniqueness rule (at least when processed together, you could try and compile each one separately) 49 // So, there are a couple of options: 50 // 1. you hand-edit the schemas to rename those problematic id constraints 51 // 2. you upgrade to v2 52 // Well, there is a third alternative, which is a fix integrated in XmlBeans v1, may or may not be feasible 53 // 54 // Radu 55 56 //I've taken option (1) and fixed the schemas 57 58 //The schemas have been fixed by sun, we can use the official schemas. 59 60 public void testApplicationClient13ToApplicationClient14Transform() throws Exception { 61 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/application-client-13.xml"); 62 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/application-client-14.xml"); 63 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 64 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 65 SchemaConversionUtils.validateDD(expected); 66 xmlObject = SchemaConversionUtils.convertToApplicationClientSchema(xmlObject); 67 // System.out.println(xmlObject.toString()); 68 // System.out.println(expected.toString()); 69 List problems = new ArrayList(); 70 boolean ok = compareXmlObjects(xmlObject, expected, problems); 71 assertTrue("Differences: " + problems, ok); 72 //make sure trying to convert twice has no bad effects 73 XmlCursor cursor2 = xmlObject.newCursor(); 74 try { 75 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"; 76 String version = "1.4"; 77 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 78 } finally { 79 cursor2.dispose(); 80 } 81 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 82 assertTrue("Differences after reconverting to schema: " + problems, ok2); 83 //do the whole transform twice... 84 xmlObject = SchemaConversionUtils.convertToApplicationClientSchema(xmlObject); 85 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 86 assertTrue("Differences after reconverting to application client schema: " + problems, ok3); 87 } 88 89 public void testApplication13ToApplication14Transform() throws Exception { 90 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/application-13.xml"); 91 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/application-14.xml"); 92 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 93 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 94 SchemaConversionUtils.validateDD(expected); 95 xmlObject = SchemaConversionUtils.convertToApplicationSchema(xmlObject); 96 // System.out.println(xmlObject.toString()); 97 // System.out.println(expected.toString()); 98 List problems = new ArrayList(); 99 boolean ok = compareXmlObjects(xmlObject, expected, problems); 100 assertTrue("Differences: " + problems, ok); 101 //make sure trying to convert twice has no bad effects 102 XmlCursor cursor2 = xmlObject.newCursor(); 103 try { 104 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"; 105 String version = "1.4"; 106 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 107 } finally { 108 cursor2.dispose(); 109 } 110 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 111 assertTrue("Differences after reconverting to schema: " + problems, ok2); 112 //do the whole transform twice... 113 xmlObject = SchemaConversionUtils.convertToApplicationSchema(xmlObject); 114 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 115 assertTrue("Differences after reconverting to application schema: " + problems, ok3); 116 } 117 118 public void testConnector10ToConnector15Transform() throws Exception { 119 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/ra-10.xml"); 120 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/ra-15.xml"); 121 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 122 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 123 SchemaConversionUtils.validateDD(expected); 124 xmlObject = SchemaConversionUtils.convertToConnectorSchema(xmlObject); 125 // System.out.println(xmlObject.toString()); 126 // System.out.println(expected.toString()); 127 List problems = new ArrayList(); 128 boolean ok = compareXmlObjects(xmlObject, expected, problems); 129 assertTrue("Differences: " + problems, ok); 130 //make sure trying to convert twice has no bad effects 131 XmlCursor cursor2 = xmlObject.newCursor(); 132 try { 133 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd"; 134 String version = "1.4"; 135 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 136 } finally { 137 cursor2.dispose(); 138 } 139 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 140 assertTrue("Differences after reconverting to schema: " + problems, ok2); 141 //do the whole transform twice... 142 xmlObject = SchemaConversionUtils.convertToConnectorSchema(xmlObject); 143 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 144 assertTrue("Differences after reconverting to application schema: " + problems, ok3); 145 } 146 147 public void testEJB11ToEJB21Transform() throws Exception { 148 File srcXml = new File(basedir, "src/test-data/j2ee_1_2dtd/ejb-1-11.xml"); 149 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_2dtd/ejb-1-21.xml"); 150 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 151 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 152 SchemaConversionUtils.validateDD(expected); 153 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 154 // System.out.println(xmlObject.toString()); 155 // System.out.println(expected.toString()); 156 List problems = new ArrayList(); 157 boolean ok = compareXmlObjects(xmlObject, expected, problems); 158 assertTrue("Differences: " + problems, ok); 159 //make sure trying to convert twice has no bad effects 160 XmlCursor cursor2 = xmlObject.newCursor(); 161 try { 162 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"; 163 String version = "2.1"; 164 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 165 } finally { 166 cursor2.dispose(); 167 } 168 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 169 assertTrue("Differences after reconverting to schema: " + problems, ok2); 170 //do the whole transform twice... 171 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 172 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 173 assertTrue("Differences after reconverting to ejb schema: " + problems, ok3); 174 } 175 176 public void testEJB20ToEJB21Transform() throws Exception { 177 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/ejb-jar.xml"); 178 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/ejb-jar-21.xml"); 179 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 180 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 181 SchemaConversionUtils.validateDD(expected); 182 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 183 // System.out.println(xmlObject.toString()); 184 // System.out.println(expected.toString()); 185 List problems = new ArrayList(); 186 boolean ok = compareXmlObjects(xmlObject, expected, problems); 187 assertTrue("Differences: " + problems, ok); 188 //make sure trying to convert twice has no bad effects 189 XmlCursor cursor2 = xmlObject.newCursor(); 190 try { 191 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"; 192 String version = "2.1"; 193 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 194 } finally { 195 cursor2.dispose(); 196 } 197 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 198 assertTrue("Differences after reconverting to schema: " + problems, ok2); 199 //do the whole transform twice... 200 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 201 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 202 assertTrue("Differences after reconverting to ejb schema: " + problems, ok3); 203 } 204 205 public void testMDB20To21Transform() throws Exception { 206 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/mdb-ejb-jar-20.xml"); 207 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/mdb-ejb-jar-21.xml"); 208 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 209 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 210 SchemaConversionUtils.validateDD(expected); 211 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 212 // System.out.println(xmlObject.toString()); 213 // System.out.println(expected.toString()); 214 List problems = new ArrayList(); 215 boolean ok = compareXmlObjects(xmlObject, expected, problems); 216 assertTrue("Differences: " + problems, ok); 217 //make sure trying to convert twice has no bad effects 218 XmlCursor cursor2 = xmlObject.newCursor(); 219 try { 220 String schemaLocationURL = "http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd"; 221 String version = "2.1"; 222 assertFalse(SchemaConversionUtils.convertToSchema(cursor2, SchemaConversionUtils.J2EE_NAMESPACE, schemaLocationURL, version)); 223 } finally { 224 cursor2.dispose(); 225 } 226 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 227 assertTrue("Differences after reconverting to schema: " + problems, ok2); 228 //do the whole transform twice... 229 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 230 boolean ok3 = compareXmlObjects(xmlObject, expected, problems); 231 assertTrue("Differences after reconverting to ejb schema: " + problems, ok3); 232 } 233 234 public void testOrderDescriptionGroup() throws Exception { 235 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/DescriptionGroupTestSource.xml"); 236 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/DescriptionGroupTestExpected.xml"); 237 XmlObject srcObject = XmlObject.Factory.parse(srcXml); 238 XmlCursor srcCursor = srcObject.newCursor(); 239 XmlCursor moveable = srcObject.newCursor(); 240 try { 241 srcCursor.toFirstChild(); 242 srcCursor.toFirstChild(); 243 assertTrue(srcCursor.getName().toString(), "filter".equals(srcCursor.getName().getLocalPart())); 244 do { 245 srcCursor.push(); 246 srcCursor.toFirstChild(); 247 SchemaConversionUtils.convertToDescriptionGroup(srcCursor, moveable); 248 srcCursor.pop(); 249 } while (srcCursor.toNextSibling()); 250 } finally { 251 srcCursor.dispose(); 252 } 253 // System.out.println(srcObject.toString()); 254 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 255 List problems = new ArrayList(); 256 boolean ok = compareXmlObjects(srcObject, expected, problems); 257 assertTrue("Differences: " + problems, ok); 258 } 259 260 public void testOrderJNDIEnvironmentRefsGroup() throws Exception { 261 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/JNDIEnvironmentRefsGroupTestSource.xml"); 262 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/JNDIEnvironmentRefsGroupTestExpected.xml"); 263 XmlObject srcObject = XmlObject.Factory.parse(srcXml); 264 XmlCursor srcCursor = srcObject.newCursor(); 265 XmlCursor moveable = srcObject.newCursor(); 266 try { 267 srcCursor.toFirstChild(); 268 srcCursor.toFirstChild(); 269 assertTrue(srcCursor.getName().toString(), "web-app".equals(srcCursor.getName().getLocalPart())); 270 do { 271 srcCursor.push(); 272 srcCursor.toFirstChild(); 273 srcCursor.toNextSibling(); 274 srcCursor.toNextSibling(); 275 moveable.toCursor(srcCursor); 276 SchemaConversionUtils.convertToJNDIEnvironmentRefsGroup(srcCursor, moveable); 277 srcCursor.pop(); 278 } while (srcCursor.toNextSibling()); 279 } finally { 280 srcCursor.dispose(); 281 } 282 // System.out.println(srcObject.toString()); 283 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 284 List problems = new ArrayList(); 285 boolean ok = compareXmlObjects(srcObject, expected, problems); 286 assertTrue("Differences: " + problems, ok); 287 } 288 289 public void testWeb23To24Transform() throws Exception { 290 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/web-23.xml"); 291 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/web-24.xml"); 292 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 293 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 294 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 295 // System.out.println(xmlObject.toString()); 296 // System.out.println(expected.toString()); 297 List problems = new ArrayList(); 298 boolean ok = compareXmlObjects(xmlObject, expected, problems); 299 assertTrue("Differences: " + problems, ok); 300 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 301 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 302 assertTrue("Differences: " + problems, ok2); 303 } 304 305 public void testWeb23To24OtherTransform() throws Exception { 306 File srcXml = new File(basedir, "src/test-data/j2ee_1_3dtd/web-1-23.xml"); 307 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_3dtd/web-1-24.xml"); 308 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 309 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 310 // System.out.println(xmlObject.toString()); 311 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 312 List problems = new ArrayList(); 313 boolean ok = compareXmlObjects(xmlObject, expected, problems); 314 assertTrue("Differences: " + problems, ok); 315 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 316 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 317 assertTrue("Differences: " + problems, ok2); 318 } 319 320 public void testWeb22To24Transform1() throws Exception { 321 File srcXml = new File(basedir, "src/test-data/j2ee_1_2dtd/web-1-22.xml"); 322 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_2dtd/web-1-24.xml"); 323 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 324 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 325 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 326 // System.out.println(xmlObject.toString()); 327 // System.out.println(expected.toString()); 328 List problems = new ArrayList(); 329 boolean ok = compareXmlObjects(xmlObject, expected, problems); 330 assertTrue("Differences: " + problems, ok); 331 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 332 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 333 assertTrue("Differences: " + problems, ok2); 334 } 335 336 public void testWebRejectBad24() throws Exception { 337 File srcXml = new File(basedir, "src/test-data/j2ee_1_4schema/web-1-24.xml"); 338 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 339 try { 340 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 341 fail("doc src/test-data/j2ee_1_4schema/web-1-24.xml is invalid, should not have validated"); 342 } catch (XmlException e) { 343 //expected 344 } 345 } 346 347 public void testParseWeb24() throws Exception { 348 File srcXml = new File(basedir, "src/test-data/j2ee_1_4schema/web-2-24.xml"); 349 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 350 xmlObject = SchemaConversionUtils.convertToServletSchema(xmlObject); 351 } 352 353 public void testEJB21To21DoesNothing() throws Exception { 354 File srcXml = new File(basedir, "src/test-data/j2ee_1_4schema/ejb-jar.xml"); 355 File expectedOutputXml = new File(basedir, "src/test-data/j2ee_1_4schema/ejb-jar.xml"); 356 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 357 xmlObject = SchemaConversionUtils.convertToEJBSchema(xmlObject); 358 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 359 List problems = new ArrayList(); 360 boolean ok = compareXmlObjects(xmlObject, expected, problems); 361 assertTrue("Differences: " + problems, ok); 362 } 363 364 public void testGeronimoNamingNamespaceChange() throws Exception { 365 File srcXml = new File(basedir, "src/test-data/geronimo/ejb-naming-pre.xml"); 366 File expectedOutputXml = new File(basedir, "src/test-data/geronimo/ejb-naming-post.xml"); 367 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 368 xmlObject = SchemaConversionUtils.convertToGeronimoNamingSchema(xmlObject); 369 // System.out.println(xmlObject.toString()); 370 XmlObject expected = XmlObject.Factory.parse(expectedOutputXml); 371 List problems = new ArrayList(); 372 boolean ok = compareXmlObjects(xmlObject, expected, problems); 373 assertTrue("Differences: " + problems, ok); 374 xmlObject = SchemaConversionUtils.convertToGeronimoNamingSchema(xmlObject); 375 boolean ok2 = compareXmlObjects(xmlObject, expected, problems); 376 assertTrue("Differences: " + problems, ok2); 377 } 378 // 379 public void testGetNestedObjectAsType() throws Exception { 380 File srcXml = new File(basedir, "src/test-data/geronimo/ejb-naming-pre.xml"); 381 // File expectedOutputXml = new File(basedir, "src/test-data/geronimo/ejb-naming-post.xml"); 382 XmlObject xmlObject = XmlObject.Factory.parse(srcXml); 383 //this is not a usable type, we'll see what happens though 384 xmlObject = SchemaConversionUtils.getNestedObjectAsType(xmlObject, "openejb-jar", EjbJarType.type); 385 // System.out.println(xmlObject.toString()); 386 } 387 388 private boolean compareXmlObjects(XmlObject xmlObject, XmlObject expectedObject, List problems) { 389 XmlCursor test = xmlObject.newCursor(); 390 XmlCursor expected = expectedObject.newCursor(); 391 boolean similar = true; 392 int elementCount = 0; 393 while (toNextStartToken(test)) { 394 elementCount++; 395 if (!toNextStartToken(expected)) { 396 problems.add("test longer than expected at element: " + elementCount); 397 return false; 398 } 399 String actualChars = test.getName().getLocalPart(); 400 String expectedChars = expected.getName().getLocalPart(); 401 if (!actualChars.equals(expectedChars)) { 402 problems.add("Different elements at elementCount: " + elementCount + ", test: " + actualChars + ", expected: " + expectedChars); 403 similar = false; 404 } 405 test.toNextToken(); 406 expected.toNextToken(); 407 } 408 return similar; 409 } 410 411 private boolean toNextStartToken(XmlCursor cursor) { 412 while (!cursor.isStart()) { 413 if (!cursor.hasNextToken()) { 414 return false; 415 } 416 cursor.toNextToken(); 417 } 418 return true; 419 } 420 421 }