Skip to content

Commit b76e42b

Browse files
authored
#86 test for number classes (#110)
* #86 Add test to confirm all JDK Number types are covered * #86 JDK Number types test: Change to org.reflections - Change from Guava to org.reflections, in hopes of making the test work also for the GitHub Action builds
1 parent e50e8d1 commit b76e42b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@
167167
<scope>provided</scope>
168168
</dependency>
169169

170+
<dependency>
171+
<groupId>org.reflections</groupId>
172+
<artifactId>reflections</artifactId>
173+
<version>0.10.2</version>
174+
<scope>test</scope>
175+
</dependency>
170176
<dependency>
171177
<groupId>org.junit.jupiter</groupId>
172178
<artifactId>junit-jupiter</artifactId>

src/test/java/ch/jalu/typeresolver/numbers/NumberTypesTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package ch.jalu.typeresolver.numbers;
22

3+
import org.junit.jupiter.api.Disabled;
34
import org.junit.jupiter.api.Test;
5+
import org.reflections.Reflections;
6+
import org.reflections.scanners.Scanners;
47

58
import java.lang.reflect.Field;
69
import java.lang.reflect.Modifier;
710
import java.math.BigDecimal;
11+
import java.util.Arrays;
812
import java.util.Collections;
913
import java.util.EnumSet;
1014
import java.util.HashSet;
@@ -77,6 +81,43 @@ void shouldUnwrapObjectToStandardNumberType() {
7781
assertThat(NumberTypes.unwrapToStandardNumberType(Collections.emptyList()), nullValue());
7882
}
7983

84+
/**
85+
* If this test fails, it means there are new Number classes in the JDK that should be addressed; the Javadoc
86+
* in {@link NumberType} guarantees that certain methods can deal with any JDK Number type.
87+
*/
88+
@Disabled // Seems impossible to get JDK classes via Maven, so this can only be run locally
89+
@Test
90+
void shouldSupportAllJdkNumberTypes() {
91+
// given
92+
Reflections reflections = new Reflections("java", Scanners.SubTypes);
93+
// If no classes are found, you can try passing in a custom class loader into Reflections,
94+
// though note that this has also not made it possible to fix this test to run correctly in Maven
95+
96+
// when
97+
Set<String> numberClasses = reflections.getSubTypesOf(Number.class).stream()
98+
.filter(clz -> !Modifier.isAbstract(clz.getModifiers()))
99+
.map(Class::getName)
100+
.collect(Collectors.toSet());
101+
102+
// then
103+
Set<String> expectedNumberClasses = new HashSet<>(Arrays.asList(
104+
"java.lang.Byte",
105+
"java.lang.Short",
106+
"java.lang.Integer",
107+
"java.lang.Long",
108+
"java.lang.Float",
109+
"java.lang.Double",
110+
"java.math.BigInteger",
111+
"java.math.BigDecimal",
112+
"java.util.concurrent.atomic.AtomicInteger",
113+
"java.util.concurrent.atomic.AtomicLong",
114+
"java.util.concurrent.atomic.DoubleAccumulator",
115+
"java.util.concurrent.atomic.DoubleAdder",
116+
"java.util.concurrent.atomic.LongAccumulator",
117+
"java.util.concurrent.atomic.LongAdder"));
118+
assertThat(numberClasses, equalTo(expectedNumberClasses));
119+
}
120+
80121
@Test
81122
void shouldStreamThroughAllNumberTypeInstances() {
82123
// given
@@ -108,6 +149,7 @@ void shouldReturnNumberTypeForClass() {
108149
assertThat(NumberTypes.from(Byte.class), equalTo(StandardNumberType.BYTE));
109150
assertThat(NumberTypes.from(long.class), equalTo(StandardNumberType.LONG));
110151
assertThat(NumberTypes.from(Long.class), equalTo(StandardNumberType.LONG));
152+
assertThat(NumberTypes.from(char.class), equalTo(MoreNumberTypes.CHARACTER));
111153
assertThat(NumberTypes.from(Character.class), equalTo(MoreNumberTypes.CHARACTER));
112154
assertThat(NumberTypes.from(AtomicInteger.class), equalTo(MoreNumberTypes.ATOMIC_INTEGER));
113155
assertThat(NumberTypes.from(AtomicLong.class), equalTo(MoreNumberTypes.ATOMIC_LONG));

0 commit comments

Comments
 (0)