Skip to content

Commit

Permalink
Fix deprecations in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Oct 21, 2020
1 parent 16f5c68 commit 171073f
Show file tree
Hide file tree
Showing 162 changed files with 412 additions and 333 deletions.
Empty file.
99 changes: 53 additions & 46 deletions stream/src/test/java/com/annimon/stream/CollectorsTest.java
@@ -1,7 +1,6 @@
package com.annimon.stream;

import com.annimon.stream.function.*;
import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -10,22 +9,23 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.function.ThrowingRunnable;
import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

/**
* Tests {@code Collectors}.
*
* @see com.annimon.stream.Collectors
*/
@SuppressWarnings("ConstantConditions")
public class CollectorsTest {

@Rule
public final ExpectedException expectedException = ExpectedException.none();

@Test
public void testToCollection() {
Collection<Integer> result = Stream.range(0, 5)
Expand Down Expand Up @@ -69,9 +69,8 @@ public void testToUnmodifiableList() {
} catch (UnsupportedOperationException expected) { }
}

@Test
@Test(expected = NullPointerException.class)
public void testToUnmodifiableListWithNullValues() {
expectedException.expect(NullPointerException.class);
Stream.of(0, 1, null, 3, 4, null)
.collect(Collectors.toUnmodifiableList());
}
Expand Down Expand Up @@ -100,9 +99,8 @@ public void testToUnmodifiableSet() {
} catch (UnsupportedOperationException expected) { }
}

@Test
@Test(expected = NullPointerException.class)
public void testToUnmodifiableSetWithNullValues() {
expectedException.expect(NullPointerException.class);
Stream.of(0, 1, null, 3, 4, null)
.collect(Collectors.toUnmodifiableSet());
}
Expand Down Expand Up @@ -144,9 +142,8 @@ public void testToMapWithIdentityValueMapper() {
));
}

@Test
@Test(expected = NullPointerException.class)
public void testToMapWithValueMapperThatReturnsNullValue() {
expectedException.expect(NullPointerException.class);
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
Stream.of("a0", "b0", "c0", "d0")
.collect(Collectors.toMap(keyMapper, new UnaryOperator<String>() {
Expand All @@ -161,25 +158,39 @@ public String apply(String value) {

@Test
public void testToMapWithDefaultValueMapperAndDuplicatingKeys() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Duplicate key a (attempted merging values a0 and a2)");
Stream.of("a0", "b1", "a2", "d3")
.collect(Collectors.toMap(Functions.firstCharacterExtractor()));
IllegalStateException exc = assertThrows(
IllegalStateException.class,
new ThrowingRunnable() {
@Override
public void run() {
Stream.of("a0", "b1", "a2", "d3")
.collect(Collectors.toMap(Functions.firstCharacterExtractor()));
}
}
);
assertEquals("Duplicate key a (attempted merging values a0 and a2)", exc.getMessage());

}

@Test
public void testToUnmodifiableMapDuplicatingKeys() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage("Duplicate key a (attempted merging values a0 and a2)");
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
final UnaryOperator<String> valueMapper = UnaryOperator.Util.identity();
Stream.of("a0", "b1", "a2", "d3")
.collect(Collectors.toUnmodifiableMap(keyMapper, valueMapper));
IllegalStateException exc = assertThrows(
IllegalStateException.class,
new ThrowingRunnable() {
@Override
public void run() {
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
final UnaryOperator<String> valueMapper = UnaryOperator.Util.identity();
Stream.of("a0", "b1", "a2", "d3")
.collect(Collectors.toUnmodifiableMap(keyMapper, valueMapper));
}
}
);
assertEquals("Duplicate key a (attempted merging values a0 and a2)", exc.getMessage());
}

@Test
@Test(expected = NullPointerException.class)
public void testToUnmodifiableMapWithNullKey() {
expectedException.expect(NullPointerException.class);
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
final UnaryOperator<String> valueMapper = new UnaryOperator<String>() {
@Override
Expand All @@ -192,9 +203,8 @@ public String apply(String value) {
.collect(Collectors.toUnmodifiableMap(keyMapper, valueMapper));
}

@Test
@Test(expected = NullPointerException.class)
public void testToUnmodifiableMapWithNullValue() {
expectedException.expect(NullPointerException.class);
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
final UnaryOperator<String> valueMapper = new UnaryOperator<String>() {
@Override
Expand Down Expand Up @@ -305,9 +315,8 @@ public String apply(String oldValue, String newValue) {
} catch (UnsupportedOperationException expected) { }
}

@Test
@Test(expected = NullPointerException.class)
public void testToUnmodifiableMapWithMergerFunctionAndNullKey() {
expectedException.expect(NullPointerException.class);
final Function<String, Character> keyMapper = Functions.firstCharacterExtractor();
final UnaryOperator<String> valueMapper = new UnaryOperator<String>() {
@Override
Expand Down Expand Up @@ -642,6 +651,7 @@ public void testGroupingByStudentSpeciality() {
)));
}

@SuppressWarnings("ArraysAsListWithZeroOrOneArgument")
@Test
public void testGroupingByStudentCourse() {
Map<Integer, List<Student>> byCourse = Stream.of(Students.ALL)
Expand Down Expand Up @@ -692,7 +702,7 @@ public void testGroupingByStudentCourseCounting() {
@Test
public void testPartitioningByStudentCourse() {
Map<Boolean, List<Student>> byCourse = Stream.of(Students.ALL)
.collect(Collectors.partitioningBy(new Predicate<Student>() {
.collect(Collectors.partitioningBy(new Predicate<Student>() {
@Override
public boolean test(Student student) {
return student.getCourse() == 2;
Expand All @@ -712,7 +722,7 @@ public boolean test(Student student) {
@Test
public void testPartitioningByStudentCourseToNames() {
Map<Boolean, String> byCourse = Stream.of(Students.ALL)
.collect(Collectors.partitioningBy(new Predicate<Student>() {
.collect(Collectors.partitioningBy(new Predicate<Student>() {
@Override
public boolean test(Student student) {
return student.getCourse() > 2;
Expand Down Expand Up @@ -761,23 +771,20 @@ public void testMappingStudentNamesBySpeciality() {
Collectors.mapping(Students.studentName, Collectors.<String>toSet())));

assertThat(namesBySpeciality.get("Economics"),
containsInAnyOrder(new String[] {
Students.MARIA_ECONOMICS_1.getName(),
Students.SERGEY_ECONOMICS_2.getName(),
Students.SOPHIA_ECONOMICS_2.getName()
}));
containsInAnyOrder(
Students.MARIA_ECONOMICS_1.getName(),
Students.SERGEY_ECONOMICS_2.getName(),
Students.SOPHIA_ECONOMICS_2.getName()));
assertThat(namesBySpeciality.get("CS"),
containsInAnyOrder(new String[] {
Students.STEVE_CS_4.getName(),
Students.VICTORIA_CS_3.getName(),
Students.JOHN_CS_2.getName(),
Students.MARIA_CS_1.getName()
}));
containsInAnyOrder(
Students.STEVE_CS_4.getName(),
Students.VICTORIA_CS_3.getName(),
Students.JOHN_CS_2.getName(),
Students.MARIA_CS_1.getName()));
assertThat(namesBySpeciality.get("Law"),
containsInAnyOrder(new String[] {
Students.GEORGE_LAW_3.getName(),
Students.SERGEY_LAW_1.getName()
}));
containsInAnyOrder(
Students.GEORGE_LAW_3.getName(),
Students.SERGEY_LAW_1.getName()));
}

@Test
Expand Down
Expand Up @@ -9,9 +9,9 @@
import java.util.List;
import org.junit.Test;
import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class ComparatorCompatTest {

Expand Down
36 changes: 20 additions & 16 deletions stream/src/test/java/com/annimon/stream/ExceptionalTest.java
Expand Up @@ -9,15 +9,18 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;

/**
* Tests {@code Exceptional}.
*
* @see com.annimon.stream.Exceptional
*/
@SuppressWarnings("ConstantConditions")
public class ExceptionalTest {

@Test
Expand Down Expand Up @@ -106,30 +109,30 @@ public void testGetOrThrowWithException() throws Throwable {
}

@Test
public void testGetOrThrowRuntimeExceptionWithoutException() throws Throwable {
public void testGetOrThrowRuntimeExceptionWithoutException() {
int value = Exceptional
.of(tenSupplier)
.getOrThrowRuntimeException();
assertEquals(10, value);
}

@Test(expected = RuntimeException.class)
public void testGetOrThrowRuntimeExceptionWithException() throws Throwable {
public void testGetOrThrowRuntimeExceptionWithException() {
Exceptional
.of(ioExceptionSupplier)
.getOrThrowRuntimeException();
}

@Test
public void testGetOrThrowNewExceptionWithoutException() throws Throwable {
public void testGetOrThrowNewExceptionWithoutException() {
int value = Exceptional
.of(tenSupplier)
.getOrThrow(new ArithmeticException());
assertEquals(10, value);
}

@Test(expected = ArithmeticException.class)
public void testGetOrThrowNewExceptionWithException() throws Throwable {
public void testGetOrThrowNewExceptionWithException() {
Exceptional
.of(ioExceptionSupplier)
.getOrThrow(new ArithmeticException());
Expand Down Expand Up @@ -192,7 +195,7 @@ public Exceptional<Integer> apply(Exceptional<Integer> exceptional) {
return exceptional
.map(new ThrowableFunction<Integer, Integer, Throwable>() {
@Override
public Integer apply(Integer integer) throws Throwable {
public Integer apply(Integer integer) {
return integer + 1;
}
});
Expand All @@ -217,7 +220,7 @@ public void testCustomTerminal() {
public Integer apply(Exceptional<Integer> exceptional) {
return exceptional.map(new ThrowableFunction<Integer, Integer, Throwable>() {
@Override
public Integer apply(Integer integer) throws Throwable {
public Integer apply(Integer integer) {
return integer + 1;
}
}).getOrElse(0);
Expand All @@ -239,7 +242,7 @@ public void testMapWithoutException() {
.of(tenSupplier)
.map(new ThrowableFunction<Integer, String, Throwable>() {
@Override
public String apply(Integer value) throws Throwable {
public String apply(Integer value) {
return Integer.toString(value);
}
})
Expand All @@ -253,7 +256,7 @@ public void testMapWithException() throws Throwable {
.of(tenSupplier)
.map(new ThrowableFunction<Integer, String, Throwable>() {
@Override
public String apply(Integer value) throws Throwable {
public String apply(Integer value) {
throw new NumberFormatException();
}
})
Expand All @@ -274,7 +277,7 @@ public void testMapOnAlreadyFailedExceptional() throws Throwable {
.of(ioExceptionSupplier)
.map(new ThrowableFunction<Integer, String, Throwable>() {
@Override
public String apply(Integer value) throws Throwable {
public String apply(Integer value) {
return Integer.toString(value);
}
})
Expand Down Expand Up @@ -432,7 +435,7 @@ public Integer apply(Throwable throwable) throws IOException {
})
.recover(new ThrowableFunction<Throwable, Integer, Throwable>() {
@Override
public Integer apply(Throwable throwable) throws IOException {
public Integer apply(Throwable throwable) {
assertThat(throwable, instanceOf(FileNotFoundException.class));
return 10;
}
Expand Down Expand Up @@ -460,7 +463,7 @@ public void testRecoverWithOnNormalState() {
int value = Exceptional
.of(new ThrowableSupplier<Integer, Throwable>() {
@Override
public Integer get() throws Throwable {
public Integer get() {
return 42;
}
})
Expand Down Expand Up @@ -513,6 +516,7 @@ public void testEqualsTransitive() {
assertEquals(ten1, ten3);
}

@SuppressWarnings({"EqualsBetweenInconvertibleTypes", "SimplifiableAssertion"})
@Test
public void testEqualsWithDifferentTypes() {
final Exceptional<Integer> ten1 = Exceptional.of(tenSupplier);
Expand All @@ -524,7 +528,7 @@ public void testEqualsWithDifferentNumberTypes() {
final Exceptional<Integer> ten1 = Exceptional.of(tenSupplier);
final Exceptional<Byte> tenByte = Exceptional.of(new ThrowableSupplier<Byte, Throwable>() {
@Override
public Byte get() throws Throwable {
public Byte get() {
return (byte) 10;
}
});
Expand Down Expand Up @@ -570,7 +574,7 @@ public void testHashCodeWithSameObject() {
public void testHashCodeWithDifferentGenericType() {
final Exceptional<Byte> tenByte = Exceptional.of(new ThrowableSupplier<Byte, Throwable>() {
@Override
public Byte get() throws Throwable {
public Byte get() {
return (byte) 10;
}
});
Expand All @@ -592,7 +596,7 @@ public void testToStringWithException() {
private static final ThrowableSupplier<Integer, Throwable> tenSupplier
= new ThrowableSupplier<Integer, Throwable>() {
@Override
public Integer get() throws IOException {
public Integer get() {
return 10;
}
};
Expand Down
1 change: 0 additions & 1 deletion stream/src/test/java/com/annimon/stream/Functions.java
@@ -1,7 +1,6 @@
package com.annimon.stream;

import com.annimon.stream.function.*;

import java.lang.reflect.Array;
import java.util.Comparator;
import java.util.Iterator;
Expand Down
4 changes: 3 additions & 1 deletion stream/src/test/java/com/annimon/stream/IntPairTest.java
@@ -1,7 +1,9 @@
package com.annimon.stream;

import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;

public final class IntPairTest {

Expand Down

0 comments on commit 171073f

Please sign in to comment.