diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9dc782b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/persons.csv b/persons.csv new file mode 100644 index 0000000..15aafdf --- /dev/null +++ b/persons.csv @@ -0,0 +1,21 @@ +name,score +Alex,"[A, B, A]" +Maria,"[A, A, A]" +John,"[B, C, B]" +Elena,"[A, B, C]" +Victor,"[B, B, B]" +Anna,"[A, C, B]" +Daniel,"[C, B, A]" +Sofia,"[A, A, B]" +Mihai,"[B, C, C]" +Laura,"[A, B, A]" +Alex,"[A, B, A]" +Maria,"[A, A, A]" +John,"[B, C, B]" +Elena,"[A, B, C]" +Victor,"[B, B, B]" +Anna,"[A, C, B]" +Daniel,"[C, B, A]" +Sofia,"[A, A, B]" +Mihai,"[B, C, C]" +Laura,"[A, B, A]" diff --git a/src/main/java/org/writer/Main.java b/src/main/java/org/writer/Main.java index 40e8213..f9affbc 100644 --- a/src/main/java/org/writer/Main.java +++ b/src/main/java/org/writer/Main.java @@ -1,7 +1,29 @@ package org.writer; +import org.writer.csv.CsvFileWriter; +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!"); + String peopleFileName = "persons.csv"; + String studentshipName = "students.csv"; + + List personList = Person.getTestList(); + List studentList = Student.getTestList(); + + + CsvFileWriter csvFileWriter = new CsvFileWriter(); + + csvFileWriter.writeToFile(studentList, peopleFileName); + csvFileWriter.writeToFile(studentList, studentshipName); + + + } + + { + } } \ No newline at end of file diff --git a/src/main/java/org/writer/csv/CsvFileWriter.java b/src/main/java/org/writer/csv/CsvFileWriter.java new file mode 100644 index 0000000..e022e5b --- /dev/null +++ b/src/main/java/org/writer/csv/CsvFileWriter.java @@ -0,0 +1,58 @@ +package org.writer.csv; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.Pipeline; +import org.writer.csv.pipeline.steps.*; +import org.writer.exception.ArrayIsEmptyException; + +import java.util.List; + + +/** + * Writes Java objects to a CSV file using reflection. + * + *

The writer automatically generates a CSV header based on + * the object's fields and ensures header consistency when appending data.

+ * + *

This class is not thread-safe.

+ */ +public class CsvFileWriter implements Writable { + private Pipeline writeToCsvFilePipeline; + private CsvContext csvContext; + + + /** + * Writes a list of objects to a CSV file. + * + *

All objects in the list must be of the same type. The CSV header + * is generated automatically using reflection.

+ * + * @param data list of objects to be written; must not be empty and must contain + * objects of the same type + * @param fileName name of the target CSV file + * @throws ArrayIsEmptyException if the data list is empty + * @throws IllegalArgumentException if the list contains objects of different types + * @throws IllegalStateException if an existing CSV header does not match + */ + @Override + public void writeToFile(List data, String fileName) { + this.csvContext = new CsvContext(data, fileName); + this.writeToCsvFilePipeline = buildPipeline(); + writeToCsvFilePipeline.execute(csvContext); + } + + + private Pipeline buildPipeline() { + Pipeline writeToCsvFilePipeline = new Pipeline<>(); + + writeToCsvFilePipeline.addStep(new ValidateDataStep()); + writeToCsvFilePipeline.addStep(new CheckFileExistsStep()); + + writeToCsvFilePipeline.addStep(new ResolveMetadataStep()); + + writeToCsvFilePipeline.addStep(new GenerateHeaderStep()); + writeToCsvFilePipeline.addStep(new WriteDataToCsvFileStep()); + + return writeToCsvFilePipeline; + } +} diff --git a/src/main/java/org/writer/Writable.java b/src/main/java/org/writer/csv/Writable.java similarity index 61% rename from src/main/java/org/writer/Writable.java rename to src/main/java/org/writer/csv/Writable.java index 9f60c45..c69b0f0 100644 --- a/src/main/java/org/writer/Writable.java +++ b/src/main/java/org/writer/csv/Writable.java @@ -1,8 +1,8 @@ -package org.writer; +package org.writer.csv; import java.util.List; -public interface Writable { +public interface Writable{ void writeToFile(List data, String fileName); diff --git a/src/main/java/org/writer/csv/context/ContextInterface.java b/src/main/java/org/writer/csv/context/ContextInterface.java new file mode 100644 index 0000000..ba274f2 --- /dev/null +++ b/src/main/java/org/writer/csv/context/ContextInterface.java @@ -0,0 +1,4 @@ +package org.writer.csv.context; + +public interface ContextInterface { +} diff --git a/src/main/java/org/writer/csv/context/CsvContext.java b/src/main/java/org/writer/csv/context/CsvContext.java new file mode 100644 index 0000000..6e53aab --- /dev/null +++ b/src/main/java/org/writer/csv/context/CsvContext.java @@ -0,0 +1,29 @@ +package org.writer.csv.context; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.lang.reflect.Field; +import java.nio.file.Path; +import java.util.List; + +@Getter +@Setter +//@Builder +public class CsvContext implements ContextInterface { + private List data; + private Path path; + private boolean isFileExists = false; + private Class clazz; + private Field[] fields; + private String header; + + public CsvContext(List data, String fileName) { + this.data = data; + this.path = Path.of(fileName); + + } + + +} diff --git a/src/main/java/org/writer/csv/pipeline/Pipeline.java b/src/main/java/org/writer/csv/pipeline/Pipeline.java new file mode 100644 index 0000000..7f69178 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/Pipeline.java @@ -0,0 +1,26 @@ +package org.writer.csv.pipeline; + +import org.writer.csv.context.ContextInterface; + +import java.util.LinkedList; +import java.util.List; + +public class Pipeline { + private final List> steps = new LinkedList<>(); + + public void execute(TContext context) { + steps.forEach(pipelineStep -> pipelineStep.execute(context)); + } + + public void addStep(PipelineStep pipelineStep) { + steps.add(pipelineStep); + } + public void addStepByCondition(boolean condition, PipelineStep firstStep, PipelineStep secondStep){ + if (condition){ + steps.add(firstStep); + }else { + steps.add(secondStep); + } + } + +} diff --git a/src/main/java/org/writer/csv/pipeline/PipelineStep.java b/src/main/java/org/writer/csv/pipeline/PipelineStep.java new file mode 100644 index 0000000..31d0236 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/PipelineStep.java @@ -0,0 +1,7 @@ +package org.writer.csv.pipeline; + +import org.writer.csv.context.ContextInterface; + +public interface PipelineStep { + void execute(TContext context); +} diff --git a/src/main/java/org/writer/csv/pipeline/steps/CheckFileExistsStep.java b/src/main/java/org/writer/csv/pipeline/steps/CheckFileExistsStep.java new file mode 100644 index 0000000..7683624 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/steps/CheckFileExistsStep.java @@ -0,0 +1,27 @@ +package org.writer.csv.pipeline.steps; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.PipelineStep; + +import java.io.IOException; +import java.nio.file.Files; + +public class CheckFileExistsStep implements PipelineStep { + /** + * Checks whether the target CSV file exists and is not empty. + * Stores the result in the CsvContext. + * + * @param csvContext context containing the path to the CSV file + * @throws RuntimeException if an I/O error occurs while checking the file + */ + + @Override + public void execute(CsvContext csvContext) { + try { + boolean isFileExists = Files.exists(csvContext.getPath()) && Files.size(csvContext.getPath()) > 0; + csvContext.setFileExists(isFileExists); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/org/writer/csv/pipeline/steps/GenerateHeaderStep.java b/src/main/java/org/writer/csv/pipeline/steps/GenerateHeaderStep.java new file mode 100644 index 0000000..e3598f2 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/steps/GenerateHeaderStep.java @@ -0,0 +1,50 @@ +package org.writer.csv.pipeline.steps; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.PipelineStep; +import org.writer.exception.CsvHeaderMismatchException; + +import java.io.BufferedReader; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.stream.Collectors; + + +public class GenerateHeaderStep implements PipelineStep { + /** + * Generates a CSV header based on the model fields and stores it in the context. + * If the CSV file already exists, validates that the generated header matches + * the header stored in the file. + * + * @param csvContext context containing file path and model metadata + * @throws CsvHeaderMismatchException if the existing file header does not match the generated one + * @throws RuntimeException if an I/O error occurs while reading the file + */ + @Override + public void execute(CsvContext csvContext) { + + csvContext.setHeader(Arrays.stream(csvContext.getFields()) + .map(Field::getName) + .collect(Collectors.joining(","))); + + if (csvContext.isFileExists()) { + try (BufferedReader reader = Files.newBufferedReader(csvContext.getPath())) { + String fileHeader = reader.readLine(); + + if (!fileHeader.equals(csvContext.getHeader())) { + throw new CsvHeaderMismatchException( + "CSV header mismatch. Expected: " + + csvContext.getHeader() + + ", Actual: " + + fileHeader + ); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + } +} diff --git a/src/main/java/org/writer/csv/pipeline/steps/ResolveMetadataStep.java b/src/main/java/org/writer/csv/pipeline/steps/ResolveMetadataStep.java new file mode 100644 index 0000000..f899898 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/steps/ResolveMetadataStep.java @@ -0,0 +1,31 @@ +package org.writer.csv.pipeline.steps; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.PipelineStep; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Comparator; + +public class ResolveMetadataStep implements PipelineStep { + /** + * Resolves metadata required for CSV generation. + * Determines the model class from the data list, extracts its fields, + * sorts them by name and makes them accessible. + * + * @param csvContext context containing the data to be written + * @throws IndexOutOfBoundsException if the data list is empty + */ + + @Override + public void execute(CsvContext csvContext) { + csvContext.setClazz(csvContext.getData().get(0).getClass()); + + Field[] fields = csvContext.getClazz().getDeclaredFields(); + Arrays.sort(fields, Comparator.comparing(Field::getName)); + for (Field f : fields) { + f.setAccessible(true); + } + csvContext.setFields(fields); + } +} diff --git a/src/main/java/org/writer/csv/pipeline/steps/ValidateDataStep.java b/src/main/java/org/writer/csv/pipeline/steps/ValidateDataStep.java new file mode 100644 index 0000000..c342ef5 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/steps/ValidateDataStep.java @@ -0,0 +1,20 @@ +package org.writer.csv.pipeline.steps; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.PipelineStep; +import org.writer.exception.ArrayIsEmptyException; + +public class ValidateDataStep implements PipelineStep { + /** + * Validates that the data list is not empty. + * + * @param csvContext context containing the data to be written + * @throws ArrayIsEmptyException if the data list is empty + */ + @Override + public void execute(CsvContext csvContext) { + if (csvContext.getData().isEmpty()) { + throw new ArrayIsEmptyException("Fill array with data!"); + } + } +} diff --git a/src/main/java/org/writer/csv/pipeline/steps/WriteDataToCsvFileStep.java b/src/main/java/org/writer/csv/pipeline/steps/WriteDataToCsvFileStep.java new file mode 100644 index 0000000..5a1b4d5 --- /dev/null +++ b/src/main/java/org/writer/csv/pipeline/steps/WriteDataToCsvFileStep.java @@ -0,0 +1,104 @@ +package org.writer.csv.pipeline.steps; + +import org.writer.csv.context.CsvContext; +import org.writer.csv.pipeline.PipelineStep; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.util.Arrays; +import java.util.stream.Collectors; + +public class WriteDataToCsvFileStep implements PipelineStep { + /** + * Writes data from the context to a CSV file. + * Writes the header if the file does not already exist, + * then appends all data rows. + * + * @param csvContext context containing file path, header and data + * @throws RuntimeException if an I/O error occurs while writing to the file + */ + + @Override + public void execute(CsvContext csvContext) { + System.out.println("Start to file writing"); + try (BufferedWriter writer = Files.newBufferedWriter( + csvContext.getPath(), + StandardOpenOption.CREATE, + StandardOpenOption.APPEND + )) { + if (!csvContext.isFileExists()) { + writeCsvRow(writer, csvContext.getHeader()); + } + for (Object element : csvContext.getData()) { + String csvRow = joinFieldsToCsvRow(element, csvContext.getFields()); + writeCsvRow(writer, csvRow); + } + System.out.println("The data was successfully written"); + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + + /** + * Joins field values into a CSV string + * + * @param objectToRow the object whose data we will connect + * @param fields fields of the object to be serialized + * @return joined row for CSV + */ + private String joinFieldsToCsvRow(Object objectToRow, Field[] fields) { + return Arrays.stream(fields) + .map(field -> { + try { + Object value = field.get(objectToRow); + return escapeCsv(value == null ? "" : value.toString()); + } catch (IllegalAccessException e) { + throw new RuntimeException("Access was not permitted ", e); + } + }) + .collect(Collectors.joining(",")); + + } + + /** + * Writes a single CSV row to the given writer. + * + *

The method appends the row followed by a line separator.

+ * + * @param writer the {@link BufferedWriter} used to write data to the CSV file + * @param csvRow the CSV-formatted row to write + * @throws RuntimeException if an I/O error occurs while writing the row + */ + private void writeCsvRow(BufferedWriter writer, String csvRow) { + try { + writer.write(csvRow); + writer.newLine(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * Escapes a value according to CSV format rules. + * + *

If the value contains a comma, double quote, or line break, + * it is wrapped in double quotes and internal quotes are escaped + * by doubling them.

+ * + * @param value the raw value to escape + * @return a CSV-safe representation of the value + */ + private String escapeCsv(String value) { + + if (value.contains(",") || value.contains("\"") || value.contains("\n")) { + value = value.replace("\"", "\"\""); + return "\"" + value + "\""; + } + return value; + } +} diff --git a/src/main/java/org/writer/exception/ArrayIsEmptyException.java b/src/main/java/org/writer/exception/ArrayIsEmptyException.java new file mode 100644 index 0000000..7c01890 --- /dev/null +++ b/src/main/java/org/writer/exception/ArrayIsEmptyException.java @@ -0,0 +1,6 @@ +package org.writer.exception; + +public class ArrayIsEmptyException extends RuntimeException { + public ArrayIsEmptyException(String s) { + } +} diff --git a/src/main/java/org/writer/exception/CsvHeaderMismatchException.java b/src/main/java/org/writer/exception/CsvHeaderMismatchException.java new file mode 100644 index 0000000..47b79e6 --- /dev/null +++ b/src/main/java/org/writer/exception/CsvHeaderMismatchException.java @@ -0,0 +1,6 @@ +package org.writer.exception; + +public class CsvHeaderMismatchException extends RuntimeException { + public CsvHeaderMismatchException(String s) { + } +} diff --git a/src/main/java/org/writer/exception/CsvWriteException.java b/src/main/java/org/writer/exception/CsvWriteException.java new file mode 100644 index 0000000..232b80c --- /dev/null +++ b/src/main/java/org/writer/exception/CsvWriteException.java @@ -0,0 +1,8 @@ +package org.writer.exception; + +import java.io.IOException; + +public class CsvWriteException extends RuntimeException { + public CsvWriteException(String failedToWriteCsvFile, IOException e) { + } +} diff --git a/src/main/java/org/writer/model/Person.java b/src/main/java/org/writer/model/Person.java index ba8576b..9db9959 100644 --- a/src/main/java/org/writer/model/Person.java +++ b/src/main/java/org/writer/model/Person.java @@ -4,6 +4,8 @@ import lombok.Builder; import lombok.Data; +import java.util.List; + @Data @Builder @AllArgsConstructor @@ -19,4 +21,91 @@ public class Person { private int yearOfBirth; + + + public static List getTestList(){ + return List.of( + Person.builder() + .firstName("Alex") + .lastName("Ivanov") + .dayOfBirth(12) + .monthOfBirth(Months.JANUARY) + .yearOfBirth(1995) + .build(), + + Person.builder() + .firstName("Maria") + .lastName("Petrova") + .dayOfBirth(3) + .monthOfBirth(Months.FEBRUARY) + .yearOfBirth(1998) + .build(), + + Person.builder() + .firstName("John") + .lastName("Smith") + .dayOfBirth(21) + .monthOfBirth(Months.MARCH) + .yearOfBirth(1990) + .build(), + + Person.builder() + .firstName("Elena") + .lastName("Popescu") + .dayOfBirth(9) + .monthOfBirth(Months.APRIL) + .yearOfBirth(1997) + .build(), + + Person.builder() + .firstName("Victor") + .lastName("Radu") + .dayOfBirth(30) + .monthOfBirth(Months.MAY) + .yearOfBirth(1993) + .build(), + + Person.builder() + .firstName("Anna") + .lastName("Kowalski") + .dayOfBirth(14) + .monthOfBirth(Months.JUNE) + .yearOfBirth(2000) + .build(), + + Person.builder() + .firstName("Daniel") + .lastName("Novak") + .dayOfBirth(7) + .monthOfBirth(Months.JULY) + .yearOfBirth(1992) + .build(), + + Person.builder() + .firstName("Sofia") + .lastName("Marin") + .dayOfBirth(18) + .monthOfBirth(Months.AUGUST) + .yearOfBirth(1996) + .build(), + + Person.builder() + .firstName("Mihai") + .lastName("Dumitru") + .dayOfBirth(25) + .monthOfBirth(Months.SEPTEMBER) + .yearOfBirth(1994) + .build(), + + Person.builder() + .firstName("Laura") + .lastName("Bianchi") + .dayOfBirth(2) + .monthOfBirth(Months.OCTOBER) + .yearOfBirth(1999) + .build() + ); + + } + } diff --git a/src/main/java/org/writer/model/Student.java b/src/main/java/org/writer/model/Student.java index 2b549c4..1133b21 100644 --- a/src/main/java/org/writer/model/Student.java +++ b/src/main/java/org/writer/model/Student.java @@ -14,4 +14,60 @@ public class Student { private String name; private List score; + + + public static List getTestList(){ + return List.of( + Student.builder() + .name("Alex") + .score(List.of("A", "B", "A")) + .build(), + + Student.builder() + .name("Maria") + .score(List.of("A", "A", "A")) + .build(), + + Student.builder() + .name("John") + .score(List.of("B", "C", "B")) + .build(), + + Student.builder() + .name("Elena") + .score(List.of("A", "B", "C")) + .build(), + + Student.builder() + .name("Victor") + .score(List.of("B", "B", "B")) + .build(), + + Student.builder() + .name("Anna") + .score(List.of("A", "C", "B")) + .build(), + + Student.builder() + .name("Daniel") + .score(List.of("C", "B", "A")) + .build(), + + Student.builder() + .name("Sofia") + .score(List.of("A", "A", "B")) + .build(), + + Student.builder() + .name("Mihai") + .score(List.of("B", "C", "C")) + .build(), + + Student.builder() + .name("Laura") + .score(List.of("A", "B", "A")) + .build() + ); + + } } \ No newline at end of file diff --git a/src/test/java/org/writer/csv/CsvFileWriterTest.java b/src/test/java/org/writer/csv/CsvFileWriterTest.java new file mode 100644 index 0000000..b3dc7bf --- /dev/null +++ b/src/test/java/org/writer/csv/CsvFileWriterTest.java @@ -0,0 +1,107 @@ +package org.writer.csv; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.writer.exception.CsvHeaderMismatchException; +import org.writer.csv.testdata.StudentTestData; +import org.writer.exception.ArrayIsEmptyException; +import org.writer.model.Student; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class CsvFileWriterTest { + + private final CsvFileWriter writer = new CsvFileWriter(); + + @TempDir + Path tempDir; + + private Student alex; + private Student maria; + private List data; + + @BeforeEach + void setUp() { + this.alex = StudentTestData.alex(); + this.maria = StudentTestData.maria(); + this.data = List.of(alex, maria); + } + + @Test + void shouldWriteCsvWithHeaderAndRows() throws IOException { + Path file = tempDir.resolve("students.csv"); + + writer.writeToFile(data, file.toString()); + + List lines = Files.readAllLines(file); + + assertEquals(3, lines.size()); + + // порядок полей алфавитный: name, score + assertEquals("name,score", lines.get(0)); + + assertEquals("Alex,\"[A, B, A]\"", lines.get(1)); + assertEquals("Maria,\"[A, A, A]\"", lines.get(2)); + } + + @Test + void shouldAppendDataWithoutDuplicatingHeader() throws IOException { + Path file = tempDir.resolve("students.csv"); + + writer.writeToFile(List.of(alex), file.toString()); + writer.writeToFile(List.of(maria), file.toString()); + + List lines = Files.readAllLines(file); + + assertEquals(3, lines.size()); + assertEquals("name,score", lines.get(0)); + assertEquals("Alex,\"[A, B, A]\"", lines.get(1)); + assertEquals("Maria,\"[A, A, A]\"", lines.get(2)); + } + + @Test + void shouldEscapeCsvValues() throws IOException { + Path file = tempDir.resolve("students.csv"); + + Student withComma = Student.builder() + .name("Alex, Jr.") + .score(List.of("A", "B")) + .build(); + + writer.writeToFile(List.of(withComma), file.toString()); + + List lines = Files.readAllLines(file); + + assertEquals("name,score", lines.get(0)); + assertEquals("\"Alex, Jr.\",\"[A, B]\"", lines.get(1)); + } + + @Test + void shouldThrowExceptionWhenDataIsEmpty() { + Path file = tempDir.resolve("students.csv"); + + assertThrows( + ArrayIsEmptyException.class, + () -> writer.writeToFile(List.of(), file.toString()) + ); + } + + @Test + void shouldThrowExceptionWhenHeaderDoesNotMatch() throws IOException { + Path file = tempDir.resolve("students.csv"); + + //CSV с неправильным заголовком + Files.write(file, List.of("wrong,header")); + + assertThrows( + CsvHeaderMismatchException.class, + () -> writer.writeToFile(data, file.toString()) + ); + } +} diff --git a/src/test/java/org/writer/csv/testdata/StudentTestData.java b/src/test/java/org/writer/csv/testdata/StudentTestData.java new file mode 100644 index 0000000..31ba46a --- /dev/null +++ b/src/test/java/org/writer/csv/testdata/StudentTestData.java @@ -0,0 +1,39 @@ +package org.writer.csv.testdata; + +import org.writer.model.Student; + +import java.util.List; + +public final class StudentTestData { + + private StudentTestData() {} + + public static Student alex() { + return Student.builder() + .name("Alex") + .score(List.of("A", "B", "A")) + .build(); + } + + public static Student maria() { + return Student.builder() + .name("Maria") + .score(List.of("A", "A", "A")) + .build(); + } + + public static Student withCommaInName() { + return Student.builder() + .name("John, Jr.") + .score(List.of("A", "B")) + .build(); + } + + public static List students() { + return List.of( + alex(), + maria() + ); + } +} + diff --git a/students.csv b/students.csv new file mode 100644 index 0000000..15aafdf --- /dev/null +++ b/students.csv @@ -0,0 +1,21 @@ +name,score +Alex,"[A, B, A]" +Maria,"[A, A, A]" +John,"[B, C, B]" +Elena,"[A, B, C]" +Victor,"[B, B, B]" +Anna,"[A, C, B]" +Daniel,"[C, B, A]" +Sofia,"[A, A, B]" +Mihai,"[B, C, C]" +Laura,"[A, B, A]" +Alex,"[A, B, A]" +Maria,"[A, A, A]" +John,"[B, C, B]" +Elena,"[A, B, C]" +Victor,"[B, B, B]" +Anna,"[A, C, B]" +Daniel,"[C, B, A]" +Sofia,"[A, A, B]" +Mihai,"[B, C, C]" +Laura,"[A, B, A]"