PageRenderTime 38ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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