-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an admin schem list command and test class
Lists available schems for game mode #563
- Loading branch information
1 parent
39c3861
commit 05e5424
Showing
4 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/world/bentobox/bentobox/api/commands/admin/schem/AdminSchemListCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package world.bentobox.bentobox.api.commands.admin.schem; | ||
|
||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import world.bentobox.bentobox.api.commands.CompositeCommand; | ||
import world.bentobox.bentobox.api.user.User; | ||
|
||
public class AdminSchemListCommand extends CompositeCommand { | ||
|
||
public AdminSchemListCommand(AdminSchemCommand parent) { | ||
super(parent, "list"); | ||
} | ||
|
||
|
||
@Override | ||
public void setup() { | ||
setDescription("commands.admin.schem.list.description"); | ||
} | ||
|
||
@Override | ||
public boolean canExecute(User user, String label, List<String> args) { | ||
if (!args.isEmpty()) { | ||
showHelp(this, user); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
@Override | ||
public boolean execute(User user, String label, List<String> args) { | ||
File schems = new File(getAddon().getDataFolder(), "schems"); | ||
if (!schems.exists()) { | ||
user.sendMessage("commands.admin.schem.list.no-schems"); | ||
return false; | ||
} | ||
FilenameFilter schemFilter = (File dir, String name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".schem"); | ||
List<String> schemList = Arrays.stream(Objects.requireNonNull(schems.list(schemFilter))).map(name -> name.substring(0, name.length() - 6)).collect(Collectors.toList()); | ||
if (schemList.isEmpty()) { | ||
user.sendMessage("commands.admin.schem.list.no-schems"); | ||
return false; | ||
} | ||
user.sendMessage("commands.admin.schem.list.available-schems"); | ||
schemList.forEach(user::sendRawMessage); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...test/java/world/bentobox/bentobox/api/commands/admin/schem/AdminSchemListCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* | ||
*/ | ||
package world.bentobox.bentobox.api.commands.admin.schem; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.powermock.reflect.Whitebox; | ||
|
||
import world.bentobox.bentobox.BentoBox; | ||
import world.bentobox.bentobox.Settings; | ||
import world.bentobox.bentobox.api.addons.Addon; | ||
import world.bentobox.bentobox.api.user.User; | ||
import world.bentobox.bentobox.managers.CommandsManager; | ||
import world.bentobox.bentobox.managers.LocalesManager; | ||
|
||
/** | ||
* @author tastybento | ||
* | ||
*/ | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) | ||
public class AdminSchemListCommandTest { | ||
|
||
@Mock | ||
private AdminSchemCommand ac; | ||
@Mock | ||
private Addon addon; | ||
@Mock | ||
private User user; | ||
private AdminSchemListCommand list; | ||
private File dataFolder; | ||
|
||
/** | ||
* @throws java.lang.Exception | ||
*/ | ||
@Before | ||
public void setUp() throws Exception { | ||
// Set up plugin | ||
BentoBox plugin = mock(BentoBox.class); | ||
Whitebox.setInternalState(BentoBox.class, "instance", plugin); | ||
|
||
// Command manager | ||
CommandsManager cm = mock(CommandsManager.class); | ||
when(plugin.getCommandsManager()).thenReturn(cm); | ||
|
||
// Settings | ||
Settings s = mock(Settings.class); | ||
when(s.getResetCooldown()).thenReturn(0); | ||
when(plugin.getSettings()).thenReturn(s); | ||
|
||
// Sometimes use Mockito.withSettings().verboseLogging() | ||
User.setPlugin(plugin); | ||
|
||
// Parent command has no aliases | ||
when(ac.getAddon()).thenReturn(addon); | ||
when(ac.getLabel()).thenReturn("schem"); | ||
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); | ||
when(ac.getTopLabel()).thenReturn("admin"); | ||
|
||
// Locales | ||
LocalesManager lm = mock(LocalesManager.class); | ||
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); | ||
when(plugin.getLocalesManager()).thenReturn(lm); | ||
|
||
// Addon | ||
dataFolder = new File("dataFolder"); | ||
dataFolder.mkdirs(); | ||
|
||
when(addon.getDataFolder()).thenReturn(dataFolder); | ||
// Class | ||
list = new AdminSchemListCommand(ac); | ||
|
||
|
||
} | ||
|
||
/** | ||
* @throws java.lang.Exception | ||
*/ | ||
@After | ||
public void tearDown() throws Exception { | ||
User.clearUsers(); | ||
Files.walk(dataFolder.toPath()) | ||
.sorted(Comparator.reverseOrder()) | ||
.map(Path::toFile) | ||
.forEach(File::delete); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#AdminSchemListCommand(world.bentobox.bentobox.api.commands.admin.schem.AdminSchemCommand)}. | ||
*/ | ||
@Test | ||
public void testAdminSchemListCommand() { | ||
|
||
assertEquals("list", list.getLabel()); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#setup()}. | ||
*/ | ||
@Test | ||
public void testSetup() { | ||
assertEquals("commands.admin.schem.list.description", list.getDescription()); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testCanExecute() { | ||
assertTrue(list.canExecute(user, "", Collections.emptyList())); | ||
assertFalse(list.canExecute(user, "", Collections.singletonList("extraneous"))); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfStringNoSchemsFolder() { | ||
assertFalse(list.execute(user, "", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.admin.schem.list.no-schems"); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfStringNoSchemsFilesEmptyFolder() { | ||
File schemFolder = new File(dataFolder, "schems"); | ||
schemFolder.mkdirs(); | ||
assertFalse(list.execute(user, "", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.admin.schem.list.no-schems"); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
* @throws IOException | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfStringNoSchemsFiles() throws IOException { | ||
File schemFolder = new File(dataFolder, "schems"); | ||
schemFolder.mkdirs(); | ||
new File(schemFolder, "random.txt").createNewFile(); | ||
assertFalse(list.execute(user, "", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.admin.schem.list.no-schems"); | ||
} | ||
|
||
/** | ||
* Test method for {@link world.bentobox.bentobox.api.commands.admin.schem.AdminSchemListCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. | ||
* @throws IOException | ||
*/ | ||
@Test | ||
public void testExecuteUserStringListOfStringWithSchemsFiles() throws IOException { | ||
File schemFolder = new File(dataFolder, "schems"); | ||
schemFolder.mkdirs(); | ||
new File(schemFolder, "island.schem").createNewFile(); | ||
new File(schemFolder, "nether-island.schem").createNewFile(); | ||
new File(schemFolder, "end-island.schem").createNewFile(); | ||
new File(schemFolder, "random.txt").createNewFile(); | ||
|
||
assertTrue(list.execute(user, "", Collections.emptyList())); | ||
Mockito.verify(user).sendMessage("commands.admin.schem.list.available-schems"); | ||
Mockito.verify(user).sendRawMessage("island"); | ||
Mockito.verify(user).sendRawMessage("nether-island"); | ||
Mockito.verify(user).sendRawMessage("end-island"); | ||
Mockito.verify(user, Mockito.times(3)).sendRawMessage(Mockito.anyString()); | ||
} | ||
|
||
} |