Skip to content

Commit d1e3223

Browse files
authored
Merge pull request #4 from MBenincasa/develop
Developed the tests of the available tools
2 parents 52b6816 + 7928496 commit d1e3223

File tree

7 files changed

+885
-0
lines changed

7 files changed

+885
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,11 @@
133133
<artifactId>opencsv</artifactId>
134134
<version>5.7.1</version>
135135
</dependency>
136+
<dependency>
137+
<groupId>org.junit.jupiter</groupId>
138+
<artifactId>junit-jupiter</artifactId>
139+
<version>RELEASE</version>
140+
<scope>test</scope>
141+
</dependency>
136142
</dependencies>
137143
</project>

src/test/java/tools/ConverterTest.java

Lines changed: 476 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package tools;
2+
3+
import exceptions.ExtensionNotValidException;
4+
import exceptions.OpenWorkbookException;
5+
import exceptions.SheetNotFoundException;
6+
import org.apache.commons.io.FilenameUtils;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.List;
13+
14+
class ExcelUtilityTest {
15+
16+
private final File excelFile = new File("./src/test/resources/employee.xlsx");
17+
18+
@Test
19+
void countAllRowsOfAllSheets() throws OpenWorkbookException, ExtensionNotValidException, IOException {
20+
List<Integer> results = ExcelUtility.countAllRowsOfAllSheets(excelFile);
21+
Assertions.assertEquals(3, results.get(0));
22+
Assertions.assertEquals(3, results.get(1));
23+
}
24+
25+
@Test
26+
void testCountAllRowsOfAllSheets() throws OpenWorkbookException, ExtensionNotValidException, IOException {
27+
List<Integer> results = ExcelUtility.countAllRowsOfAllSheets(excelFile, false);
28+
Assertions.assertEquals(3, results.get(0));
29+
Assertions.assertEquals(3, results.get(1));
30+
}
31+
32+
@Test
33+
void countAllRows() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
34+
Integer count = ExcelUtility.countAllRows(excelFile, "Office");
35+
Assertions.assertEquals(3, count);
36+
}
37+
38+
@Test
39+
void testCountAllRows() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
40+
Integer count = ExcelUtility.countAllRows(excelFile, "Office", false);
41+
Assertions.assertEquals(3, count);
42+
}
43+
44+
@Test
45+
void checkExcelExtension() throws ExtensionNotValidException {
46+
String filename = excelFile.getName();
47+
String extension = ExcelUtility.checkExcelExtension(filename);
48+
Assertions.assertEquals("xlsx", extension);
49+
}
50+
51+
@Test
52+
void isValidExcelExtension() {
53+
String filename = excelFile.getName();
54+
String extension = FilenameUtils.getExtension(filename);
55+
Assertions.assertEquals(true, ExcelUtility.isValidExcelExtension(extension));
56+
}
57+
58+
@Test
59+
void getIndexLastRow() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
60+
Integer index = ExcelUtility.getIndexLastRow(excelFile);
61+
Assertions.assertEquals(3, index);
62+
}
63+
64+
@Test
65+
void testGetIndexLastRow() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
66+
String sheetName = "Employee";
67+
Integer index = ExcelUtility.getIndexLastRow(excelFile, sheetName);
68+
Assertions.assertEquals(3, index);
69+
}
70+
71+
@Test
72+
void getIndexLastColumn() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
73+
Integer index = ExcelUtility.getIndexLastColumn(excelFile);
74+
Assertions.assertEquals(8, index);
75+
}
76+
77+
@Test
78+
void testGetIndexLastColumn() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
79+
String sheetName = "Employee";
80+
Integer index = ExcelUtility.getIndexLastColumn(excelFile, sheetName);
81+
Assertions.assertEquals(8, index);
82+
}
83+
84+
@Test
85+
void testGetIndexLastColumn1() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
86+
Integer index = ExcelUtility.getIndexLastColumn(excelFile, 1);
87+
Assertions.assertEquals(8, index);
88+
}
89+
90+
@Test
91+
void testGetIndexLastColumn2() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
92+
String sheetName = "Employee";
93+
Integer index = ExcelUtility.getIndexLastColumn(excelFile, sheetName, 1);
94+
Assertions.assertEquals(8, index);
95+
}
96+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package tools;
2+
3+
import exceptions.ExtensionNotValidException;
4+
import exceptions.OpenWorkbookException;
5+
import exceptions.SheetNotFoundException;
6+
import org.apache.poi.ss.usermodel.Sheet;
7+
import org.apache.poi.ss.usermodel.Workbook;
8+
import org.junit.jupiter.api.Assertions;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
class SheetUtilityTest {
16+
17+
private final File excelFile = new File("./src/test/resources/employee.xlsx");
18+
19+
@Test
20+
void length() throws OpenWorkbookException, ExtensionNotValidException, IOException {
21+
Integer length = SheetUtility.length(excelFile);
22+
Assertions.assertEquals(length, 2);
23+
}
24+
25+
@Test
26+
void getNames() throws OpenWorkbookException, ExtensionNotValidException, IOException {
27+
List<String> names = SheetUtility.getNames(excelFile);
28+
Assertions.assertEquals(names.get(0), "Employee");
29+
Assertions.assertEquals(names.get(1), "Office");
30+
31+
}
32+
33+
@Test
34+
void getIndex() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
35+
Integer index = SheetUtility.getIndex(excelFile, "Employee");
36+
Assertions.assertEquals(index, 0);
37+
}
38+
39+
@Test
40+
void getName() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
41+
String name = SheetUtility.getName(excelFile, 0);
42+
Assertions.assertEquals(name, "Employee");
43+
}
44+
45+
@Test
46+
void create() throws OpenWorkbookException, ExtensionNotValidException, IOException {
47+
Sheet sheet = SheetUtility.create(excelFile);
48+
Assertions.assertNotNull(sheet);
49+
}
50+
51+
@Test
52+
void testCreate() throws OpenWorkbookException, ExtensionNotValidException, IOException {
53+
String sheetName = "Admin";
54+
Sheet sheet = SheetUtility.create(excelFile, sheetName);
55+
Assertions.assertNotNull(sheet);
56+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
57+
}
58+
59+
@Test
60+
void testCreate1() throws ExtensionNotValidException {
61+
String extension = "xlsx";
62+
Workbook workbook = WorkbookUtility.create(extension);
63+
Sheet sheet = SheetUtility.create(workbook);
64+
Assertions.assertNotNull(sheet);
65+
}
66+
67+
@Test
68+
void testCreate2() throws ExtensionNotValidException {
69+
String extension = "xlsx";
70+
String sheetName = "Admin";
71+
Workbook workbook = WorkbookUtility.create(extension);
72+
Sheet sheet = SheetUtility.create(workbook, sheetName);
73+
Assertions.assertNotNull(sheet);
74+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
75+
}
76+
77+
@Test
78+
void get() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
79+
Sheet sheet = SheetUtility.get(excelFile);
80+
Assertions.assertNotNull(sheet);
81+
}
82+
83+
@Test
84+
void testGet() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
85+
String sheetName = "Employee";
86+
Sheet sheet = SheetUtility.get(excelFile, sheetName);
87+
Assertions.assertNotNull(sheet);
88+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
89+
}
90+
91+
@Test
92+
void testGet1() throws OpenWorkbookException, SheetNotFoundException, ExtensionNotValidException, IOException {
93+
String sheetName = "Employee";
94+
Sheet sheet = SheetUtility.get(excelFile, 0);
95+
Assertions.assertNotNull(sheet);
96+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
97+
}
98+
99+
@Test
100+
void testGet2() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
101+
Workbook workbook = WorkbookUtility.open(excelFile);
102+
Sheet sheet = SheetUtility.get(workbook);
103+
Assertions.assertNotNull(sheet);
104+
}
105+
106+
@Test
107+
void testGet3() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
108+
String sheetName = "Employee";
109+
Workbook workbook = WorkbookUtility.open(excelFile);
110+
Sheet sheet = SheetUtility.get(workbook, sheetName);
111+
Assertions.assertNotNull(sheet);
112+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
113+
}
114+
115+
@Test
116+
void testGet4() throws OpenWorkbookException, ExtensionNotValidException, IOException, SheetNotFoundException {
117+
String sheetName = "Employee";
118+
Workbook workbook = WorkbookUtility.open(excelFile);
119+
Sheet sheet = SheetUtility.get(workbook, 0);
120+
Assertions.assertNotNull(sheet);
121+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
122+
}
123+
124+
@Test
125+
void getOrCreate() throws OpenWorkbookException, ExtensionNotValidException, IOException {
126+
String sheetName = "Admin";
127+
Workbook workbook = WorkbookUtility.open(excelFile);
128+
Sheet sheet = SheetUtility.getOrCreate(workbook, sheetName);
129+
Assertions.assertNotNull(sheet);
130+
Assertions.assertEquals(sheet.getSheetName(), sheetName);
131+
}
132+
133+
@Test
134+
void isPresent() throws OpenWorkbookException, ExtensionNotValidException, IOException {
135+
String sheetName = "Employee";
136+
Workbook workbook = WorkbookUtility.open(excelFile);
137+
Assertions.assertEquals(true, SheetUtility.isPresent(workbook, sheetName));
138+
}
139+
140+
@Test
141+
void testIsPresent() throws OpenWorkbookException, ExtensionNotValidException, IOException {
142+
Workbook workbook = WorkbookUtility.open(excelFile);
143+
Assertions.assertEquals(true, SheetUtility.isPresent(workbook, 1));
144+
}
145+
146+
@Test
147+
void isNull() throws OpenWorkbookException, ExtensionNotValidException, IOException {
148+
String sheetName = "Car";
149+
Workbook workbook = WorkbookUtility.open(excelFile);
150+
Assertions.assertEquals(true, SheetUtility.isNull(workbook, sheetName));
151+
}
152+
153+
@Test
154+
void testIsNull() throws OpenWorkbookException, ExtensionNotValidException, IOException {
155+
Workbook workbook = WorkbookUtility.open(excelFile);
156+
Assertions.assertEquals(false, SheetUtility.isNull(workbook, 1));
157+
}
158+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package tools;
2+
3+
import com.opencsv.CSVReader;
4+
import com.opencsv.CSVWriter;
5+
import enums.Extension;
6+
import exceptions.ExtensionNotValidException;
7+
import exceptions.OpenWorkbookException;
8+
import org.apache.commons.io.FilenameUtils;
9+
import org.apache.poi.ss.usermodel.Workbook;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.io.*;
14+
15+
class WorkbookUtilityTest {
16+
17+
private final File excelFile = new File("./src/test/resources/employee.xlsx");
18+
private final File csvFile = new File("./src/test/resources/employee.csv");
19+
20+
@Test
21+
void open() throws OpenWorkbookException, ExtensionNotValidException, IOException {
22+
Workbook workbook = WorkbookUtility.open(excelFile);
23+
Assertions.assertNotNull(workbook);
24+
}
25+
26+
@Test
27+
void testOpen() throws IOException, OpenWorkbookException, ExtensionNotValidException {
28+
String extension = FilenameUtils.getExtension(excelFile.getName());
29+
FileInputStream fileInputStream = new FileInputStream(excelFile);
30+
Workbook workbook = WorkbookUtility.open(fileInputStream, extension);
31+
Assertions.assertNotNull(workbook);
32+
fileInputStream.close();
33+
}
34+
35+
@Test
36+
void create() {
37+
Workbook workbook = WorkbookUtility.create();
38+
Assertions.assertNotNull(workbook);
39+
}
40+
41+
@Test
42+
void testCreate() throws ExtensionNotValidException {
43+
String extension = "xlsx";
44+
Workbook workbook = WorkbookUtility.create(extension);
45+
Assertions.assertNotNull(workbook);
46+
}
47+
48+
@Test
49+
void testCreate1() {
50+
Workbook workbook = WorkbookUtility.create(Extension.XLSX);
51+
Assertions.assertNotNull(workbook);
52+
}
53+
54+
@Test
55+
void close() {
56+
Workbook workbook = WorkbookUtility.create(Extension.XLSX);
57+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook));
58+
}
59+
60+
@Test
61+
void testClose() throws FileNotFoundException {
62+
FileInputStream fileInputStream = new FileInputStream(excelFile);
63+
Workbook workbook = WorkbookUtility.create();
64+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook, fileInputStream));
65+
}
66+
67+
@Test
68+
void testClose1() throws FileNotFoundException {
69+
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
70+
Workbook workbook = WorkbookUtility.create();
71+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook, fileOutputStream));
72+
}
73+
74+
@Test
75+
void testClose2() throws FileNotFoundException {
76+
FileInputStream fileInputStream = new FileInputStream(excelFile);
77+
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
78+
Workbook workbook = WorkbookUtility.create();
79+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook, fileOutputStream, fileInputStream));
80+
}
81+
82+
@Test
83+
void testClose3() throws IOException {
84+
Workbook workbook = WorkbookUtility.create();
85+
FileWriter fileWriter = new FileWriter(csvFile);
86+
CSVWriter csvWriter = new CSVWriter(fileWriter);
87+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook, csvWriter));
88+
}
89+
90+
@Test
91+
void testClose4() throws FileNotFoundException {
92+
Workbook workbook = WorkbookUtility.create();
93+
FileReader fileReader = new FileReader(csvFile);
94+
CSVReader csvReader = new CSVReader(fileReader);
95+
FileOutputStream fileOutputStream = new FileOutputStream(excelFile);
96+
Assertions.assertDoesNotThrow(() -> WorkbookUtility.close(workbook, fileOutputStream, csvReader));
97+
98+
}
99+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package tools.utils;
2+
3+
import annotations.ExcelBodyStyle;
4+
import annotations.ExcelField;
5+
import annotations.ExcelHeaderStyle;
6+
import lombok.AllArgsConstructor;
7+
import lombok.NoArgsConstructor;
8+
import lombok.ToString;
9+
import org.apache.poi.ss.usermodel.HorizontalAlignment;
10+
import org.apache.poi.ss.usermodel.IndexedColors;
11+
import org.apache.poi.ss.usermodel.VerticalAlignment;
12+
13+
@AllArgsConstructor
14+
@NoArgsConstructor
15+
@ToString
16+
@ExcelHeaderStyle(cellColor = IndexedColors.ORANGE, horizontal = HorizontalAlignment.RIGHT, vertical = VerticalAlignment.BOTTOM, autoSize = true)
17+
@ExcelBodyStyle(cellColor = IndexedColors.LIGHT_ORANGE, horizontal = HorizontalAlignment.RIGHT, vertical = VerticalAlignment.BOTTOM)
18+
public class Address {
19+
20+
@ExcelField(name = "CITY")
21+
private String city;
22+
@ExcelField(name = "ADDRESS")
23+
private String address;
24+
}

0 commit comments

Comments
 (0)