Skip to content

Commit fb7f426

Browse files
committed
Basic unit test for DamageEntityEvent
1 parent 1c2c7af commit fb7f426

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.event.entity;
26+
27+
import static org.junit.jupiter.api.Assertions.*;
28+
29+
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
30+
import org.junit.jupiter.api.BeforeEach;
31+
import org.junit.jupiter.api.Test;
32+
import org.mockito.Mockito;
33+
import org.spongepowered.api.Sponge;
34+
import org.spongepowered.api.entity.Entity;
35+
import org.spongepowered.api.entity.EntityTypes;
36+
import org.spongepowered.api.event.EventManager;
37+
import org.spongepowered.api.event.Listener;
38+
import org.spongepowered.api.event.cause.entity.damage.DamageTypes;
39+
import org.spongepowered.api.event.cause.entity.damage.source.DamageSource;
40+
import org.spongepowered.api.event.entity.DamageEntityEvent;
41+
import org.spongepowered.api.world.DefaultWorldKeys;
42+
import org.spongepowered.api.world.server.ServerLocation;
43+
import org.spongepowered.math.vector.Vector3d;
44+
import org.spongepowered.plugin.PluginContainer;
45+
46+
public class DamageEventTest {
47+
private ServerLocation location;
48+
49+
@BeforeEach
50+
public void prepare() {
51+
this.location = Sponge.server().worldManager().world(DefaultWorldKeys.DEFAULT).get().location(Vector3d.ZERO);
52+
}
53+
54+
@Test
55+
public void testDamageEvent() {
56+
final EventManager eventManager = Sponge.eventManager();
57+
final PluginContainer plugin = Mockito.mock(PluginContainer.class);
58+
final DamageEventListener listener = new DamageEventListener();
59+
final Entity sheep = this.location.createEntity(EntityTypes.SHEEP.get());
60+
final DamageSource damageSource = DamageSource.builder().type(DamageTypes.CACTUS).build();
61+
62+
eventManager.registerListeners(plugin, listener);
63+
64+
sheep.damage(15, damageSource);
65+
66+
assertEquals(1, listener.events, "events count");
67+
assertNotNull(listener.lastEvent, "last event");
68+
assertEquals(15, listener.lastEvent.baseDamage(), "base damage");
69+
assertEquals(damageSource, listener.lastEvent.cause().root(), "root cause");
70+
71+
eventManager.unregisterListeners(listener);
72+
}
73+
74+
private static class DamageEventListener {
75+
@MonotonicNonNull DamageEntityEvent lastEvent;
76+
int events = 0;
77+
78+
@Listener
79+
public void onEvent(final DamageEntityEvent event) {
80+
this.events++;
81+
this.lastEvent = event;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)