You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestHSSFSheet.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Apache" and "Apache Software Foundation" and
  27. * "Apache POI" must not be used to endorse or promote products
  28. * derived from this software without prior written permission. For
  29. * written permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * "Apache POI", nor may "Apache" appear in their name, without
  33. * prior written permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.poi.hssf.usermodel;
  55. import junit.framework.TestCase;
  56. import org.apache.poi.hssf.model.Sheet;
  57. import org.apache.poi.hssf.record.HCenterRecord;
  58. import org.apache.poi.hssf.record.VCenterRecord;
  59. import org.apache.poi.hssf.record.WSBoolRecord;
  60. import java.io.File;
  61. import java.io.FileInputStream;
  62. import java.io.FileOutputStream;
  63. /**
  64. * Tests HSSFSheet. This test case is very incomplete at the moment.
  65. *
  66. *
  67. * @author Glen Stampoultzis (glens at apache.org)
  68. * @version %I%, %G%
  69. */
  70. public class TestHSSFSheet
  71. extends TestCase
  72. {
  73. public TestHSSFSheet(String s)
  74. {
  75. super(s);
  76. }
  77. /**
  78. * Test the gridset field gets set as expected.
  79. */
  80. public void testBackupRecord()
  81. throws Exception
  82. {
  83. HSSFWorkbook wb = new HSSFWorkbook();
  84. HSSFSheet s = wb.createSheet();
  85. Sheet sheet = s.getSheet();
  86. assertEquals(true, sheet.getGridsetRecord().getGridset());
  87. s.setGridsPrinted(true);
  88. assertEquals(false, sheet.getGridsetRecord().getGridset());
  89. }
  90. /**
  91. * Test vertically centered output.
  92. */
  93. public void testVerticallyCenter()
  94. throws Exception
  95. {
  96. HSSFWorkbook wb = new HSSFWorkbook();
  97. HSSFSheet s = wb.createSheet();
  98. Sheet sheet = s.getSheet();
  99. VCenterRecord record =
  100. (VCenterRecord) sheet.findFirstRecordBySid(VCenterRecord.sid);
  101. assertEquals(false, record.getVCenter());
  102. s.setVerticallyCenter(true);
  103. assertEquals(true, record.getVCenter());
  104. // wb.write(new FileOutputStream("c:\\test.xls"));
  105. }
  106. /**
  107. * Test horizontally centered output.
  108. */
  109. public void testHorizontallyCenter()
  110. throws Exception
  111. {
  112. HSSFWorkbook wb = new HSSFWorkbook();
  113. HSSFSheet s = wb.createSheet();
  114. Sheet sheet = s.getSheet();
  115. HCenterRecord record =
  116. (HCenterRecord) sheet.findFirstRecordBySid(HCenterRecord.sid);
  117. assertEquals(false, record.getHCenter());
  118. s.setHorizontallyCenter(true);
  119. assertEquals(true, record.getHCenter());
  120. }
  121. /**
  122. * Test WSBboolRecord fields get set in the user model.
  123. */
  124. public void testWSBool()
  125. {
  126. HSSFWorkbook wb = new HSSFWorkbook();
  127. HSSFSheet s = wb.createSheet();
  128. Sheet sheet = s.getSheet();
  129. WSBoolRecord record =
  130. (WSBoolRecord) sheet.findFirstRecordBySid(WSBoolRecord.sid);
  131. // Check defaults
  132. assertEquals(true, record.getAlternateExpression());
  133. assertEquals(true, record.getAlternateFormula());
  134. assertEquals(false, record.getAutobreaks());
  135. assertEquals(false, record.getDialog());
  136. assertEquals(false, record.getDisplayGuts());
  137. assertEquals(true, record.getFitToPage());
  138. assertEquals(false, record.getRowSumsBelow());
  139. assertEquals(false, record.getRowSumsRight());
  140. // Alter
  141. s.setAlternativeExpression(false);
  142. s.setAlternativeFormula(false);
  143. s.setAutobreaks(true);
  144. s.setDialog(true);
  145. s.setDisplayGuts(true);
  146. s.setFitToPage(false);
  147. s.setRowSumsBelow(true);
  148. s.setRowSumsRight(true);
  149. // Check
  150. assertEquals(false, record.getAlternateExpression());
  151. assertEquals(false, record.getAlternateFormula());
  152. assertEquals(true, record.getAutobreaks());
  153. assertEquals(true, record.getDialog());
  154. assertEquals(true, record.getDisplayGuts());
  155. assertEquals(false, record.getFitToPage());
  156. assertEquals(true, record.getRowSumsBelow());
  157. assertEquals(true, record.getRowSumsRight());
  158. assertEquals(false, s.getAlternateExpression());
  159. assertEquals(false, s.getAlternateFormula());
  160. assertEquals(true, s.getAutobreaks());
  161. assertEquals(true, s.getDialog());
  162. assertEquals(true, s.getDisplayGuts());
  163. assertEquals(false, s.getFitToPage());
  164. assertEquals(true, s.getRowSumsBelow());
  165. assertEquals(true, s.getRowSumsRight());
  166. }
  167. public void testReadBooleans()
  168. throws Exception
  169. {
  170. HSSFWorkbook workbook = new HSSFWorkbook();
  171. HSSFSheet sheet = workbook.createSheet("Test boolean");
  172. HSSFRow row = sheet.createRow((short) 2);
  173. HSSFCell cell = row.createCell((short) 9);
  174. cell.setCellValue(true);
  175. cell = row.createCell((short) 11);
  176. cell.setCellValue(true);
  177. File tempFile = File.createTempFile("bool", "test.xls");
  178. FileOutputStream stream = new FileOutputStream(tempFile);
  179. workbook.write(stream);
  180. stream.close();
  181. FileInputStream readStream = new FileInputStream(tempFile);
  182. workbook = new HSSFWorkbook(readStream);
  183. sheet = workbook.getSheetAt(0);
  184. row = sheet.getRow(2);
  185. stream.close();
  186. tempFile.delete();
  187. assertNotNull(row);
  188. assertEquals(2, row.getPhysicalNumberOfCells());
  189. }
  190. public void testRemoveRow()
  191. {
  192. HSSFWorkbook workbook = new HSSFWorkbook();
  193. HSSFSheet sheet = workbook.createSheet("Test boolean");
  194. HSSFRow row = sheet.createRow((short) 2);
  195. sheet.removeRow(row);
  196. }
  197. public void testCloneSheet() {
  198. HSSFWorkbook workbook = new HSSFWorkbook();
  199. HSSFSheet sheet = workbook.createSheet("Test Clone");
  200. HSSFRow row = sheet.createRow((short) 0);
  201. HSSFCell cell = row.createCell((short) 0);
  202. cell.setCellValue("clone_test");
  203. HSSFSheet cloned = workbook.cloneSheet(0);
  204. //Check for a good clone
  205. assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
  206. //Check that the cells are not somehow linked
  207. cell.setCellValue("Difference Check");
  208. assertEquals(cloned.getRow((short)0).getCell((short)0).getStringCellValue(), "clone_test");
  209. }
  210. /**
  211. * Tests the shiftRows function. Does three different shifts.
  212. * After each shift, writes the workbook to file and reads back to
  213. * check. This ensures that if some changes code that breaks
  214. * writing or what not, they realize it.
  215. *
  216. * Shawn Laubach (slaubach at apache dot org)
  217. */
  218. public void testShiftRows() throws Exception
  219. {
  220. // Read initial file in
  221. String filename = System.getProperty( "HSSF.testdata.path" );
  222. filename = filename + "/SimpleMultiCell.xls";
  223. FileInputStream fin = new FileInputStream( filename );
  224. HSSFWorkbook wb = new HSSFWorkbook( fin );
  225. fin.close();
  226. HSSFSheet s = wb.getSheetAt( 0 );
  227. // Shift the second row down 1 and write to temp file
  228. s.shiftRows( 1, 1, 1 );
  229. File tempFile = File.createTempFile( "shift", "test.xls" );
  230. FileOutputStream fout = new FileOutputStream( tempFile );
  231. wb.write( fout );
  232. fout.close();
  233. // Read from temp file and check the number of cells in each
  234. // row (in original file each row was unique)
  235. fin = new FileInputStream( tempFile );
  236. wb = new HSSFWorkbook( fin );
  237. fin.close();
  238. s = wb.getSheetAt( 0 );
  239. assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 1 );
  240. assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 );
  241. assertEquals( s.getRow( 2 ).getPhysicalNumberOfCells(), 2 );
  242. assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 4 );
  243. assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 );
  244. // Shift rows 1-3 down 3 in the current one. This tests when
  245. // 1 row is blank. Write to a another temp file
  246. s.shiftRows( 0, 2, 3 );
  247. tempFile = File.createTempFile( "shift", "test.xls" );
  248. fout = new FileOutputStream( tempFile );
  249. wb.write( fout );
  250. fout.close();
  251. // Read and ensure things are where they should be
  252. fin = new FileInputStream( tempFile );
  253. wb = new HSSFWorkbook( fin );
  254. fin.close();
  255. s = wb.getSheetAt( 0 );
  256. assertTrue( s.getRow( 0 ) == null || s.getRow( 0 ).getPhysicalNumberOfCells() == 0 );
  257. assertTrue( s.getRow( 1 ) == null || s.getRow( 1 ).getPhysicalNumberOfCells() == 0 );
  258. assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 );
  259. assertEquals( s.getRow( 3 ).getPhysicalNumberOfCells(), 1 );
  260. assertTrue( s.getRow( 4 ) == null || s.getRow( 4 ).getPhysicalNumberOfCells() == 0 );
  261. assertEquals( s.getRow( 5 ).getPhysicalNumberOfCells(), 2 );
  262. // Read the first file again
  263. fin = new FileInputStream( filename );
  264. wb = new HSSFWorkbook( fin );
  265. fin.close();
  266. s = wb.getSheetAt( 0 );
  267. // Shift rows 3 and 4 up and write to temp file
  268. s.shiftRows( 2, 3, -2 );
  269. tempFile = File.createTempFile( "shift", "test.xls" );
  270. fout = new FileOutputStream( tempFile );
  271. wb.write( fout );
  272. fout.close();
  273. // Read file and test
  274. fin = new FileInputStream( tempFile );
  275. wb = new HSSFWorkbook( fin );
  276. fin.close();
  277. s = wb.getSheetAt( 0 );
  278. assertEquals( s.getRow( 0 ).getPhysicalNumberOfCells(), 3 );
  279. assertEquals( s.getRow( 1 ).getPhysicalNumberOfCells(), 4 );
  280. assertTrue( s.getRow( 2 ) == null || s.getRow( 2 ).getPhysicalNumberOfCells() == 0 );
  281. assertTrue( s.getRow( 3 ) == null || s.getRow( 3 ).getPhysicalNumberOfCells() == 0 );
  282. assertEquals( s.getRow( 4 ).getPhysicalNumberOfCells(), 5 );
  283. }
  284. }