Further XSSF Themes unit testing
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1694093 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4f9ed87f6b
commit
885368e2ce
@ -89,6 +89,14 @@ public class XSSFColor extends ExtendedColor {
|
|||||||
return ctColor.isSetTheme();
|
return ctColor.isSetTheme();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A boolean value indicating if the ctColor has a tint or not
|
||||||
|
*/
|
||||||
|
public boolean hasTint() {
|
||||||
|
if (! ctColor.isSetRgb()) return false;
|
||||||
|
return ctColor.getRgb().length == 4;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
|
* Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
|
||||||
*/
|
*/
|
||||||
|
@ -21,11 +21,14 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Hex;
|
import org.apache.commons.codec.binary.Hex;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.util.CellReference;
|
||||||
import org.apache.poi.xssf.XSSFTestDataSamples;
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFColor;
|
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||||
import org.apache.poi.xssf.usermodel.XSSFFont;
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
||||||
@ -35,12 +38,27 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestThemesTable {
|
public class TestThemesTable {
|
||||||
private String testFile = "Themes.xlsx";
|
private String testFileSimple = "Themes.xlsx";
|
||||||
|
private String testFileComplex = "Themes2.xlsx";
|
||||||
|
// TODO .xls version available too, add HSSF support then check
|
||||||
|
|
||||||
@Test
|
// What our theme names are
|
||||||
public void testThemesTableColors() throws Exception {
|
private static String[] themeEntries = {
|
||||||
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);
|
"lt1",
|
||||||
String rgbExpected[] = {
|
"dk1",
|
||||||
|
"lt2",
|
||||||
|
"dk2",
|
||||||
|
"accent1",
|
||||||
|
"accent2",
|
||||||
|
"accent3",
|
||||||
|
"accent4",
|
||||||
|
"accent5",
|
||||||
|
"accent6",
|
||||||
|
"hlink",
|
||||||
|
"folhlink"
|
||||||
|
};
|
||||||
|
// What colours they should show up as
|
||||||
|
private static String rgbExpected[] = {
|
||||||
"ffffff", // Lt1
|
"ffffff", // Lt1
|
||||||
"000000", // Dk1
|
"000000", // Dk1
|
||||||
"eeece1", // Lt2
|
"eeece1", // Lt2
|
||||||
@ -54,34 +72,90 @@ public class TestThemesTable {
|
|||||||
"0000ff", // Hlink
|
"0000ff", // Hlink
|
||||||
"800080" // FolHlink
|
"800080" // FolHlink
|
||||||
};
|
};
|
||||||
boolean createFile = false;
|
|
||||||
int i=0;
|
@Test
|
||||||
for (Row row : workbook.getSheetAt(0)) {
|
public void testThemesTableColors() throws Exception {
|
||||||
XSSFFont font = ((XSSFRow)row).getCell(0).getCellStyle().getFont();
|
// Load our two test workbooks
|
||||||
|
XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
|
||||||
|
XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
|
||||||
|
// Save and re-load them, to check for stability across that
|
||||||
|
XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
|
||||||
|
XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
|
||||||
|
// Fetch fresh copies to test with
|
||||||
|
simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
|
||||||
|
complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
|
||||||
|
// Files and descriptions
|
||||||
|
Map<String,XSSFWorkbook> workbooks = new HashMap<String, XSSFWorkbook>();
|
||||||
|
workbooks.put(testFileSimple, simple);
|
||||||
|
workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
|
||||||
|
// TODO Fix these to work!
|
||||||
|
// workbooks.put(testFileComplex, complex);
|
||||||
|
// workbooks.put("Re-Saved_" + testFileComplex, complexRS);
|
||||||
|
|
||||||
|
// Sanity check
|
||||||
|
assertEquals(themeEntries.length, rgbExpected.length);
|
||||||
|
|
||||||
|
// For offline testing
|
||||||
|
boolean createFiles = false;
|
||||||
|
|
||||||
|
// Check each workbook in turn, and verify that the colours
|
||||||
|
// for the theme-applied cells in Column A are correct
|
||||||
|
for (String whatWorkbook : workbooks.keySet()) {
|
||||||
|
XSSFWorkbook workbook = workbooks.get(whatWorkbook);
|
||||||
|
XSSFSheet sheet = workbook.getSheetAt(0);
|
||||||
|
int startRN = 0;
|
||||||
|
if (whatWorkbook.endsWith(testFileComplex)) startRN++;
|
||||||
|
|
||||||
|
for (int rn=startRN; rn<themeEntries.length+startRN; rn++) {
|
||||||
|
XSSFRow row = sheet.getRow(rn);
|
||||||
|
assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
|
||||||
|
String ref = (new CellReference(rn, 0)).formatAsString();
|
||||||
|
XSSFCell cell = row.getCell(0);
|
||||||
|
assertNotNull(
|
||||||
|
"Missing cell " + ref + " in " + whatWorkbook, cell);
|
||||||
|
assertEquals(
|
||||||
|
"Wrong theme at " + ref + " in " + whatWorkbook,
|
||||||
|
themeEntries[rn], cell.getStringCellValue());
|
||||||
|
|
||||||
|
XSSFFont font = cell.getCellStyle().getFont();
|
||||||
XSSFColor color = font.getXSSFColor();
|
XSSFColor color = font.getXSSFColor();
|
||||||
assertEquals("Failed color theme "+i, rgbExpected[i], Hex.encodeHexString(color.getRgb()));
|
|
||||||
|
// Theme colours aren't tinted
|
||||||
|
assertEquals(false, color.hasTint());
|
||||||
|
// Check the RGB part (no tint)
|
||||||
|
assertEquals(
|
||||||
|
"Wrong theme colour " + themeEntries[rn] + " on " + whatWorkbook,
|
||||||
|
rgbExpected[rn], Hex.encodeHexString(color.getRGB()));
|
||||||
|
// Check the Theme ID
|
||||||
|
int expectedThemeIdx = rn - startRN;
|
||||||
long themeIdx = font.getCTFont().getColorArray(0).getTheme();
|
long themeIdx = font.getCTFont().getColorArray(0).getTheme();
|
||||||
assertEquals("Failed color theme "+i, i, themeIdx);
|
assertEquals(
|
||||||
if (createFile) {
|
"Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook,
|
||||||
XSSFCellStyle cs = (XSSFCellStyle)row.getSheet().getWorkbook().createCellStyle();
|
expectedThemeIdx, themeIdx);
|
||||||
|
|
||||||
|
if (createFiles) {
|
||||||
|
XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
|
||||||
cs.setFillForegroundColor(color);
|
cs.setFillForegroundColor(color);
|
||||||
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
||||||
row.createCell(1).setCellStyle(cs);
|
row.createCell(1).setCellStyle(cs);
|
||||||
}
|
}
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createFile) {
|
if (createFiles) {
|
||||||
FileOutputStream fos = new FileOutputStream("foobaa.xlsx");
|
FileOutputStream fos = new FileOutputStream("Generated_"+whatWorkbook);
|
||||||
workbook.write(fos);
|
workbook.write(fos);
|
||||||
fos.close();
|
fos.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Check the complex parts
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@SuppressWarnings("resource")
|
||||||
public void testAddNew() throws Exception {
|
public void testAddNew() throws Exception {
|
||||||
XSSFWorkbook wb = new XSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
XSSFSheet s = wb.createSheet();
|
wb.createSheet();
|
||||||
assertEquals(null, wb.getTheme());
|
assertEquals(null, wb.getTheme());
|
||||||
|
|
||||||
StylesTable styles = wb.getStylesSource();
|
StylesTable styles = wb.getStylesSource();
|
||||||
|
Loading…
Reference in New Issue
Block a user