aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/hssf/usermodel/TestUnicodeWorkbook.java
blob: 2a9215285781e676959834c5fd59358ed67587ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.poi.hssf.usermodel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.util.TempFile;

import junit.framework.TestCase;

public class TestUnicodeWorkbook extends TestCase {

    public TestUnicodeWorkbook(String s) {
        super(s);
    }
    
    /** Tests that all of the unicode capable string fields can be set, written and then read back
     * 
     *
     */
    public void testUnicodeInAll() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        //Create a unicode dataformat (contains euro symbol)
        HSSFDataFormat df = wb.createDataFormat();
        final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";
        short fmt = df.getFormat(formatStr);
        
        //Create a unicode sheet name (euro symbol)
        HSSFSheet s = wb.createSheet("\u20ac");
        
        //Set a unicode header (you guessed it the euro symbol)
        HSSFHeader h = s.getHeader();
        h.setCenter("\u20ac");
        h.setLeft("\u20ac");
        h.setRight("\u20ac");
        
        //Set a unicode footer
        HSSFFooter f = s.getFooter();
        f.setCenter("\u20ac");
        f.setLeft("\u20ac");
        f.setRight("\u20ac");                

        HSSFRow r = s.createRow(0);
        HSSFCell c = r.createCell(1);
        c.setCellValue(12.34);
        c.getCellStyle().setDataFormat(fmt);
        
        HSSFCell c2 = r.createCell(2);
        c.setCellValue(new HSSFRichTextString("\u20ac"));

        HSSFCell c3 = r.createCell(3);
        String formulaString = "TEXT(12.34,\"\u20ac###,##\")";
        c3.setCellFormula(formulaString);

        
        File tempFile = TempFile.createTempFile("unicode", "test.xls");
        FileOutputStream stream = new FileOutputStream(tempFile);
        wb.write(stream);
        
        wb = null;
        FileInputStream in = new FileInputStream(tempFile);
        wb = new HSSFWorkbook(in);

        //Test the sheetname
        s = wb.getSheet("\u20ac");
        assertNotNull(s);
        
        //Test the header
        h = s.getHeader();
        assertEquals(h.getCenter(), "\u20ac");
        assertEquals(h.getLeft(), "\u20ac");
        assertEquals(h.getRight(), "\u20ac");
        
        //Test the footer
        f = s.getFooter();
        assertEquals(f.getCenter(), "\u20ac");
        assertEquals(f.getLeft(), "\u20ac");
        assertEquals(f.getRight(), "\u20ac");                

        //Test the dataformat
        r = s.getRow(0);
        c = r.getCell(1);
        df = wb.createDataFormat();
        assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));
        
        //Test the cell string value
        c2 = r.getCell(2);
        assertEquals(c.getRichStringCellValue().getString(), "\u20ac");
        
        //Test the cell formula
        c3 = r.getCell(3);
        assertEquals(c3.getCellFormula(), formulaString);
    }
    
    /** Tests Bug38230
     *  That a Umlat is written  and then read back.
     *  It should have been written as a compressed unicode.
     * 
     * 
     *
     */
    public void testUmlatReadWrite() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        
        //Create a unicode sheet name (euro symbol)
        HSSFSheet s = wb.createSheet("test");
        
        HSSFRow r = s.createRow(0);
        HSSFCell c = r.createCell(1);
        c.setCellValue(new HSSFRichTextString("\u00e4"));
        
        //Confirm that the sring will be compressed
        assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0);
        
        File tempFile = TempFile.createTempFile("umlat", "test.xls");
        FileOutputStream stream = new FileOutputStream(tempFile);
        wb.write(stream);
        
        wb = null;
        FileInputStream in = new FileInputStream(tempFile);
        wb = new HSSFWorkbook(in);

        //Test the sheetname
        s = wb.getSheet("test");
        assertNotNull(s);
        
        c = r.getCell(1);
        assertEquals(c.getRichStringCellValue().getString(), "\u00e4");
    }    

}