Skip to content

Commit

Permalink
Added unit test for entry and exit flags to put the matter to rest.
Browse files Browse the repository at this point in the history
Also cleaned up a few other things regarding tests.
  • Loading branch information
wizjany committed Mar 2, 2013
1 parent 2d50659 commit a34e48a
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 21 deletions.
19 changes: 7 additions & 12 deletions src/test/java/com/sk89q/worldguard/TestPlayer.java
Expand Up @@ -19,10 +19,10 @@

package com.sk89q.worldguard;

import java.util.Set;
import java.util.HashSet;
import java.util.Set;

import com.sk89q.worldedit.Vector;
import com.sk89q.worldguard.LocalPlayer;

@org.junit.Ignore
public class TestPlayer extends LocalPlayer {
Expand All @@ -49,32 +49,27 @@ public boolean hasGroup(String group) {

@Override
public Vector getPosition() {
// TODO Auto-generated method stub
return null;
return new Vector(0, 0, 0);
}

@Override
public void kick(String msg) {
// TODO Auto-generated method stub

System.out.println("TestPlayer{" + this.name + "} kicked!");
}

@Override
public void ban(String msg) {
// TODO Auto-generated method stub

System.out.println("TestPlayer{" + this.name + "} banned!");
}

@Override
public void printRaw(String msg) {
// TODO Auto-generated method stub

System.out.println("-> TestPlayer{" + this.name + "}: " + msg);
}

@Override
public String[] getGroups() {
// TODO Auto-generated method stub
return null;
return groups.toArray(new String[groups.size()]);
}

@Override
Expand Down
@@ -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);
}
}
146 changes: 146 additions & 0 deletions src/test/java/com/sk89q/worldguard/protection/RegionEntryExitTest.java
@@ -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));
}

}
Expand Up @@ -19,26 +19,27 @@

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.BlockVector2D;
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.StateFlag;
import com.sk89q.worldguard.protection.managers.FlatRegionManager;
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.ProtectedPolygonalRegion;
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 {
static String COURTYARD_ID = "courtyard";
Expand Down Expand Up @@ -166,7 +167,6 @@ public void testPlayer2BuildAccess() {
HashSet<ProtectedRegion> test = new HashSet<ProtectedRegion>();
test.add(courtyard);
test.add(fountain);
System.out.println(test);

// Outside
appl = manager.getApplicableRegions(outside);
Expand Down

0 comments on commit a34e48a

Please sign in to comment.