PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/ojc-core/component-common/xmlbeans/xbean-src/2.3.0/xmlbeans/test/src/compile/scomp/detailed/XmlBeanCompilationTests.java

https://bitbucket.org/pymma/openesb-components
Java | 327 lines | 186 code | 51 blank | 90 comment | 23 complexity | 7057334ec69a318a5193ffc4a43f7fb8 MD5 | raw file
  1. /* Copyright 2004 The Apache Software Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package compile.scomp.detailed;
  16. import junit.framework.Assert;
  17. import org.apache.xmlbeans.*;
  18. import org.apache.xmlbeans.impl.xb.xmlconfig.ConfigDocument;
  19. import java.io.*;
  20. import java.util.*;
  21. import compile.scomp.common.CompileCommon;
  22. import compile.scomp.common.CompileTestBase;
  23. import compile.scomp.common.mockobj.TestFiler;
  24. import compile.scomp.common.mockobj.TestBindingConfig;
  25. import javax.xml.namespace.QName;
  26. /**
  27. * Ensure that several compilation mechanisms all generate
  28. * the same schematypesystem
  29. *
  30. */
  31. public class XmlBeanCompilationTests extends CompileTestBase
  32. {
  33. public List xm_errors;
  34. public XmlOptions xm_opts;
  35. Vector expBinType;
  36. Vector expSrcType;
  37. public XmlBeanCompilationTests(String name)
  38. {
  39. super(name);
  40. expBinType = new Vector();
  41. expBinType.add("schema/system/apiCompile/atypedb57type.xsb");
  42. expBinType.add("schema/system/apiCompile/elname429edoctype.xsb");
  43. expBinType.add("schema/system/apiCompile/elnameelement.xsb");
  44. expBinType.add("schema/system/apiCompile/index.xsb");
  45. expBinType.add("schema/element/http_3A_2F_2Fbaz/elName.xsb");
  46. expBinType.add("schema/type/http_3A_2F_2Fbaz/aType.xsb");
  47. expBinType.add("schema/namespace/http_3A_2F_2Fbaz/xmlns.xsb");
  48. expBinType.add("schema/javaname/baz/ElNameDocument.xsb");
  49. expBinType.add("schema/javaname/baz/AType.xsb");
  50. expBinType.add("schema/system/apiCompile/TypeSystemHolder.class");
  51. expSrcType = new Vector();
  52. expSrcType.add("baz.AType");
  53. expSrcType.add("baz.impl.ATypeImpl");
  54. expSrcType.add("baz.ElNameDocument");
  55. expSrcType.add("baz.impl.ElNameDocumentImpl");
  56. xm_errors = new ArrayList();
  57. xm_opts = new XmlOptions();
  58. xm_opts.setErrorListener(xm_errors);
  59. xm_opts.setSavePrettyPrint();
  60. }
  61. public void tearDown() throws Exception
  62. {
  63. super.tearDown();
  64. if (xm_errors.size() > 0)
  65. xm_errors.clear();
  66. }
  67. /**
  68. * Filer != null for BindingConfig to be used
  69. *
  70. * @throws Exception
  71. */
  72. public void test_bindingconfig_extension_compilation() throws Exception
  73. {
  74. TestFiler f = new TestFiler();
  75. //initialize all of the values
  76. String extCaseDir = XBEAN_CASE_ROOT + P + "extensions" + P;
  77. String extSrcDir = CASEROOT + P +
  78. ".." + P + "src" + P + "xmlobject" + P + "extensions" + P;
  79. File[] cPath = CompileTestBase.getClassPath();
  80. String dir = extCaseDir + P + "interfaceFeature" + P + "averageCase";
  81. String dir2 = extCaseDir + P + "prePostFeature" + P +
  82. "ValueRestriction";
  83. ConfigDocument.Config bConf = ConfigDocument.Factory.parse(
  84. new File(dir + P + "po.xsdconfig"))
  85. .getConfig();
  86. ConfigDocument.Config cConf = ConfigDocument.Factory.parse(
  87. new File(dir2 + P + "company.xsdconfig"))
  88. .getConfig();
  89. String simpleConfig = "<xb:config " +
  90. "xmlns:xb=\"http://xml.apache.org/xmlbeans/2004/02/xbean/config\"\n" +
  91. " xmlns:ep=\"http://xbean.interface_feature/averageCase/PurchaseOrder\">\n" +
  92. "<xb:namespace uri=\"http://xbean.interface_feature/averageCase/PurchaseOrder\">\n" +
  93. "<xb:package>com.easypo</xb:package>\n" +
  94. "</xb:namespace></xb:config>";
  95. ConfigDocument.Config confDoc = ConfigDocument.Factory.parse(simpleConfig).getConfig();
  96. ConfigDocument.Config[] confs = new ConfigDocument.Config[]{bConf, confDoc, cConf};
  97. String fooHandlerPath = extSrcDir + P + "interfaceFeature" + P +
  98. "averageCase" + P + "existing" + P + "FooHandler.java";
  99. String iFooPath = extSrcDir + P + "interfaceFeature" + P +
  100. "averageCase" + P + "existing" + P + "IFoo.java";
  101. String iSetterPath = extSrcDir + P + "prePostFeature" + P +
  102. "ValueRestriction" + P + "existing" + P + "ISetter.java";
  103. String setterHandlerPath = extSrcDir + P + "prePostFeature" + P +
  104. "ValueRestriction" + P + "existing" + P + "SetterHandler.java";
  105. File[] fList = new File[]{new File(fooHandlerPath), new File(iFooPath),
  106. new File(iSetterPath),
  107. new File(setterHandlerPath)};
  108. //use created BindingConfig
  109. TestBindingConfig bind = new TestBindingConfig(confs, fList, cPath);
  110. //set XSDs
  111. XmlObject obj1 = XmlObject.Factory.parse(new File(dir + P + "po.xsd"));
  112. XmlObject obj2 = XmlObject.Factory.parse(
  113. new File(dir2 + P + "company.xsd"));
  114. XmlObject[] schemas = new XmlObject[]{obj1, obj2};
  115. //filer must be present on this method
  116. SchemaTypeSystem apiSts = XmlBeans.compileXmlBeans("apiCompile", null,
  117. schemas, bind, XmlBeans.getBuiltinTypeSystem(), f, xm_opts);
  118. if (!bind.isIslookupPrefixForNamespace())
  119. throw new Exception("isIslookupPrefixForNamespace not invoked");
  120. if (!bind.isIslookupPackageForNamespace())
  121. throw new Exception("isIslookupPackageForNamespace not invoked");
  122. if (!bind.isIslookupSuffixForNamespace())
  123. throw new Exception("isIslookupSuffixForNamespace not invoked");
  124. if (!bind.isIslookupJavanameForQName())
  125. throw new Exception("isIslookupJavanameForQName not invoked");
  126. if (!bind.isIsgetInterfaceExtensionsString())
  127. throw new Exception("isIsgetInterfaceExtensionsString not invoked");
  128. if (!bind.isIsgetInterfaceExtensions())
  129. throw new Exception("isIsgetInterfaceExtensions not invoked");
  130. if (!bind.isIsgetPrePostExtensions())
  131. throw new Exception("isIsgetPrePostExtensions not invoked");
  132. if (!bind.isIsgetInterfaceExtensions())
  133. throw new Exception("isIsgetInterfaceExtensions not invoked");
  134. if (!bind.isIsgetPrePostExtensionsString())
  135. throw new Exception("isIsgetPrePostExtensionsString not invoked");
  136. }
  137. /**
  138. * Verify basic incremental compilation
  139. * and compilation with partial SOM usages
  140. */
  141. public void test_incrCompile() throws Exception
  142. {
  143. XmlObject obj1 = XmlObject.Factory.parse(forXsd);
  144. obj1.documentProperties().setSourceName("OBJ1");
  145. XmlObject[] schemas = new XmlObject[]{obj1};
  146. QName sts1 = new QName("http://baz", "elName");
  147. XmlObject obj2 = XmlObject.Factory.parse(incrXsd);
  148. obj2.documentProperties().setSourceName("OBJ2");
  149. XmlObject[] schemas2 = new XmlObject[]{obj2};
  150. QName sts2 = new QName("http://bar", "elName");
  151. XmlObject obj3 = XmlObject.Factory.parse(errXsd);
  152. obj3.documentProperties().setSourceName("OBJ3");
  153. XmlObject[] schemas3 = new XmlObject[]{obj3};
  154. QName sts3 = new QName("http://bar", "elErrName");
  155. SchemaTypeSystem sts;
  156. ArrayList err = new ArrayList();
  157. XmlOptions opt = new XmlOptions().setErrorListener(err);
  158. opt.put("COMPILE_PARTIAL_TYPESYSTEM");
  159. //BASIC COMPILATION
  160. sts = XmlBeans.compileXmlBeans(null,
  161. null, schemas, null,
  162. XmlBeans.getBuiltinTypeSystem(), null, opt);
  163. Assert.assertTrue("Errors should have been empty", err.isEmpty());
  164. // find element in the type System
  165. if (!findGlobalElement(sts.globalElements(), sts1))
  166. throw new Exception(
  167. "Could Not find Type from first Type System: " + sts1);
  168. //SIMPLE INCR COMPILATION
  169. sts = XmlBeans.compileXmlBeans(null,
  170. sts, schemas2, null,
  171. XmlBeans.getBuiltinTypeSystem(), null, opt);
  172. Assert.assertTrue("Errors should have been empty", err.isEmpty());
  173. // find element in the type System
  174. if (!findGlobalElement(sts.globalElements(), sts1))
  175. throw new Exception("Could Not find Type from first Type System: " +
  176. sts1);
  177. if (!findGlobalElement(sts.globalElements(), sts2))
  178. throw new Exception("Could Not find Type from 2nd Type System: " +
  179. sts2);
  180. System.out.println("Building over Existing");
  181. //BUILDING OFF BASE SIMPLE INCR COMPILATION
  182. sts = XmlBeans.compileXmlBeans(null,
  183. sts, schemas2, null,
  184. sts, null, opt);
  185. Assert.assertTrue("Errors should have been empty", err.isEmpty());
  186. // find element in the type System
  187. if (!findGlobalElement(sts.globalElements(), sts1))
  188. throw new Exception("Could Not find Type from first Type System: " +
  189. sts1);
  190. if (!findGlobalElement(sts.globalElements(), sts2))
  191. throw new Exception("Could Not find Type from 2nd Type System: " +
  192. sts2);
  193. //INCR COMPILATION WITH RECOVERABLE ERROR
  194. err.clear();
  195. SchemaTypeSystem b = XmlBeans.compileXmlBeans(null,
  196. sts, schemas3, null,
  197. XmlBeans.getBuiltinTypeSystem(), null, opt);
  198. // find element in the type System
  199. if (!findGlobalElement(b.globalElements(), sts1))
  200. throw new Exception("Could Not find Type from first Type System: " +
  201. sts1);
  202. if (!findGlobalElement(b.globalElements(), sts2))
  203. throw new Exception("Could Not find Type from 2nd Type System: " +
  204. sts2);
  205. if (!findGlobalElement(b.globalElements(), sts3))
  206. throw new Exception("Could Not find Type from 3rd Type System: " +
  207. sts3);
  208. printSTS(b);
  209. //INSPECT ERRORS
  210. boolean psom_expError = false;
  211. // print out the recovered xm_errors
  212. if (!err.isEmpty()) {
  213. System.out.println(
  214. "Schema invalid: partial schema type system recovered");
  215. for (Iterator i = err.iterator(); i.hasNext();) {
  216. XmlError xErr = (XmlError) i.next();
  217. System.out.println(xErr);
  218. //compare to the expected xm_errors
  219. if ((xErr.getErrorCode().compareTo("src-resolve") == 0) &&
  220. (xErr.getMessage().compareTo(
  221. "type 'bType@http://baz' not found.") ==
  222. 0))
  223. psom_expError = true;
  224. }
  225. }
  226. if (!psom_expError)
  227. throw new Exception("Error Code was not as Expected");
  228. }
  229. /*public void test_diff_compilationMethods() throws IOException,
  230. XmlException, Exception
  231. {
  232. //initialize the schema compiler
  233. SchemaCompiler.Parameters params = new SchemaCompiler.Parameters();
  234. params.setXsdFiles(new File[]{scompFile});
  235. params.setSrcDir(scompDirFile);
  236. params.setClassesDir(scompDirFile);
  237. //save out schema for use in scomp later
  238. XmlObject obj1 = XmlObject.Factory.parse(forXsd);
  239. obj1.save(scompFile);
  240. //scomp saved out schema
  241. SchemaCompiler.compile(params);
  242. //use new api to get typesystem
  243. XmlObject[] schemas = new XmlObject[]{obj1};
  244. SchemaTypeSystem apiSts = XmlBeans.compileXmlBeans("apiCompile", null,
  245. schemas, null, XmlBeans.getBuiltinTypeSystem(), null, xm_opts);
  246. //use alternative api to get typesystem
  247. SchemaTypeSystem altSts = XmlBeans.compileXsd(schemas,
  248. XmlBeans.getBuiltinTypeSystem(), null);
  249. //save out sts for diff later
  250. SchemaCodeGenerator.saveTypeSystem(apiSts, apiDirFile, null, null,
  251. null);
  252. SchemaCodeGenerator.saveTypeSystem(altSts, baseDirFile, null, null,
  253. null);
  254. //diff new api to old api
  255. xm_errors = null;
  256. xm_errors = new ArrayList();
  257. Diff.dirsAsTypeSystems(apiDirFile, baseDirFile, xm_errors);
  258. if (xm_errors.size() >= 1)
  259. throw new Exception("API STS ERRORS: " + xm_errors.toString());
  260. //diff scomp sts to new api
  261. xm_errors = null;
  262. xm_errors = new ArrayList();
  263. Diff.dirsAsTypeSystems(apiDirFile, scompDirFile, xm_errors);
  264. if (xm_errors.size() >= 1)
  265. throw new Exception("API SCOMP ERRORS: " + xm_errors.toString());
  266. } */
  267. }