PageRenderTime 80ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ojc-core/encodersl/encoder-coco/test/com/sun/encoder/coco/runtime/TestRuntime.java

https://bitbucket.org/pymma/openesb-components
Java | 558 lines | 454 code | 48 blank | 56 comment | 82 complexity | 3a4d9776646f41d903996ba5ade70693 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)TestRuntime.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.encoder.coco.runtime;
  30. import java.io.ByteArrayInputStream;
  31. import java.io.File;
  32. import java.io.FileInputStream;
  33. import java.io.FileOutputStream;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.io.OutputStream;
  37. import java.io.StringReader;
  38. import java.net.URL;
  39. import javax.xml.namespace.QName;
  40. import javax.xml.transform.Transformer;
  41. import javax.xml.transform.TransformerException;
  42. import javax.xml.transform.TransformerFactory;
  43. import javax.xml.transform.TransformerFactoryConfigurationError;
  44. import javax.xml.transform.dom.DOMResult;
  45. import javax.xml.transform.dom.DOMSource;
  46. import javax.xml.transform.sax.SAXResult;
  47. import javax.xml.transform.sax.SAXSource;
  48. import javax.xml.transform.stream.StreamSource;
  49. import org.xml.sax.Attributes;
  50. import org.xml.sax.ContentHandler;
  51. import org.xml.sax.EntityResolver;
  52. import org.xml.sax.InputSource;
  53. import org.xml.sax.Locator;
  54. import org.xml.sax.SAXException;
  55. import com.sun.encoder.Encoder;
  56. import com.sun.encoder.EncoderConfigurationException;
  57. import com.sun.encoder.EncoderException;
  58. import com.sun.encoder.EncoderFactory;
  59. import com.sun.encoder.coco.CocoEncoderProvider;
  60. /**
  61. * This class facilitates simple functional and performance testing on
  62. * COBOL Copybook encoder.
  63. *
  64. * @author Jun Xu
  65. */
  66. public class TestRuntime {
  67. private final QName mTopElementName;
  68. private final URL mSchemaLocation;
  69. public TestRuntime(URL schemaLocation, QName topElementName) {
  70. mTopElementName = topElementName;
  71. mSchemaLocation = schemaLocation;
  72. }
  73. public void testDecodeFromString(String stringData, int loops,
  74. boolean doPrint)
  75. throws IOException, SAXException {
  76. System.out.println("data size (characters) = " + stringData.length());
  77. //Prepare content handler
  78. ContentHandler handler =
  79. new SimpleContentHandler(doPrint);
  80. System.out.println("Look up encoder ...");
  81. Encoder encoder;
  82. try {
  83. EncoderFactory factory = EncoderFactory.newInstance();
  84. encoder =
  85. factory.newEncoder(
  86. factory.makeType(CocoEncoderProvider.STYLE_ID),
  87. factory.makeMeta(mSchemaLocation, mTopElementName));
  88. } catch (EncoderConfigurationException e1) {
  89. e1.printStackTrace();
  90. return;
  91. }
  92. System.out.println("Start ...");
  93. long startedAt = System.currentTimeMillis();
  94. final int finalLoops = loops;
  95. int i;
  96. SAXSource source;
  97. for (i = 0; i < finalLoops; i++) {
  98. try {
  99. source = (SAXSource) encoder.decodeFromString(stringData);
  100. source.getXMLReader().setContentHandler(handler);
  101. source.getXMLReader().parse(source.getInputSource());
  102. } catch (EncoderException e) {
  103. e.printStackTrace();
  104. break;
  105. }
  106. }
  107. long endedAt = System.currentTimeMillis();
  108. long elapsed = endedAt - startedAt;
  109. if (elapsed < 0) {
  110. elapsed = (Long.MAX_VALUE - startedAt) + endedAt;
  111. }
  112. System.out.println("End ...");
  113. System.out.println("Elapsed: " + elapsed + "ms");
  114. if (elapsed == 0) {
  115. elapsed = 1;
  116. }
  117. System.out.println("Characters/ms: "
  118. + i * stringData.length() / elapsed);
  119. }
  120. public void testDecodeFromBytes(byte[] bytes, int loops, boolean doPrint)
  121. throws IOException, SAXException {
  122. System.out.println("data size (bytes) = " + bytes.length);
  123. //Prepare content handler
  124. ContentHandler handler =
  125. new SimpleContentHandler(doPrint);
  126. System.out.println("Look up encoder ...");
  127. Encoder encoder;
  128. try {
  129. EncoderFactory factory = EncoderFactory.newInstance();
  130. encoder =
  131. factory.newEncoder(
  132. factory.makeType(CocoEncoderProvider.STYLE_ID),
  133. factory.makeMeta(mSchemaLocation, mTopElementName));
  134. } catch (EncoderConfigurationException e1) {
  135. e1.printStackTrace();
  136. return;
  137. }
  138. System.out.println("Start ...");
  139. long startedAt = System.currentTimeMillis();
  140. final int finalLoops = loops;
  141. int i;
  142. SAXSource source;
  143. for (i = 0; i < finalLoops; i++) {
  144. try {
  145. source = (SAXSource) encoder.decodeFromBytes(bytes);
  146. source.getXMLReader().setContentHandler(handler);
  147. source.getXMLReader().parse(source.getInputSource());
  148. } catch (EncoderException e) {
  149. e.printStackTrace();
  150. break;
  151. }
  152. }
  153. long endedAt = System.currentTimeMillis();
  154. long elapsed = endedAt - startedAt;
  155. if (elapsed < 0) {
  156. elapsed = (Long.MAX_VALUE - startedAt) + endedAt;
  157. }
  158. System.out.println("End ...");
  159. System.out.println("Elapsed: " + elapsed + "ms");
  160. if (elapsed == 0) {
  161. elapsed = 1;
  162. }
  163. System.out.println("Bytes/ms: "
  164. + i * bytes.length / elapsed);
  165. }
  166. public byte[] testEncodeToBytes(String sourceData, int loops,
  167. boolean doPrint)
  168. throws IOException, TransformerFactoryConfigurationError,
  169. TransformerException, EncoderException {
  170. System.out.println("source data size (characters) = "
  171. + sourceData.length());
  172. System.out.println("Prepare DOM Source ...");
  173. DOMSource domSource = getDOMData(sourceData);
  174. System.out.println("Look up encoder ...");
  175. Encoder encoder;
  176. try {
  177. EncoderFactory factory = EncoderFactory.newInstance();
  178. encoder =
  179. factory.newEncoder(
  180. factory.makeType(CocoEncoderProvider.STYLE_ID),
  181. factory.makeMeta(mSchemaLocation, mTopElementName));
  182. } catch (EncoderConfigurationException e1) {
  183. e1.printStackTrace();
  184. return null;
  185. }
  186. final int finalLoops = loops;
  187. int i;
  188. Transformer transformer =
  189. TransformerFactory.newInstance().newTransformer();
  190. System.out.println("Start ...");
  191. long startedAt = System.currentTimeMillis();
  192. for (i = 0; i < finalLoops; i++) {
  193. encoder.encodeToBytes(domSource);
  194. }
  195. long endedAt = System.currentTimeMillis();
  196. long elapsed = endedAt - startedAt;
  197. if (elapsed < 0) {
  198. elapsed = (Long.MAX_VALUE - startedAt) + endedAt;
  199. }
  200. //Either visiting DOM tree or transforming is very costy, we need to
  201. //exclude the cost. We use the SimpleContentHandler to run it this
  202. //time. SimpleContentHandler almost does nothing when doPrint is false.
  203. SAXResult saxResult = new SAXResult(
  204. new SimpleContentHandler(false));
  205. System.out.println("Estimate cost ...");
  206. startedAt = System.currentTimeMillis();
  207. for (i = 0; i < finalLoops; i++) {
  208. transformer.transform(domSource, saxResult);
  209. }
  210. endedAt = System.currentTimeMillis();
  211. long elapsed2 = endedAt - startedAt;
  212. if (elapsed2 < 0) {
  213. elapsed2 = (Long.MAX_VALUE - startedAt) + endedAt;
  214. }
  215. elapsed = elapsed - elapsed2;
  216. byte[] resultBytes = encoder.encodeToBytes(domSource);
  217. if (doPrint) {
  218. StringBuffer buf = new StringBuffer();
  219. HexDump.dump(resultBytes, buf, 0);
  220. System.out.println(buf.toString());
  221. }
  222. System.out.println("End ...");
  223. System.out.println("Elapsed: " + elapsed + "ms");
  224. if (elapsed == 0) {
  225. elapsed = 1;
  226. }
  227. System.out.println("result data size (bytes) = " + resultBytes.length);
  228. System.out.println("result bytes/ms: "
  229. + i * resultBytes.length / elapsed);
  230. return resultBytes;
  231. }
  232. private static byte[] getBytes(File file) throws IOException {
  233. FileInputStream fis = new FileInputStream(file);
  234. byte[] bytes = new byte[(int) file.length()];
  235. fis.read(bytes);
  236. return bytes;
  237. }
  238. private DOMSource getDOMData(String stringData)
  239. throws TransformerFactoryConfigurationError, TransformerException {
  240. StreamSource streamSource = new StreamSource(
  241. new StringReader(stringData));
  242. Transformer transformer =
  243. TransformerFactory.newInstance().newTransformer();
  244. DOMResult domResult = new DOMResult();
  245. transformer.transform(streamSource, domResult);
  246. return new DOMSource(domResult.getNode());
  247. }
  248. private DOMSource getDOMData(byte[] bytesData)
  249. throws TransformerFactoryConfigurationError, TransformerException {
  250. StreamSource streamSource = new StreamSource(
  251. new ByteArrayInputStream(bytesData));
  252. Transformer transformer =
  253. TransformerFactory.newInstance().newTransformer();
  254. DOMResult domResult = new DOMResult();
  255. transformer.transform(streamSource, domResult);
  256. return new DOMSource(domResult.getNode());
  257. }
  258. class SimpleContentHandler implements ContentHandler {
  259. private final boolean mDoPrint;
  260. int mIndent = 0;
  261. boolean mHasChar = false;
  262. boolean mIsOpen = false;
  263. SimpleContentHandler(boolean doPrint) {
  264. mDoPrint = doPrint;
  265. }
  266. public void setDocumentLocator(Locator locator) {
  267. }
  268. public void startDocument() throws SAXException {
  269. if (!mDoPrint) {
  270. return;
  271. }
  272. System.out.print(
  273. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  274. }
  275. public void endDocument() throws SAXException {
  276. if (!mDoPrint) {
  277. return;
  278. }
  279. System.out.println();
  280. }
  281. public void startPrefixMapping(String prefix, String uri)
  282. throws SAXException {
  283. }
  284. public void endPrefixMapping(String prefix)
  285. throws SAXException {
  286. }
  287. public final void startElement(String uri,
  288. String localName, String qName, Attributes atts)
  289. throws SAXException {
  290. if (!mDoPrint) {
  291. return;
  292. }
  293. System.out.println();
  294. if (mIsOpen) {
  295. mIndent++;
  296. }
  297. printIndent();
  298. System.out.print("<" + localName
  299. + " xmlns=\"" + uri + "\"");
  300. for (int i = 0; i < atts.getLength(); i++) {
  301. System.out.print(" " + atts.getQName(i));
  302. System.out.print("=\"");
  303. System.out.print(atts.getValue(i));
  304. System.out.print("\"");
  305. }
  306. System.out.print(">");
  307. mHasChar = false;
  308. mIsOpen = true;
  309. }
  310. public final void endElement(String uri, String localName,
  311. String qName) throws SAXException {
  312. if (!mDoPrint) {
  313. return;
  314. }
  315. if (mHasChar) {
  316. System.out.print("</" + localName + ">");
  317. mHasChar = false;
  318. } else {
  319. System.out.println();
  320. mIndent--;
  321. printIndent();
  322. System.out.print("</" + localName + ">");
  323. }
  324. mIsOpen = false;
  325. }
  326. public final void characters(char[] ch, int start,
  327. int length) throws SAXException {
  328. if (!mDoPrint) {
  329. return;
  330. }
  331. String chars = new String(ch, start, length);
  332. chars = chars.replace("&", "&amp;");
  333. System.out.print(chars);
  334. if (chars.length() > 0) {
  335. mHasChar = true;
  336. }
  337. }
  338. public void ignorableWhitespace(char[] ch, int start,
  339. int length) throws SAXException {
  340. }
  341. public void processingInstruction(String target,
  342. String data) throws SAXException {
  343. }
  344. public void skippedEntity(String name)
  345. throws SAXException {
  346. }
  347. private void printIndent() {
  348. for (int i = 0; i < mIndent; i++) {
  349. System.out.print(" ");
  350. }
  351. }
  352. }
  353. private static class JarEntityResolver implements EntityResolver {
  354. public InputSource resolveEntity(String publicId, String systemId)
  355. throws SAXException, IOException {
  356. InputStream in = new URL(systemId).openStream();
  357. InputSource inputSource = new InputSource(in);
  358. inputSource.setPublicId(publicId);
  359. inputSource.setSystemId(systemId);
  360. return inputSource;
  361. }
  362. }
  363. public static void main(String[] args) {
  364. String testWhat = "db";
  365. int loops = 1;
  366. boolean printResult = false;
  367. File testDir =
  368. new File("test");
  369. String inputFileName = null;
  370. String outputFileName = null;
  371. String schemaFileName = null;
  372. String schemaURLString = null;
  373. String rootElementName = null;
  374. for (int i = 0; args != null && i < args.length; i++) {
  375. if ("-t".equals(args[i])) {
  376. //test what?
  377. if (i + 1 < args.length) {
  378. testWhat = args[i + 1];
  379. }
  380. } else if ("-d".equals(args[i])) {
  381. //testing directory
  382. if (i + 1 < args.length) {
  383. testDir = new File(args[i + 1]);
  384. }
  385. } else if ("-l".equals(args[i])) {
  386. //number of loops
  387. if (i + 1 < args.length) {
  388. loops = Integer.parseInt(args[i + 1]);
  389. }
  390. } else if ("-p".equals(args[i])) {
  391. //print result?
  392. printResult = true;
  393. } else if ("-i".equals(args[i])) {
  394. //input file
  395. if (i + 1 < args.length) {
  396. inputFileName = args[i + 1];
  397. }
  398. } else if ("-o".equals(args[i])) {
  399. //output file
  400. if (i + 1 < args.length) {
  401. outputFileName = args[i + 1];
  402. }
  403. } else if ("-s".equals(args[i])) {
  404. //schema file using file path
  405. if (i + 1 < args.length) {
  406. schemaFileName = args[i + 1];
  407. }
  408. } else if ("-u".equals(args[i])) {
  409. //schema file using URL
  410. if (i + 1 < args.length) {
  411. schemaURLString = args[i + 1];
  412. }
  413. } else if ("-r".equals(args[i])) {
  414. //root element
  415. if (i + 1 < args.length) {
  416. rootElementName = args[i + 1];
  417. }
  418. } else if ("-help".equals(args[i]) || "/?".equals(args[i])) {
  419. System.out.println("Usage: java SimplePerfTest "
  420. + "[-d <testing dir>] "
  421. + "[-t <test what, value=(ls, lb, ds, db, es or eb)] "
  422. + "[-l <number of loops>] "
  423. + "[-p]");
  424. System.exit(1);
  425. }
  426. }
  427. try {
  428. System.out.println("Load input data ...");
  429. byte[] bytesData = new byte[0];
  430. if (inputFileName != null) {
  431. bytesData = getBytes(new File(testDir, inputFileName));
  432. // Writer writer =
  433. // new OutputStreamWriter(
  434. // new FileOutputStream(
  435. // new File(testDir, inputFileName)),
  436. // "Cp037");
  437. // writer.write(stringData);
  438. // writer.flush();
  439. // writer.close();
  440. }
  441. System.out.println("Prepare XSD ...");
  442. File schemaFile = null;
  443. URL schemaURL = null;
  444. if (schemaFileName == null && schemaURLString == null) {
  445. System.err.println("No schema file specified.");
  446. return;
  447. }
  448. if (schemaFileName != null) {
  449. schemaFile = new File(testDir, schemaFileName);
  450. } else {
  451. schemaURL = new URL(schemaURLString);
  452. }
  453. if (rootElementName == null) {
  454. System.err.println("No top element specified.");
  455. return;
  456. }
  457. QName qName;
  458. if (rootElementName.charAt(0) == '{') {
  459. int pos = rootElementName.indexOf('}');
  460. qName =
  461. new QName(rootElementName.substring(1, pos),
  462. rootElementName.substring(pos + 1));
  463. } else {
  464. qName = new QName(rootElementName);
  465. }
  466. TestRuntime perfTest;
  467. if (schemaURL != null) {
  468. perfTest = new TestRuntime(schemaURL, qName);
  469. } else {
  470. perfTest = new TestRuntime(schemaFile.toURL(), qName);
  471. }
  472. if ("db".equals(testWhat)) {
  473. perfTest.testDecodeFromBytes(bytesData, loops, printResult);
  474. } else if ("eb".equals(testWhat)) {
  475. byte[] result = perfTest.testEncodeToBytes(
  476. new String(bytesData, "UTF-8"), loops, printResult);
  477. if (result != null && outputFileName != null) {
  478. OutputStream out =
  479. new FileOutputStream(
  480. new File(testDir, outputFileName));
  481. out.write(result);
  482. out.close();
  483. }
  484. } else {
  485. System.out.println("Usage: java SimplePerfTest "
  486. + "[-d <testing dir>] "
  487. + "[-t <test what, value=(db or eb)] "
  488. + "[-l <number of loops>] "
  489. + "[-p]");
  490. System.exit(1);
  491. }
  492. } catch (IOException e) {
  493. e.printStackTrace();
  494. System.exit(3);
  495. } catch (TransformerFactoryConfigurationError e) {
  496. e.printStackTrace();
  497. System.exit(4);
  498. } catch (SAXException e) {
  499. e.printStackTrace();
  500. System.exit(5);
  501. } catch (TransformerException e) {
  502. e.printStackTrace();
  503. System.exit(6);
  504. } catch (EncoderException e) {
  505. e.printStackTrace();
  506. System.exit(7);
  507. }
  508. System.exit(0);
  509. }
  510. }