Added basic tests for the DinnerPermsResolver

Dieser Commit ist enthalten in:
zml2008 2011-12-12 23:17:00 -08:00
Ursprung 9240b5cb6e
Commit c4031c38ba
3 geänderte Dateien mit 209 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -68,6 +68,13 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.0-rc1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

Datei anzeigen

@ -0,0 +1,68 @@
package com.sk89q.wepif;
import org.bukkit.Server;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class DinnerPermsResolverTest {
private DinnerPermsResolver resolver;
@Before
public void setUp() {
Server server = mock(Server.class);
when(server.getPluginManager()).thenReturn(mock(PluginManager.class));
resolver = new DinnerPermsResolver(server);
}
@Test
public void testBasicResolving() {
final TestOfflinePermissible permissible = new TestOfflinePermissible();
permissible.setPermission("testperm.test1", true);
assertTrue(resolver.hasPermission(permissible, "testperm.test1"));
assertFalse(resolver.hasPermission(permissible, "testperm.somethingelse"));
assertFalse(resolver.hasPermission(permissible, "testperm.test1.anything"));
assertFalse(resolver.hasPermission(permissible, "completely.unrelated"));
permissible.clearPermissions();
}
@Test
public void testBasicWildcardResolution() {
final TestOfflinePermissible permissible = new TestOfflinePermissible();
permissible.setPermission("commandbook.spawnmob.*", true);
assertTrue(resolver.hasPermission(permissible, "commandbook.spawnmob.pig"));
assertTrue(resolver.hasPermission(permissible, "commandbook.spawnmob.spider"));
assertTrue(resolver.hasPermission(permissible, "commandbook.spawnmob.spider.skeleton"));
permissible.clearPermissions();
}
@Test
public void testNegatingNodes() {
final TestOfflinePermissible permissible = new TestOfflinePermissible();
permissible.setPermission("commandbook.*", true);
permissible.setPermission("commandbook.cuteasianboys", false);
permissible.setPermission("commandbook.warp.*", false);
permissible.setPermission("commandbook.warp.create", true);
assertTrue(resolver.hasPermission(permissible, "commandbook.motd"));
assertFalse(resolver.hasPermission(permissible, "commandbook.cuteasianboys"));
assertFalse(resolver.hasPermission(permissible, "commandbook.warp.remove"));
assertTrue(resolver.hasPermission(permissible, "commandbook.warp.create"));
permissible.clearPermissions();
}
@Test
public void testInGroup() {
final TestOfflinePermissible permissible = new TestOfflinePermissible();
permissible.setPermission("group.a", true);
permissible.setPermission("group.b", true);
assertTrue(resolver.inGroup(permissible, "a"));
assertTrue(resolver.inGroup(permissible, "b"));
assertFalse(resolver.inGroup(permissible, "c"));
}
}

Datei anzeigen

@ -0,0 +1,134 @@
package com.sk89q.wepif;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.permissions.*;
import org.bukkit.plugin.Plugin;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class TestOfflinePermissible implements OfflinePlayer, Permissible {
private boolean op;
private final Map<String, Boolean> assignedPermissions = new HashMap<String, Boolean>();
public boolean isOp() {
return op;
}
public void setOp(boolean b) {
this.op = b;
}
public boolean isPermissionSet(String s) {
return assignedPermissions.containsKey(s.toLowerCase());
}
public boolean isPermissionSet(Permission permission) {
return isPermissionSet(permission.getName());
}
public boolean hasPermission(String s) {
if (isPermissionSet(s)) {
return assignedPermissions.get(s.toLowerCase());
}
return false;
}
public boolean hasPermission(Permission permission) {
return hasPermission(permission.getName());
}
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public PermissionAttachment addAttachment(Plugin plugin) {
throw new UnsupportedOperationException("Not supported yet.");
}
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b, int i) {
throw new UnsupportedOperationException("Not supported yet.");
}
public PermissionAttachment addAttachment(Plugin plugin, int i) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void removeAttachment(PermissionAttachment permissionAttachment) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void recalculatePermissions() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
Set<PermissionAttachmentInfo> ret = new HashSet<PermissionAttachmentInfo>();
for (Map.Entry<String, Boolean> entry : assignedPermissions.entrySet()) {
ret.add(new PermissionAttachmentInfo(this, entry.getKey(), null, entry.getValue()));
}
return ret;
}
public void setPermission(String permission, boolean value) {
assignedPermissions.put(permission.toLowerCase(), value);
}
public void unsetPermission(String permission) {
assignedPermissions.remove(permission.toLowerCase());
}
public void clearPermissions() {
assignedPermissions.clear();
}
// -- Unneeded OfflinePlayer methods
public boolean isOnline() {
return false;
}
public String getName() {
return "Tester";
}
public boolean isBanned() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void setBanned(boolean b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean isWhitelisted() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void setWhitelisted(boolean b) {
throw new UnsupportedOperationException("Not supported yet.");
}
public Player getPlayer() {
throw new UnsupportedOperationException("Not supported yet.");
}
public long getFirstPlayed() {
throw new UnsupportedOperationException("Not supported yet.");
}
public long getLastPlayed() {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean hasPlayedBefore() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Map<String, Object> serialize() {
throw new UnsupportedOperationException("Not supported yet.");
}
}