This commit is contained in:
Claudio Maggioni 2023-04-02 16:24:23 +02:00
parent 59be76cf17
commit d6dcb72b1b
2 changed files with 38 additions and 15 deletions

View file

@ -11,7 +11,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.platform.version>1.7.0-M1</junit.platform.version> <junit.platform.version>1.7.0-M1</junit.platform.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version> <junit.jupiter.version>5.6.2</junit.jupiter.version>
<junit.vintage.version>5.6.2</junit.vintage.version>
<junit.version>4.13.1</junit.version> <junit.version>4.13.1</junit.version>
</properties> </properties>
@ -93,12 +92,6 @@
<version>${junit.version}</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit.vintage.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<reporting> <reporting>
<plugins> <plugins>

View file

@ -6,9 +6,9 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
class CollectionUtilsTest { public class CollectionUtilsTest {
private static <T extends Comparable<T>> void checkSort(List<T> source, List<T> expected) { private static <T extends Comparable<T>> void checkSort(List<T> source, List<T> expected) {
List<T> target = new ArrayList<>(source); List<T> target = new ArrayList<>(source);
@ -20,14 +20,44 @@ class CollectionUtilsTest {
return Arrays.asList(elements); return Arrays.asList(elements);
} }
private static <T> ArrayList<T> empty() { @Test
return new ArrayList<T>(); public void checkSorted() {
assertThrows(IllegalArgumentException.class, () -> CollectionUtils.sort(null));
checkSort(new ArrayList<Integer>(), new ArrayList<>());
checkSort(list(0, 1, 2, 3, 4), list(0, 1, 2, 3, 4));
checkSort(list(8, 4, 5, 6, 7), list(4, 5, 6, 7, 8));
checkSort(list(8, 7), list(7, 8));
} }
@Test @Test
public void checkSorted() { public void checkStability() {
checkSort(empty(), empty()); class DummyAllSame implements Comparable<DummyAllSame> {
checkSort(list(0, 1, 2, 3, 4), list(0, 1, 2, 3, 4)); @Override
checkSort(list(8, 4, 5, 6, 7), list(4, 5, 6, 7, 8)); public int compareTo(DummyAllSame o) {
return 0; // all are same
}
}
final DummyAllSame a = new DummyAllSame();
final DummyAllSame b = new DummyAllSame();
final DummyAllSame c = new DummyAllSame();
final List<DummyAllSame> list = Arrays.asList(a, b, c);
CollectionUtils.sort(list);
assertSame(a, list.get(0));
assertSame(b, list.get(1));
assertSame(c, list.get(2));
assertEquals(3, list.size());
}
@Test
public void checkConstructor() {
try {
new CollectionUtils();
} catch (final Exception ignored) {
fail(ignored);
}
} }
} }