Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/main/java/org/writer/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
package org.writer;

import org.writer.model.Months;
import org.writer.model.Person;
import org.writer.model.Student;

import java.util.List;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Writable writable = new WritableImpl();

List<Student> students = List.of(
new Student("Student 1", List.of("5", "4", "3", "2")),
new Student("Student 2", List.of("2", "1", "1", "2")),
new Student("Student 3", List.of("2", "3", "4", "5"))
);
writable.writeToFile(students, "Students.csv");

List<Person> persons = List.of(
new Person("Имя 1", "Фамилия 1", 17, Months.SEPTEMBER, 1996),
new Person("Имя 2", "Фамилия 2", 18, Months.OCTOBER, 1995),
new Person("Имя 3", "Фамилия 3", 19, Months.NOVEMBER, 1994),
new Person("Имя 4", "Фамилия 4", 20, Months.DECEMBER, 1993),
new Person("Имя 5", "Фамилия 5", 21, Months.JANUARY, 1992)
);
writable.writeToFile(persons, "Persons.csv");
}
}
76 changes: 76 additions & 0 deletions src/main/java/org/writer/WritableImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.writer;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;


public class WritableImpl implements Writable {

/**
* Writes CSV file from List.
*
* @param data List containing data to be written into CSV file.
* @param fileName Name of CSV file.
*/
@Override
public void writeToFile(List<?> data, String fileName) {
List<List<String>> fullDataList = new ArrayList<>();
Set<String> fieldNames = new LinkedHashSet<>();

for (Object object : data) {
List<String> subList = new ArrayList<>();
Field[] fields = object.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
fieldNames.add(field.getName());
Object fieldValue;
try {
fieldValue = field.get(object);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}

String stringToAdd;
if (fieldValue instanceof Collection<?> collection) {
String joined = collection.stream()
.map(Object::toString)
.map(this::escapeCsv)
.collect(Collectors.joining(", "));
stringToAdd = escapeCsv(joined);
} else {
stringToAdd = escapeCsv(fieldValue.toString());
}
subList.add(stringToAdd);

}
fullDataList.add(subList);
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.append(String.join(",", fieldNames));
writer.newLine();

List<String> tempList = fullDataList.stream()
.map(strings -> String.join(",", strings))
.toList();
for (String string : tempList) {
writer.append(string);
writer.newLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private String escapeCsv(String value) {
if (value == null) return "";
if (value.contains("\"") || value.contains(",") || value.contains("\n")) {
return "\"" + value.replace("\"", "\"\"") + "\"";
}
return value;
}
}
134 changes: 134 additions & 0 deletions src/test/java/org/writer/WritableImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package org.writer;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.writer.model.Student;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
class WritableImplTest {

@TempDir
Path tempDir;

@Test
void simpleWriteToFile() throws IOException {
WritableImpl writable = new WritableImpl();
String fileName = "CorrectFileName";
Path csvFile = tempDir.resolve(fileName);
List<Student> students = List.of(
new Student("Student 1", List.of("5", "4", "3", "2")),
new Student("Student 2", List.of("2", "1", "1", "2")),
new Student("Student 3", List.of("2", "3", "4", "5"))
);

writable.writeToFile(students, csvFile.toString());

assertAll(
() -> assertTrue(Files.exists(csvFile), "���� ������ ������������"),
() -> assertTrue(Files.isRegularFile(csvFile), "������ ���� ����, � �� ����������"),
() -> assertTrue(Files.size(csvFile) > 0, "���� �� ������ ���� ������")
);

List<String> actual = Files.readAllLines(csvFile);
List<String> expected = List.of(
"name,score",
"Student 1,\"5, 4, 3, 2\"",
"Student 2,\"2, 1, 1, 2\"",
"Student 3,\"2, 3, 4, 5\""
);
assertEquals(expected, actual);
}

@Test
void dataHasDoubleQuotes() throws IOException {
WritableImpl writable = new WritableImpl();
String fileName = "CorrectFileName";
Path csvFile = tempDir.resolve(fileName);
List<Student> students = List.of(
new Student("St\"ude\"nt 1", List.of("5", "4", "3", "2")),
new Student("S\"tud\"ent 2", List.of("2", "1", "1", "2")),
new Student("\"Stu\"dent 3", List.of("2", "3", "4", "5"))
);

writable.writeToFile(students, csvFile.toString());

assertAll(
() -> assertTrue(Files.exists(csvFile), "���� ������ ������������"),
() -> assertTrue(Files.isRegularFile(csvFile), "������ ���� ����, � �� ����������"),
() -> assertTrue(Files.size(csvFile) > 0, "���� �� ������ ���� ������")
);

List<String> actual = Files.readAllLines(csvFile);
List<String> expected = List.of(
"name,score",
"\"St\"\"ude\"\"nt 1\",\"5, 4, 3, 2\"",
"\"S\"\"tud\"\"ent 2\",\"2, 1, 1, 2\"",
"\"\"\"Stu\"\"dent 3\",\"2, 3, 4, 5\""
);
assertEquals(expected, actual);
}

@Test
void dataHasComma() throws IOException {
WritableImpl writable = new WritableImpl();
String fileName = "CorrectFileName";
Path csvFile = tempDir.resolve(fileName);
List<Student> students = List.of(
new Student("St,udent 1", List.of("5", "4", "3", "2")),
new Student("Stu,dent 2", List.of("2", "1", "1", "2")),
new Student("Stud,ent 3", List.of("2", "3", "4", "5"))
);

writable.writeToFile(students, csvFile.toString());

assertAll(
() -> assertTrue(Files.exists(csvFile), "���� ������ ������������"),
() -> assertTrue(Files.isRegularFile(csvFile), "������ ���� ����, � �� ����������"),
() -> assertTrue(Files.size(csvFile) > 0, "���� �� ������ ���� ������")
);

List<String> actual = Files.readAllLines(csvFile);
List<String> expected = List.of(
"name,score",
"\"St,udent 1\",\"5, 4, 3, 2\"",
"\"Stu,dent 2\",\"2, 1, 1, 2\"",
"\"Stud,ent 3\",\"2, 3, 4, 5\""
);
assertEquals(expected, actual);
}

@Test
void dataHasEmptyCollection() throws IOException {
WritableImpl writable = new WritableImpl();
String fileName = "CorrectFileName";
Path csvFile = tempDir.resolve(fileName);
List<Student> students = List.of(
new Student("Student 1", Collections.emptyList()),
new Student("Student 2", Collections.emptyList()),
new Student("Student 3", Collections.emptyList())
);

writable.writeToFile(students, csvFile.toString());

assertAll(
() -> assertTrue(Files.exists(csvFile), "���� ������ ������������"),
() -> assertTrue(Files.isRegularFile(csvFile), "������ ���� ����, � �� ����������"),
() -> assertTrue(Files.size(csvFile) > 0, "���� �� ������ ���� ������")
);

List<String> actual = Files.readAllLines(csvFile);
List<String> expected = List.of(
"name,score",
"Student 1,",
"Student 2,",
"Student 3,"
);
assertEquals(expected, actual);
}
}