Skip to content

Commit 567c04a

Browse files
committed
Added tests for commands
1 parent 96f0ec1 commit 567c04a

File tree

5 files changed

+466
-8
lines changed

5 files changed

+466
-8
lines changed

src/main/java/world/bentobox/aoneblock/commands/island/IslandRespawnBlockCommand.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package world.bentobox.aoneblock.commands.island;
22

33
import java.util.List;
4+
import java.util.Objects;
45

56
import org.bukkit.Bukkit;
67
import org.bukkit.Material;
@@ -67,14 +68,10 @@ public boolean canExecute(User user, String label, List<String> args)
6768
public boolean execute(User user, String label, List<String> args)
6869
{
6970
Island island = this.getIslands().getIsland(this.getWorld(), user);
70-
71-
if (island == null)
72-
{
73-
// Hmm, lost island so fast. Well, no, just idea null-pointer check bypass.
74-
user.sendMessage("general.errors.no-island");
75-
}
76-
else if (Material.BEDROCK.equals(island.getCenter().getBlock().getType()) ||
77-
Material.AIR.equals(island.getCenter().getBlock().getType()))
71+
Objects.requireNonNull(island);
72+
73+
if (island.getCenter().getBlock().getType() == Material.BEDROCK ||
74+
island.getCenter().getBlock().getType() == Material.AIR)
7875
{
7976
// Trigger manual block break event.
8077
Bukkit.getServer().getPluginManager().callEvent(
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package world.bentobox.aoneblock.commands.island;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.eq;
8+
import static org.mockito.Mockito.verify;
9+
import static org.mockito.Mockito.when;
10+
11+
import java.util.List;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
import org.mockito.Mock;
17+
18+
import world.bentobox.aoneblock.AOneBlock;
19+
import world.bentobox.aoneblock.CommonTestSetup;
20+
import world.bentobox.aoneblock.Settings;
21+
import world.bentobox.bentobox.api.commands.CompositeCommand;
22+
import world.bentobox.bentobox.api.user.User;
23+
24+
/**
25+
* Test class
26+
*/
27+
class IslandActionBarCommandTest extends CommonTestSetup {
28+
29+
@Mock
30+
private CompositeCommand ac;
31+
32+
@Mock
33+
private User user;
34+
@Mock
35+
private AOneBlock addon;
36+
37+
private IslandActionBarCommand abc;
38+
39+
40+
/**
41+
* @throws java.lang.Exception
42+
*/
43+
@Override
44+
@BeforeEach
45+
public void setUp() throws Exception {
46+
super.setUp();
47+
48+
// Settings
49+
Settings settings = new Settings();
50+
when(addon.getSettings()).thenReturn(settings);
51+
52+
abc = new IslandActionBarCommand(ac, settings.getActionBarCommand().split(" ")[0],
53+
settings.getActionBarCommand().split(" "));
54+
}
55+
56+
/**
57+
* @throws java.lang.Exception
58+
*/
59+
@Override
60+
@AfterEach
61+
public void tearDown() throws Exception {
62+
super.tearDown();
63+
}
64+
65+
/**
66+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandActionBarCommand#IslandActionBarCommand(world.bentobox.bentobox.api.commands.CompositeCommand, java.lang.String, java.lang.String[])}.
67+
*/
68+
@Test
69+
void testIslandActionBarCommand() {
70+
assertNotNull(abc);
71+
}
72+
73+
/**
74+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandActionBarCommand#setup()}.
75+
*/
76+
@Test
77+
void testSetup() {
78+
assertEquals("island.actionbar", abc.getPermission());
79+
assertEquals("aoneblock.commands.island.actionbar.description", abc.getDescription());
80+
assertTrue(abc.isOnlyPlayer());
81+
}
82+
83+
/**
84+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandActionBarCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
85+
*/
86+
@Test
87+
void testExecuteUserStringListOfString() {
88+
abc.execute(user, "", List.of());
89+
verify(user).getMetaData("aoneblock.actionbar");
90+
verify(user).putMetaData(eq("aoneblock.actionbar"), any());
91+
verify(user).sendMessage("aoneblock.commands.island.actionbar.status_off");
92+
}
93+
94+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package world.bentobox.aoneblock.commands.island;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.eq;
8+
import static org.mockito.Mockito.verify;
9+
import static org.mockito.Mockito.when;
10+
11+
import java.util.List;
12+
import java.util.Optional;
13+
14+
import org.junit.jupiter.api.AfterEach;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.mockito.Mock;
18+
19+
import world.bentobox.aoneblock.AOneBlock;
20+
import world.bentobox.aoneblock.CommonTestSetup;
21+
import world.bentobox.aoneblock.Settings;
22+
import world.bentobox.aoneblock.listeners.BossBarListener;
23+
import world.bentobox.bentobox.api.commands.CompositeCommand;
24+
import world.bentobox.bentobox.api.user.User;
25+
26+
/**
27+
* Test class
28+
*/
29+
class IslandBossBarCommandTest extends CommonTestSetup {
30+
31+
@Mock
32+
private CompositeCommand ac;
33+
34+
@Mock
35+
private User user;
36+
@Mock
37+
private AOneBlock addon;
38+
39+
private IslandBossBarCommand bbc;
40+
41+
@Mock
42+
private BossBarListener bb;
43+
44+
45+
/**
46+
* @throws java.lang.Exception
47+
*/
48+
@Override
49+
@BeforeEach
50+
public void setUp() throws Exception {
51+
super.setUp();
52+
53+
when(ac.getAddon()).thenReturn(addon);
54+
55+
when(addon.getBossBar()).thenReturn(bb);
56+
57+
when(user.getLocation()).thenReturn(location);
58+
59+
when(im.getIslandAt(location)).thenReturn(Optional.of(island));
60+
61+
// Settings
62+
Settings settings = new Settings();
63+
when(addon.getSettings()).thenReturn(settings);
64+
65+
bbc = new IslandBossBarCommand(ac, settings.getBossBarCommand().split(" ")[0],
66+
settings.getBossBarCommand().split(" "));
67+
}
68+
69+
/**
70+
* @throws java.lang.Exception
71+
*/
72+
@Override
73+
@AfterEach
74+
public void tearDown() throws Exception {
75+
super.tearDown();
76+
}
77+
78+
/**
79+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandBossBarCommand#IslandBossBarCommand(world.bentobox.bentobox.api.commands.CompositeCommand, java.lang.String, java.lang.String[])}.
80+
*/
81+
@Test
82+
void testIslandBossBarCommand() {
83+
assertNotNull(bbc);
84+
}
85+
86+
/**
87+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandBossBarCommand#setup()}.
88+
*/
89+
@Test
90+
void testSetup() {
91+
assertEquals("island.bossbar", bbc.getPermission());
92+
assertEquals("aoneblock.commands.island.bossbar.description", bbc.getDescription());
93+
assertTrue(bbc.isOnlyPlayer());
94+
}
95+
96+
/**
97+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandBossBarCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
98+
*/
99+
@Test
100+
void testExecuteUserStringListOfString() {
101+
bbc.execute(user, "", List.of());
102+
verify(bb).toggleUser(user);
103+
verify(user).sendMessage("aoneblock.bossbar.not-active");
104+
}
105+
106+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package world.bentobox.aoneblock.commands.island;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
9+
import java.util.List;
10+
import java.util.TreeMap;
11+
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import org.mockito.Mock;
16+
17+
import world.bentobox.aoneblock.AOneBlock;
18+
import world.bentobox.aoneblock.CommonTestSetup;
19+
import world.bentobox.aoneblock.Settings;
20+
import world.bentobox.aoneblock.oneblocks.OneBlocksManager;
21+
import world.bentobox.bentobox.api.addons.AddonDescription;
22+
import world.bentobox.bentobox.api.commands.CompositeCommand;
23+
import world.bentobox.bentobox.api.localization.TextVariables;
24+
import world.bentobox.bentobox.api.user.User;
25+
26+
/**
27+
* Test class
28+
*/
29+
class IslandPhasesCommandTest extends CommonTestSetup {
30+
31+
@Mock
32+
private CompositeCommand ac;
33+
@Mock
34+
private User user;
35+
@Mock
36+
private AOneBlock addon;
37+
@Mock
38+
private OneBlocksManager obm;
39+
40+
private IslandPhasesCommand ipc;
41+
42+
/**
43+
* @throws java.lang.Exception
44+
*/
45+
@Override
46+
@BeforeEach
47+
public void setUp() throws Exception {
48+
super.setUp();
49+
when(ac.getAddon()).thenReturn(addon);
50+
when(obm.getBlockProbs()).thenReturn(new TreeMap<>());
51+
when(addon.getOneBlockManager()).thenReturn(obm);
52+
when(addon.getIslandsManager()).thenReturn(im);
53+
AddonDescription desc = new AddonDescription.Builder("name", "", "").build();
54+
when(addon.getDescription()).thenReturn(desc);
55+
56+
// Settings
57+
Settings settings = new Settings();
58+
when(addon.getSettings()).thenReturn(settings);
59+
ipc = new IslandPhasesCommand(ac, settings.getPhasesCommand().split(" ")[0],
60+
settings.getPhasesCommand().split(" "));
61+
}
62+
63+
/**
64+
* @throws java.lang.Exception
65+
*/
66+
@Override
67+
@AfterEach
68+
public void tearDown() throws Exception {
69+
super.tearDown();
70+
}
71+
72+
/**
73+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandPhasesCommand#IslandPhasesCommand(world.bentobox.bentobox.api.commands.CompositeCommand, java.lang.String, java.lang.String[])}.
74+
*/
75+
@Test
76+
void testIslandPhasesCommand() {
77+
assertNotNull(ipc);
78+
}
79+
80+
/**
81+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandPhasesCommand#setup()}.
82+
*/
83+
@Test
84+
void testSetup() {
85+
assertEquals("phases", ipc.getPermission());
86+
assertEquals("aoneblock.commands.phases.description", ipc.getDescription());
87+
assertTrue(ipc.isOnlyPlayer());
88+
}
89+
90+
/**
91+
* Test method for {@link world.bentobox.aoneblock.commands.island.IslandPhasesCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
92+
*/
93+
@Test
94+
void testExecuteUserStringListOfString() {
95+
ipc.execute(user, "", List.of());
96+
verify(addon).logError("There are no available phases for selection!");
97+
verify(user).sendMessage("no-phases",
98+
TextVariables.GAMEMODE, this.addon.getDescription().getName());
99+
}
100+
101+
}

0 commit comments

Comments
 (0)