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

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

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