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.

TestRowStyle.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* ====================================================================
  2. Copyright 2002-2004 Apache Software Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ==================================================================== */
  13. package org.apache.poi.hssf.usermodel;
  14. import junit.framework.TestCase;
  15. import org.apache.poi.hssf.HSSFTestDataSamples;
  16. /**
  17. * Class to test row styling functionality
  18. *
  19. * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  20. */
  21. public final class TestRowStyle extends TestCase {
  22. /**
  23. * TEST NAME: Test Write Sheet Font <P>
  24. * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
  25. * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
  26. * Last row, first row is tested against the correct values (99,0).<P>
  27. * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
  28. * HSSFSheet last row or first row is incorrect. <P>
  29. *
  30. */
  31. public void testWriteSheetFont() {
  32. HSSFWorkbook wb = new HSSFWorkbook();
  33. HSSFSheet s = wb.createSheet();
  34. HSSFRow r = null;
  35. HSSFFont fnt = wb.createFont();
  36. HSSFCellStyle cs = wb.createCellStyle();
  37. fnt.setColor(HSSFFont.COLOR_RED);
  38. fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  39. cs.setFont(fnt);
  40. for (int rownum = 0; rownum < 100; rownum++)
  41. {
  42. r = s.createRow(rownum);
  43. r.setRowStyle(cs);
  44. r.createCell(0);
  45. }
  46. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  47. SanityChecker sanityChecker = new SanityChecker();
  48. sanityChecker.checkHSSFWorkbook(wb);
  49. assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
  50. assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
  51. }
  52. /**
  53. * Tests that is creating a file with a date or an calendar works correctly.
  54. */
  55. public void testDataStyle() {
  56. HSSFWorkbook wb = new HSSFWorkbook();
  57. HSSFSheet s = wb.createSheet();
  58. HSSFCellStyle cs = wb.createCellStyle();
  59. HSSFRow row = s.createRow(0);
  60. // with Date:
  61. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  62. row.setRowStyle(cs);
  63. row.createCell(0);
  64. // with Calendar:
  65. row = s.createRow(1);
  66. cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
  67. row.setRowStyle(cs);
  68. row.createCell(0);
  69. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  70. SanityChecker sanityChecker = new SanityChecker();
  71. sanityChecker.checkHSSFWorkbook(wb);
  72. assertEquals("LAST ROW ", 1, s.getLastRowNum());
  73. assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
  74. }
  75. /**
  76. * TEST NAME: Test Write Sheet Style <P>
  77. * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
  78. * and borders.<P>
  79. * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
  80. * Last row, first row is tested against the correct values (99,0).<P>
  81. * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
  82. * HSSFSheet last row or first row is incorrect. <P>
  83. *
  84. */
  85. public void testWriteSheetStyle() {
  86. HSSFWorkbook wb = new HSSFWorkbook();
  87. HSSFSheet s = wb.createSheet();
  88. HSSFRow r = null;
  89. HSSFFont fnt = wb.createFont();
  90. HSSFCellStyle cs = wb.createCellStyle();
  91. HSSFCellStyle cs2 = wb.createCellStyle();
  92. cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  93. cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  94. cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
  95. cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
  96. cs.setFillForegroundColor(( short ) 0xA);
  97. cs.setFillPattern(( short ) 1);
  98. fnt.setColor(( short ) 0xf);
  99. fnt.setItalic(true);
  100. cs2.setFillForegroundColor(( short ) 0x0);
  101. cs2.setFillPattern(( short ) 1);
  102. cs2.setFont(fnt);
  103. for (int rownum = 0; rownum < 100; rownum++)
  104. {
  105. r = s.createRow(rownum);
  106. r.setRowStyle(cs);
  107. r.createCell(0);
  108. rownum++;
  109. if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/
  110. r = s.createRow(rownum);
  111. r.setRowStyle(cs2);
  112. r.createCell(0);
  113. }
  114. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  115. SanityChecker sanityChecker = new SanityChecker();
  116. sanityChecker.checkHSSFWorkbook(wb);
  117. assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
  118. assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
  119. s = wb.getSheetAt(0);
  120. assertNotNull("Sheet is not null", s);
  121. for (int rownum = 0; rownum < 100; rownum++)
  122. {
  123. r = s.getRow(rownum);
  124. assertNotNull("Row is not null", r);
  125. cs = r.getRowStyle();
  126. assertEquals("FillForegroundColor for row: ", cs.getBorderBottom(), HSSFCellStyle.BORDER_THIN);
  127. assertEquals("FillPattern for row: ", cs.getBorderLeft(), HSSFCellStyle.BORDER_THIN);
  128. assertEquals("FillForegroundColor for row: ", cs.getBorderRight(), HSSFCellStyle.BORDER_THIN);
  129. assertEquals("FillPattern for row: ", cs.getBorderTop(), HSSFCellStyle.BORDER_THIN);
  130. assertEquals("FillForegroundColor for row: ", cs.getFillForegroundColor(), 0xA);
  131. assertEquals("FillPattern for row: ", cs.getFillPattern(), (short) 0x1);
  132. rownum++;
  133. if (rownum >= 100) break; // I feel too lazy to check if this isreqd :-/
  134. r = s.getRow(rownum);
  135. assertNotNull("Row is not null", r);
  136. cs2 = r.getRowStyle();
  137. assertEquals("FillForegroundColor for row: ", cs2.getFillForegroundColor(), (short) 0x0);
  138. assertEquals("FillPattern for row: ", cs2.getFillPattern(), (short) 0x1);
  139. }
  140. }
  141. }