PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pymma/openesb-components
Java | 445 lines | 262 code | 60 blank | 123 comment | 0 complexity | 60d17d0ffd9f8d6e33446011cf9261e1 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. public class JavaBigDecimalTypeMappingTest extends TestCase {
  20. private static Log log=LogFactory.getLog(JavaBigDecimalTypeMappingTest.class);
  21. public static final String DeafultHostCodePage="CP1144";
  22. /**
  23. * testo un packed decimal -- comp3
  24. * la dichiarazione è
  25. * PIC 9(18) comp-3
  26. * valore 123456789123456789
  27. *
  28. */
  29. public void testPackedDecimal9_18(){
  30. try{
  31. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  32. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  33. cobolTypeDescriptor.setIntegerPartLength(18);
  34. BigDecimal value=new BigDecimal("123456789123456789");
  35. byte[] buffer=new byte[10];
  36. byte[] expectedBuffer=new byte[] {(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,(byte)0x89,(byte)0x12,(byte)0x34,(byte)0x56,(byte)0x78,(byte)0x9f};
  37. long millis1=System.currentTimeMillis();
  38. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  39. long millis2=System.currentTimeMillis();
  40. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  41. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  42. log.debug("conversion time="+(millis2-millis1)+" millis");
  43. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  44. // unformat
  45. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  46. assertEquals(value, ((BigDecimal)result));
  47. }
  48. catch(Exception e){
  49. e.printStackTrace();
  50. fail(e.getMessage());
  51. }
  52. }
  53. /**
  54. * testo un packed decimal -- comp3
  55. * la dichiarazione è
  56. * PIC 9(18)v9(2) comp-3
  57. * valore 123456789123456789.12
  58. *
  59. */
  60. public void testPackedDecimal9_18_9_2(){
  61. try{
  62. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  63. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  64. cobolTypeDescriptor.setIntegerPartLength(18);
  65. cobolTypeDescriptor.setDecimalPartLength(2);
  66. log.debug("virtual decimal point: "+cobolTypeDescriptor.getVirtualDecimalPoint());
  67. BigDecimal value=new BigDecimal("123456789123456789.12");
  68. byte[] buffer=new byte[11];
  69. byte[] expectedBuffer=new byte[] {(byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,(byte)0x89,(byte)0x12,(byte)0x34,(byte)0x56,(byte)0x78,(byte)0x91,(byte)0x2f};
  70. long millis1=System.currentTimeMillis();
  71. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  72. long millis2=System.currentTimeMillis();
  73. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  74. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  75. log.debug("conversion time="+(millis2-millis1)+" millis");
  76. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  77. // unformat
  78. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  79. assertEquals(value, ((BigDecimal)result));
  80. }
  81. catch(Exception e){
  82. e.printStackTrace();
  83. fail(e.getMessage());
  84. }
  85. }
  86. /**
  87. * testo un packed decimal -- comp3
  88. * la dichiarazione è
  89. * PIC 9(18) comp-3
  90. * valore 10
  91. *
  92. */
  93. public void testPackedDecimal9_18nc(){
  94. try{
  95. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  96. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  97. cobolTypeDescriptor.setIntegerPartLength(18);
  98. BigDecimal value=new BigDecimal("10");
  99. byte[] buffer=new byte[10];
  100. byte[] expectedBuffer=new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01,(byte)0x0f};
  101. long millis1=System.currentTimeMillis();
  102. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  103. long millis2=System.currentTimeMillis();
  104. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  105. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  106. log.debug("conversion time="+(millis2-millis1)+" millis");
  107. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  108. // unformat
  109. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  110. assertEquals(value, ((BigDecimal)result));
  111. }
  112. catch(Exception e){
  113. e.printStackTrace();
  114. fail(e.getMessage());
  115. }
  116. }
  117. /**
  118. * testo un packed decimal -- comp3
  119. * la dichiarazione è
  120. * PIC 9(18)v9(2) comp-3
  121. * valore 10.12
  122. *
  123. */
  124. public void testPackedDecimal9_18_9_2nc(){
  125. try{
  126. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  127. cobolTypeDescriptor.setType(CobolType.PACKED_DECIMAL);
  128. cobolTypeDescriptor.setIntegerPartLength(18);
  129. cobolTypeDescriptor.setDecimalPartLength(2);
  130. log.debug("virtual decimal point: "+cobolTypeDescriptor.getVirtualDecimalPoint());
  131. BigDecimal value=new BigDecimal("10.12");
  132. byte[] buffer=new byte[11];
  133. byte[] expectedBuffer=new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01,(byte)0x01,(byte)0x2f};
  134. long millis1=System.currentTimeMillis();
  135. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  136. long millis2=System.currentTimeMillis();
  137. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  138. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  139. log.debug("conversion time="+(millis2-millis1)+" millis");
  140. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  141. // unformat
  142. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  143. assertEquals(value, ((BigDecimal)result));
  144. }
  145. catch(Exception e){
  146. e.printStackTrace();
  147. fail(e.getMessage());
  148. }
  149. }
  150. /**
  151. * testo un zoned
  152. * PIC 9(10)
  153. * con valore 10
  154. *
  155. */
  156. public void testZoned9_10(){
  157. try{
  158. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  159. cobolTypeDescriptor.setType(CobolType.ZONED);
  160. cobolTypeDescriptor.setIntegerPartLength(10);
  161. cobolTypeDescriptor.setCodePage(DeafultHostCodePage);
  162. cobolTypeDescriptor.setZonedSignFormat(CobolTypeDescriptor.SIGN_FORMAT_TRAILING);
  163. cobolTypeDescriptor.setPadCharacter(" ");
  164. BigDecimal value=new BigDecimal("10");
  165. byte[] buffer=new byte[10];
  166. byte[] expectedBuffer=new byte[] {(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf0,(byte)0xf1,(byte)0xf0};
  167. // '0' '0' '0' '0' '0' '0' '0' '0' '1' '0'
  168. long millis1=System.currentTimeMillis();
  169. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  170. long millis2=System.currentTimeMillis();
  171. log.debug("buffer dopo il format in string: ["+new String(buffer,DeafultHostCodePage)+"]");
  172. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  173. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  174. log.debug("conversion time="+(millis2-millis1)+" millis");
  175. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  176. // unformat
  177. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  178. assertEquals(value, ((BigDecimal)result));
  179. }
  180. catch(Exception e){
  181. e.printStackTrace();
  182. fail(e.getMessage());
  183. }
  184. }
  185. /**
  186. * testo un zoned
  187. * PIC 9(10)v9(2)
  188. * con valore 10.10
  189. *
  190. */
  191. public void testZoned9_10_9_2(){
  192. try{
  193. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  194. cobolTypeDescriptor.setType(CobolType.ZONED);
  195. cobolTypeDescriptor.setIntegerPartLength(10);
  196. cobolTypeDescriptor.setDecimalPartLength(2);
  197. cobolTypeDescriptor.setCodePage(DeafultHostCodePage);
  198. cobolTypeDescriptor.setZonedSignFormat(CobolTypeDescriptor.SIGN_FORMAT_TRAILING);
  199. cobolTypeDescriptor.setPadCharacter(" ");
  200. BigDecimal value=new BigDecimal("10.10");
  201. byte[] buffer=new byte[12];
  202. 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};
  203. // '0' '0' '0' '0' '0' '0' '0' '0' '1' '0' '1' '0'
  204. long millis1=System.currentTimeMillis();
  205. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  206. long millis2=System.currentTimeMillis();
  207. log.debug("buffer dopo il format in string: ["+new String(buffer,DeafultHostCodePage)+"]");
  208. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  209. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  210. log.debug("conversion time="+(millis2-millis1)+" millis");
  211. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  212. // unformat
  213. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  214. assertEquals(value, ((BigDecimal)result));
  215. }
  216. catch(Exception e){
  217. e.printStackTrace();
  218. fail(e.getMessage());
  219. }
  220. }
  221. /**
  222. * testo un integer -- comp
  223. * la dichiarazione è
  224. * PIC 9(2) comp
  225. * valore 123456789
  226. *
  227. * PIC 9(1)-9(2) occupa 1 byte
  228. * PIC 9(3)-9(4) occupa 2 byte
  229. * PIC 9(5)-9(9) occupa 4 byte
  230. * PIC 9(10)-9(18) occupa 8 byte
  231. * PIC 9(19) .. occupa 16 byte
  232. *
  233. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  234. */
  235. public void testInteger9_2(){
  236. try{
  237. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  238. cobolTypeDescriptor.setType(CobolType.INTEGER);
  239. cobolTypeDescriptor.setIntegerPartLength(2);
  240. cobolTypeDescriptor.setBigEndian(true);
  241. BigDecimal value=new BigDecimal("12");
  242. //String value="pippo";
  243. byte[] buffer=new byte[1];
  244. byte[] expectedBuffer=BigInteger.valueOf(12L).toByteArray(); //cosi' semplice??
  245. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  246. long millis1=System.currentTimeMillis();
  247. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  248. long millis2=System.currentTimeMillis();
  249. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  250. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  251. log.debug("conversion time="+(millis2-millis1)+" millis");
  252. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  253. // unformat
  254. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  255. assertEquals(value, new BigDecimal(result.toString()));
  256. }
  257. catch(Exception e){
  258. e.printStackTrace();
  259. fail(e.getMessage());
  260. }
  261. }
  262. /**
  263. * testo un integer -- comp
  264. * la dichiarazione è
  265. * PIC 9(4) comp
  266. * valore 123456789
  267. *
  268. * PIC 9(1)-9(2) occupa 1 byte
  269. * PIC 9(3)-9(4) occupa 2 byte
  270. * PIC 9(5)-9(9) occupa 4 byte
  271. * PIC 9(10)-9(18) occupa 8 byte
  272. * PIC 9(19) .. occupa 16 byte
  273. *
  274. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  275. */
  276. public void testInteger9_4(){
  277. try{
  278. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  279. cobolTypeDescriptor.setType(CobolType.INTEGER);
  280. cobolTypeDescriptor.setIntegerPartLength(4);
  281. cobolTypeDescriptor.setBigEndian(true);
  282. BigDecimal value=new BigDecimal("1234");
  283. //String value="pippo";
  284. byte[] buffer=new byte[2];
  285. byte[] expectedBuffer=BigInteger.valueOf(1234L).toByteArray(); //cosi' semplice??
  286. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  287. long millis1=System.currentTimeMillis();
  288. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  289. long millis2=System.currentTimeMillis();
  290. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  291. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  292. log.debug("conversion time="+(millis2-millis1)+" millis");
  293. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  294. // unformat
  295. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  296. assertEquals(value, new BigDecimal(result.toString()));
  297. }
  298. catch(Exception e){
  299. e.printStackTrace();
  300. fail(e.getMessage());
  301. }
  302. }
  303. /**
  304. * testo un integer -- comp
  305. * la dichiarazione è
  306. * PIC 9(9) comp
  307. * valore 123456789
  308. *
  309. * PIC 9(1)-9(2) occupa 1 byte
  310. * PIC 9(3)-9(4) occupa 2 byte
  311. * PIC 9(5)-9(9) occupa 4 byte
  312. * PIC 9(10)-9(18) occupa 8 byte
  313. * PIC 9(19) .. occupa 16 byte
  314. *
  315. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  316. */
  317. public void testInteger9_9(){
  318. try{
  319. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  320. cobolTypeDescriptor.setType(CobolType.INTEGER);
  321. cobolTypeDescriptor.setIntegerPartLength(9);
  322. cobolTypeDescriptor.setBigEndian(true);
  323. BigDecimal value=new BigDecimal("123456789");
  324. //String value="pippo";
  325. byte[] buffer=new byte[4];
  326. byte[] expectedBuffer=BigInteger.valueOf(123456789L).toByteArray(); //cosi' semplice??
  327. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  328. long millis1=System.currentTimeMillis();
  329. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  330. long millis2=System.currentTimeMillis();
  331. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  332. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  333. log.debug("conversion time="+(millis2-millis1)+" millis");
  334. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  335. // unformat
  336. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  337. assertEquals(value, new BigDecimal(result.toString()));
  338. }
  339. catch(Exception e){
  340. e.printStackTrace();
  341. fail(e.getMessage());
  342. }
  343. }
  344. /**
  345. * testo un integer -- comp
  346. * la dichiarazione è
  347. * PIC 9(18) comp
  348. * valore 123456789
  349. *
  350. * PIC 9(1)-9(2) occupa 1 byte
  351. * PIC 9(3)-9(4) occupa 2 byte
  352. * PIC 9(5)-9(9) occupa 4 byte
  353. * PIC 9(10)-9(18) occupa 8 byte
  354. * PIC 9(19) .. occupa 16 byte
  355. *
  356. * la differenza fra comp e comp-4 è che comp controlla che la reappresentazione in cifre non superi quello dichiarato
  357. */
  358. public void testInteger9_18(){
  359. try{
  360. CobolTypeDescriptor cobolTypeDescriptor=new CobolTypeDescriptor();
  361. cobolTypeDescriptor.setType(CobolType.INTEGER);
  362. cobolTypeDescriptor.setIntegerPartLength(18);
  363. cobolTypeDescriptor.setBigEndian(true);
  364. BigDecimal value=new BigDecimal("123456789123456789");
  365. //String value="pippo";
  366. byte[] buffer=new byte[8];
  367. byte[] expectedBuffer=BigInteger.valueOf(123456789123456789L).toByteArray();//cosi' semplice??
  368. //byte[] expectedBuffer=value.getBytes(); //cosi' semplice??
  369. long millis1=System.currentTimeMillis();
  370. CobolFieldFormatter.format(value,buffer,cobolTypeDescriptor,0);
  371. long millis2=System.currentTimeMillis();
  372. log.debug("buffer dopo il format in esadecimale: ["+HexDump.toHex(buffer)+"]");
  373. log.debug("buffer expected in esadecimale: ["+HexDump.toHex(expectedBuffer)+"]");
  374. log.debug("conversion time="+(millis2-millis1)+" millis");
  375. assertTrue("conversione non corretta",Arrays.equals(expectedBuffer,buffer));
  376. // unformat
  377. Object result=CobolFieldFormatter.unformat(buffer, cobolTypeDescriptor, 0);
  378. assertEquals(value, new BigDecimal(result.toString()));
  379. }
  380. catch(Exception e){
  381. e.printStackTrace();
  382. fail(e.getMessage());
  383. }
  384. }
  385. }