PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

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