|
| 1 | +/* |
| 2 | + * WorldGuard, a suite of tools for Minecraft |
| 3 | + * Copyright (C) sk89q <http://www.sk89q.com> |
| 4 | + * Copyright (C) WorldGuard team and contributors |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU Lesser General Public License as published by the |
| 8 | + * Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 14 | + * for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.sk89q.worldguard.protection; |
| 21 | + |
| 22 | +import com.sk89q.worldguard.protection.flags.Flags; |
| 23 | +import com.sk89q.worldguard.protection.flags.MapFlag; |
| 24 | +import com.sk89q.worldguard.protection.flags.StateFlag; |
| 25 | +import com.sk89q.worldguard.protection.flags.StringFlag; |
| 26 | +import com.sk89q.worldguard.protection.regions.ProtectedRegion; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | +import static org.hamcrest.Matchers.equalTo; |
| 34 | + |
| 35 | +@SuppressWarnings({"UnusedDeclaration"}) |
| 36 | +public class FlagValueCalculatorMapFlagTest { |
| 37 | + @Test |
| 38 | + public void testGetEffectiveMapFlagWithFallback() throws Exception { |
| 39 | + MockApplicableRegionSet mock = new MockApplicableRegionSet(); |
| 40 | + |
| 41 | + ProtectedRegion global = mock.global(); |
| 42 | + global.setFlag(Flags.BUILD, StateFlag.State.DENY); |
| 43 | + MapFlag<String, StateFlag.State> mapFlag = |
| 44 | + new MapFlag<>("test", new StringFlag(null), new StateFlag(null, true)); |
| 45 | + Map<String, StateFlag.State> map = new HashMap<>(); |
| 46 | + map.put("allow", StateFlag.State.ALLOW); |
| 47 | + map.put("deny", StateFlag.State.DENY); |
| 48 | + global.setFlag(mapFlag, map); |
| 49 | + |
| 50 | + ApplicableRegionSet applicableSet = mock.getApplicableSet(); |
| 51 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "allow", Flags.BUILD), |
| 52 | + equalTo(StateFlag.State.ALLOW)); |
| 53 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "deny", Flags.BUILD), |
| 54 | + equalTo(StateFlag.State.DENY)); |
| 55 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "undefined", Flags.BUILD), |
| 56 | + equalTo(StateFlag.State.DENY)); |
| 57 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "allow", null), |
| 58 | + equalTo(StateFlag.State.ALLOW)); |
| 59 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "deny", null), |
| 60 | + equalTo(StateFlag.State.DENY)); |
| 61 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "undefined", null), |
| 62 | + equalTo(StateFlag.State.ALLOW)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testGetEffectiveMapFlagWithSamePriority() throws Exception { |
| 67 | + MockApplicableRegionSet mock = new MockApplicableRegionSet(); |
| 68 | + |
| 69 | + ProtectedRegion region1 = mock.add(0); |
| 70 | + ProtectedRegion region2 = mock.add(0); |
| 71 | + |
| 72 | + MapFlag<String, StateFlag.State> mapFlag = |
| 73 | + new MapFlag<>("test", new StringFlag(null), new StateFlag(null, true)); |
| 74 | + Map<String, StateFlag.State> map1 = new HashMap<>(); |
| 75 | + Map<String, StateFlag.State> map2 = new HashMap<>(); |
| 76 | + |
| 77 | + map1.put("should-deny", StateFlag.State.ALLOW); |
| 78 | + map2.put("should-deny", StateFlag.State.DENY); |
| 79 | + |
| 80 | + map1.put("should-allow", StateFlag.State.ALLOW); |
| 81 | + |
| 82 | + map1.put("should-allow2", StateFlag.State.ALLOW); |
| 83 | + map2.put("should-allow2", StateFlag.State.ALLOW); |
| 84 | + |
| 85 | + region1.setFlag(mapFlag, map1); |
| 86 | + region2.setFlag(mapFlag, map2); |
| 87 | + |
| 88 | + ApplicableRegionSet applicableSet = mock.getApplicableSet(); |
| 89 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "should-deny", null), |
| 90 | + equalTo(StateFlag.State.DENY)); |
| 91 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "should-allow", null), |
| 92 | + equalTo(StateFlag.State.ALLOW)); |
| 93 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "should-allow2", null), |
| 94 | + equalTo(StateFlag.State.ALLOW)); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testGetEffectiveMapFlagWithInheritance() throws Exception { |
| 100 | + MockApplicableRegionSet mock = new MockApplicableRegionSet(); |
| 101 | + |
| 102 | + ProtectedRegion parent = mock.add(0); |
| 103 | + ProtectedRegion child = mock.add(0, parent); |
| 104 | + |
| 105 | + MapFlag<String, StateFlag.State> mapFlag = |
| 106 | + new MapFlag<>("test", new StringFlag(null), new StateFlag(null, true)); |
| 107 | + Map<String, StateFlag.State> parentMap = new HashMap<>(); |
| 108 | + Map<String, StateFlag.State> childMap = new HashMap<>(); |
| 109 | + |
| 110 | + parentMap.put("useChildValue", StateFlag.State.ALLOW); |
| 111 | + childMap.put("useChildValue", StateFlag.State.DENY); |
| 112 | + |
| 113 | + parentMap.put("useParentValue", StateFlag.State.ALLOW); |
| 114 | + |
| 115 | + parent.setFlag(mapFlag, parentMap); |
| 116 | + child.setFlag(mapFlag, childMap); |
| 117 | + |
| 118 | + ApplicableRegionSet applicableSet = mock.getApplicableSet(); |
| 119 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "useChildValue", null), |
| 120 | + equalTo(StateFlag.State.DENY)); |
| 121 | + assertThat(applicableSet.queryMapValue(null, mapFlag, "useParentValue", null), |
| 122 | + equalTo(StateFlag.State.ALLOW)); |
| 123 | + } |
| 124 | +} |
0 commit comments