PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/main/java/org/digant/ExcelUnit.java

https://bitbucket.org/abrin/excelunit
Java | 271 lines | 226 code | 45 blank | 0 comment | 22 complexity | afb2273468a9143c8e7491d51efe8bf1 MD5 | raw file
  1. package org.tdar.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
  7. import org.apache.poi.ss.usermodel.Cell;
  8. import org.apache.poi.ss.usermodel.DataFormatter;
  9. import org.apache.poi.ss.usermodel.Font;
  10. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  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.usermodel.WorkbookFactory;
  15. import org.junit.Assert;
  16. public class ExcelUnit {
  17. private Workbook workbook;
  18. private Sheet sheet;
  19. private DataFormatter formatter = new HSSFDataFormatter();
  20. private FormulaEvaluator formulaEvaluator;
  21. public void open(File file) {
  22. try {
  23. setWorkbook(WorkbookFactory.create(new FileInputStream(file)));
  24. setFormulaEvaluator(getWorkbook().getCreationHelper().createFormulaEvaluator());
  25. } catch (Exception e) {
  26. Assert.fail("could not open file: " + file.getName() + " :" + e);
  27. }
  28. }
  29. public void open(InputStream inputStream) {
  30. try {
  31. WorkbookFactory.create(inputStream);
  32. setFormulaEvaluator(getWorkbook().getCreationHelper().createFormulaEvaluator());
  33. } catch (Exception e) {
  34. Assert.fail("could not open input stream");
  35. }
  36. }
  37. public void assertCellEquals(int row, int column, Object obj) {
  38. Cell cell = getCell(row, column);
  39. Assert.assertEquals(getCellAsString(cell), obj.toString());
  40. }
  41. private String getCellAsString(Cell cell) {
  42. return formatter.formatCellValue(cell, formulaEvaluator);
  43. }
  44. public void assertCellContains(int row, int column, Object obj) {
  45. Cell cell = getCell(row, column);
  46. String value = getCellAsString(cell);
  47. Assert.assertTrue(value.contains(obj.toString()));
  48. }
  49. public void assertRowContains(int rowNum, Object obj) {
  50. boolean seen = rowContains(rowNum, obj);
  51. Assert.assertTrue(String.format("row did not contain %s", obj), seen);
  52. }
  53. public void assertRowDoesNotContain(int rowNum, Object obj) {
  54. boolean seen = rowContains(rowNum, obj);
  55. Assert.assertFalse(String.format("row did contain %s", obj), seen);
  56. }
  57. private boolean rowContains(int rowNum, Object obj) {
  58. boolean seen = false;
  59. Row row = sheet.getRow(rowNum);
  60. for (int i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
  61. if (cellContains(rowNum, i, obj)) {
  62. seen = true;
  63. }
  64. }
  65. return seen;
  66. }
  67. public void assertCellContainsIgnoreCase(int row, int column, Object obj) {
  68. Assert.assertFalse(String.format("cell does not contain (caseInsensitive): %s | %s", obj, getCell(row, column).getStringCellValue()),
  69. cellContainsIgnoreCase(row, column, obj));
  70. }
  71. private boolean cellContainsIgnoreCase(int row, int column, Object obj) {
  72. Cell cell = getCell(row, column);
  73. String value = getCellAsString(cell).toLowerCase();
  74. return value.contains(obj.toString().toLowerCase());
  75. }
  76. private boolean cellContains(int row, int column, Object obj) {
  77. Cell cell = getCell(row, column);
  78. String value = getCellAsString(cell);
  79. return value.contains(obj.toString());
  80. }
  81. public void assertCellDoesNotContainIgnoreCase(int row, int column, Object obj) {
  82. Assert.assertFalse(String.format("cell does not contain: %s", obj), cellContainsIgnoreCase(row, column, obj));
  83. }
  84. private boolean isCellBold(Cell cell) {
  85. try {
  86. return Font.BOLDWEIGHT_NORMAL != sheet.getWorkbook().getFontAt(cell.getCellStyle().getFontIndex()).getBoldweight();
  87. } catch (Exception e) {
  88. return false;
  89. }
  90. }
  91. public void assertCellIsBold(Cell cell) {
  92. Assert.assertTrue("cell should be bold", isCellBold(cell));
  93. }
  94. public void assertCellNotBold(Cell cell) {
  95. Assert.assertFalse("cell should not be bold", isCellBold(cell));
  96. }
  97. public void assertCellIsItalic(Cell cell) {
  98. try {
  99. Assert.assertTrue("cell is not italic", sheet.getWorkbook().getFontAt(cell.getCellStyle().getFontIndex()).getItalic());
  100. } catch (Exception e) {
  101. Assert.fail("cell is not italic");
  102. }
  103. }
  104. public void assertCellIsSizeInPoints(Cell cell, short size) {
  105. String message = "cell size is not " + size;
  106. try {
  107. Assert.assertEquals(message, size, sheet.getWorkbook().getFontAt(cell.getCellStyle().getFontIndex()).getFontHeightInPoints());
  108. } catch (Exception e) {
  109. Assert.fail(message);
  110. }
  111. }
  112. public void assertCellEqualsString(int row, int column, String obj) {
  113. Cell cell = getCell(row, column);
  114. Assert.assertEquals(formulaEvaluator.evaluate(cell).getStringValue(), obj);
  115. }
  116. public void assertCellEqualsNumeric(int row, int column, double obj) {
  117. Cell cell = getCell(row, column);
  118. Assert.assertEquals(formulaEvaluator.evaluate(cell).getNumberValue(), obj);
  119. }
  120. public void assertCellEqualsBoolean(int row, int column, boolean obj) {
  121. Cell cell = getCell(row, column);
  122. Assert.assertEquals(formulaEvaluator.evaluate(cell).getBooleanValue(), obj);
  123. }
  124. public void assertCellIsNumbericType(int row, int column) {
  125. assertCellType(getCell(row, column), Cell.CELL_TYPE_NUMERIC);
  126. }
  127. public void assertCellIsString(int row, int column) {
  128. assertCellType(getCell(row, column), Cell.CELL_TYPE_STRING);
  129. }
  130. public void assertCellIsFormula(int row, int column) {
  131. assertCellType(getCell(row, column), Cell.CELL_TYPE_FORMULA);
  132. }
  133. private void assertCellType(Cell cell, int type) {
  134. switch (type) {
  135. case Cell.CELL_TYPE_BLANK:
  136. if (type != cell.getCellType()) {
  137. Assert.fail("cell was not 'blank'");
  138. }
  139. break;
  140. case Cell.CELL_TYPE_BOOLEAN:
  141. if (type != cell.getCellType()) {
  142. Assert.fail("cell was not 'boolean'");
  143. }
  144. break;
  145. case Cell.CELL_TYPE_ERROR:
  146. if (type != cell.getCellType()) {
  147. Assert.fail("cell was not 'error'");
  148. }
  149. break;
  150. case Cell.CELL_TYPE_FORMULA:
  151. if (type != cell.getCellType()) {
  152. Assert.fail("cell was not 'formula'");
  153. }
  154. break;
  155. case Cell.CELL_TYPE_NUMERIC:
  156. if (type != cell.getCellType()) {
  157. Assert.fail("cell was not 'numeric'");
  158. }
  159. break;
  160. case Cell.CELL_TYPE_STRING:
  161. if (type != cell.getCellType()) {
  162. Assert.fail("cell was not 'string'");
  163. }
  164. break;
  165. }
  166. }
  167. public Cell getCell(int row, int column) {
  168. if (sheet == null) {
  169. selectSheet(0);
  170. }
  171. Cell cell = sheet.getRow(row).getCell(column);
  172. return cell;
  173. }
  174. public void selectSheet(String name) {
  175. try {
  176. setSheet(workbook.getSheet(name));
  177. Assert.assertNotNull("could not find sheet: " + name, sheet);
  178. } catch (Exception e) {
  179. Assert.fail(e.getMessage());
  180. }
  181. }
  182. public void selectSheet(int number) {
  183. try {
  184. setSheet(workbook.getSheetAt(number));
  185. Assert.assertNotNull("could not find sheet number: " + number, sheet);
  186. } catch (Exception e) {
  187. Assert.fail(e.getMessage());
  188. }
  189. }
  190. public Workbook getWorkbook() {
  191. return workbook;
  192. }
  193. public void setWorkbook(Workbook workbook) {
  194. this.workbook = workbook;
  195. }
  196. public Sheet getSheet() {
  197. return sheet;
  198. }
  199. public void setSheet(Sheet sheet) {
  200. this.sheet = sheet;
  201. }
  202. public FormulaEvaluator getFormulaEvaluator() {
  203. return formulaEvaluator;
  204. }
  205. public void setFormulaEvaluator(FormulaEvaluator formulaEvaluator) {
  206. this.formulaEvaluator = formulaEvaluator;
  207. }
  208. public void assertRowIsEmpty(int i) {
  209. Assert.assertTrue("row should be empty", isRowEmpty(i));
  210. }
  211. public void assertRowNotEmpty(int i) {
  212. Assert.assertFalse("row should not be empty", isRowEmpty(i));
  213. }
  214. private boolean isRowEmpty(int i) {
  215. Row row = getSheet().getRow(i);
  216. if(row == null) return true;
  217. for(int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
  218. if(!isCellEmpty(row.getCell(j)))
  219. return false;
  220. }
  221. return true;
  222. }
  223. private boolean isCellEmpty(Cell cell) {
  224. return StringUtils.isBlank(getCellAsString(cell));
  225. }
  226. }