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: package org.apache.poi.hssf.usermodel;
018:
019: import java.io.File;
020: import java.io.FileOutputStream;
021: import java.io.FileInputStream;
022:
023: import org.apache.poi.util.TempFile;
024:
025: import junit.framework.TestCase;
026:
027: public class TestUnicodeWorkbook extends TestCase {
028:
029: public TestUnicodeWorkbook(String s) {
030: super (s);
031: }
032:
033: /** Tests that all of the unicode capable string fields can be set, written and then read back
034: *
035: *
036: */
037: public void testUnicodeInAll() throws Exception {
038: HSSFWorkbook wb = new HSSFWorkbook();
039: //Create a unicode dataformat (contains euro symbol)
040: HSSFDataFormat df = wb.createDataFormat();
041: final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";
042: short fmt = df.getFormat(formatStr);
043:
044: //Create a unicode sheet name (euro symbol)
045: HSSFSheet s = wb.createSheet("\u20ac");
046:
047: //Set a unicode header (you guessed it the euro symbol)
048: HSSFHeader h = s.getHeader();
049: h.setCenter("\u20ac");
050: h.setLeft("\u20ac");
051: h.setRight("\u20ac");
052:
053: //Set a unicode footer
054: HSSFFooter f = s.getFooter();
055: f.setCenter("\u20ac");
056: f.setLeft("\u20ac");
057: f.setRight("\u20ac");
058:
059: HSSFRow r = s.createRow(0);
060: HSSFCell c = r.createCell((short) 1);
061: c.setCellValue(12.34);
062: c.getCellStyle().setDataFormat(fmt);
063:
064: HSSFCell c2 = r.createCell((short) 2);
065: c.setCellValue(new HSSFRichTextString("\u20ac"));
066:
067: HSSFCell c3 = r.createCell((short) 3);
068: String formulaString = "TEXT(12.34,\"\u20ac###,##\")";
069: c3.setCellFormula(formulaString);
070:
071: File tempFile = TempFile.createTempFile("unicode", "test.xls");
072: FileOutputStream stream = new FileOutputStream(tempFile);
073: wb.write(stream);
074:
075: wb = null;
076: FileInputStream in = new FileInputStream(tempFile);
077: wb = new HSSFWorkbook(in);
078:
079: //Test the sheetname
080: s = wb.getSheet("\u20ac");
081: assertNotNull(s);
082:
083: //Test the header
084: h = s.getHeader();
085: assertEquals(h.getCenter(), "\u20ac");
086: assertEquals(h.getLeft(), "\u20ac");
087: assertEquals(h.getRight(), "\u20ac");
088:
089: //Test the footer
090: f = s.getFooter();
091: assertEquals(f.getCenter(), "\u20ac");
092: assertEquals(f.getLeft(), "\u20ac");
093: assertEquals(f.getRight(), "\u20ac");
094:
095: //Test the dataformat
096: r = s.getRow(0);
097: c = r.getCell((short) 1);
098: df = wb.createDataFormat();
099: assertEquals(formatStr, df.getFormat(c.getCellStyle()
100: .getDataFormat()));
101:
102: //Test the cell string value
103: c2 = r.getCell((short) 2);
104: assertEquals(c.getRichStringCellValue().getString(), "\u20ac");
105:
106: //Test the cell formula
107: c3 = r.getCell((short) 3);
108: assertEquals(c3.getCellFormula(), formulaString);
109: }
110:
111: /** Tests Bug38230
112: * That a Umlat is written and then read back.
113: * It should have been written as a compressed unicode.
114: *
115: *
116: *
117: */
118: public void testUmlatReadWrite() throws Exception {
119: HSSFWorkbook wb = new HSSFWorkbook();
120:
121: //Create a unicode sheet name (euro symbol)
122: HSSFSheet s = wb.createSheet("test");
123:
124: HSSFRow r = s.createRow(0);
125: HSSFCell c = r.createCell((short) 1);
126: c.setCellValue(new HSSFRichTextString("\u00e4"));
127:
128: //Confirm that the sring will be compressed
129: assertEquals(c.getRichStringCellValue().getUnicodeString()
130: .getOptionFlags(), 0);
131:
132: File tempFile = TempFile.createTempFile("umlat", "test.xls");
133: FileOutputStream stream = new FileOutputStream(tempFile);
134: wb.write(stream);
135:
136: wb = null;
137: FileInputStream in = new FileInputStream(tempFile);
138: wb = new HSSFWorkbook(in);
139:
140: //Test the sheetname
141: s = wb.getSheet("test");
142: assertNotNull(s);
143:
144: c = r.getCell((short) 1);
145: assertEquals(c.getRichStringCellValue().getString(), "\u00e4");
146: }
147:
148: }
|