Updating code related to the article BAEL-7017 (#15009)
* Update pom.xml Update pom with the apache dependency * Update WriteHashmaptoCVSFileUnitTest.java Adding a new test method to solve the problem using Apache Commons CSV.
This commit is contained in:
parent
7d99ed3d2d
commit
3298d5fb96
@ -73,6 +73,11 @@
|
|||||||
<version>4.13.1</version>
|
<version>4.13.1</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-csv</artifactId>
|
||||||
|
<version>1.5</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -10,18 +10,26 @@ import java.nio.file.Paths;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.csv.CSVFormat;
|
||||||
|
import org.apache.commons.csv.CSVPrinter;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class WriteHashmaptoCVSFileUnitTest {
|
public class WriteHashmaptoCVSFileUnitTest {
|
||||||
|
public Map<String, String> employeeData;
|
||||||
|
|
||||||
@Test
|
public WriteHashmaptoCVSFileUnitTest() {
|
||||||
public void givenEmployeeData_whenWriteToCSV_thenCSVFileIsCreated() {
|
employeeData = new HashMap<>();
|
||||||
Map<String, String> employeeData = new HashMap<>();
|
|
||||||
employeeData.put("Name", "John Doe");
|
employeeData.put("Name", "John Doe");
|
||||||
employeeData.put("Title", "Software Engineer");
|
employeeData.put("Title", "Software Engineer");
|
||||||
employeeData.put("Department", "Engineering");
|
employeeData.put("Department", "Engineering");
|
||||||
employeeData.put("Salary", "75000");
|
employeeData.put("Salary", "75000");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmployeeData_whenWriteToCSVUsingFileWriter_thenCSVFileIsCreated() {
|
||||||
|
|
||||||
try (FileWriter csvWriter = new FileWriter("employee_data.csv")) {
|
try (FileWriter csvWriter = new FileWriter("employee_data.csv")) {
|
||||||
// Write header row
|
// Write header row
|
||||||
csvWriter.append("Name,Title,Department,Salary\n");
|
csvWriter.append("Name,Title,Department,Salary\n");
|
||||||
@ -40,23 +48,19 @@ public class WriteHashmaptoCVSFileUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCSVFile_whenRead_thenContentsMatchExpected() {
|
public void givenCSVFile_whenWriteToCSVUsingApacheCommons_thenContentsMatchExpected() {
|
||||||
// Read the actual content of the CSV file
|
|
||||||
StringBuilder actualCsvContent = new StringBuilder();
|
|
||||||
try {
|
|
||||||
Files.lines(Paths.get("employee_data.csv"))
|
|
||||||
.forEach(line -> actualCsvContent.append(line).append("\n"));
|
|
||||||
|
|
||||||
// Define the expected CSV content
|
try (CSVPrinter csvPrinter = new CSVPrinter(new FileWriter("employee_data2.csv"), CSVFormat.DEFAULT)) {
|
||||||
String expectedCsvContent = "Name,Title,Department,Salary\n" +
|
// Write header row
|
||||||
"John Doe,Software Engineer,Engineering,75000\n";
|
csvPrinter.printRecord("Name", "Title", "Department", "Salary");
|
||||||
|
|
||||||
// Compare the actual content with the expected content
|
// Write data row
|
||||||
assertEquals(expectedCsvContent, actualCsvContent.toString());
|
csvPrinter.printRecord(employeeData.get("Name"), employeeData.get("Title"), employeeData.get("Department"), employeeData.get("Salary"));
|
||||||
|
|
||||||
System.out.println("CSV file created successfully.");
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure the CSV file exists
|
||||||
|
assertTrue(new File("employee_data2.csv").exists());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user