You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestProper.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.ss.formula.functions;
  16. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  17. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  18. import org.apache.poi.ss.formula.eval.StringEval;
  19. import org.apache.poi.ss.formula.eval.ValueEval;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.CellType;
  22. import org.apache.poi.ss.usermodel.CellValue;
  23. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.Workbook;
  26. import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
  27. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  28. import junit.framework.AssertionFailedError;
  29. import junit.framework.TestCase;
  30. import org.junit.Test;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.junit.Assert.assertTrue;
  33. public final class TestProper {
  34. private Cell cell11;
  35. private FormulaEvaluator evaluator;
  36. @Test
  37. public void testValidHSSF() {
  38. HSSFWorkbook wb = new HSSFWorkbook();
  39. evaluator = new HSSFFormulaEvaluator(wb);
  40. confirm(wb);
  41. }
  42. @Test
  43. public void testValidXSSF() {
  44. XSSFWorkbook wb = new XSSFWorkbook();
  45. evaluator = new XSSFFormulaEvaluator(wb);
  46. confirm(wb);
  47. }
  48. private void confirm(Workbook wb) {
  49. Sheet sheet = wb.createSheet("new sheet");
  50. cell11 = sheet.createRow(0).createCell(0);
  51. cell11.setCellType(CellType.FORMULA);
  52. confirm("PROPER(\"hi there\")", "Hi There"); //simple case
  53. confirm("PROPER(\"what's up\")", "What'S Up"); //apostrophes are treated as word breaks
  54. confirm("PROPER(\"I DON'T TH!NK SO!\")", "I Don'T Th!Nk So!"); //capitalization is ignored, special punctuation is treated as a word break
  55. confirm("PROPER(\"dr\u00dcb\u00f6'\u00e4 \u00e9lo\u015f|\u00eb\u00e8 \")", "Dr\u00fcb\u00f6'\u00c4 \u00c9lo\u015f|\u00cb\u00e8 ");
  56. confirm("PROPER(\"hi123 the123re\")", "Hi123 The123Re"); //numbers are treated as word breaks
  57. confirm("PROPER(\"-\")", "-"); //nothing happens with ascii punctuation that is not upper or lower case
  58. confirm("PROPER(\"!\u00a7$\")", "!\u00a7$"); //nothing happens with unicode punctuation (section sign) that is not upper or lower case
  59. confirm("PROPER(\"/&%\")", "/&%"); //nothing happens with ascii punctuation that is not upper or lower case
  60. confirm("PROPER(\"Apache POI\")", "Apache Poi"); //acronyms are not special
  61. confirm("PROPER(\" hello world\")", " Hello World"); //leading whitespace is ignored
  62. final String scharfes = "\u00df"; //German lowercase eszett, scharfes s, sharp s
  63. confirm("PROPER(\"stra"+scharfes+"e\")", "Stra"+scharfes+"e");
  64. assertTrue(Character.isLetter(scharfes.charAt(0)));
  65. // CURRENTLY FAILS: result: "SSUnd"+scharfes
  66. // LibreOffice 5.0.3.2 behavior: "Sund"+scharfes
  67. // Excel 2013 behavior: ???
  68. confirm("PROPER(\""+scharfes+"und"+scharfes+"\")", "SSund"+scharfes);
  69. // also test longer string
  70. StringBuilder builder = new StringBuilder("A");
  71. StringBuilder expected = new StringBuilder("A");
  72. for(int i = 1;i < 254;i++) {
  73. builder.append((char)(65 + (i % 26)));
  74. expected.append((char)(97 + (i % 26)));
  75. }
  76. confirm("PROPER(\"" + builder.toString() + "\")", expected.toString());
  77. }
  78. private void confirm(String formulaText, String expectedResult) {
  79. cell11.setCellFormula(formulaText);
  80. evaluator.clearAllCachedResultValues();
  81. CellValue cv = evaluator.evaluate(cell11);
  82. if (cv.getCellTypeEnum() != CellType.STRING) {
  83. throw new AssertionFailedError("Wrong result type: " + cv.formatAsString());
  84. }
  85. String actualValue = cv.getStringValue();
  86. assertEquals(expectedResult, actualValue);
  87. }
  88. @Test
  89. public void test() {
  90. checkProper("", "");
  91. checkProper("a", "A");
  92. checkProper("abc", "Abc");
  93. checkProper("abc abc", "Abc Abc");
  94. checkProper("abc/abc", "Abc/Abc");
  95. checkProper("ABC/ABC", "Abc/Abc");
  96. checkProper("aBc/ABC", "Abc/Abc");
  97. checkProper("aBc@#$%^&*()_+=-ABC", "Abc@#$%^&*()_+=-Abc");
  98. checkProper("aBc25aerg/ABC", "Abc25Aerg/Abc");
  99. checkProper("aBc/\u00C4\u00F6\u00DF\u00FC/ABC", "Abc/\u00C4\u00F6\u00DF\u00FC/Abc"); // Some German umlauts with uppercase first letter is not changed
  100. checkProper("\u00FC", "\u00DC");
  101. checkProper("\u00DC", "\u00DC");
  102. checkProper("\u00DF", "SS"); // German "scharfes s" is uppercased to "SS"
  103. checkProper("\u00DFomesing", "SSomesing"); // German "scharfes s" is uppercased to "SS"
  104. checkProper("aBc/\u00FC\u00C4\u00F6\u00DF\u00FC/ABC", "Abc/\u00DC\u00E4\u00F6\u00DF\u00FC/Abc"); // Some German umlauts with lowercase first letter is changed to uppercase
  105. }
  106. @Test
  107. public void testMicroBenchmark() {
  108. ValueEval strArg = new StringEval("some longer text that needs a number of replacements to check for runtime of different implementations");
  109. long start = System.currentTimeMillis();
  110. for(int i = 0;i < 300000;i++) {
  111. final ValueEval ret = TextFunction.PROPER.evaluate(new ValueEval[]{strArg}, 0, 0);
  112. assertEquals("Some Longer Text That Needs A Number Of Replacements To Check For Runtime Of Different Implementations", ((StringEval)ret).getStringValue());
  113. }
  114. // Took aprox. 600ms on a decent Laptop in July 2016
  115. System.out.println("Took: " + (System.currentTimeMillis() - start) + "ms");
  116. }
  117. private void checkProper(String input, String expected) {
  118. ValueEval strArg = new StringEval(input);
  119. final ValueEval ret = TextFunction.PROPER.evaluate(new ValueEval[]{strArg}, 0, 0);
  120. assertEquals(expected, ((StringEval)ret).getStringValue());
  121. }
  122. }