aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java
blob: ebf84dc4245286e755ea98efa5246b53ff09d640 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* ====================================================================
   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.ss.util;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Tests SheetUtil.
 *
 * @see org.apache.poi.ss.util.SheetUtil
 */
public final class TestSheetUtil {
    @Test
    public void testCellWithMerges() throws Exception {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet s = wb.createSheet();

            // Create some test data
            Row r2 = s.createRow(1);
            r2.createCell(0).setCellValue(10);
            r2.createCell(1).setCellValue(11);
            Row r3 = s.createRow(2);
            r3.createCell(0).setCellValue(20);
            r3.createCell(1).setCellValue(21);

            assertEquals(0, s.addMergedRegion(new CellRangeAddress(2, 3, 0, 0)));
            assertEquals(1, s.addMergedRegion(new CellRangeAddress(2, 2, 1, 4)));

            // With a cell that isn't defined, we'll get null
            assertNull(SheetUtil.getCellWithMerges(s, 0, 0));

            // With a cell that's not in a merged region, we'll get that
            Cell cell = SheetUtil.getCellWithMerges(s, 1, 0);
            assertNotNull(cell);
            assertEquals(10.0, cell.getNumericCellValue(), 0.01);
            cell = SheetUtil.getCellWithMerges(s, 1, 1);
            assertNotNull(cell);
            assertEquals(11.0, cell.getNumericCellValue(), 0.01);

            // With a cell that's the primary one of a merged region, we get that cell
            cell = SheetUtil.getCellWithMerges(s, 2, 0);
            assertNotNull(cell);
            assertEquals(20.0, cell.getNumericCellValue(), 0.01);
            cell = SheetUtil.getCellWithMerges(s, 2, 1);
            assertNotNull(cell);
            assertEquals(21., cell.getNumericCellValue(), 0.01);

            // With a cell elsewhere in the merged region, get top-left
            cell = SheetUtil.getCellWithMerges(s, 3, 0);
            assertNotNull(cell);
            assertEquals(20.0, cell.getNumericCellValue(), 0.01);
            cell = SheetUtil.getCellWithMerges(s, 2, 2);
            assertNotNull(cell);
            assertEquals(21.0, cell.getNumericCellValue(), 0.01);
            cell = SheetUtil.getCellWithMerges(s, 2, 3);
            assertNotNull(cell);
            assertEquals(21.0, cell.getNumericCellValue(), 0.01);
            assertNotNull(cell);
            cell = SheetUtil.getCellWithMerges(s, 2, 4);
            assertNotNull(cell);
            assertEquals(21.0, cell.getNumericCellValue(), 0.01);
        }
    }

    @Test
    public void testCanComputeWidthHSSF() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            // cannot check on result because on some machines we get back false here!
            SheetUtil.canComputeColumnWidth(wb.getFontAt(0));
        }
    }

    @Test
    public void testGetCellWidthEmpty() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet sheet = wb.createSheet("sheet");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);

            // no contents: cell.setCellValue("sometext");
            assertEquals(-1.0, SheetUtil.getCellWidth(cell, 1, null, true), 0.01);
        }
    }

    @Test
    public void testGetCellWidthString() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet sheet = wb.createSheet("sheet");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);

            cell.setCellValue("sometext");

            assertTrue(SheetUtil.getCellWidth(cell, 1, null, true) > 0);
        }
    }

    @Test
    public void testGetCellWidthNumber() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet sheet = wb.createSheet("sheet");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);

            cell.setCellValue(88.234);

            assertTrue(SheetUtil.getCellWidth(cell, 1, null, true) > 0);
        }
    }

    @Test
    public void testGetCellWidthBoolean() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet sheet = wb.createSheet("sheet");
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);

            cell.setCellValue(false);

            assertTrue(SheetUtil.getCellWidth(cell, 1, null, false) > 0);
        }
    }

    @Test
    public void testGetColumnWidthString() throws IOException {
        try (Workbook wb = new HSSFWorkbook()) {
            Sheet sheet = wb.createSheet("sheet");
            Row row = sheet.createRow(0);
            sheet.createRow(1);
            sheet.createRow(2);
            Cell cell = row.createCell(0);

            cell.setCellValue("sometext");

            assertTrue(SheetUtil.getColumnWidth(sheet, 0, true) > 0, "Having some width for rows with actual cells");
            assertEquals(-1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01, "Not having any widht for rows with all empty cells");
        }
    }
}