Source Code Cross Referenced for TestHSSFPalette.java in  » Collaboration » poi-3.0.2-beta2 » org » apache » poi » hssf » usermodel » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Collaboration » poi 3.0.2 beta2 » org.apache.poi.hssf.usermodel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.