blob: ea5192638954a2358394b4e7380f831c598dee0a [file] [log] [blame]
/* ====================================================================
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.format;
import static org.junit.Assert.assertEquals;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.xssf.XSSFITestDataProvider;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
/** Test the individual CellFormatPart types. */
public class TestCellFormatPart extends CellFormatTestBase {
private static Locale userLocale;
@BeforeClass
public static void setLocale() {
userLocale = LocaleUtil.getUserLocale();
LocaleUtil.setUserLocale(Locale.ROOT);
}
@AfterClass
public static void unsetLocale() {
LocaleUtil.setUserLocale(userLocale);
}
private static final Pattern NUMBER_EXTRACT_FMT = Pattern.compile(
"([-+]?[0-9]+)(\\.[0-9]+)?.*(?:(e).*?([+-]?[0-9]+))",
Pattern.CASE_INSENSITIVE);
public TestCellFormatPart() {
super(XSSFITestDataProvider.instance);
}
@Test
public void testGeneralFormat() throws Exception {
runFormatTests("GeneralFormatTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
switch (CellFormat.ultimateTypeEnum(cell)) {
case BOOLEAN:
return cell.getBooleanCellValue();
case NUMERIC:
return cell.getNumericCellValue();
default:
return cell.getStringCellValue();
}
}
});
}
public void testNumberFormat() throws Exception {
runFormatTests("NumberFormatTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
return cell.getNumericCellValue();
}
});
}
@Test
public void testNumberApproxFormat() throws Exception {
runFormatTests("NumberFormatApproxTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
return cell.getNumericCellValue();
}
@Override
void equivalent(String expected, String actual,
CellFormatPart format) {
double expectedVal = extractNumber(expected);
double actualVal = extractNumber(actual);
// equal within 1%
double delta = expectedVal / 100;
assertEquals("format \"" + format + "\"," + expected + " ~= " +
actual, expectedVal, actualVal, delta);
}
});
}
@Test
public void testDateFormat() throws Exception {
TimeZone tz = LocaleUtil.getUserTimeZone();
LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
try {
runFormatTests("DateFormatTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
return cell.getDateCellValue();
}
});
} finally {
LocaleUtil.setUserTimeZone(tz);
}
}
@Test
public void testElapsedFormat() throws Exception {
runFormatTests("ElapsedFormatTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
return cell.getNumericCellValue();
}
});
}
@Test
public void testTextFormat() throws Exception {
runFormatTests("TextFormatTests.xlsx", new CellValue() {
@Override
public Object getValue(Cell cell) {
switch(CellFormat.ultimateTypeEnum(cell)) {
case BOOLEAN:
return cell.getBooleanCellValue();
default:
return cell.getStringCellValue();
}
}
});
}
@Test
public void testConditions() throws Exception {
runFormatTests("FormatConditionTests.xlsx", new CellValue() {
@Override
Object getValue(Cell cell) {
return cell.getNumericCellValue();
}
});
}
private double extractNumber(String str) {
Matcher m = NUMBER_EXTRACT_FMT.matcher(str);
if (!m.find())
throw new IllegalArgumentException(
"Cannot find numer in \"" + str + "\"");
StringBuffer sb = new StringBuffer();
// The groups in the pattern are the parts of the number
for (int i = 1; i <= m.groupCount(); i++) {
String part = m.group(i);
if (part != null)
sb.append(part);
}
return Double.valueOf(sb.toString());
}
}