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: /*
019: * TestCellStyle.java
020: *
021: * Created on December 11, 2001, 5:51 PM
022: */
023: package org.apache.poi.hssf.usermodel;
024:
025: import java.io.*;
026:
027: import java.util.*;
028:
029: import junit.framework.*;
030: import org.apache.poi.util.TempFile;
031:
032: /**
033: * Class to test cell styling functionality
034: *
035: * @author Andrew C. Oliver
036: */
037:
038: public class TestCellStyle extends TestCase {
039:
040: /** Creates a new instance of TestCellStyle */
041:
042: public TestCellStyle(String name) {
043: super (name);
044: }
045:
046: /**
047: * TEST NAME: Test Write Sheet Font <P>
048: * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with fonts.<P>
049: * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
050: * Last row, first row is tested against the correct values (99,0).<P>
051: * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
052: * HSSFSheet last row or first row is incorrect. <P>
053: *
054: */
055:
056: public void testWriteSheetFont() throws IOException {
057: File file = TempFile.createTempFile("testWriteSheetFont",
058: ".xls");
059: FileOutputStream out = new FileOutputStream(file);
060: HSSFWorkbook wb = new HSSFWorkbook();
061: HSSFSheet s = wb.createSheet();
062: HSSFRow r = null;
063: HSSFCell c = null;
064: HSSFFont fnt = wb.createFont();
065: HSSFCellStyle cs = wb.createCellStyle();
066:
067: fnt.setColor(HSSFFont.COLOR_RED);
068: fnt.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
069: cs.setFont(fnt);
070: for (short rownum = (short) 0; rownum < 100; rownum++) {
071: r = s.createRow(rownum);
072:
073: // r.setRowNum(( short ) rownum);
074: for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
075: c = r.createCell(cellnum);
076: c
077: .setCellValue(rownum
078: * 10000
079: + cellnum
080: + (((double) rownum / 1000) + ((double) cellnum / 10000)));
081: c = r.createCell((short) (cellnum + 1));
082: c.setCellValue("TEST");
083: c.setCellStyle(cs);
084: }
085: }
086: wb.write(out);
087: out.close();
088: SanityChecker sanityChecker = new SanityChecker();
089: sanityChecker.checkHSSFWorkbook(wb);
090: assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
091: assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
092:
093: // assert((s.getLastRowNum() == 99));
094: }
095:
096: /**
097: * Tests that is creating a file with a date or an calendar works correctly.
098: */
099: public void testDataStyle() throws Exception {
100: File file = TempFile.createTempFile("testWriteSheetStyleDate",
101: ".xls");
102: FileOutputStream out = new FileOutputStream(file);
103: HSSFWorkbook wb = new HSSFWorkbook();
104: HSSFSheet s = wb.createSheet();
105: HSSFCellStyle cs = wb.createCellStyle();
106: HSSFRow row = s.createRow((short) 0);
107:
108: // with Date:
109: HSSFCell cell = row.createCell((short) 1);
110: cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
111: cell.setCellStyle(cs);
112: cell.setCellValue(new Date());
113:
114: // with Calendar:
115: cell = row.createCell((short) 2);
116: cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
117: cell.setCellStyle(cs);
118: Calendar cal = Calendar.getInstance();
119: cal.setTime(new Date());
120: cell.setCellValue(cal);
121:
122: wb.write(out);
123: out.close();
124: SanityChecker sanityChecker = new SanityChecker();
125: sanityChecker.checkHSSFWorkbook(wb);
126:
127: assertEquals("LAST ROW ", 0, s.getLastRowNum());
128: assertEquals("FIRST ROW ", 0, s.getFirstRowNum());
129:
130: }
131:
132: /**
133: * TEST NAME: Test Write Sheet Style <P>
134: * OBJECTIVE: Test that HSSF can create a simple spreadsheet with numeric and string values and styled with colors
135: * and borders.<P>
136: * SUCCESS: HSSF creates a sheet. Filesize matches a known good. HSSFSheet objects
137: * Last row, first row is tested against the correct values (99,0).<P>
138: * FAILURE: HSSF does not create a sheet or excepts. Filesize does not match the known good.
139: * HSSFSheet last row or first row is incorrect. <P>
140: *
141: */
142:
143: public void testWriteSheetStyle() throws IOException {
144: File file = TempFile.createTempFile("testWriteSheetStyle",
145: ".xls");
146: FileOutputStream out = new FileOutputStream(file);
147: HSSFWorkbook wb = new HSSFWorkbook();
148: HSSFSheet s = wb.createSheet();
149: HSSFRow r = null;
150: HSSFCell c = null;
151: HSSFFont fnt = wb.createFont();
152: HSSFCellStyle cs = wb.createCellStyle();
153: HSSFCellStyle cs2 = wb.createCellStyle();
154:
155: cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
156: cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
157: cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
158: cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
159: cs.setFillForegroundColor((short) 0xA);
160: cs.setFillPattern((short) 1);
161: fnt.setColor((short) 0xf);
162: fnt.setItalic(true);
163: cs2.setFillForegroundColor((short) 0x0);
164: cs2.setFillPattern((short) 1);
165: cs2.setFont(fnt);
166: for (short rownum = (short) 0; rownum < 100; rownum++) {
167: r = s.createRow(rownum);
168:
169: // r.setRowNum(( short ) rownum);
170: for (short cellnum = (short) 0; cellnum < 50; cellnum += 2) {
171: c = r.createCell(cellnum);
172: c
173: .setCellValue(rownum
174: * 10000
175: + cellnum
176: + (((double) rownum / 1000) + ((double) cellnum / 10000)));
177: c.setCellStyle(cs);
178: c = r.createCell((short) (cellnum + 1));
179: c.setCellValue("TEST");
180: c.setCellStyle(cs2);
181: }
182: }
183: wb.write(out);
184: out.close();
185: SanityChecker sanityChecker = new SanityChecker();
186: sanityChecker.checkHSSFWorkbook(wb);
187: assertEquals("LAST ROW == 99", 99, s.getLastRowNum());
188: assertEquals("FIRST ROW == 0", 0, s.getFirstRowNum());
189:
190: // assert((s.getLastRowNum() == 99));
191: }
192:
193: public static void main(String[] ignored_args) {
194: System.out
195: .println("Testing org.apache.poi.hssf.usermodel.HSSFCellStyle");
196: junit.textui.TestRunner.run(TestCellStyle.class);
197: }
198: }
|