package com.trafficaudit.reportexport.service;
|
|
import com.trafficaudit.common.util.RegionUtil;
|
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.DataFormat;
|
import org.apache.poi.ss.usermodel.Font;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.junit.jupiter.api.Test;
|
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
class ReportExportServiceTest {
|
|
@Test
|
void shouldAlignCityByRegionMonthlyBlocksWithTheirHeaderRows() throws Exception {
|
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
Sheet sheet = wb.createSheet("城市分市州");
|
for (int r = 0; r < 88; r++) sheet.createRow(r);
|
sheet.getRow(66).createCell(1).setCellValue(-999);
|
|
Map<String, double[]> metrics = new LinkedHashMap<>();
|
List<String> cities = RegionUtil.cityList();
|
for (int i = 0; i < cities.size(); i++) {
|
double[] values = new double[19];
|
values[0] = 100 + i;
|
values[1] = 1000 + i;
|
metrics.put(cities.get(i), values);
|
}
|
|
new ReportExportService().smWriteCityByRegion(wb, 10, metrics, metrics, metrics, metrics);
|
|
assertEquals(1836.0, sheet.getRow(48).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(100.0, sheet.getRow(49).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(101.0, sheet.getRow(50).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(116.0, sheet.getRow(65).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(-999.0, sheet.getRow(66).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(17136.0, sheet.getRow(70).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(1000.0, sheet.getRow(71).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(1001.0, sheet.getRow(72).getCell(1).getNumericCellValue(), 0.0);
|
assertEquals(1016.0, sheet.getRow(87).getCell(1).getNumericCellValue(), 0.0);
|
}
|
}
|
|
@Test
|
void shouldForcePercentFormatOnMonthlyTurnoverYoyWithoutChangingValues() throws Exception {
|
double[] values = {0.036745795811, 0.032845599616, 0.041259651978, -0.123225341069, 0.427173495721};
|
|
try (XSSFWorkbook wb = new XSSFWorkbook()) {
|
Sheet sheet = wb.createSheet("中口径分析");
|
DataFormat dataFormat = wb.createDataFormat();
|
CellStyle integerStyle = wb.createCellStyle();
|
integerStyle.setDataFormat(dataFormat.getFormat("0_ "));
|
integerStyle.setAlignment(HorizontalAlignment.CENTER);
|
Font font = wb.createFont();
|
font.setBold(true);
|
integerStyle.setFont(font);
|
|
for (int i = 0; i < values.length; i++) {
|
Row row = sheet.createRow(11 + i);
|
Cell cell = row.createCell(6);
|
cell.setCellValue(values[i]);
|
cell.setCellStyle(integerStyle);
|
assertEquals("0_ ", cell.getCellStyle().getDataFormatString());
|
}
|
|
int changed = ReportExportService.smSetPercentFormat(sheet, 11, 15, 6, "0.00%");
|
|
assertEquals(values.length, changed);
|
for (int i = 0; i < values.length; i++) {
|
Cell cell = sheet.getRow(11 + i).getCell(6);
|
assertEquals("0.00%", cell.getCellStyle().getDataFormatString());
|
assertEquals(HorizontalAlignment.CENTER, cell.getCellStyle().getAlignment());
|
assertTrue(wb.getFontAt(cell.getCellStyle().getFontIndex()).getBold());
|
assertEquals(values[i], cell.getNumericCellValue(), 0.0);
|
}
|
|
assertEquals(0, ReportExportService.smSetPercentFormat(sheet, 11, 15, 6, "0.00%"));
|
}
|
}
|
}
|