Skip to content

Commit 8d6cda4

Browse files
committed
Remove annotation usage and align code to the upstream
1 parent 866cdb7 commit 8d6cda4

File tree

8 files changed

+96
-67
lines changed

8 files changed

+96
-67
lines changed

src/main/java/net/minestom/testing/Collector.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
import java.util.List;
66
import java.util.function.Consumer;
7+
import java.util.function.Predicate;
78

89
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertFalse;
911
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
1013

1114
/**
1215
* A utility interface for collecting and asserting on collections of elements in tests.
@@ -36,11 +39,12 @@ public interface Collector<T> {
3639
* @param consumer the consumer to apply to the single element
3740
* @param <P> the type of the single element
3841
*/
39-
default <P extends T> void assertSingle(@NotNull Class<P> type, @NotNull Consumer<P> consumer) {
42+
default <P extends T> void assertSingle(Class<P> type, Consumer<P> consumer) {
4043
List<T> elements = collect();
4144
assertEquals(1, elements.size(), "Expected 1 element, got " + elements);
42-
T element = elements.getFirst();
45+
var element = elements.getFirst();
4346
assertInstanceOf(type, element, "Expected type " + type.getSimpleName() + ", got " + element.getClass().getSimpleName());
47+
//noinspection unchecked
4448
consumer.accept((P) element);
4549
}
4650

@@ -49,7 +53,7 @@ default <P extends T> void assertSingle(@NotNull Class<P> type, @NotNull Consume
4953
*
5054
* @param consumer the consumer to apply to the single element
5155
*/
52-
default void assertSingle(@NotNull Consumer<T> consumer) {
56+
default void assertSingle(Consumer<T> consumer) {
5357
List<T> elements = collect();
5458
assertEquals(1, elements.size(), "Expected 1 element, got " + elements);
5559
consumer.accept(elements.getFirst());
@@ -78,4 +82,31 @@ default void assertSingle() {
7882
default void assertEmpty() {
7983
assertCount(0);
8084
}
85+
86+
/**
87+
* Asserts that at least one element matches the given predicate.
88+
*/
89+
default void assertAnyMatch(Predicate<T> predicate) {
90+
List<T> elements = collect();
91+
assertTrue(elements.stream().anyMatch(predicate),
92+
"No elements matched the predicate. Elements: " + elements);
93+
}
94+
95+
/**
96+
* Asserts that no elements match the given predicate.
97+
*/
98+
default void assertNoneMatch(Predicate<T> predicate) {
99+
List<T> elements = collect();
100+
assertFalse(elements.stream().anyMatch(predicate),
101+
"Found elements that matched the predicate: " + elements.stream().filter(predicate).toList());
102+
}
103+
104+
/**
105+
* Asserts that all elements match the given predicate.
106+
*/
107+
default void assertAllMatch(Predicate<T> predicate) {
108+
List<T> elements = collect();
109+
assertTrue(elements.stream().allMatch(predicate),
110+
"Not all elements matched the predicate. Elements: " + elements);
111+
}
81112
}

src/main/java/net/minestom/testing/Env.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import net.minestom.server.instance.IChunkLoader;
99
import net.minestom.server.instance.Instance;
1010
import net.minestom.server.instance.block.Block;
11+
import net.minestom.server.network.player.GameProfile;
1112
import org.jetbrains.annotations.Contract;
1213
import org.jetbrains.annotations.NotNull;
1314
import org.jetbrains.annotations.Nullable;
1415

1516
import java.time.Duration;
17+
import java.util.UUID;
1618
import java.util.function.BooleanSupplier;
1719

1820
/**
@@ -46,7 +48,7 @@ public interface Env {
4648
* @return a new instance of {@link Env}
4749
*/
4850
@Contract(value = "_ -> new", pure = true)
49-
static @NotNull Env createInstance(@NotNull ServerProcess process) {
51+
static Env createInstance(ServerProcess process) {
5052
return new EnvImpl(process);
5153
}
5254

@@ -55,14 +57,24 @@ public interface Env {
5557
*
5658
* @return the server process
5759
*/
58-
@NotNull ServerProcess process();
60+
ServerProcess process();
5961

6062
/**
6163
* Creates a new {@link TestConnection} which can be used in the test environment.
6264
*
6365
* @return the created connection
6466
*/
65-
@NotNull TestConnection createConnection();
67+
default TestConnection createConnection() {
68+
return createConnection(new GameProfile(UUID.randomUUID(), "RandName"));
69+
}
70+
71+
/**
72+
* Creates a new {@link TestConnection} which can be used in the test environment.
73+
*
74+
* @param gameProfile the game profile to use for the connection
75+
* @return the created connection
76+
*/
77+
TestConnection createConnection(GameProfile gameProfile);
6678

6779
/**
6880
* Tracks a specific event type in the test environment.
@@ -74,7 +86,7 @@ public interface Env {
7486
* @param <H> the handler type
7587
* @return the {@link Collector} instance to use
7688
*/
77-
<E extends Event, H> @NotNull Collector<E> trackEvent(@NotNull Class<E> eventType, @NotNull EventFilter<? super E, H> filter, @NotNull H actor);
89+
<E extends Event, H> Collector<E> trackEvent(Class<E> eventType, EventFilter<? super E, H> filter, @NotNull H actor);
7890

7991
/**
8092
* Listen for a specific event type in the test environment.
@@ -83,7 +95,7 @@ public interface Env {
8395
* @param <E> the event type
8496
* @return the {@link FlexibleListener} instance to use
8597
*/
86-
<E extends Event> @NotNull FlexibleListener<E> listen(@NotNull Class<E> eventType);
98+
<E extends Event> FlexibleListener<E> listen(Class<E> eventType);
8799

88100
/**
89101
* Ticks the {@link ServerProcess} which is involved into the env instance.

src/main/java/net/minestom/testing/EnvImpl.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.minestom.server.event.Event;
55
import net.minestom.server.event.EventFilter;
66
import net.minestom.server.event.EventListener;
7+
import net.minestom.server.network.player.GameProfile;
78
import org.jetbrains.annotations.NotNull;
89

910
import java.util.List;
@@ -13,7 +14,7 @@
1314
import static org.junit.jupiter.api.Assertions.assertTrue;
1415
import static org.junit.jupiter.api.Assertions.fail;
1516

16-
final class EnvImpl implements Env {
17+
public final class EnvImpl implements Env {
1718
private final ServerProcess process;
1819
private final List<FlexibleListenerImpl<?>> listeners = new CopyOnWriteArrayList<>();
1920

@@ -29,24 +30,24 @@ public EnvImpl(ServerProcess process) {
2930
}
3031

3132
@Override
32-
public @NotNull ServerProcess process() {
33+
public ServerProcess process() {
3334
return process;
3435
}
3536

3637
@Override
37-
public @NotNull TestConnection createConnection() {
38-
return new TestConnectionImpl(this);
38+
public TestConnection createConnection(GameProfile profile) {
39+
return new TestConnectionImpl(this, profile);
3940
}
4041

4142
@Override
42-
public @NotNull <E extends Event, H> Collector<E> trackEvent(@NotNull Class<E> eventType, @NotNull EventFilter<? super E, H> filter, @NotNull H actor) {
43+
public <E extends Event, H> Collector<E> trackEvent(Class<E> eventType, EventFilter<? super E, H> filter, @NotNull H actor) {
4344
var tracker = new EventCollector<E>(actor);
4445
this.process.eventHandler().map(actor, filter).addListener(eventType, tracker.events::add);
4546
return tracker;
4647
}
4748

4849
@Override
49-
public @NotNull <E extends Event> FlexibleListener<E> listen(@NotNull Class<E> eventType) {
50+
public <E extends Event> FlexibleListener<E> listen(Class<E> eventType) {
5051
var handler = process.eventHandler();
5152
var flexible = new FlexibleListenerImpl<>(eventType);
5253
var listener = EventListener.of(eventType, e -> flexible.handler.accept(e));
@@ -70,7 +71,7 @@ public EventCollector(Object handler) {
7071
}
7172

7273
@Override
73-
public @NotNull List<E> collect() {
74+
public List<E> collect() {
7475
process.eventHandler().unmap(handler);
7576
return List.copyOf(events);
7677
}
@@ -88,7 +89,7 @@ static final class FlexibleListenerImpl<E extends Event> implements FlexibleList
8889
}
8990

9091
@Override
91-
public void followup(@NotNull Consumer<E> handler) {
92+
public void followup(Consumer<E> handler) {
9293
updateHandler(handler);
9394
}
9495

@@ -97,7 +98,7 @@ public void failFollowup() {
9798
updateHandler(e -> fail("Event " + e.getClass().getSimpleName() + " was not expected"));
9899
}
99100

100-
void updateHandler(@NotNull Consumer<E> handler) {
101+
void updateHandler(Consumer<E> handler) {
101102
check();
102103
this.initialized = true;
103104
this.called = false;

src/main/java/net/minestom/testing/FlexibleListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.minestom.testing;
22

33
import net.minestom.server.event.Event;
4-
import org.jetbrains.annotations.NotNull;
54

65
import java.util.function.Consumer;
76

@@ -21,7 +20,7 @@ public interface FlexibleListener<E extends Event> {
2120
*
2221
* @param handler the consumer to handle the next event
2322
*/
24-
void followup(@NotNull Consumer<E> handler);
23+
void followup( Consumer<E> handler);
2524

2625
/**
2726
* Empty followup handler.

src/main/java/net/minestom/testing/TestConnection.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.minestom.server.entity.Player;
55
import net.minestom.server.instance.Instance;
66
import net.minestom.server.network.packet.server.ServerPacket;
7-
import org.jetbrains.annotations.NotNull;
87

98
/**
109
* The {@link TestConnection} represents a connection from a player to a test server instance.
@@ -22,15 +21,15 @@ public interface TestConnection {
2221
* @param pos the position to connect at
2322
* @return the connected player
2423
*/
25-
@NotNull Player connect(@NotNull Instance instance, @NotNull Pos pos);
24+
Player connect(Instance instance, Pos pos);
2625

2726
/**
2827
* Connects a player to the given instance at the default position (0, 0, 0).
2928
*
3029
* @param instance the instance to connect to
3130
* @return the connected player
3231
*/
33-
default @NotNull Player connect(@NotNull Instance instance) {
32+
default Player connect(Instance instance) {
3433
return connect(instance, Pos.ZERO);
3534
}
3635

@@ -41,14 +40,14 @@ public interface TestConnection {
4140
* @param <T> the type of the packet
4241
* @return a collector for the specified packet type
4342
*/
44-
<T extends ServerPacket> @NotNull Collector<T> trackIncoming(@NotNull Class<T> type);
43+
<T extends ServerPacket> Collector<T> trackIncoming(Class<T> type);
4544

4645
/**
4746
* Tracks incoming packets of the default type {@link ServerPacket}.
4847
*
4948
* @return a collector for the default packet type
5049
*/
51-
default @NotNull Collector<ServerPacket> trackIncoming() {
50+
default Collector<ServerPacket> trackIncoming() {
5251
return trackIncoming(ServerPacket.class);
5352
}
5453
}

src/main/java/net/minestom/testing/TestConnectionImpl.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,36 @@
1212
import net.minestom.server.network.packet.server.ServerPacket;
1313
import net.minestom.server.network.player.GameProfile;
1414
import net.minestom.server.network.player.PlayerConnection;
15-
import org.jetbrains.annotations.NotNull;
1615

1716
import java.net.InetSocketAddress;
1817
import java.net.SocketAddress;
1918
import java.util.List;
2019
import java.util.Objects;
21-
import java.util.UUID;
2220
import java.util.concurrent.CompletableFuture;
2321
import java.util.concurrent.CopyOnWriteArrayList;
2422
import java.util.concurrent.atomic.AtomicBoolean;
2523

2624
final class TestConnectionImpl implements TestConnection {
27-
private final Env env;
25+
2826
private final ServerProcess process;
27+
private final GameProfile gameProfile;
2928
private final PlayerConnectionImpl playerConnection = new PlayerConnectionImpl();
3029

3130
private final AtomicBoolean connected = new AtomicBoolean(false);
3231

3332
private final List<IncomingCollector<ServerPacket>> incomingTrackers = new CopyOnWriteArrayList<>();
3433

35-
TestConnectionImpl(Env env) {
36-
this.env = env;
34+
TestConnectionImpl(Env env, GameProfile gameProfile) {
3735
this.process = env.process();
36+
this.gameProfile = gameProfile;
3837
}
3938

4039
@Override
41-
public @NotNull Player connect(@NotNull Instance instance, @NotNull Pos pos) {
40+
public Player connect(Instance instance, Pos pos) {
4241
if (!connected.compareAndSet(false, true)) {
4342
throw new IllegalStateException("Already connected");
4443
}
4544

46-
final GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "RandName");
4745
var player = process.connection().createPlayer(playerConnection, gameProfile);
4846
player.eventNode().addListener(AsyncPlayerConfigurationEvent.class, event -> {
4947
event.setSpawningInstance(instance);
@@ -67,7 +65,7 @@ final class TestConnectionImpl implements TestConnection {
6765
}
6866

6967
@Override
70-
public @NotNull <T extends ServerPacket> Collector<T> trackIncoming(@NotNull Class<T> type) {
68+
public <T extends ServerPacket> Collector<T> trackIncoming(Class<T> type) {
7169
var tracker = new IncomingCollector<>(type);
7270
this.incomingTrackers.add(IncomingCollector.class.cast(tracker));
7371
return tracker;
@@ -77,7 +75,7 @@ final class PlayerConnectionImpl extends PlayerConnection {
7775
private boolean online = true;
7876

7977
@Override
80-
public void sendPacket(@NotNull SendablePacket packet) {
78+
public void sendPacket(SendablePacket packet) {
8179
final var serverPacket = this.extractPacket(packet);
8280
for (var tracker : incomingTrackers) {
8381
if (tracker.type.isAssignableFrom(serverPacket.getClass())) tracker.packets.add(serverPacket);
@@ -100,7 +98,7 @@ private ServerPacket extractPacket(final SendablePacket packet) {
10098
}
10199

102100
@Override
103-
public @NotNull SocketAddress getRemoteAddress() {
101+
public SocketAddress getRemoteAddress() {
104102
return new InetSocketAddress("localhost", 25565);
105103
}
106104

@@ -124,7 +122,7 @@ public IncomingCollector(Class<T> type) {
124122
}
125123

126124
@Override
127-
public @NotNull List<T> collect() {
125+
public List<T> collect() {
128126
incomingTrackers.remove(this);
129127
return List.copyOf(packets);
130128
}

src/main/java/net/minestom/testing/TestPlayerImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import net.minestom.server.instance.Chunk;
55
import net.minestom.server.network.player.GameProfile;
66
import net.minestom.server.network.player.PlayerConnection;
7-
import org.jetbrains.annotations.NotNull;
87

98
/**
109
* The test environment can't really use a real player for the test.
@@ -21,7 +20,7 @@ public class TestPlayerImpl extends Player {
2120
* @param playerConnection the player's connection
2221
* @param gameProfile the player's game profile
2322
*/
24-
public TestPlayerImpl(@NotNull PlayerConnection playerConnection, @NotNull GameProfile gameProfile) {
23+
public TestPlayerImpl(PlayerConnection playerConnection, GameProfile gameProfile) {
2524
super(playerConnection, gameProfile);
2625
}
2726

@@ -31,7 +30,7 @@ public TestPlayerImpl(@NotNull PlayerConnection playerConnection, @NotNull GameP
3130
* @param chunk the chunk to send
3231
*/
3332
@Override
34-
public void sendChunk(@NotNull Chunk chunk) {
33+
public void sendChunk(Chunk chunk) {
3534
// Send immediately
3635
sendPacket(chunk.getFullDataPacket());
3736
}

0 commit comments

Comments
 (0)