1 2 17 18 package org.apache.poi.hssf.usermodel; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import junit.framework.TestCase; 27 import org.apache.poi.hssf.record.PaletteRecord; 28 import org.apache.poi.hssf.util.HSSFColor; 29 import org.apache.poi.util.TempFile; 30 31 34 public class TestHSSFPalette extends TestCase 35 { 36 private PaletteRecord palette; 37 private HSSFPalette hssfPalette; 38 39 public TestHSSFPalette(String name) 40 { 41 super(name); 42 } 43 44 public void setUp() 45 { 46 palette = new PaletteRecord(PaletteRecord.sid); 47 hssfPalette = new HSSFPalette(palette); 48 } 49 50 53 public void testCustomPalette() throws IOException 54 { 55 String dir = System.getProperty("HSSF.testdata.path"); 57 File sample = new File (dir + "/Simple.xls"); 58 assertTrue("Simple.xls exists and is readable", sample.canRead()); 59 FileInputStream fis = new FileInputStream (sample); 60 HSSFWorkbook book = new HSSFWorkbook(fis); 61 fis.close(); 62 63 HSSFPalette palette = book.getCustomPalette(); 65 palette.setColorAtIndex((short) 0x12, (byte) 101, (byte) 230, (byte) 100); 66 palette.setColorAtIndex((short) 0x3b, (byte) 0, (byte) 255, (byte) 52); 67 68 File temp = TempFile.createTempFile("testCustomPalette", ".xls"); 70 FileOutputStream fos = new FileOutputStream (temp); 71 book.write(fos); 72 fos.close(); 73 74 fis = new FileInputStream (temp); 75 book = new HSSFWorkbook(fis); 76 fis.close(); 77 78 palette = book.getCustomPalette(); 79 HSSFColor color = palette.getColor(HSSFColor.CORAL.index); assertNotNull("Unexpected null in custom palette (unmodified index)", color); 81 short[] expectedRGB = HSSFColor.CORAL.triplet; 82 short[] actualRGB = color.getTriplet(); 83 String msg = "Expected palette position to remain unmodified"; 84 assertEquals(msg, expectedRGB[0], actualRGB[0]); 85 assertEquals(msg, expectedRGB[1], actualRGB[1]); 86 assertEquals(msg, expectedRGB[2], actualRGB[2]); 87 88 color = palette.getColor((short) 0x12); 89 assertNotNull("Unexpected null in custom palette (modified)", color); 90 actualRGB = color.getTriplet(); 91 msg = "Expected palette modification to be preserved across save"; 92 assertEquals(msg, (short) 101, actualRGB[0]); 93 assertEquals(msg, (short) 230, actualRGB[1]); 94 assertEquals(msg, (short) 100, actualRGB[2]); 95 } 96 97 101 public void testGnumericStrings() 102 { 103 compareToDefaults(new ColorComparator() { 104 public void compare(HSSFColor expected, HSSFColor palette) 105 { 106 assertEquals(expected.getHexString(), palette.getHexString()); 107 } 108 }); 109 } 110 111 114 public void testBadIndexes() 115 { 116 hssfPalette.setColorAtIndex((short) 2, (byte) 255, (byte) 255, (byte) 255); 118 hssfPalette.setColorAtIndex((short) 0x45, (byte) 255, (byte) 255, (byte) 255); 120 121 compareToDefaults(new ColorComparator() { 123 public void compare(HSSFColor expected, HSSFColor palette) 124 { 125 short[] s1 = expected.getTriplet(); 126 short[] s2 = palette.getTriplet(); 127 assertEquals(s1[0], s2[0]); 128 assertEquals(s1[1], s2[1]); 129 assertEquals(s1[2], s2[2]); 130 } 131 }); 132 } 133 134 private void compareToDefaults(ColorComparator c) 135 { 136 Map colors = HSSFColor.getIndexHash(); 137 Iterator it = colors.keySet().iterator(); 138 while (it.hasNext()) 139 { 140 Number index = (Number ) it.next(); 141 HSSFColor expectedColor = (HSSFColor) colors.get(index); 142 HSSFColor paletteColor = hssfPalette.getColor(index.shortValue()); 143 c.compare(expectedColor, paletteColor); 144 } 145 } 146 147 public void testAddColor() throws Exception 148 { 149 try 150 { 151 HSSFColor hssfColor = hssfPalette.addColor((byte)10,(byte)10,(byte)10); 152 fail(); 153 } 154 catch ( RuntimeException e ) 155 { 156 } 158 } 159 160 private static interface ColorComparator 161 { 162 void compare(HSSFColor expected, HSSFColor palette); 163 } 164 } 165 | Popular Tags |