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