Merge pull request into develop from feature/datasource
This commit is contained in:
@@ -42,6 +42,12 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-csv</artifactId>
|
||||
<version>1.14.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.googlecode.lanterna</groupId>
|
||||
<artifactId>lanterna</artifactId>
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package ru.project.collectors;
|
||||
|
||||
import java.util.stream.Collector;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class MyCollectors
|
||||
{
|
||||
private MyCollectors() {
|
||||
public class MyCollectors {
|
||||
private MyCollectors() {}
|
||||
|
||||
}
|
||||
|
||||
public static <T> Collector<T, MyList<T>, MyList<T>> toMyList()
|
||||
{
|
||||
public static <T> Collector<T, MyList<T>, MyList<T>> toMyList() {
|
||||
return new MyListCollector<>();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
package ru.project.collectors;
|
||||
import java.util.Collections;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
public class MyListCollector <T> implements Collector<T, MyList<T>, MyList<T>>
|
||||
{
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class MyListCollector<T> implements Collector<T, MyList<T>, MyList<T>> {
|
||||
@Override
|
||||
public Supplier<MyList<T>> supplier()
|
||||
{
|
||||
public Supplier<MyList<T>> supplier() {
|
||||
return () -> new MyLinkedList<>();
|
||||
}
|
||||
|
||||
@@ -29,15 +30,12 @@ public class MyListCollector <T> implements Collector<T, MyList<T>, MyList<T>>
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<MyList<T>, MyList<T>> finisher()
|
||||
{
|
||||
public Function<MyList<T>, MyList<T>> finisher() {
|
||||
return list -> list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Collector.Characteristics> characteristics() {
|
||||
return java.util.Set.of(
|
||||
Collector.Characteristics.IDENTITY_FINISH
|
||||
);
|
||||
return java.util.Set.of(Collector.Characteristics.IDENTITY_FINISH);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package ru.project.finder;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
public class StudentOccurrenceIndexFinder
|
||||
{
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentOccurrenceIndexFinder {
|
||||
public int[] findOccurrences(MyList<Student> students, Student target) {
|
||||
if (students == null) {
|
||||
throw new IllegalArgumentException();
|
||||
@@ -32,7 +36,9 @@ public class StudentOccurrenceIndexFinder
|
||||
final int start = fromIndex;
|
||||
final int end = toIndex;
|
||||
|
||||
Future<MyList<Integer>> future = executor.submit(() -> {
|
||||
Future<MyList<Integer>> future =
|
||||
executor.submit(
|
||||
() -> {
|
||||
return findInRange(students, target, start, end);
|
||||
});
|
||||
futures.add(future);
|
||||
@@ -110,7 +116,8 @@ public class StudentOccurrenceIndexFinder
|
||||
return (size + threadCount - 1) / threadCount;
|
||||
}
|
||||
|
||||
private MyList<Integer> findInRange(MyList<Student> students, Student target, int fromIndex, int toIndex) {
|
||||
private MyList<Integer> findInRange(
|
||||
MyList<Student> students, Student target, int fromIndex, int toIndex) {
|
||||
MyList<Integer> foundIndices = new MyLinkedList<>();
|
||||
|
||||
for (int i = fromIndex; i < toIndex; i++) {
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package ru.project.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVParser;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.student.StudentBuilder;
|
||||
public class StudentCsvReader implements Iterator<Student>, AutoCloseable {
|
||||
private static final String GROUP_NUMBER = "groupNumber";
|
||||
private static final String AVERAGE_GRADE = "averageGrade";
|
||||
private static final String RECORD_BOOK_NUMBER = "recordBookNumber";
|
||||
private final CSVParser parser;
|
||||
private final Iterator<CSVRecord> records;
|
||||
private int lineOfFile=0;
|
||||
public StudentCsvReader(Path path) {
|
||||
if (!path.isAbsolute()) {
|
||||
throw new IllegalArgumentException("File path must be absolute");
|
||||
}
|
||||
try {
|
||||
CSVFormat format = CSVFormat.DEFAULT.builder()
|
||||
.setHeader()
|
||||
.setSkipHeaderRecord(true)
|
||||
.get();
|
||||
this.parser = CSVParser.parse(path, StandardCharsets.UTF_8, format);
|
||||
validateHeader(parser.getHeaderMap());
|
||||
this.records = parser.iterator();
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return records.hasNext();
|
||||
}
|
||||
@Override
|
||||
public Student next() {
|
||||
if (!records.hasNext()) {
|
||||
throw new NoSuchElementException("No more records at line " + (lineOfFile + 1));
|
||||
}
|
||||
lineOfFile++;
|
||||
CSVRecord record = records.next();
|
||||
if (record.size() != 3) {
|
||||
throw new IllegalArgumentException(
|
||||
"Problem at line " + lineOfFile + ". Expected 3 columns, but got " + record.size());
|
||||
}
|
||||
String groupNumber = record.get(GROUP_NUMBER);
|
||||
String averageGradeStr = record.get(AVERAGE_GRADE);
|
||||
String recordBookNumberStr = record.get(RECORD_BOOK_NUMBER);
|
||||
double averageGrade;
|
||||
int recordBookNumber;
|
||||
try {
|
||||
averageGrade = Double.parseDouble(averageGradeStr);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Problem at line " + lineOfFile + ": cannot parse averageGrade '" + averageGradeStr + "' as double", e);
|
||||
}
|
||||
try {
|
||||
recordBookNumber = Integer.parseInt(recordBookNumberStr);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Problem at line " + lineOfFile + ": cannot parse recordBookNumber '" + recordBookNumberStr + "' as int", e);
|
||||
}
|
||||
return new StudentBuilder()
|
||||
.setGroupNumber(groupNumber)
|
||||
.setAverageGrade(averageGrade)
|
||||
.setRecordBookNumber(recordBookNumber)
|
||||
.build();
|
||||
}
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
parser.close();
|
||||
}
|
||||
private static void validateHeader(Map<String, Integer> headerMap) {
|
||||
String[] expected = {GROUP_NUMBER, AVERAGE_GRADE, RECORD_BOOK_NUMBER};
|
||||
if (headerMap.size() != expected.length
|
||||
|| !headerMap.keySet().containsAll(Arrays.asList(expected))) {
|
||||
throw new IllegalArgumentException("Invalid CSV header");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package ru.project.search;
|
||||
|
||||
import ru.project.list.MyList;
|
||||
import ru.project.list.MyLinkedList;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentSearchService {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package ru.test.maven.spring;
|
||||
import org.junit.jupiter.api.Test;
|
||||
package ru.project.collection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
public class MyLinkedListTest
|
||||
{
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MyLinkedListTest {
|
||||
@Test
|
||||
public void shouldTestAdd() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
@@ -14,13 +16,14 @@ public class MyLinkedListTest
|
||||
assertEquals(20, list.get(1));
|
||||
assertEquals(30, list.get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTestGet()
|
||||
{
|
||||
public void shouldTestGet() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
list.add(10);
|
||||
assertEquals(10, list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldTestRemove() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
@@ -33,6 +36,7 @@ public class MyLinkedListTest
|
||||
assertEquals(20, list.get(0));
|
||||
assertEquals(30, list.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestSet() {
|
||||
MyLinkedList<String> list = new MyLinkedList<>();
|
||||
@@ -45,23 +49,23 @@ public class MyLinkedListTest
|
||||
assertEquals("B", oldValue);
|
||||
assertEquals("Z", list.get(1));
|
||||
assertEquals(3, list.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestSize()
|
||||
{
|
||||
void shouldTestSize() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
list.add(1);
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
assertEquals(3, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestisEmpty()
|
||||
{
|
||||
void shouldTestisEmpty() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestGet_OutOfBounds_ThrowsException() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
@@ -71,6 +75,7 @@ public class MyLinkedListTest
|
||||
assertThrows(IllegalArgumentException.class, () -> list.get(1)); // index == size
|
||||
assertThrows(IllegalArgumentException.class, () -> list.get(5)); // index > size
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestSet_OutOfBounds_ThrowsException() {
|
||||
MyLinkedList<Integer> list = new MyLinkedList<>();
|
||||
@@ -79,12 +84,14 @@ public class MyLinkedListTest
|
||||
assertThrows(IllegalArgumentException.class, () -> list.set(-1, 99));
|
||||
assertThrows(IllegalArgumentException.class, () -> list.set(1, 99));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestRemove_IndexGreaterThanSize_ThrowsException() {
|
||||
MyLinkedList<String> list = new MyLinkedList<>();
|
||||
list.add("man");
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestRemove_NegativeIndex_ThrowsException() {
|
||||
MyLinkedList<String> list = new MyLinkedList<>();
|
||||
@@ -93,6 +100,7 @@ public class MyLinkedListTest
|
||||
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestaddAll_nonEmptySource_toEmptyList_addsAllElementsInOrder() {
|
||||
MyList<String> list = new MyLinkedList<>();
|
||||
|
||||
@@ -1,34 +1,30 @@
|
||||
package ru.test.maven.spring;
|
||||
import org.junit.jupiter.api.Test;
|
||||
package ru.project.collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.Collections;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.project.collection.MyList;
|
||||
|
||||
public class MyListCollectorTest
|
||||
{
|
||||
public class MyListCollectorTest {
|
||||
private final MyListCollector<Integer> collector = new MyListCollector<>();
|
||||
|
||||
@Test
|
||||
void shouldTestCollectInOrderSequential() {
|
||||
MyList<Integer> result = IntStream.range(0, 10)
|
||||
.boxed()
|
||||
.collect(MyCollectors.toMyList());
|
||||
MyList<Integer> result = IntStream.range(0, 10).boxed().collect(MyCollectors.toMyList());
|
||||
|
||||
assertEquals(10, result.size());
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
assertEquals(i, (int) result.get(i));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestCollectInOrderParallel() {
|
||||
MyList<Integer> result = IntStream.range(0, 100)
|
||||
.parallel()
|
||||
.boxed()
|
||||
.collect(MyCollectors.toMyList());
|
||||
MyList<Integer> result =
|
||||
IntStream.range(0, 100).parallel().boxed().collect(MyCollectors.toMyList());
|
||||
|
||||
assertEquals(100, result.size());
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
@@ -38,27 +34,25 @@ public class MyListCollectorTest
|
||||
|
||||
@Test
|
||||
void shouldTestHandleEmptyStream() {
|
||||
MyList<Integer> result = IntStream.empty()
|
||||
.boxed()
|
||||
.collect(MyCollectors.toMyList());
|
||||
MyList<Integer> result = IntStream.empty().boxed().collect(MyCollectors.toMyList());
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestNotUseStandardList() {
|
||||
MyList<String> result = IntStream.range(0, 5)
|
||||
.mapToObj(i -> "item" + i)
|
||||
.collect(MyCollectors.toMyList());
|
||||
MyList<String> result =
|
||||
IntStream.range(0, 5).mapToObj(i -> "item" + i).collect(MyCollectors.toMyList());
|
||||
|
||||
assertInstanceOf(MyList.class, result);
|
||||
assertFalse(result instanceof java.util.List);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTestHaveCorrectCharacteristics() {
|
||||
Set<Collector.Characteristics> characteristics = collector.characteristics();
|
||||
assertTrue(characteristics.contains(java.util.stream.Collector.Characteristics.IDENTITY_FINISH));
|
||||
assertTrue(
|
||||
characteristics.contains(java.util.stream.Collector.Characteristics.IDENTITY_FINISH));
|
||||
assertFalse(characteristics.contains(java.util.stream.Collector.Characteristics.UNORDERED));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package ru.project.finder;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class StudentOccurrenceIndexFinderTest
|
||||
{
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
public class StudentOccurrenceIndexFinderTest {
|
||||
private StudentOccurrenceIndexFinder finder;
|
||||
private MyList<Student> students;
|
||||
|
||||
@@ -21,6 +23,7 @@ public class StudentOccurrenceIndexFinderTest
|
||||
int[] result = finder.findOccurrences(students, new Student("A12", 4.5, 1));
|
||||
assertEquals(0, result.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_noMatches_returnsEmptyArray() {
|
||||
students.add(new Student("A12", 4.0, 101));
|
||||
@@ -30,6 +33,7 @@ public class StudentOccurrenceIndexFinderTest
|
||||
int[] result = finder.findOccurrences(students, target);
|
||||
assertEquals(0, result.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_singleMatch_returnsCorrectIndex() {
|
||||
students.add(new Student("A12", 4.0, 101));
|
||||
@@ -42,6 +46,7 @@ public class StudentOccurrenceIndexFinderTest
|
||||
assertEquals(1, result.length);
|
||||
assertEquals(1, result[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_multipleMatches_returnsAllIndicesSorted() {
|
||||
// индексы: 0, 2, 4 — должны вернуться в порядке возрастания
|
||||
@@ -55,7 +60,7 @@ public class StudentOccurrenceIndexFinderTest
|
||||
int[] result = finder.findOccurrences(students, target);
|
||||
|
||||
assertEquals(3, result.length);
|
||||
assertArrayEquals(new int[]{0, 2, 4}, result);
|
||||
assertArrayEquals(new int[] {0, 2, 4}, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,7 +72,7 @@ public class StudentOccurrenceIndexFinderTest
|
||||
|
||||
int[] result = finder.findOccurrences(students, s);
|
||||
assertEquals(3, result.length);
|
||||
assertArrayEquals(new int[]{0, 1, 2}, result);
|
||||
assertArrayEquals(new int[] {0, 1, 2}, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,18 +102,17 @@ public class StudentOccurrenceIndexFinderTest
|
||||
assertEquals(0, result.length);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_nullList_throwsException() {
|
||||
Student target = new Student("A12", 4.5, 101);
|
||||
assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(null, target));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_nullTarget_throwsException() {
|
||||
assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(students, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldTesfindOccurrences_resultIsSorted() {
|
||||
String[] validGroups = {"A00", "A01", "B12", "C99", "X55"};
|
||||
@@ -126,7 +130,6 @@ public class StudentOccurrenceIndexFinderTest
|
||||
Student target = new Student("A12", 4.5, 101);
|
||||
int[] result = finder.findOccurrences(students, target);
|
||||
|
||||
|
||||
for (int i = 1; i < result.length; i++) {
|
||||
assertTrue(result[i] > result[i - 1], "Результат должен быть отсортирован");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package ru.project.io;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Spliterators;
|
||||
import java.util.stream.StreamSupport;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import ru.project.student.Student;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.collectors.MyCollectors;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
class StudentCsvReaderTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
@Test
|
||||
void shouldTestreadValidCsvIntoMyList() throws IOException {
|
||||
Path csv = tempDir.resolve("students.csv");
|
||||
String content = String.join("\n",
|
||||
"groupNumber,averageGrade,recordBookNumber",
|
||||
"M10,4.5,12345",
|
||||
"B05,3.8,12346"
|
||||
);
|
||||
Files.writeString(csv, content, StandardCharsets.UTF_8);
|
||||
try (var reader = new StudentCsvReader(csv.toAbsolutePath())) {
|
||||
var studentStream = StreamSupport.stream(
|
||||
Spliterators.spliteratorUnknownSize(reader, 0),
|
||||
false
|
||||
);
|
||||
MyList<Student> students = studentStream
|
||||
.collect(MyCollectors.toMyList());
|
||||
assertEquals(2, students.size());
|
||||
assertEquals("M10", students.get(0).getGroupNumber());
|
||||
assertEquals(4.5, students.get(0).getAverageGrade(), 0.0);
|
||||
assertEquals(12345, students.get(0).getRecordBookNumber());
|
||||
assertEquals("B05", students.get(1).getGroupNumber());
|
||||
assertEquals(3.8, students.get(1).getAverageGrade(), 0.0);
|
||||
assertEquals(12346, students.get(1).getRecordBookNumber());
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void shouldTestinvalidHeaderThrowsException() throws IOException {
|
||||
Path csv = tempDir.resolve("bad-header.csv");
|
||||
String content = """
|
||||
badGroup,badGrade,badBook
|
||||
M101,4.5,12345
|
||||
""";
|
||||
Files.writeString(csv, content, StandardCharsets.UTF_8);
|
||||
IllegalArgumentException ex = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> new StudentCsvReader(csv.toAbsolutePath())
|
||||
);
|
||||
assertTrue(ex.getMessage().contains("Invalid CSV header"));
|
||||
}
|
||||
@Test
|
||||
void shouldTestwrongColumnCountThrowsException() throws IOException {
|
||||
Path csv = tempDir.resolve("wrong-columns.csv");
|
||||
String content = """
|
||||
groupNumber,averageGrade,recordBookNumber
|
||||
M101,4.5
|
||||
""";
|
||||
Files.writeString(csv, content, StandardCharsets.UTF_8);
|
||||
try (var reader = new StudentCsvReader(csv.toAbsolutePath())) {
|
||||
assertTrue(reader.hasNext());
|
||||
|
||||
IllegalArgumentException ex = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
reader::next
|
||||
);
|
||||
assertTrue(ex.getMessage().contains("Expected 3 columns"));
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void shouldTestparseErrorThrowsExceptionWithDetails() throws IOException {
|
||||
Path csv = tempDir.resolve("parse-error.csv");
|
||||
String content = """
|
||||
groupNumber,averageGrade,recordBookNumber
|
||||
M101,not-a-number,12345
|
||||
""";
|
||||
Files.writeString(csv, content, StandardCharsets.UTF_8);
|
||||
try (var reader = new StudentCsvReader(csv.toAbsolutePath())) {
|
||||
assertTrue(reader.hasNext());
|
||||
|
||||
IllegalArgumentException ex = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
reader::next
|
||||
);
|
||||
assertTrue(ex.getMessage().contains("cannot parse averageGrade"));
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void shouldTestemptyDataAfterHeaderThrowsNoSuchElement() throws IOException {
|
||||
Path csv = tempDir.resolve("empty-data.csv");
|
||||
String content = """
|
||||
groupNumber,averageGrade,recordBookNumber
|
||||
""";
|
||||
Files.writeString(csv, content, StandardCharsets.UTF_8);
|
||||
|
||||
// ИСПРАВЛЕНО: добавлен try-with-resources
|
||||
try (var reader = new StudentCsvReader(csv.toAbsolutePath())) {
|
||||
assertFalse(reader.hasNext());
|
||||
|
||||
assertThrows(NoSuchElementException.class, reader::next);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void shouldTestrelativePathThrowsException() {
|
||||
Path relative = Path.of("relative-path.csv");
|
||||
|
||||
IllegalArgumentException ex = assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> new StudentCsvReader(relative)
|
||||
);
|
||||
assertTrue(ex.getMessage().contains("absolute"));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package ru.project.search;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.project.list.MyList;
|
||||
import ru.project.list.MyLinkedList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.project.collection.MyLinkedList;
|
||||
import ru.project.collection.MyList;
|
||||
import ru.project.student.Student;
|
||||
|
||||
class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
@@ -39,16 +39,13 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testGroupNumberRangeInvalidMinMaxThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new GroupNumberRangeCriteria("C30", "A10"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria("C30", "A10"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGroupNumberRangeNullValuesThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new GroupNumberRangeCriteria(null, "A10"));
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new GroupNumberRangeCriteria("A10", null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria(null, "A10"));
|
||||
assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria("A10", null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,8 +85,7 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testAverageGradeRangeInvalidMinMaxThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new AverageGradeRangeCriteria(5.0, 4.0));
|
||||
assertThrows(IllegalArgumentException.class, () -> new AverageGradeRangeCriteria(5.0, 4.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,27 +125,29 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testRecordBookNumberRangeInvalidMinMaxThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new RecordBookNumberRangeCriteria(200, 100));
|
||||
assertThrows(IllegalArgumentException.class, () -> new RecordBookNumberRangeCriteria(200, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompositeIntersectionEmptyReturnsTrue() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
Student student = new Student("A12", 4.5, 123);
|
||||
assertTrue(composite.matches(student));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompositeUnionEmptyReturnsFalse() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
Student student = new Student("A12", 4.5, 123);
|
||||
assertFalse(composite.matches(student));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompositeIntersectionAllTrue() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
composite.add(new GroupNumberRangeCriteria("A10", "C30"));
|
||||
composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
Student student = new Student("B20", 4.5, 123);
|
||||
@@ -158,7 +156,8 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testCompositeIntersectionOneFalse() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
composite.add(new GroupNumberRangeCriteria("A10", "C30"));
|
||||
composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
Student student = new Student("B20", 3.5, 123);
|
||||
@@ -167,7 +166,8 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testCompositeUnionOneTrue() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
composite.add(new GroupNumberRangeCriteria("A10", "C30"));
|
||||
composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
Student student = new Student("D00", 4.5, 123);
|
||||
@@ -176,7 +176,8 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testCompositeUnionAllFalse() {
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
composite.add(new GroupNumberRangeCriteria("A10", "C30"));
|
||||
composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
Student student = new Student("D00", 3.5, 123);
|
||||
@@ -185,10 +186,12 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testCompositeWithNestedComposite() {
|
||||
CompositeStudentSearchCriteria outer = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
CompositeStudentSearchCriteria outer =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
outer.add(new GroupNumberRangeCriteria("A10", "C30"));
|
||||
|
||||
CompositeStudentSearchCriteria inner = new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
CompositeStudentSearchCriteria inner =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.UNION);
|
||||
inner.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
inner.add(new RecordBookNumberRangeCriteria(100, 200));
|
||||
|
||||
@@ -280,8 +283,7 @@ class StudentSearchTest {
|
||||
|
||||
@Test
|
||||
void testSearchServiceWithNullCriteriaThrowsException() {
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> new StudentSearchService(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> new StudentSearchService(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -291,7 +293,8 @@ class StudentSearchTest {
|
||||
students.add(new Student("B20", 4.5, 2));
|
||||
students.add(new Student("C30", 3.5, 3));
|
||||
|
||||
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
CompositeStudentSearchCriteria composite =
|
||||
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
|
||||
composite.add(new GroupNumberRangeCriteria("A10", "B20"));
|
||||
composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user