2013-06-20 09:37:26 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.ss.usermodel;
|
|
|
|
|
2015-09-10 20:56:16 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
2013-06-20 09:37:26 -04:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
2016-06-24 19:31:12 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
2015-09-10 20:56:16 -04:00
|
|
|
import org.apache.poi.util.LocaleUtil;
|
|
|
|
import org.junit.Test;
|
2013-06-20 09:37:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the Fraction Formatting part of DataFormatter.
|
|
|
|
* Largely taken from bug #54686
|
|
|
|
*/
|
2015-09-10 20:56:16 -04:00
|
|
|
public final class TestFractionFormat {
|
|
|
|
@Test
|
|
|
|
public void testSingle() throws Exception {
|
2013-06-20 09:37:26 -04:00
|
|
|
FractionFormat f = new FractionFormat("", "##");
|
|
|
|
double val = 321.321;
|
|
|
|
String ret = f.format(val);
|
|
|
|
assertEquals("26027/81", ret);
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:56:16 -04:00
|
|
|
@Test
|
2013-06-20 09:37:26 -04:00
|
|
|
public void testTruthFile() throws Exception {
|
|
|
|
File truthFile = HSSFTestDataSamples.getSampleFile("54686_fraction_formats.txt");
|
2015-09-10 20:56:16 -04:00
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(truthFile), LocaleUtil.CHARSET_1252));
|
2013-06-20 09:37:26 -04:00
|
|
|
Workbook wb = HSSFTestDataSamples.openSampleWorkbook("54686_fraction_formats.xls");
|
|
|
|
Sheet sheet = wb.getSheetAt(0);
|
|
|
|
DataFormatter formatter = new DataFormatter();
|
|
|
|
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
|
|
|
// Skip over the header row
|
|
|
|
String truthLine = reader.readLine();
|
|
|
|
String[] headers = truthLine.split("\t");
|
|
|
|
truthLine = reader.readLine();
|
|
|
|
|
|
|
|
for (int i = 1; i < sheet.getLastRowNum() && truthLine != null; i++){
|
|
|
|
Row r = sheet.getRow(i);
|
|
|
|
String[] truths = truthLine.split("\t");
|
|
|
|
// Intentionally ignore the last column (tika-1132), for now
|
|
|
|
for (short j = 3; j < 12; j++){
|
2016-06-24 19:31:12 -04:00
|
|
|
Cell cell = r.getCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK);
|
2013-06-20 09:37:26 -04:00
|
|
|
String formatted = clean(formatter.formatCellValue(cell, evaluator));
|
|
|
|
if (truths.length <= j){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
String truth = clean(truths[j]);
|
|
|
|
String testKey = truths[0]+":"+truths[1]+":"+headers[j];
|
|
|
|
assertEquals(testKey, truth, formatted);
|
|
|
|
}
|
|
|
|
truthLine = reader.readLine();
|
|
|
|
}
|
2015-09-10 20:56:16 -04:00
|
|
|
wb.close();
|
2013-06-20 09:37:26 -04:00
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String clean(String s){
|
|
|
|
s = s.trim().replaceAll(" +", " ").replaceAll("- +", "-");
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|