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

/contrib-imola/cics-bc/jbi4cics/src/test/java/it/imolinfo/jbi4cics/test/mapping/coboltypes/JavaDoubleTypeMappingTest.java

https://bitbucket.org/pymma/openesb-components
Java | 417 lines | 239 code | 55 blank | 123 comment | 0 complexity | 7ada75ec7d71b8074ac572e4012ef584 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2005, 2006 Imola Informatica.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the LGPL License v2.1
  5. * which accompanies this distribution, and is available at
  6. * http://www.gnu.org/licenses/lgpl.html
  7. *******************************************************************************/
  8. package it.imolinfo.jbi4cics.test.mapping.coboltypes;
  9. import it.imolinfo.jbi4cics.typemapping.cobol.CobolFieldFormatter;
  10. import it.imolinfo.jbi4cics.typemapping.cobol.CobolType;
  11. import it.imolinfo.jbi4cics.typemapping.cobol.CobolTypeDescriptor;
  12. import it.imolinfo.jbi4cics.typemapping.cobol.HexDump;
  13. import java.math.BigDecimal;
  14. import java.math.BigInteger;
  15. import java.util.Arrays;
  16. import junit.framework.TestCase;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. /**
  20. * falliscono per problemi di approssimazione:
  21. * testPackedDecimal9_18
  22. * testPackedDecimal9_18_9_2
  23. * testInteger9_18
  24. *
  25. * il problema sembra legato alla precisione intrinseca del tipo double,
  26. * la precisione del double sembra essere intorno alle 16/17 cifre. Ho deciso di accorciare i test a questa lunghezza
  27. * I test diventano dunque:
  28. *
  29. * testPackedDecimal9_16
  30. * testPackedDecimal9_14_9_2
  31. * testInteger9_16
  32. *
  33. * @author raffaele
  34. *
  35. */
  36. public class JavaDoubleTypeMappingTest extends TestCase {
  37. private static Log log=LogFactory.getLog(JavaDoubleTypeMappingTest.class);
  38. public static final String DeafultHostCodePage="CP1144";
  39. /**
  40. * testo un packed decimal -- comp3
  41. * la dichiarazione è
  42. * PIC 9(16) comp-3
  43. * valore 1234567891234567
  44. *
  45. */
  46. public void testPackedDecimal9_16(){
  47. try{
  48. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  49. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  50. cobolTypeDescriptor.setIntegerPartLength(16);
  51. Double value=Double.valueOf(1234567891234567L);
  52. log.debug("value.toString: "+value.toString());
  53. byte[] buffer=new byte[9];
  54. byte[] expectedBuffer=new byte[] {(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,(byte)0x89,(byte)0x12,(byte)0x34,(byte)0x56,(byte)0x7f};
  55. long millis1=System.currentTimeMillis();
  56. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  57. long millis2=System.currentTimeMillis();
  58. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  59. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  60. log.debug("conversion time="+(millis2-millis1)+" millis");
  61. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  62. // unformat
  63. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  64. assertEquals(value, ((BigDecimal)result).doubleValue());
  65. }
  66. catch(Exception e){
  67. e.printStackTrace();
  68. fail(e.getMessage());
  69. }
  70. }
  71. /**
  72. * testo un packed decimal -- comp3
  73. * la dichiarazione è
  74. * PIC 9(14)v9(2) comp-3
  75. * valore 12345678912345.12
  76. *
  77. */
  78. public void testPackedDecimal9_14_9_2(){
  79. try{
  80. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  81. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  82. cobolTypeDescriptor.setIntegerPartLength(14);
  83. cobolTypeDescriptor.setDecimalPartLength(2);
  84. log.debug("virtual decimal point: "+cobolTypeDescriptor.getVirtualDecimalPoint());
  85. Double value=new Double("12345678912345.12");
  86. byte[] buffer=new byte[9];
  87. byte[] expectedBuffer=new byte[] {(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,(byte)0x89,(byte)0x12,(byte)0x34,(byte)0x51,(byte)0x2f};
  88. long millis1=System.currentTimeMillis();
  89. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  90. long millis2=System.currentTimeMillis();
  91. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  92. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  93. log.debug("conversion time="+(millis2-millis1)+" millis");
  94. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  95. // unformat
  96. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  97. assertEquals(value, ((BigDecimal)result).doubleValue());
  98. }
  99. catch(Exception e){
  100. e.printStackTrace();
  101. fail(e.getMessage());
  102. }
  103. }
  104. /**
  105. * testo un zoned
  106. * PIC 9(10)
  107. * con valore 10
  108. *
  109. */
  110. public void testZoned9_10(){
  111. try{
  112. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  113. cobolTypeDescriptor.setType(CobolType.ZONED);
  114. cobolTypeDescriptor.setIntegerPartLength(10);
  115. cobolTypeDescriptor.setCodePage(DeafultHostCodePage);
  116. cobolTypeDescriptor.setZonedSignFormat(CobolTypeDescriptor.SIGN_FORMAT_TRAILING);
  117. cobolTypeDescriptor.setPadCharacter(" ");
  118. Double value=Double.valueOf(10);
  119. byte[] buffer=new byte[10];
  120. byte[] expectedBuffer=new byte[] {(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf1,(byte)0xf0};
  121. // '0' '0' '0' '0' '0' '0' '0' '0' '1' '0'
  122. long millis1=System.currentTimeMillis();
  123. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  124. long millis2=System.currentTimeMillis();
  125. log.debug("buffer dopo il format in string: ["+new String(buffer,DeafultHostCodePage)+"]");
  126. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  127. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  128. log.debug("conversion time="+(millis2-millis1)+" millis");
  129. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  130. // unformat
  131. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  132. assertEquals(value, ((BigDecimal)result).doubleValue());
  133. }
  134. catch(Exception e){
  135. e.printStackTrace();
  136. fail(e.getMessage());
  137. }
  138. }
  139. /**
  140. * testo un zoned
  141. * PIC 9(10)v9(2)
  142. * con valore 10.10
  143. *
  144. */
  145. public void testZoned9_10_9_2(){
  146. try{
  147. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  148. cobolTypeDescriptor.setType(CobolType.ZONED);
  149. cobolTypeDescriptor.setIntegerPartLength(10);
  150. cobolTypeDescriptor.setDecimalPartLength(2);
  151. cobolTypeDescriptor.setCodePage(DeafultHostCodePage);
  152. cobolTypeDescriptor.setZonedSignFormat(CobolTypeDescriptor.SIGN_FORMAT_TRAILING);
  153. cobolTypeDescriptor.setPadCharacter(" ");
  154. Double value=Double.valueOf("10.10");
  155. byte[] buffer=new byte[12];
  156. byte[] expectedBuffer=new byte[] {(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf1,(byte)0xf0,(byte)0xf1,(byte)0xf0};
  157. // '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '0'
  158. long millis1=System.currentTimeMillis();
  159. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  160. long millis2=System.currentTimeMillis();
  161. log.debug("buffer dopo il format in string: ["+new String(buffer,DeafultHostCodePage)+"]");
  162. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  163. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  164. log.debug("conversion time="+(millis2-millis1)+" millis");
  165. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  166. // unformat
  167. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  168. assertEquals(value, ((BigDecimal)result).doubleValue());
  169. }
  170. catch(Exception e){
  171. e.printStackTrace();
  172. fail(e.getMessage());
  173. }
  174. }
  175. /**
  176. * testo un integer -- comp
  177. * la dichiarazione è
  178. * PIC 9(2) comp
  179. * valore 123456789
  180. *
  181. * PIC 9(1)-9(2) occupa 1 byte
  182. * PIC 9(3)-9(4) occupa 2 byte
  183. * PIC 9(5)-9(9) occupa 4 byte
  184. * PIC 9(10)-9(18) occupa 8 byte
  185. * PIC 9(19) .. occupa 16 byte
  186. *
  187. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  188. */
  189. public void testInteger9_2(){
  190. try{
  191. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  192. cobolTypeDescriptor.setType(CobolType.INTEGER);
  193. cobolTypeDescriptor.setIntegerPartLength(2);
  194. cobolTypeDescriptor.setBigEndian(true);
  195. Double value=Double.valueOf(12L);
  196. //String value="pippo";
  197. byte[] buffer=new byte[1];
  198. byte[] expectedBuffer=BigInteger.valueOf(12L).toByteArray(); //cosi' semplice??
  199. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  200. long millis1=System.currentTimeMillis();
  201. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  202. long millis2=System.currentTimeMillis();
  203. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  204. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  205. log.debug("conversion time="+(millis2-millis1)+" millis");
  206. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  207. // unformat
  208. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  209. assertEquals(value, ((BigInteger)result).doubleValue());
  210. }
  211. catch(Exception e){
  212. e.printStackTrace();
  213. fail(e.getMessage());
  214. }
  215. }
  216. /**
  217. * testo un integer -- comp
  218. * la dichiarazione è
  219. * PIC 9(4) comp
  220. * valore 123456789
  221. *
  222. * PIC 9(1)-9(2) occupa 1 byte
  223. * PIC 9(3)-9(4) occupa 2 byte
  224. * PIC 9(5)-9(9) occupa 4 byte
  225. * PIC 9(10)-9(18) occupa 8 byte
  226. * PIC 9(19) .. occupa 16 byte
  227. *
  228. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  229. */
  230. public void testInteger9_4(){
  231. try{
  232. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  233. cobolTypeDescriptor.setType(CobolType.INTEGER);
  234. cobolTypeDescriptor.setIntegerPartLength(4);
  235. cobolTypeDescriptor.setBigEndian(true);
  236. Double value=Double.valueOf(1234L);
  237. //String value="pippo";
  238. byte[] buffer=new byte[2];
  239. byte[] expectedBuffer=BigInteger.valueOf(1234L).toByteArray(); //cosi' semplice??
  240. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  241. long millis1=System.currentTimeMillis();
  242. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  243. long millis2=System.currentTimeMillis();
  244. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  245. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  246. log.debug("conversion time="+(millis2-millis1)+" millis");
  247. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  248. // unformat
  249. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  250. assertEquals(value, ((BigInteger)result).doubleValue());
  251. }
  252. catch(Exception e){
  253. e.printStackTrace();
  254. fail(e.getMessage());
  255. }
  256. }
  257. /**
  258. * testo un integer -- comp
  259. * la dichiarazione è
  260. * PIC 9(9) comp
  261. * valore 123456789
  262. *
  263. * PIC 9(1)-9(2) occupa 1 byte
  264. * PIC 9(3)-9(4) occupa 2 byte
  265. * PIC 9(5)-9(9) occupa 4 byte
  266. * PIC 9(10)-9(18) occupa 8 byte
  267. * PIC 9(19) .. occupa 16 byte
  268. *
  269. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  270. */
  271. public void testInteger9_9(){
  272. try{
  273. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  274. cobolTypeDescriptor.setType(CobolType.INTEGER);
  275. cobolTypeDescriptor.setIntegerPartLength(9);
  276. cobolTypeDescriptor.setBigEndian(true);
  277. Double value=Double.valueOf(123456789L);
  278. //String value="pippo";
  279. byte[] buffer=new byte[4];
  280. byte[] expectedBuffer=BigInteger.valueOf(123456789L).toByteArray(); //cosi' semplice??
  281. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  282. long millis1=System.currentTimeMillis();
  283. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  284. long millis2=System.currentTimeMillis();
  285. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  286. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  287. log.debug("conversion time="+(millis2-millis1)+" millis");
  288. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  289. // unformat
  290. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  291. assertEquals(value, ((BigInteger)result).doubleValue());
  292. }
  293. catch(Exception e){
  294. e.printStackTrace();
  295. fail(e.getMessage());
  296. }
  297. }
  298. /**
  299. * testo un integer -- comp
  300. * la dichiarazione è
  301. * PIC 9(16) comp
  302. * valore 1234567891234567
  303. *
  304. * PIC 9(1)-9(2) occupa 1 byte
  305. * PIC 9(3)-9(4) occupa 2 byte
  306. * PIC 9(5)-9(9) occupa 4 byte
  307. * PIC 9(10)-9(18) occupa 8 byte
  308. * PIC 9(19) .. occupa 16 byte
  309. *
  310. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  311. */
  312. public void testInteger9_16(){
  313. try{
  314. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  315. cobolTypeDescriptor.setType(CobolType.INTEGER);
  316. cobolTypeDescriptor.setIntegerPartLength(16);
  317. cobolTypeDescriptor.setBigEndian(true);
  318. Double value=Double.valueOf(1234567891234567L);
  319. byte[] buffer=new byte[8];
  320. byte[] expectedBuffer=new byte[] {0x00, 0x04, 0x62, (byte)0xD5, 0x3C, (byte)0x9B, (byte)0xAF, 0x07};
  321. long millis1=System.currentTimeMillis();
  322. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  323. long millis2=System.currentTimeMillis();
  324. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  325. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  326. log.debug("conversion time="+(millis2-millis1)+" millis");
  327. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  328. // unformat
  329. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  330. assertEquals(value, ((BigInteger)result).doubleValue());
  331. }
  332. catch(Exception e){
  333. e.printStackTrace();
  334. fail(e.getMessage());
  335. }
  336. }
  337. public void testIntegerm9_16(){
  338. try{
  339. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  340. cobolTypeDescriptor.setType(CobolType.INTEGER);
  341. cobolTypeDescriptor.setIntegerPartLength(16);
  342. cobolTypeDescriptor.setBigEndian(true);
  343. Double value=Double.valueOf(-1234567891234567L);
  344. byte[] buffer=new byte[8];
  345. byte[] expectedBuffer=new byte[] {(byte)0xFF, (byte)0xFB, (byte)0x9D, 0x2A, (byte)0xC3, 0x64, 0x50, (byte)0xF9};
  346. long millis1=System.currentTimeMillis();
  347. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  348. long millis2=System.currentTimeMillis();
  349. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  350. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  351. log.debug("conversion time="+(millis2-millis1)+" millis");
  352. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  353. // unformat
  354. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  355. assertEquals(value, ((BigInteger)result).doubleValue());
  356. }
  357. catch(Exception e){
  358. e.printStackTrace();
  359. fail(e.getMessage());
  360. }
  361. }
  362. }