test: add temp directory for test csv's, add test for invalid header
This commit is contained in:
@@ -6,39 +6,59 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.io.UncheckedIOException;
|
import java.io.UncheckedIOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.io.TempDir;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import ru.project.student.Student;
|
import ru.project.student.Student;
|
||||||
|
|
||||||
class StudentCsvReaderTest {
|
class StudentCsvReaderTest {
|
||||||
private final String ABSOLUTE_DIR = "C:\\Users\\Dexp\\TestData\\"
|
@TempDir Path tempDir;
|
||||||
private StudentCsvReader valideReader;
|
private Path studentPath;
|
||||||
|
|
||||||
// Testing StudentCsvReader
|
// Testing StudentCsvReader
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
void generateStudentTestFile(){
|
||||||
|
studentPath = tempDir.resolve("students.csv");
|
||||||
|
Files.write(
|
||||||
|
studentPath,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNumber",
|
||||||
|
"A21,5.0,331",
|
||||||
|
"A21,4.0,332"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
valideReader = new StudentCsvReader(ABSOLUTE_DIR + "sample.csv");
|
valideReader = new StudentCsvReader(studentPath.toAbsolutePath().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void readerTestRejectRelativePath() {
|
void readerTestRejectRelativePath() {
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> new StudentCsvReader("/Users/Dexp/TestData/sample.csv"));
|
() -> new StudentCsvReader(studentPath.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void readerRejectPathIsDir() {
|
void readerRejectPathIsDir() {
|
||||||
assertThrows(
|
assertThrows(
|
||||||
UncheckedIOException.class, () -> new StudentCsvReader(ABSOLUTE_DIR));
|
UncheckedIOException.class, () -> new StudentCsvReader(tempDir.toAbsolutePath()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void readerTestFileNotExist() {
|
void readerTestFileNotExist() {
|
||||||
assertThrows(
|
assertThrows(
|
||||||
UncheckedIOException.class,
|
UncheckedIOException.class,
|
||||||
() -> new StudentCsvReader(ABSOLUTE_DIR + "sss.csv"));
|
() -> new StudentCsvReader(tempDir.resolve("sss.csv").toAbsolutePath().toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -88,8 +108,17 @@ class StudentCsvReaderTest {
|
|||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> {
|
() -> {
|
||||||
|
Path path = tempDir.resolve("fewparameters.csv");
|
||||||
|
Files.write(
|
||||||
|
path,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNumber",
|
||||||
|
"A21,5.0"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
StudentCsvReader reader =
|
StudentCsvReader reader =
|
||||||
new StudentCsvReader(ABSOLUTE_DIR + "fewparameters.csv");
|
new StudentCsvReader(path.toAbsolutePath().toString());
|
||||||
Student student = reader.next();
|
Student student = reader.next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -99,8 +128,18 @@ class StudentCsvReaderTest {
|
|||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> {
|
() -> {
|
||||||
|
Path path = tempDir.resolve("invalid_groupnumber.csv");
|
||||||
|
Files.write(
|
||||||
|
path,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNumber",
|
||||||
|
"AAA,5.0,331",
|
||||||
|
"A21,4.0,332"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
StudentCsvReader reader =
|
StudentCsvReader reader =
|
||||||
new StudentCsvReader(ABSOLUTE_DIR + "invalid_groupnumber.csv");
|
new StudentCsvReader(path.toAbsolutePath().toString());
|
||||||
Student student = reader.next();
|
Student student = reader.next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -110,8 +149,18 @@ class StudentCsvReaderTest {
|
|||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> {
|
() -> {
|
||||||
|
Path path = tempDir.resolve("invalid_averagegrade.csv");
|
||||||
|
Files.write(
|
||||||
|
path,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNumber",
|
||||||
|
"A21,-5.0,331",
|
||||||
|
"A21,4.0,332"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
StudentCsvReader reader =
|
StudentCsvReader reader =
|
||||||
new StudentCsvReader(ABSOLUTE_DIR + "invalid_averagegrade.csv");
|
new StudentCsvReader(path.toAbsolutePath().toString());
|
||||||
Student student = reader.next();
|
Student student = reader.next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -121,9 +170,40 @@ class StudentCsvReaderTest {
|
|||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> {
|
() -> {
|
||||||
|
Path path = tempDir.resolve("invalid_recordbooknumber.csv");
|
||||||
|
Files.write(
|
||||||
|
path,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNumber",
|
||||||
|
"A21,5.0,-331",
|
||||||
|
"A21,4.0,332"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
StudentCsvReader reader =
|
StudentCsvReader reader =
|
||||||
new StudentCsvReader(ABSOLUTE_DIR + "invalid_recordbooknumber.csv");
|
new StudentCsvReader(path.toAbsolutePath().toString());
|
||||||
Student student = reader.next();
|
Student student = reader.next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testInvalidHeader(){
|
||||||
|
assertThrows(
|
||||||
|
IllegalArgumentException.class,
|
||||||
|
() -> {
|
||||||
|
Path path = tempDir.resolve("invalid_header.csv");
|
||||||
|
Files.write(
|
||||||
|
path,
|
||||||
|
new String[]{
|
||||||
|
"groupNumber,averageGrade,recordBookNomber",
|
||||||
|
"A21,5.0,-331",
|
||||||
|
"A21,4.0,332"
|
||||||
|
},
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
);
|
||||||
|
StudentCsvReader reader =
|
||||||
|
new StudentCsvReader(path.toAbsolutePath().toString());
|
||||||
|
Student student = reader.next();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user