1
1
package com .baeldung .poi .excel .merge ;
2
2
3
- import static org .junit .Assert .assertEquals ;
4
- import static org .junit .Assert .assertNotNull ;
5
3
6
- import java .io .FileOutputStream ;
4
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
5
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
6
+
7
7
import java .io .IOException ;
8
- import java .io .InputStream ;
9
8
import java .net .URISyntaxException ;
10
- import java .nio .file .Files ;
11
9
import java .nio .file .Paths ;
12
10
13
11
import org .apache .poi .ss .usermodel .Cell ;
19
17
import org .apache .poi .ss .usermodel .Workbook ;
20
18
import org .apache .poi .ss .util .CellRangeAddress ;
21
19
import org .apache .poi .xssf .usermodel .XSSFWorkbook ;
22
- import org .junit .Before ;
23
- import org .junit .Test ;
20
+ import org .junit .jupiter .api .BeforeEach ;
21
+ import org .junit .jupiter .api .Test ;
22
+
23
+ class ExcelCellMergerUnitTest {
24
24
25
- public class ExcelCellMergerUnitTest {
26
25
private static final String FILE_NAME = "ExcelCellFormatterTest.xlsx" ;
27
26
28
27
private static final String FILE_NAME_2 = "MergedAlignCell.xlsx" ;
@@ -31,21 +30,25 @@ public class ExcelCellMergerUnitTest {
31
30
32
31
private String fileLocation2 ;
33
32
34
- @ Before
35
- public void setup () throws IOException , URISyntaxException {
36
- fileLocation = Paths .get (ClassLoader .getSystemResource (FILE_NAME ).toURI ()).toString ();
37
- fileLocation2 = Paths .get (ClassLoader .getSystemResource (FILE_NAME_2 ).toURI ()).toString ();
33
+ @ BeforeEach
34
+ void setup () throws IOException , URISyntaxException {
35
+ fileLocation = Paths .get (ClassLoader .getSystemResource (FILE_NAME )
36
+ .toURI ())
37
+ .toString ();
38
+ fileLocation2 = Paths .get (ClassLoader .getSystemResource (FILE_NAME_2 )
39
+ .toURI ())
40
+ .toString ();
38
41
}
39
42
40
43
@ Test
41
- public void givenCellIndex_whenAddMergeRegion_thenMergeRegionCreated () throws IOException {
44
+ void givenCellIndex_whenAddMergeRegion_thenMergeRegionCreated () throws IOException {
42
45
Workbook workbook = new XSSFWorkbook (fileLocation );
43
46
Sheet sheet = workbook .getSheetAt (0 );
44
47
45
48
assertEquals (0 , sheet .getNumMergedRegions ());
46
- int firstRow = 0 ;
47
- int lastRow = 0 ;
48
- int firstCol = 0 ;
49
+ int firstRow = 0 ;
50
+ int lastRow = 0 ;
51
+ int firstCol = 0 ;
49
52
int lastCol = 2 ;
50
53
sheet .addMergedRegion (new CellRangeAddress (firstRow , lastRow , firstCol , lastCol ));
51
54
assertEquals (1 , sheet .getNumMergedRegions ());
@@ -54,19 +57,19 @@ public void givenCellIndex_whenAddMergeRegion_thenMergeRegionCreated() throws IO
54
57
}
55
58
56
59
@ Test
57
- public void givenCellRefString_whenAddMergeRegion_thenMergeRegionCreated () throws IOException {
60
+ void givenCellRefString_whenAddMergeRegion_thenMergeRegionCreated () throws IOException {
58
61
Workbook workbook = new XSSFWorkbook (fileLocation );
59
62
Sheet sheet = workbook .getSheetAt (0 );
60
63
61
- assertEquals (0 , sheet .getNumMergedRegions ());
64
+ assertEquals (0 , sheet .getNumMergedRegions ());
62
65
sheet .addMergedRegion (CellRangeAddress .valueOf ("A1:C1" ));
63
66
assertEquals (1 , sheet .getNumMergedRegions ());
64
67
65
68
workbook .close ();
66
69
}
67
70
68
71
@ Test
69
- public void givenCellIndex_whenAddMergeRegionWithCenterAlignment_thenMergeRegionWithCenterCreated () throws IOException {
72
+ void givenCellIndex_whenAddMergeRegionWithCenterAlignment_thenMergeRegionWithCenterCreated () throws IOException {
70
73
Workbook workbook = new XSSFWorkbook (fileLocation2 );
71
74
Sheet sheet = workbook .getSheetAt (0 );
72
75
@@ -91,7 +94,8 @@ public void givenCellIndex_whenAddMergeRegionWithCenterAlignment_thenMergeRegion
91
94
assertEquals (1 , sheet .getNumMergedRegions ());
92
95
93
96
// Verify cell style (alignment)
94
- Cell readCell = sheet .getRow (firstRow ).getCell (firstCol );
97
+ Cell readCell = sheet .getRow (firstRow )
98
+ .getCell (firstCol );
95
99
assertNotNull (readCell );
96
100
CellStyle readCellStyle = readCell .getCellStyle ();
97
101
assertEquals (HorizontalAlignment .CENTER , readCellStyle .getAlignment ());
0 commit comments