PageRenderTime 50ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/poi-3.6/src/testcases/org/apache/poi/hssf/record/formula/atp/TestYearFracCalculatorFromSpreadsheet.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 178 lines | 130 code | 20 blank | 28 comment | 18 complexity | 2db1f275f052f589d15602844b785e60 MD5 | raw file
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record.formula.atp;
  16. import java.io.PrintStream;
  17. import java.util.Calendar;
  18. import java.util.GregorianCalendar;
  19. import java.util.Iterator;
  20. import junit.framework.Assert;
  21. import junit.framework.AssertionFailedError;
  22. import junit.framework.ComparisonFailure;
  23. import junit.framework.TestCase;
  24. import org.apache.poi.hssf.HSSFTestDataSamples;
  25. import org.apache.poi.hssf.record.formula.eval.EvaluationException;
  26. import org.apache.poi.hssf.usermodel.HSSFCell;
  27. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  28. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  29. import org.apache.poi.hssf.usermodel.HSSFRow;
  30. import org.apache.poi.hssf.usermodel.HSSFSheet;
  31. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  32. /**
  33. * Tests YearFracCalculator using test-cases listed in a sample spreadsheet
  34. *
  35. * @author Josh Micich
  36. */
  37. public final class TestYearFracCalculatorFromSpreadsheet extends TestCase {
  38. private static final class SS {
  39. public static final int BASIS_COLUMN = 1; // "B"
  40. public static final int START_YEAR_COLUMN = 2; // "C"
  41. public static final int END_YEAR_COLUMN = 5; // "F"
  42. public static final int YEARFRAC_FORMULA_COLUMN = 11; // "L"
  43. public static final int EXPECTED_RESULT_COLUMN = 13; // "N"
  44. }
  45. public void testAll() {
  46. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("yearfracExamples.xls");
  47. HSSFSheet sheet = wb.getSheetAt(0);
  48. HSSFFormulaEvaluator formulaEvaluator = new HSSFFormulaEvaluator(wb);
  49. int nSuccess = 0;
  50. int nFailures = 0;
  51. int nUnexpectedErrors = 0;
  52. Iterator rowIterator = sheet.rowIterator();
  53. while(rowIterator.hasNext()) {
  54. HSSFRow row = (HSSFRow) rowIterator.next();
  55. HSSFCell cell = row.getCell(SS.YEARFRAC_FORMULA_COLUMN);
  56. if (cell == null || cell.getCellType() != HSSFCell.CELL_TYPE_FORMULA) {
  57. continue;
  58. }
  59. try {
  60. processRow(row, cell, formulaEvaluator);
  61. nSuccess++;
  62. } catch (RuntimeException e) {
  63. nUnexpectedErrors ++;
  64. printShortStackTrace(System.err, e);
  65. } catch (AssertionFailedError e) {
  66. nFailures ++;
  67. printShortStackTrace(System.err, e);
  68. }
  69. }
  70. if (nUnexpectedErrors + nFailures > 0) {
  71. String msg = nFailures + " failures(s) and " + nUnexpectedErrors
  72. + " unexpected errors(s) occurred. See stderr for details";
  73. throw new AssertionFailedError(msg);
  74. }
  75. if (nSuccess < 1) {
  76. throw new RuntimeException("No test sample cases found");
  77. }
  78. }
  79. private static void processRow(HSSFRow row, HSSFCell cell, HSSFFormulaEvaluator formulaEvaluator) {
  80. double startDate = makeDate(row, SS.START_YEAR_COLUMN);
  81. double endDate = makeDate(row, SS.END_YEAR_COLUMN);
  82. int basis = getIntCell(row, SS.BASIS_COLUMN);
  83. double expectedValue = getDoubleCell(row, SS.EXPECTED_RESULT_COLUMN);
  84. double actualValue;
  85. try {
  86. actualValue = YearFracCalculator.calculate(startDate, endDate, basis);
  87. } catch (EvaluationException e) {
  88. throw new RuntimeException(e);
  89. }
  90. if (expectedValue != actualValue) {
  91. throw new ComparisonFailure("Direct calculate failed - row " + (row.getRowNum()+1),
  92. String.valueOf(expectedValue), String.valueOf(actualValue));
  93. }
  94. actualValue = formulaEvaluator.evaluate(cell).getNumberValue();
  95. if (expectedValue != actualValue) {
  96. throw new ComparisonFailure("Formula evaluate failed - row " + (row.getRowNum()+1),
  97. String.valueOf(expectedValue), String.valueOf(actualValue));
  98. }
  99. }
  100. private static double makeDate(HSSFRow row, int yearColumn) {
  101. int year = getIntCell(row, yearColumn + 0);
  102. int month = getIntCell(row, yearColumn + 1);
  103. int day = getIntCell(row, yearColumn + 2);
  104. Calendar c = new GregorianCalendar(year, month-1, day, 0, 0, 0);
  105. c.set(Calendar.MILLISECOND, 0);
  106. return HSSFDateUtil.getExcelDate(c.getTime());
  107. }
  108. private static int getIntCell(HSSFRow row, int colIx) {
  109. double dVal = getDoubleCell(row, colIx);
  110. if (Math.floor(dVal) != dVal) {
  111. throw new RuntimeException("Non integer value (" + dVal
  112. + ") cell found at column " + (char)('A' + colIx));
  113. }
  114. return (int)dVal;
  115. }
  116. private static double getDoubleCell(HSSFRow row, int colIx) {
  117. HSSFCell cell = row.getCell(colIx);
  118. if (cell == null) {
  119. throw new RuntimeException("No cell found at column " + colIx);
  120. }
  121. double dVal = cell.getNumericCellValue();
  122. return dVal;
  123. }
  124. /**
  125. * Useful to keep output concise when expecting many failures to be reported by this test case
  126. * TODO - refactor duplicates in other Test~FromSpreadsheet classes
  127. */
  128. private static void printShortStackTrace(PrintStream ps, Throwable e) {
  129. StackTraceElement[] stes = e.getStackTrace();
  130. int startIx = 0;
  131. // skip any top frames inside junit.framework.Assert
  132. while(startIx<stes.length) {
  133. if(!stes[startIx].getClassName().equals(Assert.class.getName())) {
  134. break;
  135. }
  136. startIx++;
  137. }
  138. // skip bottom frames (part of junit framework)
  139. int endIx = startIx+1;
  140. while(endIx < stes.length) {
  141. if(stes[endIx].getClassName().equals(TestCase.class.getName())) {
  142. break;
  143. }
  144. endIx++;
  145. }
  146. if(startIx >= endIx) {
  147. // something went wrong. just print the whole stack trace
  148. e.printStackTrace(ps);
  149. }
  150. endIx -= 4; // skip 4 frames of reflection invocation
  151. ps.println(e.toString());
  152. for(int i=startIx; i<endIx; i++) {
  153. ps.println("\tat " + stes[i].toString());
  154. }
  155. }
  156. }