Merge pull request into develop from feature/datasource

This commit is contained in:
DELETED
2026-09-01 19:02:14 +04:00
committed by GitHub
11 changed files with 958 additions and 736 deletions
+6
View File
@@ -42,6 +42,12 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.14.1</version>
</dependency>
<dependency> <dependency>
<groupId>com.googlecode.lanterna</groupId> <groupId>com.googlecode.lanterna</groupId>
<artifactId>lanterna</artifactId> <artifactId>lanterna</artifactId>
@@ -1,15 +1,12 @@
package ru.project.collectors; package ru.project.collectors;
import java.util.stream.Collector; import java.util.stream.Collector;
import ru.project.collection.MyList;
public class MyCollectors public class MyCollectors {
{ private 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<>(); return new MyListCollector<>();
} }
} }
@@ -1,16 +1,17 @@
package ru.project.collectors; package ru.project.collectors;
import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.BinaryOperator; import java.util.function.BinaryOperator;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collector; 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 @Override
public Supplier<MyList<T>> supplier() public Supplier<MyList<T>> supplier() {
{
return () -> new MyLinkedList<>(); return () -> new MyLinkedList<>();
} }
@@ -29,15 +30,12 @@ public class MyListCollector <T> implements Collector<T, MyList<T>, MyList<T>>
} }
@Override @Override
public Function<MyList<T>, MyList<T>> finisher() public Function<MyList<T>, MyList<T>> finisher() {
{
return list -> list; return list -> list;
} }
@Override @Override
public Set<Collector.Characteristics> characteristics() { public Set<Collector.Characteristics> characteristics() {
return java.util.Set.of( return java.util.Set.of(Collector.Characteristics.IDENTITY_FINISH);
Collector.Characteristics.IDENTITY_FINISH
);
} }
} }
@@ -1,9 +1,13 @@
package ru.project.finder; package ru.project.finder;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; 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) { public int[] findOccurrences(MyList<Student> students, Student target) {
if (students == null) { if (students == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
@@ -32,7 +36,9 @@ public class StudentOccurrenceIndexFinder
final int start = fromIndex; final int start = fromIndex;
final int end = toIndex; final int end = toIndex;
Future<MyList<Integer>> future = executor.submit(() -> { Future<MyList<Integer>> future =
executor.submit(
() -> {
return findInRange(students, target, start, end); return findInRange(students, target, start, end);
}); });
futures.add(future); futures.add(future);
@@ -110,7 +116,8 @@ public class StudentOccurrenceIndexFinder
return (size + threadCount - 1) / threadCount; 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<>(); MyList<Integer> foundIndices = new MyLinkedList<>();
for (int i = fromIndex; i < toIndex; i++) { 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; package ru.project.search;
import ru.project.list.MyList; import ru.project.collection.MyLinkedList;
import ru.project.list.MyLinkedList; import ru.project.collection.MyList;
import ru.project.student.Student; import ru.project.student.Student;
public class StudentSearchService { public class StudentSearchService {
@@ -1,8 +1,10 @@
package ru.test.maven.spring; package ru.project.collection;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class MyLinkedListTest
{ import org.junit.jupiter.api.Test;
public class MyLinkedListTest {
@Test @Test
public void shouldTestAdd() { public void shouldTestAdd() {
MyLinkedList<Integer> list = new MyLinkedList<>(); MyLinkedList<Integer> list = new MyLinkedList<>();
@@ -14,13 +16,14 @@ public class MyLinkedListTest
assertEquals(20, list.get(1)); assertEquals(20, list.get(1));
assertEquals(30, list.get(2)); assertEquals(30, list.get(2));
} }
@Test @Test
public void shouldTestGet() public void shouldTestGet() {
{
MyLinkedList<Integer> list = new MyLinkedList<>(); MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(10); list.add(10);
assertEquals(10, list.get(0)); assertEquals(10, list.get(0));
} }
@Test @Test
public void shouldTestRemove() { public void shouldTestRemove() {
MyLinkedList<Integer> list = new MyLinkedList<>(); MyLinkedList<Integer> list = new MyLinkedList<>();
@@ -33,6 +36,7 @@ public class MyLinkedListTest
assertEquals(20, list.get(0)); assertEquals(20, list.get(0));
assertEquals(30, list.get(1)); assertEquals(30, list.get(1));
} }
@Test @Test
void shouldTestSet() { void shouldTestSet() {
MyLinkedList<String> list = new MyLinkedList<>(); MyLinkedList<String> list = new MyLinkedList<>();
@@ -45,23 +49,23 @@ public class MyLinkedListTest
assertEquals("B", oldValue); assertEquals("B", oldValue);
assertEquals("Z", list.get(1)); assertEquals("Z", list.get(1));
assertEquals(3, list.size()); assertEquals(3, list.size());
} }
@Test @Test
void shouldTestSize() void shouldTestSize() {
{
MyLinkedList<Integer> list = new MyLinkedList<>(); MyLinkedList<Integer> list = new MyLinkedList<>();
list.add(1); list.add(1);
list.add(2); list.add(2);
list.add(3); list.add(3);
assertEquals(3, list.size()); assertEquals(3, list.size());
} }
@Test @Test
void shouldTestisEmpty() void shouldTestisEmpty() {
{
MyLinkedList<Integer> list = new MyLinkedList<>(); MyLinkedList<Integer> list = new MyLinkedList<>();
assertTrue(list.isEmpty()); assertTrue(list.isEmpty());
} }
@Test @Test
void shouldTestGet_OutOfBounds_ThrowsException() { void shouldTestGet_OutOfBounds_ThrowsException() {
MyLinkedList<Integer> list = new MyLinkedList<>(); 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(1)); // index == size
assertThrows(IllegalArgumentException.class, () -> list.get(5)); // index > size assertThrows(IllegalArgumentException.class, () -> list.get(5)); // index > size
} }
@Test @Test
void shouldTestSet_OutOfBounds_ThrowsException() { void shouldTestSet_OutOfBounds_ThrowsException() {
MyLinkedList<Integer> list = new MyLinkedList<>(); 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));
assertThrows(IllegalArgumentException.class, () -> list.set(1, 99)); assertThrows(IllegalArgumentException.class, () -> list.set(1, 99));
} }
@Test @Test
void shouldTestRemove_IndexGreaterThanSize_ThrowsException() { void shouldTestRemove_IndexGreaterThanSize_ThrowsException() {
MyLinkedList<String> list = new MyLinkedList<>(); MyLinkedList<String> list = new MyLinkedList<>();
list.add("man"); list.add("man");
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(5)); assertThrows(IndexOutOfBoundsException.class, () -> list.remove(5));
} }
@Test @Test
void shouldTestRemove_NegativeIndex_ThrowsException() { void shouldTestRemove_NegativeIndex_ThrowsException() {
MyLinkedList<String> list = new MyLinkedList<>(); MyLinkedList<String> list = new MyLinkedList<>();
@@ -93,6 +100,7 @@ public class MyLinkedListTest
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1)); assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
} }
@Test @Test
void shouldTestaddAll_nonEmptySource_toEmptyList_addsAllElementsInOrder() { void shouldTestaddAll_nonEmptySource_toEmptyList_addsAllElementsInOrder() {
MyList<String> list = new MyLinkedList<>(); MyList<String> list = new MyLinkedList<>();
@@ -1,34 +1,30 @@
package ru.test.maven.spring; package ru.project.collectors;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.Set; import java.util.Set;
import java.util.stream.Collector; import java.util.stream.Collector;
import java.util.stream.IntStream; 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<>(); private final MyListCollector<Integer> collector = new MyListCollector<>();
@Test @Test
void shouldTestCollectInOrderSequential() { void shouldTestCollectInOrderSequential() {
MyList<Integer> result = IntStream.range(0, 10) MyList<Integer> result = IntStream.range(0, 10).boxed().collect(MyCollectors.toMyList());
.boxed()
.collect(MyCollectors.toMyList());
assertEquals(10, result.size()); assertEquals(10, result.size());
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
assertEquals(i, (int) result.get(i)); assertEquals(i, (int) result.get(i));
} }
} }
@Test @Test
void shouldTestCollectInOrderParallel() { void shouldTestCollectInOrderParallel() {
MyList<Integer> result = IntStream.range(0, 100) MyList<Integer> result =
.parallel() IntStream.range(0, 100).parallel().boxed().collect(MyCollectors.toMyList());
.boxed()
.collect(MyCollectors.toMyList());
assertEquals(100, result.size()); assertEquals(100, result.size());
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
@@ -38,27 +34,25 @@ public class MyListCollectorTest
@Test @Test
void shouldTestHandleEmptyStream() { void shouldTestHandleEmptyStream() {
MyList<Integer> result = IntStream.empty() MyList<Integer> result = IntStream.empty().boxed().collect(MyCollectors.toMyList());
.boxed()
.collect(MyCollectors.toMyList());
assertTrue(result.isEmpty()); assertTrue(result.isEmpty());
} }
@Test @Test
void shouldTestNotUseStandardList() { void shouldTestNotUseStandardList() {
MyList<String> result = IntStream.range(0, 5) MyList<String> result =
.mapToObj(i -> "item" + i) IntStream.range(0, 5).mapToObj(i -> "item" + i).collect(MyCollectors.toMyList());
.collect(MyCollectors.toMyList());
assertInstanceOf(MyList.class, result); assertInstanceOf(MyList.class, result);
assertFalse(result instanceof java.util.List); assertFalse(result instanceof java.util.List);
} }
@Test @Test
void shouldTestHaveCorrectCharacteristics() { void shouldTestHaveCorrectCharacteristics() {
Set<Collector.Characteristics> characteristics = collector.characteristics(); 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)); assertFalse(characteristics.contains(java.util.stream.Collector.Characteristics.UNORDERED));
} }
} }
@@ -1,12 +1,14 @@
package ru.project.finder; package ru.project.finder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; 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 StudentOccurrenceIndexFinder finder;
private MyList<Student> students; private MyList<Student> students;
@@ -21,6 +23,7 @@ public class StudentOccurrenceIndexFinderTest
int[] result = finder.findOccurrences(students, new Student("A12", 4.5, 1)); int[] result = finder.findOccurrences(students, new Student("A12", 4.5, 1));
assertEquals(0, result.length); assertEquals(0, result.length);
} }
@Test @Test
void shouldTesfindOccurrences_noMatches_returnsEmptyArray() { void shouldTesfindOccurrences_noMatches_returnsEmptyArray() {
students.add(new Student("A12", 4.0, 101)); students.add(new Student("A12", 4.0, 101));
@@ -30,6 +33,7 @@ public class StudentOccurrenceIndexFinderTest
int[] result = finder.findOccurrences(students, target); int[] result = finder.findOccurrences(students, target);
assertEquals(0, result.length); assertEquals(0, result.length);
} }
@Test @Test
void shouldTesfindOccurrences_singleMatch_returnsCorrectIndex() { void shouldTesfindOccurrences_singleMatch_returnsCorrectIndex() {
students.add(new Student("A12", 4.0, 101)); students.add(new Student("A12", 4.0, 101));
@@ -42,6 +46,7 @@ public class StudentOccurrenceIndexFinderTest
assertEquals(1, result.length); assertEquals(1, result.length);
assertEquals(1, result[0]); assertEquals(1, result[0]);
} }
@Test @Test
void shouldTesfindOccurrences_multipleMatches_returnsAllIndicesSorted() { void shouldTesfindOccurrences_multipleMatches_returnsAllIndicesSorted() {
// индексы: 0, 2, 4 — должны вернуться в порядке возрастания // индексы: 0, 2, 4 — должны вернуться в порядке возрастания
@@ -97,18 +102,17 @@ public class StudentOccurrenceIndexFinderTest
assertEquals(0, result.length); assertEquals(0, result.length);
} }
@Test @Test
void shouldTesfindOccurrences_nullList_throwsException() { void shouldTesfindOccurrences_nullList_throwsException() {
Student target = new Student("A12", 4.5, 101); Student target = new Student("A12", 4.5, 101);
assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(null, target)); assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(null, target));
} }
@Test @Test
void shouldTesfindOccurrences_nullTarget_throwsException() { void shouldTesfindOccurrences_nullTarget_throwsException() {
assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(students, null)); assertThrows(IllegalArgumentException.class, () -> finder.findOccurrences(students, null));
} }
@Test @Test
void shouldTesfindOccurrences_resultIsSorted() { void shouldTesfindOccurrences_resultIsSorted() {
String[] validGroups = {"A00", "A01", "B12", "C99", "X55"}; String[] validGroups = {"A00", "A01", "B12", "C99", "X55"};
@@ -126,7 +130,6 @@ public class StudentOccurrenceIndexFinderTest
Student target = new Student("A12", 4.5, 101); Student target = new Student("A12", 4.5, 101);
int[] result = finder.findOccurrences(students, target); int[] result = finder.findOccurrences(students, target);
for (int i = 1; i < result.length; i++) { for (int i = 1; i < result.length; i++) {
assertTrue(result[i] > result[i - 1], "Результат должен быть отсортирован"); 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; 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 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 { class StudentSearchTest {
@Test @Test
@@ -39,16 +39,13 @@ class StudentSearchTest {
@Test @Test
void testGroupNumberRangeInvalidMinMaxThrowsException() { void testGroupNumberRangeInvalidMinMaxThrowsException() {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria("C30", "A10"));
() -> new GroupNumberRangeCriteria("C30", "A10"));
} }
@Test @Test
void testGroupNumberRangeNullValuesThrowsException() { void testGroupNumberRangeNullValuesThrowsException() {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria(null, "A10"));
() -> new GroupNumberRangeCriteria(null, "A10")); assertThrows(IllegalArgumentException.class, () -> new GroupNumberRangeCriteria("A10", null));
assertThrows(IllegalArgumentException.class,
() -> new GroupNumberRangeCriteria("A10", null));
} }
@Test @Test
@@ -88,8 +85,7 @@ class StudentSearchTest {
@Test @Test
void testAverageGradeRangeInvalidMinMaxThrowsException() { void testAverageGradeRangeInvalidMinMaxThrowsException() {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class, () -> new AverageGradeRangeCriteria(5.0, 4.0));
() -> new AverageGradeRangeCriteria(5.0, 4.0));
} }
@Test @Test
@@ -129,27 +125,29 @@ class StudentSearchTest {
@Test @Test
void testRecordBookNumberRangeInvalidMinMaxThrowsException() { void testRecordBookNumberRangeInvalidMinMaxThrowsException() {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class, () -> new RecordBookNumberRangeCriteria(200, 100));
() -> new RecordBookNumberRangeCriteria(200, 100));
} }
@Test @Test
void testCompositeIntersectionEmptyReturnsTrue() { void testCompositeIntersectionEmptyReturnsTrue() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
Student student = new Student("A12", 4.5, 123); Student student = new Student("A12", 4.5, 123);
assertTrue(composite.matches(student)); assertTrue(composite.matches(student));
} }
@Test @Test
void testCompositeUnionEmptyReturnsFalse() { void testCompositeUnionEmptyReturnsFalse() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.UNION);
Student student = new Student("A12", 4.5, 123); Student student = new Student("A12", 4.5, 123);
assertFalse(composite.matches(student)); assertFalse(composite.matches(student));
} }
@Test @Test
void testCompositeIntersectionAllTrue() { void testCompositeIntersectionAllTrue() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
composite.add(new GroupNumberRangeCriteria("A10", "C30")); composite.add(new GroupNumberRangeCriteria("A10", "C30"));
composite.add(new AverageGradeRangeCriteria(4.0, 5.0)); composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
Student student = new Student("B20", 4.5, 123); Student student = new Student("B20", 4.5, 123);
@@ -158,7 +156,8 @@ class StudentSearchTest {
@Test @Test
void testCompositeIntersectionOneFalse() { void testCompositeIntersectionOneFalse() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
composite.add(new GroupNumberRangeCriteria("A10", "C30")); composite.add(new GroupNumberRangeCriteria("A10", "C30"));
composite.add(new AverageGradeRangeCriteria(4.0, 5.0)); composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
Student student = new Student("B20", 3.5, 123); Student student = new Student("B20", 3.5, 123);
@@ -167,7 +166,8 @@ class StudentSearchTest {
@Test @Test
void testCompositeUnionOneTrue() { void testCompositeUnionOneTrue() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.UNION);
composite.add(new GroupNumberRangeCriteria("A10", "C30")); composite.add(new GroupNumberRangeCriteria("A10", "C30"));
composite.add(new AverageGradeRangeCriteria(4.0, 5.0)); composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
Student student = new Student("D00", 4.5, 123); Student student = new Student("D00", 4.5, 123);
@@ -176,7 +176,8 @@ class StudentSearchTest {
@Test @Test
void testCompositeUnionAllFalse() { void testCompositeUnionAllFalse() {
CompositeStudentSearchCriteria composite = new CompositeStudentSearchCriteria(SearchOperation.UNION); CompositeStudentSearchCriteria composite =
new CompositeStudentSearchCriteria(SearchOperation.UNION);
composite.add(new GroupNumberRangeCriteria("A10", "C30")); composite.add(new GroupNumberRangeCriteria("A10", "C30"));
composite.add(new AverageGradeRangeCriteria(4.0, 5.0)); composite.add(new AverageGradeRangeCriteria(4.0, 5.0));
Student student = new Student("D00", 3.5, 123); Student student = new Student("D00", 3.5, 123);
@@ -185,10 +186,12 @@ class StudentSearchTest {
@Test @Test
void testCompositeWithNestedComposite() { void testCompositeWithNestedComposite() {
CompositeStudentSearchCriteria outer = new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION); CompositeStudentSearchCriteria outer =
new CompositeStudentSearchCriteria(SearchOperation.INTERSECTION);
outer.add(new GroupNumberRangeCriteria("A10", "C30")); 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 AverageGradeRangeCriteria(4.0, 5.0));
inner.add(new RecordBookNumberRangeCriteria(100, 200)); inner.add(new RecordBookNumberRangeCriteria(100, 200));
@@ -280,8 +283,7 @@ class StudentSearchTest {
@Test @Test
void testSearchServiceWithNullCriteriaThrowsException() { void testSearchServiceWithNullCriteriaThrowsException() {
assertThrows(IllegalArgumentException.class, assertThrows(IllegalArgumentException.class, () -> new StudentSearchService(null));
() -> new StudentSearchService(null));
} }
@Test @Test
@@ -291,7 +293,8 @@ class StudentSearchTest {
students.add(new Student("B20", 4.5, 2)); students.add(new Student("B20", 4.5, 2));
students.add(new Student("C30", 3.5, 3)); 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 GroupNumberRangeCriteria("A10", "B20"));
composite.add(new AverageGradeRangeCriteria(4.0, 5.0)); composite.add(new AverageGradeRangeCriteria(4.0, 5.0));