KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > aggregates > TestValueRecordsAggregate


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

16
17 package org.apache.poi.hssf.record.aggregates;
18
19 import junit.framework.TestCase;
20 import org.apache.poi.hssf.record.*;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 public class TestValueRecordsAggregate extends TestCase
27 {
28     ValueRecordsAggregate valueRecord = new ValueRecordsAggregate();
29
30     /**
31      * Make sure the shared formula makes it to the FormulaRecordAggregate when being parsed
32      * as part of the value records
33      */

34     public void testSharedFormula()
35     {
36         List JavaDoc records = new ArrayList JavaDoc();
37         records.add( new FormulaRecord() );
38         records.add( new SharedFormulaRecord() );
39
40         valueRecord.construct( 0, records );
41         Iterator JavaDoc iterator = valueRecord.getIterator();
42         Record record = (Record) iterator.next();
43         assertNotNull( "Row contains a value", record );
44         assertTrue( "First record is a FormulaRecordsAggregate", ( record instanceof FormulaRecordAggregate ) );
45         FormulaRecordAggregate aggregate = (FormulaRecordAggregate) record;
46         assertNotNull( "SharedFormulaRecord is null", aggregate.getSharedFormulaRecord() );
47
48     }
49
50     public void testUnknownRecordsIgnored()
51     {
52         List JavaDoc records = testData();
53         valueRecord.construct( 0, records );
54         Iterator JavaDoc iterator = valueRecord.getIterator();
55         Record record1 = (Record) iterator.next();
56         Record record2 = (Record) iterator.next();
57         assertNotNull( "No record found", record1 );
58         assertNotNull( "No record found", record2 );
59         assertFalse( iterator.hasNext() );
60
61     }
62
63     private List JavaDoc testData(){
64         List JavaDoc records = new ArrayList JavaDoc();
65         FormulaRecord formulaRecord = new FormulaRecord();
66         UnknownRecord unknownRecord = new UnknownRecord();
67         BlankRecord blankRecord = new BlankRecord();
68         WindowOneRecord windowOneRecord = new WindowOneRecord();
69         formulaRecord.setRow( 1 );
70         formulaRecord.setColumn( (short) 1 );
71         blankRecord.setRow( 2 );
72         blankRecord.setColumn( (short) 2 );
73         records.add( formulaRecord );
74         records.add( unknownRecord );
75         records.add( blankRecord );
76         records.add( windowOneRecord );
77         return records;
78     }
79
80     public void testInsertCell()
81             throws Exception JavaDoc
82     {
83         Iterator JavaDoc iterator = valueRecord.getIterator();
84         assertFalse( iterator.hasNext() );
85
86         BlankRecord blankRecord = newBlankRecord();
87         valueRecord.insertCell( blankRecord );
88         iterator = valueRecord.getIterator();
89         assertTrue( iterator.hasNext() );
90     }
91
92     public void testRemoveCell()
93             throws Exception JavaDoc
94     {
95         BlankRecord blankRecord1 = newBlankRecord();
96         valueRecord.insertCell( blankRecord1 );
97         BlankRecord blankRecord2 = newBlankRecord();
98         valueRecord.removeCell( blankRecord2 );
99         Iterator JavaDoc iterator = valueRecord.getIterator();
100         assertFalse( iterator.hasNext() );
101
102         // removing an already empty cell just falls through
103
valueRecord.removeCell( blankRecord2 );
104
105         // even trying to remove null just falls through silently.
106
valueRecord.removeCell( null );
107
108     }
109
110     public void testGetPhysicalNumberOfCells() throws Exception JavaDoc
111     {
112         assertEquals(0, valueRecord.getPhysicalNumberOfCells());
113         BlankRecord blankRecord1 = newBlankRecord();
114         valueRecord.insertCell( blankRecord1 );
115         assertEquals(1, valueRecord.getPhysicalNumberOfCells());
116         valueRecord.removeCell( blankRecord1 );
117         assertEquals(0, valueRecord.getPhysicalNumberOfCells());
118     }
119
120     public void testGetFirstCellNum() throws Exception JavaDoc
121     {
122         assertEquals( -1, valueRecord.getFirstCellNum() );
123         valueRecord.insertCell( newBlankRecord( 2, 2 ) );
124         assertEquals( 2, valueRecord.getFirstCellNum() );
125         valueRecord.insertCell( newBlankRecord( 3, 3 ) );
126         assertEquals( 2, valueRecord.getFirstCellNum() );
127
128         // Note: Removal doesn't currently reset the first column. It probably should but it doesn't.
129
valueRecord.removeCell( newBlankRecord( 2, 2 ) );
130         assertEquals( 2, valueRecord.getFirstCellNum() );
131     }
132
133     public void testGetLastCellNum() throws Exception JavaDoc
134     {
135         assertEquals( -1, valueRecord.getLastCellNum() );
136         valueRecord.insertCell( newBlankRecord( 2, 2 ) );
137         assertEquals( 2, valueRecord.getLastCellNum() );
138         valueRecord.insertCell( newBlankRecord( 3, 3 ) );
139         assertEquals( 3, valueRecord.getLastCellNum() );
140
141         // Note: Removal doesn't currently reset the last column. It probably should but it doesn't.
142
valueRecord.removeCell( newBlankRecord( 3, 3 ) );
143         assertEquals( 3, valueRecord.getLastCellNum() );
144
145     }
146
147     public void testSerialize() throws Exception JavaDoc
148     {
149         byte[] actualArray = new byte[36];
150         byte[] expectedArray = new byte[]
151         {
152             (byte)0x06, (byte)0x00, (byte)0x16, (byte)0x00,
153             (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00,
154             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
155             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
156             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
157             (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
158             (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02,
159             (byte)0x06, (byte)0x00, (byte)0x02, (byte)0x00,
160             (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00,
161         };
162         List JavaDoc records = testData();
163         valueRecord.construct( 0, records );
164         int bytesWritten = valueRecord.serializeCellRow(1, 0, actualArray );
165         bytesWritten += valueRecord.serializeCellRow(2, bytesWritten, actualArray );
166         assertEquals( 36, bytesWritten );
167         for (int i = 0; i < 36; i++)
168             assertEquals( expectedArray[i], actualArray[i] );
169     }
170
171     public static void main( String JavaDoc[] args )
172     {
173         System.out.println( "Testing org.apache.poi.hssf.record.aggregates.TestValueRecordAggregate" );
174         junit.textui.TestRunner.run( TestValueRecordsAggregate.class );
175     }
176
177     private BlankRecord newBlankRecord()
178     {
179         return newBlankRecord( 2, 2 );
180     }
181
182     private BlankRecord newBlankRecord( int col, int row)
183     {
184         BlankRecord blankRecord = new BlankRecord();
185         blankRecord.setRow( row );
186         blankRecord.setColumn( (short) col );
187         return blankRecord;
188     }
189
190     public void testGetRecordSize() throws Exception JavaDoc
191     {
192         List JavaDoc records = testData();
193         valueRecord.construct( 0, records );
194         assertEquals( 36, valueRecord.getRecordSize() );
195     }
196
197     public void testClone() throws Exception JavaDoc
198     {
199         List JavaDoc records = testData();
200         valueRecord.construct( 0, records );
201         valueRecord = (ValueRecordsAggregate) valueRecord.clone();
202         assertEquals( 36, valueRecord.getRecordSize() );
203     }
204
205 }
206
Popular Tags