KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > TestHSSFPalette


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hssf.usermodel;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
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 /**
32  * @author Brian Sanders (bsanders at risklabs dot com)
33  */

34 public class TestHSSFPalette extends TestCase
35 {
36     private PaletteRecord palette;
37     private HSSFPalette hssfPalette;
38     
39     public TestHSSFPalette(String JavaDoc 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     /**
51      * Verifies that a custom palette can be created, saved, and reloaded
52      */

53     public void testCustomPalette() throws IOException JavaDoc
54     {
55         //reading sample xls
56
String JavaDoc dir = System.getProperty("HSSF.testdata.path");
57         File JavaDoc sample = new File JavaDoc(dir + "/Simple.xls");
58         assertTrue("Simple.xls exists and is readable", sample.canRead());
59         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(sample);
60         HSSFWorkbook book = new HSSFWorkbook(fis);
61         fis.close();
62         
63         //creating custom palette
64
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         //writing to disk; reading in and verifying palette
69
File JavaDoc temp = TempFile.createTempFile("testCustomPalette", ".xls");
70         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(temp);
71         book.write(fos);
72         fos.close();
73         
74         fis = new FileInputStream JavaDoc(temp);
75         book = new HSSFWorkbook(fis);
76         fis.close();
77         
78         palette = book.getCustomPalette();
79         HSSFColor color = palette.getColor(HSSFColor.CORAL.index); //unmodified
80
assertNotNull("Unexpected null in custom palette (unmodified index)", color);
81         short[] expectedRGB = HSSFColor.CORAL.triplet;
82         short[] actualRGB = color.getTriplet();
83         String JavaDoc 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     /**
98      * Verifies that the generated gnumeric-format string values match the
99      * hardcoded values in the HSSFColor default color palette
100      */

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     /**
112      * Verifies that the palette handles invalid palette indexes
113      */

114     public void testBadIndexes()
115     {
116         //too small
117
hssfPalette.setColorAtIndex((short) 2, (byte) 255, (byte) 255, (byte) 255);
118         //too large
119
hssfPalette.setColorAtIndex((short) 0x45, (byte) 255, (byte) 255, (byte) 255);
120         
121         //should still match defaults;
122
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 JavaDoc colors = HSSFColor.getIndexHash();
137         Iterator JavaDoc it = colors.keySet().iterator();
138         while (it.hasNext())
139         {
140             Number JavaDoc index = (Number JavaDoc) 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 JavaDoc
148     {
149         try
150         {
151             HSSFColor hssfColor = hssfPalette.addColor((byte)10,(byte)10,(byte)10);
152             fail();
153         }
154         catch ( RuntimeException JavaDoc e )
155         {
156             // Failing because by default there are no colours left in the palette.
157
}
158     }
159
160     private static interface ColorComparator
161     {
162         void compare(HSSFColor expected, HSSFColor palette);
163     }
164 }
165
Popular Tags