Commits

DerFrZocker authored and md_5 committed 07abf68520e
#1514: Add a test case for ClassTraverser
No tags

src/test/java/org/bukkit/craftbukkit/util/ClassTraverserTest.java

Added
1 +package org.bukkit.craftbukkit.util;
2 +
3 +import static org.junit.jupiter.api.Assertions.*;
4 +import java.io.Serializable;
5 +import java.lang.annotation.Annotation;
6 +import java.lang.constant.Constable;
7 +import java.util.ArrayList;
8 +import java.util.List;
9 +import java.util.stream.Stream;
10 +import org.bukkit.support.environment.Normal;
11 +import org.junit.jupiter.params.ParameterizedTest;
12 +import org.junit.jupiter.params.provider.Arguments;
13 +import org.junit.jupiter.params.provider.MethodSource;
14 +
15 +@Normal
16 +public class ClassTraverserTest {
17 +
18 + public static Stream<Arguments> data() {
19 + return Stream.of(
20 + Arguments.of(FirstTest.class, List.of(
21 + FirstTest.class,
22 + Object.class
23 + )),
24 + Arguments.of(SecondTest.class, List.of(
25 + SecondTest.class,
26 + SecondTestClass.class,
27 + SecondTestInterface.class,
28 + SecondTestInterface2.class,
29 + Object.class
30 + )),
31 + Arguments.of(ThirdTestInterface.class, List.of(
32 + ThirdTestInterface.class,
33 + ThirdTestInterface2.class
34 + )),
35 + Arguments.of(FourthTest.class, List.of(
36 + FourthTest.class,
37 + FourthTestInterface.class,
38 + Record.class,
39 + Object.class
40 + )),
41 + Arguments.of(FifthTest.class, List.of(
42 + FifthTest.class,
43 + Enum.class,
44 + Constable.class,
45 + Comparable.class,
46 + Serializable.class,
47 + Object.class
48 + )),
49 + Arguments.of(SixthTest.class, List.of(
50 + SixthTest.class,
51 + Annotation.class
52 + ))
53 + );
54 + }
55 +
56 + @ParameterizedTest
57 + @MethodSource("data")
58 + public void test(Class<?> clazz, List<Class<?>> expected) {
59 + ClassTraverser classTraverser = new ClassTraverser(clazz);
60 + List<Class<?>> result = new ArrayList<>();
61 +
62 + while (classTraverser.hasNext()) {
63 + result.add(classTraverser.next());
64 + }
65 +
66 + assertEquals(expected.size(), result.size(), String.format("""
67 + Expected classes:
68 + %s
69 +
70 + Result:
71 + %s
72 + """, expected, result));
73 +
74 + for (Class<?> got : expected) {
75 + assertTrue(result.contains(got), String.format("""
76 + Result classes:
77 + %s
78 +
79 + Does not contain class:
80 + %s
81 + """, result, got));
82 + }
83 + }
84 +
85 + // First Test
86 + private class FirstTest {
87 + }
88 +
89 + // Second Test
90 + private class SecondTest extends SecondTestClass implements SecondTestInterface {
91 + }
92 +
93 + private class SecondTestClass implements SecondTestInterface, SecondTestInterface2 {
94 + }
95 +
96 + private interface SecondTestInterface extends SecondTestInterface2 {
97 + }
98 +
99 + private interface SecondTestInterface2 {
100 + }
101 +
102 + // Third Test
103 + private interface ThirdTestInterface extends ThirdTestInterface2 {
104 + }
105 +
106 + private interface ThirdTestInterface2 {
107 + }
108 +
109 + // Fourth Test
110 + private record FourthTest() implements FourthTestInterface {
111 + }
112 +
113 + private interface FourthTestInterface {
114 + }
115 +
116 + // Fifth Test
117 + private enum FifthTest {
118 + }
119 +
120 + // Sixth Test
121 + private @interface SixthTest {
122 + }
123 +}

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut