import static org.junit.Assert.assertNotNull;\r
\r
import java.io.FileOutputStream;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
\r
import org.apache.commons.codec.binary.Hex;\r
import org.apache.poi.ss.usermodel.CellStyle;\r
-import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.util.CellReference;\r
import org.apache.poi.xssf.XSSFTestDataSamples;\r
+import org.apache.poi.xssf.usermodel.XSSFCell;\r
import org.apache.poi.xssf.usermodel.XSSFCellStyle;\r
import org.apache.poi.xssf.usermodel.XSSFColor;\r
import org.apache.poi.xssf.usermodel.XSSFFont;\r
import org.junit.Test;\r
\r
public class TestThemesTable {\r
- private String testFile = "Themes.xlsx";\r
-\r
- @Test\r
- public void testThemesTableColors() throws Exception {\r
- XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile);\r
- String rgbExpected[] = {\r
+ private String testFileSimple = "Themes.xlsx";\r
+ private String testFileComplex = "Themes2.xlsx";\r
+ // TODO .xls version available too, add HSSF support then check \r
+ \r
+ // What our theme names are\r
+ private static String[] themeEntries = {\r
+ "lt1",\r
+ "dk1",\r
+ "lt2",\r
+ "dk2",\r
+ "accent1",\r
+ "accent2",\r
+ "accent3",\r
+ "accent4",\r
+ "accent5",\r
+ "accent6",\r
+ "hlink",\r
+ "folhlink"\r
+ };\r
+ // What colours they should show up as\r
+ private static String rgbExpected[] = {\r
"ffffff", // Lt1\r
"000000", // Dk1\r
"eeece1", // Lt2\r
"0000ff", // Hlink\r
"800080" // FolHlink\r
};\r
- boolean createFile = false;\r
- int i=0;\r
- for (Row row : workbook.getSheetAt(0)) {\r
- XSSFFont font = ((XSSFRow)row).getCell(0).getCellStyle().getFont();\r
- XSSFColor color = font.getXSSFColor();\r
- assertEquals("Failed color theme "+i, rgbExpected[i], Hex.encodeHexString(color.getRgb()));\r
- long themeIdx = font.getCTFont().getColorArray(0).getTheme();\r
- assertEquals("Failed color theme "+i, i, themeIdx);\r
- if (createFile) {\r
- XSSFCellStyle cs = (XSSFCellStyle)row.getSheet().getWorkbook().createCellStyle();\r
- cs.setFillForegroundColor(color);\r
- cs.setFillPattern(CellStyle.SOLID_FOREGROUND);\r
- row.createCell(1).setCellStyle(cs);\r
- }\r
- i++;\r
- }\r
+\r
+ @Test\r
+ public void testThemesTableColors() throws Exception {\r
+ // Load our two test workbooks\r
+ XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);\r
+ XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);\r
+ // Save and re-load them, to check for stability across that\r
+ XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);\r
+ XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);\r
+ // Fetch fresh copies to test with\r
+ simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);\r
+ complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);\r
+ // Files and descriptions\r
+ Map<String,XSSFWorkbook> workbooks = new HashMap<String, XSSFWorkbook>();\r
+ workbooks.put(testFileSimple, simple);\r
+ workbooks.put("Re-Saved_" + testFileSimple, simpleRS);\r
+ // TODO Fix these to work!\r
+// workbooks.put(testFileComplex, complex);\r
+// workbooks.put("Re-Saved_" + testFileComplex, complexRS);\r
+ \r
+ // Sanity check\r
+ assertEquals(themeEntries.length, rgbExpected.length);\r
\r
- if (createFile) {\r
- FileOutputStream fos = new FileOutputStream("foobaa.xlsx");\r
- workbook.write(fos);\r
- fos.close();\r
+ // For offline testing\r
+ boolean createFiles = false;\r
+ \r
+ // Check each workbook in turn, and verify that the colours\r
+ // for the theme-applied cells in Column A are correct\r
+ for (String whatWorkbook : workbooks.keySet()) {\r
+ XSSFWorkbook workbook = workbooks.get(whatWorkbook);\r
+ XSSFSheet sheet = workbook.getSheetAt(0);\r
+ int startRN = 0;\r
+ if (whatWorkbook.endsWith(testFileComplex)) startRN++;\r
+ \r
+ for (int rn=startRN; rn<themeEntries.length+startRN; rn++) {\r
+ XSSFRow row = sheet.getRow(rn);\r
+ assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);\r
+ String ref = (new CellReference(rn, 0)).formatAsString();\r
+ XSSFCell cell = row.getCell(0);\r
+ assertNotNull(\r
+ "Missing cell " + ref + " in " + whatWorkbook, cell);\r
+ assertEquals(\r
+ "Wrong theme at " + ref + " in " + whatWorkbook,\r
+ themeEntries[rn], cell.getStringCellValue());\r
+\r
+ XSSFFont font = cell.getCellStyle().getFont();\r
+ XSSFColor color = font.getXSSFColor();\r
+ \r
+ // Theme colours aren't tinted\r
+ assertEquals(false, color.hasTint());\r
+ // Check the RGB part (no tint)\r
+ assertEquals(\r
+ "Wrong theme colour " + themeEntries[rn] + " on " + whatWorkbook,\r
+ rgbExpected[rn], Hex.encodeHexString(color.getRGB()));\r
+ // Check the Theme ID\r
+ int expectedThemeIdx = rn - startRN;\r
+ long themeIdx = font.getCTFont().getColorArray(0).getTheme();\r
+ assertEquals(\r
+ "Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook,\r
+ expectedThemeIdx, themeIdx);\r
+ \r
+ if (createFiles) {\r
+ XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();\r
+ cs.setFillForegroundColor(color);\r
+ cs.setFillPattern(CellStyle.SOLID_FOREGROUND);\r
+ row.createCell(1).setCellStyle(cs);\r
+ }\r
+ }\r
+ \r
+ if (createFiles) {\r
+ FileOutputStream fos = new FileOutputStream("Generated_"+whatWorkbook);\r
+ workbook.write(fos);\r
+ fos.close();\r
+ }\r
}\r
}\r
\r
+ // TODO Check the complex parts \r
+ \r
@Test\r
+ @SuppressWarnings("resource")\r
public void testAddNew() throws Exception {\r
XSSFWorkbook wb = new XSSFWorkbook();\r
- XSSFSheet s = wb.createSheet();\r
+ wb.createSheet();\r
assertEquals(null, wb.getTheme());\r
\r
StylesTable styles = wb.getStylesSource();\r
assertNotNull(styles.getTheme());\r
assertNotNull(wb.getTheme());\r
}\r
-}
\ No newline at end of file
+}\r