Added unit test for entry and exit flags to put the matter to rest.

Also cleaned up a few other things regarding tests.
This commit is contained in:
Wizjany 2013-03-01 16:14:11 -05:00
parent 2d50659b48
commit a34e48a145
4 changed files with 192 additions and 21 deletions

View File

@ -19,10 +19,10 @@
package com.sk89q.worldguard; package com.sk89q.worldguard;
import java.util.Set;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.LocalPlayer;
@org.junit.Ignore @org.junit.Ignore
public class TestPlayer extends LocalPlayer { public class TestPlayer extends LocalPlayer {
@ -49,32 +49,27 @@ public boolean hasGroup(String group) {
@Override @Override
public Vector getPosition() { public Vector getPosition() {
// TODO Auto-generated method stub return new Vector(0, 0, 0);
return null;
} }
@Override @Override
public void kick(String msg) { public void kick(String msg) {
// TODO Auto-generated method stub System.out.println("TestPlayer{" + this.name + "} kicked!");
} }
@Override @Override
public void ban(String msg) { public void ban(String msg) {
// TODO Auto-generated method stub System.out.println("TestPlayer{" + this.name + "} banned!");
} }
@Override @Override
public void printRaw(String msg) { public void printRaw(String msg) {
// TODO Auto-generated method stub System.out.println("-> TestPlayer{" + this.name + "}: " + msg);
} }
@Override @Override
public String[] getGroups() { public String[] getGroups() {
// TODO Auto-generated method stub return groups.toArray(new String[groups.size()]);
return null;
} }
@Override @Override

View File

@ -0,0 +1,30 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection;
import com.sk89q.worldguard.protection.managers.PRTreeRegionManager;
import com.sk89q.worldguard.protection.managers.RegionManager;
public class PRTreeRegionEntryExitTest extends RegionEntryExitTest {
@Override
protected RegionManager createRegionManager() throws Exception {
return new PRTreeRegionManager(null);
}
}

View File

@ -0,0 +1,146 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2013 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.TestPlayer;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.RegionGroup;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
public abstract class RegionEntryExitTest {
static String ENTRY_ID = "entry_rg";
static String EXIT_ID = "exit_rg";
static String BUILDER_GROUP = "builder";
static String VIP_GROUP = "vip";
Vector inEntry = new Vector(5, 64, 5);
Vector inExit = new Vector(-5, 65, -5);
RegionManager manager;
ProtectedRegion globalRegion;
ProtectedRegion entryRegion;
ProtectedRegion exitRegion;
TestPlayer vip_player;
TestPlayer builder_player;
protected abstract RegionManager createRegionManager() throws Exception;
@Before
public void setUp() throws Exception {
setUpGlobalRegion();
manager = createRegionManager();
setUpPlayers();
setUpEntryRegion();
setUpExitRegion();
}
void setUpPlayers() {
vip_player = new TestPlayer("dudu");
vip_player.addGroup(VIP_GROUP);
builder_player = new TestPlayer("esskay");
builder_player.addGroup(BUILDER_GROUP);
// @Test
// assertFalse(builderPlayer.wuvs(vip_player)); // causes test to fail
}
void setUpGlobalRegion() {
globalRegion = new GlobalProtectedRegion("__global__");
}
void setUpEntryRegion() {
DefaultDomain domain = new DefaultDomain();
domain.addGroup(VIP_GROUP);
ProtectedRegion region = new ProtectedCuboidRegion(ENTRY_ID, new BlockVector(1, 0, 1), new BlockVector(10, 255, 10));
region.setMembers(domain);
manager.addRegion(region);
entryRegion = region;
// this is the way it's supposed to work
// whatever the group flag is set to is the group that the flag APPLIES to
// in this case, non members (esskay) should be DENIED entry
entryRegion.setFlag(DefaultFlag.ENTRY, StateFlag.State.DENY);
entryRegion.setFlag(DefaultFlag.ENTRY.getRegionGroupFlag(), RegionGroup.NON_MEMBERS);
}
void setUpExitRegion() throws Exception {
DefaultDomain domain = new DefaultDomain();
domain.addGroup(BUILDER_GROUP);
ProtectedRegion region = new ProtectedCuboidRegion(EXIT_ID, new BlockVector(-1, 0, -1), new BlockVector(-10, 255, -10));
region.setOwners(domain);
manager.addRegion(region);
entryRegion = region;
// same as above
entryRegion.setFlag(DefaultFlag.EXIT, StateFlag.State.DENY);
entryRegion.setFlag(DefaultFlag.EXIT.getRegionGroupFlag(), RegionGroup.NON_OWNERS);
}
@Test
public void testEntry() throws Exception {
ApplicableRegionSet appl;
appl = manager.getApplicableRegions(inEntry);
// ProtectedRegion rg = appl.iterator().next();
// System.out.println("rg " + rg.getId());
// System.out.println("mem " + rg.getMembers().toGroupsString());
// System.out.println("flag " + appl.getFlag(DefaultFlag.ENTRY));
// System.out.println("grp " + appl.getFlag(DefaultFlag.ENTRY.getRegionGroupFlag()));
// System.out.println("===");
assertTrue("Allowed Entry", appl.allows(DefaultFlag.ENTRY, vip_player));
assertFalse("Forbidden Entry", appl.allows(DefaultFlag.ENTRY, builder_player));
}
@Test
public void testExit() throws Exception {
ApplicableRegionSet appl;
appl = manager.getApplicableRegions(inExit);
// ProtectedRegion rg = appl.iterator().next();
// System.out.println("rg " + rg.getId());
// System.out.println("own " + rg.getOwners().toGroupsString());
// System.out.println("flag " + appl.getFlag(DefaultFlag.EXIT));
// System.out.println("grp " + appl.getFlag(DefaultFlag.EXIT.getRegionGroupFlag()));
// System.out.println("===");
assertTrue("Allowed Exit", appl.allows(DefaultFlag.EXIT, builder_player));
assertFalse("Forbidden Exit", appl.allows(DefaultFlag.EXIT, vip_player));
}
}

View File

@ -19,6 +19,15 @@
package com.sk89q.worldguard.protection; package com.sk89q.worldguard.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashSet;
import org.junit.Before;
import org.junit.Test;
import com.sk89q.worldedit.BlockVector; import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.BlockVector2D; import com.sk89q.worldedit.BlockVector2D;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
@ -26,19 +35,11 @@
import com.sk89q.worldguard.domains.DefaultDomain; import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.flags.DefaultFlag; import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.FlatRegionManager;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion; import com.sk89q.worldguard.protection.regions.GlobalProtectedRegion;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion; import com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashSet;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public abstract class RegionOverlapTest { public abstract class RegionOverlapTest {
static String COURTYARD_ID = "courtyard"; static String COURTYARD_ID = "courtyard";
@ -166,7 +167,6 @@ public void testPlayer2BuildAccess() {
HashSet<ProtectedRegion> test = new HashSet<ProtectedRegion>(); HashSet<ProtectedRegion> test = new HashSet<ProtectedRegion>();
test.add(courtyard); test.add(courtyard);
test.add(fountain); test.add(fountain);
System.out.println(test);
// Outside // Outside
appl = manager.getApplicableRegions(outside); appl = manager.getApplicableRegions(outside);