Jelajahi Sumber

Refactoring CheckAbnormal condition

Added missing unit tests.
Zoey76 3 tahun lalu
induk
melakukan
ada8efbfe0

+ 7 - 13
src/main/java/com/l2jserver/gameserver/model/conditions/ConditionCheckAbnormal.java

@@ -19,35 +19,29 @@
 package com.l2jserver.gameserver.model.conditions;
 
 import com.l2jserver.gameserver.model.actor.L2Character;
-import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
 import com.l2jserver.gameserver.model.items.L2Item;
 import com.l2jserver.gameserver.model.skills.AbnormalType;
-import com.l2jserver.gameserver.model.skills.BuffInfo;
 import com.l2jserver.gameserver.model.skills.Skill;
 
 /**
- * Check AbnormalType and level.
+ * Check abnormal type and level.
  * @author Zoey76
- * @version 2.6.2.0
+ * @version 2.6.3.0
  */
 public class ConditionCheckAbnormal extends Condition {
 	private final AbnormalType type;
 	private final int level;
-	private final boolean mustHave;
+	private final boolean present;
 	
-	public ConditionCheckAbnormal(AbnormalType type, int level, boolean mustHave) {
+	public ConditionCheckAbnormal(AbnormalType type, int level, boolean present) {
 		this.type = type;
 		this.level = level;
-		this.mustHave = mustHave;
+		this.present = present;
 	}
 	
 	@Override
 	public boolean testImpl(L2Character effector, L2Character effected, Skill skill, L2Item item) {
-		final L2PcInstance player = effector.getActingPlayer();
-		final BuffInfo info = player.getEffectList().getBuffInfoByAbnormalType(type);
-		if (info == null) {
-			return !mustHave;
-		}
-		return (mustHave) || ((level == -1) && (level >= info.getSkill().getAbnormalLvl()));
+		final var info = effected.getEffectList().getBuffInfoByAbnormalType(type);
+		return present == (info != null) && (level <= info.getSkill().getAbnormalLvl());
 	}
 }

+ 80 - 0
src/test/java/com/l2jserver/gameserver/model/conditions/ConditionCheckAbnormalTest.java

@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2004-2021 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server 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.
+ * 
+ * L2J Server 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.l2jserver.gameserver.model.conditions;
+
+import static com.l2jserver.gameserver.model.skills.AbnormalType.MULTI_DEBUFF_WIND;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.when;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import com.l2jserver.gameserver.model.CharEffectList;
+import com.l2jserver.gameserver.model.actor.L2Character;
+import com.l2jserver.gameserver.model.skills.BuffInfo;
+import com.l2jserver.gameserver.model.skills.Skill;
+
+/**
+ * Check abnormal type and level test.
+ * @author Zoey76
+ * @version 2.6.3.0
+ */
+@ExtendWith(MockitoExtension.class)
+class ConditionCheckAbnormalTest {
+	
+	@Mock
+	private L2Character effected;
+	@Mock
+	private CharEffectList charEffectList;
+	@Mock
+	private BuffInfo buffInfo;
+	@Mock
+	private Skill skill;
+	
+	@Test
+	void testEffectedWithoutAbnormal() {
+		final var condition = new ConditionCheckAbnormal(MULTI_DEBUFF_WIND, 1, true);
+		when(effected.getEffectList()).thenReturn(charEffectList);
+		when(charEffectList.getBuffInfoByAbnormalType(MULTI_DEBUFF_WIND)).thenReturn(null);
+		assertFalse(condition.testImpl(null, effected, null, null));
+	}
+	
+	@Test
+	void testEffectedWithLowerLevelAbnormal() {
+		final var condition = new ConditionCheckAbnormal(MULTI_DEBUFF_WIND, 3, true);
+		when(effected.getEffectList()).thenReturn(charEffectList);
+		when(charEffectList.getBuffInfoByAbnormalType(MULTI_DEBUFF_WIND)).thenReturn(buffInfo);
+		when(buffInfo.getSkill()).thenReturn(skill);
+		when(skill.getAbnormalLvl()).thenReturn(1);
+		assertFalse(condition.testImpl(null, effected, null, null));
+	}
+	
+	@Test
+	void testEffectedWithAbnormal() {
+		final var condition = new ConditionCheckAbnormal(MULTI_DEBUFF_WIND, 3, true);
+		when(effected.getEffectList()).thenReturn(charEffectList);
+		when(charEffectList.getBuffInfoByAbnormalType(MULTI_DEBUFF_WIND)).thenReturn(buffInfo);
+		when(buffInfo.getSkill()).thenReturn(skill);
+		when(skill.getAbnormalLvl()).thenReturn(3);
+		assertTrue(condition.testImpl(null, effected, null, null));
+	}
+}