PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/SpudSoft BIRT Excel Emitters Tests/src/uk/co/spudsoft/birt/emitters/excel/tests/Issue36XlsColours.java

https://bitbucket.org/xunchangguo/spudsoft-birt-excel-emitters
Java | 189 lines | 129 code | 38 blank | 22 comment | 16 complexity | 6a484cbd8e198670f7720c19655d12f6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0
  1. /*************************************************************
  2. * (C) Copyright 2011, 2012 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * This file is part of The SpudSoft BIRT Excel Emitters.
  6. * The SpudSoft BIRT Excel Emitters are free software: you can
  7. * redistribute them and/or modify them under the terms of the
  8. * GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * The SpudSoft BIRT Excel Emitters are distributed in the hope
  13. * that they will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the SpudSoft BIRT Excel Emitters.
  20. * If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *************************************************************/
  23. package uk.co.spudsoft.birt.emitters.excel.tests;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertNotNull;
  26. import static org.junit.Assert.assertNull;
  27. import java.io.File;
  28. import java.io.FileOutputStream;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import org.apache.poi.hssf.usermodel.HSSFCell;
  32. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  33. import org.apache.poi.hssf.usermodel.HSSFPalette;
  34. import org.apache.poi.hssf.usermodel.HSSFRow;
  35. import org.apache.poi.hssf.usermodel.HSSFSheet;
  36. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  37. import org.apache.poi.hssf.util.HSSFColor;
  38. import org.apache.poi.ss.usermodel.Cell;
  39. import org.apache.poi.ss.usermodel.CellStyle;
  40. import org.apache.poi.ss.usermodel.Row;
  41. import org.apache.poi.ss.usermodel.Sheet;
  42. import org.apache.poi.xssf.usermodel.XSSFCell;
  43. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  44. import org.apache.poi.xssf.usermodel.XSSFColor;
  45. import org.apache.poi.xssf.usermodel.XSSFRow;
  46. import org.apache.poi.xssf.usermodel.XSSFSheet;
  47. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  48. import org.eclipse.birt.core.exception.BirtException;
  49. import org.junit.Test;
  50. public class Issue36XlsColours extends ReportRunner {
  51. private void dumpPalette( HSSFWorkbook workbook, String sheetName ) {
  52. Sheet sheet = workbook.createSheet( sheetName );
  53. HSSFPalette palette = workbook.getCustomPalette();
  54. System.out.println( "Palette: " + palette.toString() );
  55. for( short i = 0; i < 100; ++i) {
  56. Row row = sheet.createRow(i);
  57. CellStyle style = workbook.createCellStyle();
  58. style.setFillForegroundColor( i );
  59. style.setFillPattern(CellStyle.SOLID_FOREGROUND);
  60. Cell cell = row.createCell(0);
  61. cell.setCellValue( i );
  62. cell.setCellStyle(style);
  63. cell = row.createCell(1);
  64. cell.setCellValue( i );
  65. cell = row.createCell(2);
  66. cell.setCellValue( palette.getColor(i) == null ? "null" : palette.getColor(i).getHexString() );
  67. }
  68. }
  69. @Test
  70. public void generateSpectrum() throws Exception {
  71. HSSFWorkbook workbook = new HSSFWorkbook();
  72. dumpPalette(workbook, "default");
  73. HSSFPalette palette = workbook.getCustomPalette();
  74. try {
  75. palette.addColor( (byte)111, (byte)123, (byte)146);
  76. } catch( RuntimeException ex ) {
  77. System.err.println( ex.toString() );
  78. }
  79. File outputFile = createTempFile( "Issue36", ".XLS" );
  80. System.err.println( outputFile );
  81. FileOutputStream stream = new FileOutputStream(outputFile);
  82. try {
  83. workbook.write(stream);
  84. } finally {
  85. stream.close();
  86. }
  87. }
  88. @Test
  89. public void testColourMatching() throws BirtException, IOException {
  90. debug = false;
  91. InputStream inputStreamX = runAndRenderReport("issue_35_36_37_38.rptdesign", "xlsx");
  92. assertNotNull(inputStreamX);
  93. try {
  94. XSSFWorkbook workbookX = new XSSFWorkbook(inputStreamX);
  95. assertNotNull(workbookX);
  96. assertEquals( 1, workbookX.getNumberOfSheets() );
  97. XSSFSheet sheetX = workbookX.getSheetAt(0);
  98. InputStream inputStreamS = runAndRenderReport("issue_35_36_37_38.rptdesign", "xls");
  99. assertNotNull(inputStreamS);
  100. try {
  101. HSSFWorkbook workbookS = new HSSFWorkbook(inputStreamS);
  102. assertNotNull(workbookS);
  103. assertEquals( workbookX.getNumberOfSheets(), workbookS.getNumberOfSheets() );
  104. HSSFSheet sheetS = workbookS.getSheetAt(0);
  105. assertEquals( firstNullRow(sheetX), firstNullRow( sheetS ) );
  106. for( int rownum = 0; rownum < firstNullRow(sheetX); ++rownum ) {
  107. XSSFRow rowX = sheetX.getRow(rownum);
  108. HSSFRow rowS = sheetS.getRow(rownum);
  109. assertEquals( rowX.getLastCellNum(), rowS.getLastCellNum() );
  110. for( int colnum = 0; colnum < rowX.getLastCellNum(); ++colnum ) {
  111. XSSFCell cellX = rowX.getCell(colnum);
  112. HSSFCell cellS = rowS.getCell(colnum);
  113. if( cellX != null ) {
  114. assertNotNull( cellS );
  115. XSSFCellStyle styleX = cellX.getCellStyle();
  116. HSSFCellStyle styleS = cellS.getCellStyle();
  117. if( styleX != null ) {
  118. assertNotNull( styleX );
  119. XSSFColor colorX = styleX.getFillForegroundColorColor();
  120. HSSFColor colorS = styleS.getFillForegroundColorColor();
  121. if( colorX != null ) {
  122. assertNotNull( colorS );
  123. byte[] rgbX = colorX.getARgb();
  124. short[] rgbS = colorS.getTriplet();
  125. System.out.println( "Comparing " + colorX.getARGBHex() + " with " + colorS.getHexString() );
  126. assertEquals( rgbX[ 1 ], (byte)rgbS[ 0 ] );
  127. assertEquals( rgbX[ 2 ], (byte)rgbS[ 1 ] );
  128. assertEquals( rgbX[ 3 ], (byte)rgbS[ 2 ] );
  129. } else {
  130. if( colorS != null ) {
  131. if( ! "0:0:0".equals( colorS.getHexString() ) ) {
  132. assertNull( colorS.getHexString(), colorS );
  133. }
  134. }
  135. }
  136. } else {
  137. assertNull( styleS );
  138. }
  139. } else {
  140. assertNull( cellS );
  141. }
  142. }
  143. }
  144. } finally {
  145. inputStreamS.close();
  146. }
  147. } finally {
  148. inputStreamX.close();
  149. }
  150. }
  151. }