PageRenderTime 78ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/yaytay/spudsoft-birt-excel-emitters
Java | 255 lines | 197 code | 46 blank | 12 comment | 21 complexity | f8faede403b1109c7b4fd3eb5a5a1f8b MD5 | raw file
Possible License(s): EPL-1.0
  1. /*************************************************************************************
  2. * Copyright (c) 2011, 2012, 2013 James Talbut.
  3. * jim-emitters@spudsoft.co.uk
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * James Talbut - Initial implementation.
  12. ************************************************************************************/
  13. package uk.co.spudsoft.birt.emitters.excel.tests;
  14. import static org.junit.Assert.assertEquals;
  15. import static org.junit.Assert.assertNotNull;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.util.regex.Matcher;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.ss.usermodel.Cell;
  22. import org.apache.poi.ss.usermodel.Font;
  23. import org.apache.poi.ss.usermodel.IndexedColors;
  24. import org.apache.poi.ss.usermodel.Name;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.usermodel.Sheet;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.ss.util.AreaReference;
  29. import org.apache.poi.ss.util.CellReference;
  30. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  31. import org.eclipse.birt.core.exception.BirtException;
  32. import org.junit.Test;
  33. public class HyperlinksTest extends CellRangeTester {
  34. private void validateNamedRange( Workbook workbook, int index, String name, int sheetIndex, int row1, int col1, int row2, int col2 ) {
  35. Name namedRange = workbook.getNameAt(index);
  36. assertEquals( name,namedRange.getNameName() );
  37. assertEquals( sheetIndex, namedRange.getSheetIndex() );
  38. AreaReference ref = new AreaReference( namedRange.getRefersToFormula() );
  39. if( ( row1 == row2 ) && ( col1 == col2 ) ) {
  40. assertTrue( ref.isSingleCell() );
  41. assertEquals( row1, ref.getFirstCell().getRow() );
  42. assertEquals( col1, ref.getFirstCell().getCol() );
  43. } else {
  44. assertTrue( AreaReference.isContiguous( namedRange.getRefersToFormula() ) );
  45. assertEquals( row1, Math.min( ref.getFirstCell().getRow(), ref.getLastCell().getRow() ) );
  46. assertEquals( col1, Math.min( ref.getFirstCell().getCol(), ref.getLastCell().getCol() ) );
  47. assertEquals( row2, Math.max( ref.getFirstCell().getRow(), ref.getLastCell().getRow() ) );
  48. assertEquals( col2, Math.max( ref.getFirstCell().getCol(), ref.getLastCell().getCol() ) );
  49. }
  50. }
  51. protected static String buildCellReference( int zeroBasedRow, int zeroBasedCol ) {
  52. CellReference cr = new CellReference(zeroBasedRow, zeroBasedCol);
  53. return cr.formatAsString();
  54. }
  55. @Test
  56. public void testBuildCellReference() {
  57. assertEquals( "C2", buildCellReference(1, 2) );
  58. assertEquals( "Z3", buildCellReference(2, 25) );
  59. assertEquals( "AA4", buildCellReference(3, 26) );
  60. assertEquals( "AZ5", buildCellReference(4, 51) );
  61. assertEquals( "BA6", buildCellReference(5, 52) );
  62. assertEquals( "ZZ7", buildCellReference(6, 701) );
  63. assertEquals( "AAA8", buildCellReference(7, 702) );
  64. assertEquals( "BCZ9", buildCellReference(8, 1455) );
  65. assertEquals( "BDA10", buildCellReference(9, 1456) );
  66. }
  67. @Test
  68. public void testBookmarksXls() throws BirtException, IOException {
  69. debug = false;
  70. InputStream inputStream = runAndRenderReport("Bookmarks.rptdesign", "xls");
  71. assertNotNull(inputStream);
  72. try {
  73. HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
  74. assertNotNull(workbook);
  75. Sheet sheet = workbook.getSheetAt(0);
  76. int rangesValidated = 0;
  77. for( Row row : sheet ) {
  78. for( Cell cell : row ) {
  79. if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
  80. String cellValue = cell.getStringCellValue();
  81. Matcher matcher = pattern.matcher(cellValue);
  82. if( matcher.matches() ) {
  83. validateCellRange( matcher, cell );
  84. ++rangesValidated;
  85. }
  86. }
  87. }
  88. }
  89. assertEquals( 7, rangesValidated );
  90. assertEquals( 18, workbook.getNumberOfNames() );
  91. int index = 0;
  92. validateNamedRange( workbook, index++, "DataItemOne", -1, 1, 0, 1, 0 );
  93. validateNamedRange( workbook, index++, "DataItem2", -1, 1, 1, 1, 1 );
  94. validateNamedRange( workbook, index++, "Row0", -1, 1, 0, 1, 2 );
  95. validateNamedRange( workbook, index++, "_recreated__bookmark__1", -1, 2, 0, 2, 0 );
  96. validateNamedRange( workbook, index++, "DataItem4", -1, 2, 1, 2, 1 );
  97. validateNamedRange( workbook, index++, "_Row1", -1, 2, 0, 2, 2 );
  98. validateNamedRange( workbook, index++, "_recreated__bookmark__2", -1, 3, 0, 3, 0 );
  99. validateNamedRange( workbook, index++, "DataItem6", -1, 3, 1, 3, 1 );
  100. validateNamedRange( workbook, index++, "_Row2", -1, 3, 0, 3, 2 );
  101. validateNamedRange( workbook, index++, "Table_1", -1, 0, 0, 3, 2 );
  102. validateNamedRange( workbook, index++, "R5C1_R5C1", -1, 4, 0, 4, 0 );
  103. validateNamedRange( workbook, index++, "R6C1_R6C2", -1, 5, 0, 5, 0 );
  104. validateNamedRange( workbook, index++, "R7C1_R7C3", -1, 6, 0, 6, 0 );
  105. validateNamedRange( workbook, index++, "R8C1_R9C1", -1, 7, 0, 7, 0 );
  106. validateNamedRange( workbook, index++, "R10C1_R12C1", -1, 9, 0, 9, 0 );
  107. validateNamedRange( workbook, index++, "R13C1_R14C2", -1, 12, 0, 12, 0 );
  108. validateNamedRange( workbook, index++, "R15C1_R17C3", -1, 14, 0, 14, 0 );
  109. validateNamedRange( workbook, index++, "Grid_1", -1, 4, 0, 15, 2 );
  110. } finally {
  111. inputStream.close();
  112. }
  113. }
  114. @Test
  115. public void testBookmarksXlsx() throws BirtException, IOException {
  116. debug = false;
  117. InputStream inputStream = runAndRenderReport("Bookmarks.rptdesign", "xlsx");
  118. assertNotNull(inputStream);
  119. try {
  120. XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
  121. assertNotNull(workbook);
  122. Sheet sheet = workbook.getSheetAt(0);
  123. int rangesValidated = 0;
  124. for( Row row : sheet ) {
  125. for( Cell cell : row ) {
  126. if(cell.getCellType() == Cell.CELL_TYPE_STRING) {
  127. String cellValue = cell.getStringCellValue();
  128. Matcher matcher = pattern.matcher(cellValue);
  129. if( matcher.matches() ) {
  130. validateCellRange( matcher, cell );
  131. ++rangesValidated;
  132. }
  133. }
  134. }
  135. }
  136. assertEquals( 7, rangesValidated );
  137. assertEquals( 18, workbook.getNumberOfNames() );
  138. int index = 0;
  139. validateNamedRange( workbook, index++, "DataItemOne", -1, 1, 0, 1, 0 );
  140. validateNamedRange( workbook, index++, "DataItem2", -1, 1, 1, 1, 1 );
  141. validateNamedRange( workbook, index++, "Row0", -1, 1, 0, 1, 2 );
  142. validateNamedRange( workbook, index++, "_recreated__bookmark__1", -1, 2, 0, 2, 0 );
  143. validateNamedRange( workbook, index++, "DataItem4", -1, 2, 1, 2, 1 );
  144. validateNamedRange( workbook, index++, "_Row1", -1, 2, 0, 2, 2 );
  145. validateNamedRange( workbook, index++, "_recreated__bookmark__2", -1, 3, 0, 3, 0 );
  146. validateNamedRange( workbook, index++, "DataItem6", -1, 3, 1, 3, 1 );
  147. validateNamedRange( workbook, index++, "_Row2", -1, 3, 0, 3, 2 );
  148. validateNamedRange( workbook, index++, "Table_1", -1, 0, 0, 3, 2 );
  149. validateNamedRange( workbook, index++, "R5C1_R5C1", -1, 4, 0, 4, 0 );
  150. validateNamedRange( workbook, index++, "R6C1_R6C2", -1, 5, 0, 5, 0 );
  151. validateNamedRange( workbook, index++, "R7C1_R7C3", -1, 6, 0, 6, 0 );
  152. validateNamedRange( workbook, index++, "R8C1_R9C1", -1, 7, 0, 7, 0 );
  153. validateNamedRange( workbook, index++, "R10C1_R12C1", -1, 9, 0, 9, 0 );
  154. validateNamedRange( workbook, index++, "R13C1_R14C2", -1, 12, 0, 12, 0 );
  155. validateNamedRange( workbook, index++, "R15C1_R17C3", -1, 14, 0, 14, 0 );
  156. validateNamedRange( workbook, index++, "Grid_1", -1, 4, 0, 15, 2 );
  157. } finally {
  158. inputStream.close();
  159. }
  160. }
  161. @Test
  162. public void testHyperlinksXlsx() throws BirtException, IOException {
  163. debug = false;
  164. InputStream inputStream = runAndRenderReport("Hyperlinks.rptdesign", "xlsx");
  165. assertNotNull(inputStream);
  166. try {
  167. XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
  168. assertNotNull(workbook);
  169. assertEquals( 1, workbook.getNumberOfSheets() );
  170. Sheet sheet = workbook.getSheetAt(0);
  171. assertEquals( 2002, this.firstNullRow(sheet));
  172. assertEquals( 8, workbook.getNumCellStyles() );
  173. assertTrue( Font.U_SINGLE != workbook.getFontAt( sheet.getRow(0).getCell(1).getCellStyle().getFontIndex() ).getUnderline() );
  174. assertTrue( IndexedColors.BLUE.getIndex() != workbook.getFontAt( sheet.getRow(0).getCell(1).getCellStyle().getFontIndex() ).getColor() );
  175. for(int i = 1; i < 2000; ++i ) {
  176. assertEquals( "http://www.spudsoft.co.uk/?p=" + i, sheet.getRow(i).getCell(0).getHyperlink().getAddress());
  177. assertEquals( "_BK" + (i + 1000), sheet.getRow(i).getCell(1).getHyperlink().getAddress());
  178. assertEquals( Font.U_SINGLE, workbook.getFontAt( sheet.getRow(i).getCell(1).getCellStyle().getFontIndex() ).getUnderline() );
  179. assertEquals( "FF0000FF", workbook.getFontAt( sheet.getRow(i).getCell(1).getCellStyle().getFontIndex() ).getXSSFColor().getARGBHex() );
  180. }
  181. } finally {
  182. inputStream.close();
  183. }
  184. }
  185. @Test
  186. public void testHyperlinksXls() throws BirtException, IOException {
  187. debug = false;
  188. InputStream inputStream = runAndRenderReport("Hyperlinks.rptdesign", "xls");
  189. assertNotNull(inputStream);
  190. try {
  191. HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
  192. assertNotNull(workbook);
  193. assertEquals( 1, workbook.getNumberOfSheets() );
  194. Sheet sheet = workbook.getSheetAt(0);
  195. assertEquals( 2002, this.firstNullRow(sheet));
  196. assertEquals( 28, workbook.getNumCellStyles() );
  197. assertTrue( Font.U_SINGLE != workbook.getFontAt( sheet.getRow(0).getCell(1).getCellStyle().getFontIndex() ).getUnderline() );
  198. assertTrue( IndexedColors.BLUE.getIndex() != workbook.getFontAt( sheet.getRow(0).getCell(1).getCellStyle().getFontIndex() ).getColor() );
  199. for(int i = 1; i < 2000; ++i ) {
  200. assertEquals( "http://www.spudsoft.co.uk/?p=" + i, sheet.getRow(i).getCell(0).getHyperlink().getAddress());
  201. assertEquals( "_BK" + (i + 1000), sheet.getRow(i).getCell(1).getHyperlink().getAddress());
  202. assertEquals( Font.U_SINGLE, workbook.getFontAt( sheet.getRow(i).getCell(1).getCellStyle().getFontIndex() ).getUnderline() );
  203. assertEquals( IndexedColors.BLUE.getIndex(), workbook.getFontAt( sheet.getRow(i).getCell(1).getCellStyle().getFontIndex() ).getColor() );
  204. }
  205. } finally {
  206. inputStream.close();
  207. }
  208. }
  209. }