PageRenderTime 70ms CodeModel.GetById 29ms RepoModel.GetById 2ms app.codeStats 0ms

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

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