001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.util.Iterator;
025: import java.util.Map;
026: import junit.framework.TestCase;
027: import org.apache.poi.hssf.record.PaletteRecord;
028: import org.apache.poi.hssf.util.HSSFColor;
029: import org.apache.poi.util.TempFile;
030:
031: /**
032: * @author Brian Sanders (bsanders at risklabs dot com)
033: */
034: public class TestHSSFPalette extends TestCase {
035: private PaletteRecord palette;
036: private HSSFPalette hssfPalette;
037:
038: public TestHSSFPalette(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: palette = new PaletteRecord();
044: hssfPalette = new HSSFPalette(palette);
045: }
046:
047: /**
048: * Verifies that a custom palette can be created, saved, and reloaded
049: */
050: public void testCustomPalette() throws IOException {
051: //reading sample xls
052: String dir = System.getProperty("HSSF.testdata.path");
053: File sample = new File(dir + "/Simple.xls");
054: assertTrue("Simple.xls exists and is readable", sample
055: .canRead());
056: FileInputStream fis = new FileInputStream(sample);
057: HSSFWorkbook book = new HSSFWorkbook(fis);
058: fis.close();
059:
060: //creating custom palette
061: HSSFPalette palette = book.getCustomPalette();
062: palette.setColorAtIndex((short) 0x12, (byte) 101, (byte) 230,
063: (byte) 100);
064: palette.setColorAtIndex((short) 0x3b, (byte) 0, (byte) 255,
065: (byte) 52);
066:
067: //writing to disk; reading in and verifying palette
068: File temp = TempFile
069: .createTempFile("testCustomPalette", ".xls");
070: FileOutputStream fos = new FileOutputStream(temp);
071: book.write(fos);
072: fos.close();
073:
074: fis = new FileInputStream(temp);
075: book = new HSSFWorkbook(fis);
076: fis.close();
077:
078: palette = book.getCustomPalette();
079: HSSFColor color = palette.getColor(HSSFColor.CORAL.index); //unmodified
080: assertNotNull(
081: "Unexpected null in custom palette (unmodified index)",
082: color);
083: short[] expectedRGB = HSSFColor.CORAL.triplet;
084: short[] actualRGB = color.getTriplet();
085: String msg = "Expected palette position to remain unmodified";
086: assertEquals(msg, expectedRGB[0], actualRGB[0]);
087: assertEquals(msg, expectedRGB[1], actualRGB[1]);
088: assertEquals(msg, expectedRGB[2], actualRGB[2]);
089:
090: color = palette.getColor((short) 0x12);
091: assertNotNull("Unexpected null in custom palette (modified)",
092: color);
093: actualRGB = color.getTriplet();
094: msg = "Expected palette modification to be preserved across save";
095: assertEquals(msg, (short) 101, actualRGB[0]);
096: assertEquals(msg, (short) 230, actualRGB[1]);
097: assertEquals(msg, (short) 100, actualRGB[2]);
098: }
099:
100: /**
101: * Uses the palette from cell stylings
102: */
103: public void testPaletteFromCellColours() throws Exception {
104: String dir = System.getProperty("HSSF.testdata.path");
105: File sample = new File(dir + "/SimpleWithColours.xls");
106: assertTrue("SimpleWithColours.xls exists and is readable",
107: sample.canRead());
108: FileInputStream fis = new FileInputStream(sample);
109: HSSFWorkbook book = new HSSFWorkbook(fis);
110: fis.close();
111:
112: HSSFPalette p = book.getCustomPalette();
113:
114: HSSFCell cellA = book.getSheetAt(0).getRow(0)
115: .getCell((short) 0);
116: HSSFCell cellB = book.getSheetAt(0).getRow(1)
117: .getCell((short) 0);
118: HSSFCell cellC = book.getSheetAt(0).getRow(2)
119: .getCell((short) 0);
120: HSSFCell cellD = book.getSheetAt(0).getRow(3)
121: .getCell((short) 0);
122: HSSFCell cellE = book.getSheetAt(0).getRow(4)
123: .getCell((short) 0);
124:
125: // Plain
126: assertEquals("I'm plain", cellA.getStringCellValue());
127: assertEquals(64, cellA.getCellStyle().getFillForegroundColor());
128: assertEquals(64, cellA.getCellStyle().getFillBackgroundColor());
129: assertEquals(HSSFFont.COLOR_NORMAL, cellA.getCellStyle()
130: .getFont(book).getColor());
131: assertEquals(0, cellA.getCellStyle().getFillPattern());
132: assertEquals("0:0:0", p.getColor((short) 64).getHexString());
133: assertEquals(null, p.getColor((short) 32767));
134:
135: // Red
136: assertEquals("I'm red", cellB.getStringCellValue());
137: assertEquals(64, cellB.getCellStyle().getFillForegroundColor());
138: assertEquals(64, cellB.getCellStyle().getFillBackgroundColor());
139: assertEquals(10, cellB.getCellStyle().getFont(book).getColor());
140: assertEquals(0, cellB.getCellStyle().getFillPattern());
141: assertEquals("0:0:0", p.getColor((short) 64).getHexString());
142: assertEquals("FFFF:0:0", p.getColor((short) 10).getHexString());
143:
144: // Red + green bg
145: assertEquals("I'm red with a green bg", cellC
146: .getStringCellValue());
147: assertEquals(11, cellC.getCellStyle().getFillForegroundColor());
148: assertEquals(64, cellC.getCellStyle().getFillBackgroundColor());
149: assertEquals(10, cellC.getCellStyle().getFont(book).getColor());
150: assertEquals(1, cellC.getCellStyle().getFillPattern());
151: assertEquals("0:FFFF:0", p.getColor((short) 11).getHexString());
152: assertEquals("FFFF:0:0", p.getColor((short) 10).getHexString());
153:
154: // Pink with yellow
155: assertEquals("I'm pink with a yellow pattern (none)", cellD
156: .getStringCellValue());
157: assertEquals(13, cellD.getCellStyle().getFillForegroundColor());
158: assertEquals(64, cellD.getCellStyle().getFillBackgroundColor());
159: assertEquals(14, cellD.getCellStyle().getFont(book).getColor());
160: assertEquals(0, cellD.getCellStyle().getFillPattern());
161: assertEquals("FFFF:FFFF:0", p.getColor((short) 13)
162: .getHexString());
163: assertEquals("FFFF:0:FFFF", p.getColor((short) 14)
164: .getHexString());
165:
166: // Pink with yellow - full
167: assertEquals("I'm pink with a yellow pattern (full)", cellE
168: .getStringCellValue());
169: assertEquals(13, cellE.getCellStyle().getFillForegroundColor());
170: assertEquals(64, cellE.getCellStyle().getFillBackgroundColor());
171: assertEquals(14, cellE.getCellStyle().getFont(book).getColor());
172: assertEquals(0, cellE.getCellStyle().getFillPattern());
173: assertEquals("FFFF:FFFF:0", p.getColor((short) 13)
174: .getHexString());
175: assertEquals("FFFF:0:FFFF", p.getColor((short) 14)
176: .getHexString());
177: }
178:
179: /**
180: * Verifies that the generated gnumeric-format string values match the
181: * hardcoded values in the HSSFColor default color palette
182: */
183: public void testGnumericStrings() {
184: compareToDefaults(new ColorComparator() {
185: public void compare(HSSFColor expected, HSSFColor palette) {
186: assertEquals(expected.getHexString(), palette
187: .getHexString());
188: }
189: });
190: }
191:
192: /**
193: * Verifies that the palette handles invalid palette indexes
194: */
195: public void testBadIndexes() {
196: //too small
197: hssfPalette.setColorAtIndex((short) 2, (byte) 255, (byte) 255,
198: (byte) 255);
199: //too large
200: hssfPalette.setColorAtIndex((short) 0x45, (byte) 255,
201: (byte) 255, (byte) 255);
202:
203: //should still match defaults;
204: compareToDefaults(new ColorComparator() {
205: public void compare(HSSFColor expected, HSSFColor palette) {
206: short[] s1 = expected.getTriplet();
207: short[] s2 = palette.getTriplet();
208: assertEquals(s1[0], s2[0]);
209: assertEquals(s1[1], s2[1]);
210: assertEquals(s1[2], s2[2]);
211: }
212: });
213: }
214:
215: private void compareToDefaults(ColorComparator c) {
216: Map colors = HSSFColor.getIndexHash();
217: Iterator it = colors.keySet().iterator();
218: while (it.hasNext()) {
219: Number index = (Number) it.next();
220: HSSFColor expectedColor = (HSSFColor) colors.get(index);
221: HSSFColor paletteColor = hssfPalette.getColor(index
222: .shortValue());
223: c.compare(expectedColor, paletteColor);
224: }
225: }
226:
227: public void testAddColor() throws Exception {
228: try {
229: HSSFColor hssfColor = hssfPalette.addColor((byte) 10,
230: (byte) 10, (byte) 10);
231: fail();
232: } catch (RuntimeException e) {
233: // Failing because by default there are no colours left in the palette.
234: }
235: }
236:
237: private static interface ColorComparator {
238: void compare(HSSFColor expected, HSSFColor palette);
239: }
240: }
|