KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > TestFormulaRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19
20 package org.apache.poi.hssf.record;
21
22
23 import junit.framework.TestCase;
24
25 /**
26  * Tests the serialization and deserialization of the FormulaRecord
27  * class works correctly.
28  *
29  * @author Andrew C. Oliver
30  */

31 public class TestFormulaRecord
32         extends TestCase
33 {
34
35     public TestFormulaRecord(String JavaDoc name)
36     {
37         super(name);
38     }
39
40     public void testCreateFormulaRecord () {
41         FormulaRecord record = new FormulaRecord();
42         record.setColumn((short)0);
43         //record.setRow((short)1);
44
record.setRow(1);
45         record.setXFIndex((short)4);
46         
47         assertEquals(record.getColumn(),(short)0);
48         //assertEquals(record.getRow(),(short)1);
49
assertEquals((short)record.getRow(),(short)1);
50         assertEquals(record.getXFIndex(),(short)4);
51     }
52     
53     /**
54      * Make sure a NAN value is preserved
55      * This formula record is a representation of =1/0 at row 0, column 0
56      */

57     public void testCheckNanPreserve() {
58         byte[] formulaByte = new byte[29];
59         for (int i = 0; i < formulaByte.length; i++) formulaByte[i] = (byte)0;
60         formulaByte[4] = (byte)0x0F;
61         formulaByte[6] = (byte)0x02;
62         formulaByte[8] = (byte)0x07;
63         formulaByte[12] = (byte)0xFF;
64         formulaByte[13] = (byte)0xFF;
65         formulaByte[18] = (byte)0xE0;
66         formulaByte[19] = (byte)0xFC;
67         formulaByte[20] = (byte)0x07;
68         formulaByte[22] = (byte)0x1E;
69         formulaByte[23] = (byte)0x01;
70         formulaByte[25] = (byte)0x1E;
71         formulaByte[28] = (byte)0x06;
72         
73         FormulaRecord record = new FormulaRecord(FormulaRecord.sid, (short)29, formulaByte);
74         assertEquals("Row", 0, record.getRow());
75         assertEquals("Column", 0, record.getColumn());
76         assertTrue("Value is not NaN", Double.isNaN(record.getValue()));
77         
78         byte[] output = record.serialize();
79         assertEquals("Output size", 33, output.length); //includes sid+recordlength
80

81         for (int i = 5; i < 13;i++) {
82             assertEquals("FormulaByte NaN doesn't match", formulaByte[i], output[i+4]);
83         }
84         
85     }
86     
87     /**
88      * Tests to see if the shared formula cells properly reserialize the expPtg
89      *
90      */

91     public void testExpFormula() {
92         byte[] formulaByte = new byte[27];
93         
94         for (int i = 0; i < formulaByte.length; i++) formulaByte[i] = (byte)0;
95         
96         formulaByte[4] =(byte)0x0F;
97         formulaByte[14]=(byte)0x08;
98         formulaByte[18]=(byte)0xE0;
99         formulaByte[19]=(byte)0xFD;
100         formulaByte[20]=(byte)0x05;
101         formulaByte[22]=(byte)0x01;
102         FormulaRecord record = new FormulaRecord(FormulaRecord.sid, (short)27, formulaByte);
103         assertEquals("Row", 0, record.getRow());
104         assertEquals("Column", 0, record.getColumn());
105         byte[] output = record.serialize();
106         assertEquals("Output size", 31, output.length); //includes sid+recordlength
107
assertEquals("Offset 22", 1, output[26]);
108     }
109     
110     
111     public static void main(String JavaDoc [] ignored_args)
112     {
113         String JavaDoc filename = System.getProperty("HSSF.testdata.path");
114
115         System.out
116             .println("Testing org.apache.poi.hssf.record.FormulaRecord");
117         junit.textui.TestRunner.run(TestFormulaRecord.class);
118     }
119     
120     
121 }
122
Popular Tags