-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve scale calculation logic and update unit tests
Refactor the power calculation in `ScaleReverse.java` to use correct formula. Substantially extend the tests for `ScaleReverse` in `ScaleReverseTest.java`, including testing for multiple block source types and verifying associated operation calls. The Mockito library is also added to the project dependencies in `pom.xml` to support the mocking needs of the unit tests.
- Loading branch information
1 parent
d6dee13
commit b83dea1
Showing
3 changed files
with
178 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
245 changes: 171 additions & 74 deletions
245
src/test/java/org/gecko/wauh/logic/ScaleReverseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,171 @@ | ||
//package org.gecko.wauh.logic; | ||
// | ||
//import org.gecko.wauh.Main; | ||
//import org.gecko.wauh.logic.ScaleReverse; | ||
//import org.gecko.wauh.listeners.BarrierListener; | ||
//import org.gecko.wauh.listeners.BedrockListener; | ||
//import org.gecko.wauh.listeners.BucketListener; | ||
//import org.gecko.wauh.listeners.WaterBucketListener; | ||
//import org.junit.jupiter.api.Test; | ||
//import org.junit.jupiter.api.BeforeEach; | ||
//import org.junit.jupiter.api.AfterEach; | ||
//import org.mockito.Mockito; | ||
// | ||
//import java.util.HashSet; | ||
//import java.util.Set; | ||
//import org.bukkit.block.Block; | ||
// | ||
//import static org.junit.jupiter.api.Assertions.*; | ||
// | ||
//class ScaleReverseTest { | ||
// | ||
// // create dummy block | ||
// Block blockMock = Mockito.mock(Block.class); | ||
// Set<Block> markedBlocks = new HashSet<>(); | ||
// | ||
// | ||
// @Test | ||
// void testScaleReverseLogic_BedrockSource() { | ||
// ScaleReverse scaleReverse = new ScaleReverse(); | ||
// int totalRemovedCount = 10; | ||
// int radiusLimit = 2; | ||
// markedBlocks.add(blockMock); | ||
// | ||
// scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "bedrock"); | ||
// | ||
// // assertions | ||
// } | ||
// | ||
// @Test | ||
// void testScaleReverseLogic_BucketSource() { | ||
// ScaleReverse scaleReverse = new ScaleReverse(); | ||
// int totalRemovedCount = 20; | ||
// int radiusLimit = 3; | ||
// markedBlocks.add(blockMock); | ||
// | ||
// scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "bucket"); | ||
// | ||
// // assertions | ||
// } | ||
// | ||
// @Test | ||
// void testScaleReverseLogic_BarrierSource() { | ||
// ScaleReverse scaleReverse = new ScaleReverse(); | ||
// int totalRemovedCount = 30; | ||
// int radiusLimit = 4; | ||
// markedBlocks.add(blockMock); | ||
// | ||
// scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "barrier"); | ||
// | ||
// // assertions | ||
// } | ||
// | ||
// @Test | ||
// void testScaleReverseLogic_WauhSource() { | ||
// ScaleReverse scaleReverse = new ScaleReverse(); | ||
// int totalRemovedCount = 40; | ||
// int radiusLimit = 5; | ||
// markedBlocks.add(blockMock); | ||
// | ||
// scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "wauh"); | ||
// | ||
// // assertions | ||
// } | ||
//} | ||
package org.gecko.wauh.logic; | ||
|
||
import org.bukkit.block.Block; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
import org.gecko.wauh.Main; | ||
import org.gecko.wauh.listeners.BarrierListener; | ||
import org.gecko.wauh.listeners.BedrockListener; | ||
import org.gecko.wauh.listeners.BucketListener; | ||
import org.gecko.wauh.listeners.WaterBucketListener; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
class ScaleReverseTest { | ||
|
||
// create dummy block | ||
Block blockMock = Mockito.mock(Block.class); | ||
Set<Block> markedBlocks = new HashSet<>(); | ||
|
||
private static Iterator<Block> anyBlockIterator() { | ||
return new Iterator<Block>() { | ||
@Override | ||
public boolean hasNext() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Block next() { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
void testScaleReverseLogic_BucketSource() { | ||
try (MockedStatic<JavaPlugin> mocked = Mockito.mockStatic(JavaPlugin.class)) { | ||
// Arrange | ||
Main mainMock = Mockito.mock(Main.class); | ||
mocked.when(() -> JavaPlugin.getPlugin(Main.class)).thenReturn(mainMock); | ||
|
||
BucketListener bucketListenerMock = Mockito.mock(BucketListener.class); | ||
when(mainMock.getBucketListener()).thenReturn(bucketListenerMock); | ||
|
||
BarrierListener barrierListenerMock = Mockito.mock(BarrierListener.class); | ||
when(mainMock.getBarrierListener()).thenReturn(barrierListenerMock); | ||
|
||
BedrockListener bedrockListenerMock = Mockito.mock(BedrockListener.class); | ||
when(mainMock.getBedrockListener()).thenReturn(bedrockListenerMock); | ||
|
||
WaterBucketListener waterBucketListenerMock = Mockito.mock(WaterBucketListener.class); | ||
when(mainMock.getWaterBucketListener()).thenReturn(waterBucketListenerMock); | ||
|
||
ScaleReverse scaleReverse = new ScaleReverse(); | ||
int totalRemovedCount = 20; | ||
int radiusLimit = 3; | ||
markedBlocks.add(blockMock); | ||
|
||
// Act | ||
scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "bucket"); | ||
|
||
// Assertions | ||
// Verify that CleanRemove() method of BucketListener is called once | ||
verify(bucketListenerMock, times(1)).CleanRemove(anyInt(), anyBlockIterator()); | ||
} | ||
} | ||
|
||
@Test | ||
void testScaleReverseLogic_BarrierSource() { | ||
try (MockedStatic<JavaPlugin> mocked = Mockito.mockStatic(JavaPlugin.class)) { | ||
// Arrange | ||
Main mainMock = Mockito.mock(Main.class); | ||
mocked.when(() -> JavaPlugin.getPlugin(Main.class)).thenReturn(mainMock); | ||
|
||
BucketListener bucketListenerMock = Mockito.mock(BucketListener.class); | ||
when(mainMock.getBucketListener()).thenReturn(bucketListenerMock); | ||
|
||
BarrierListener barrierListenerMock = Mockito.mock(BarrierListener.class); | ||
when(mainMock.getBarrierListener()).thenReturn(barrierListenerMock); | ||
|
||
BedrockListener bedrockListenerMock = Mockito.mock(BedrockListener.class); | ||
when(mainMock.getBedrockListener()).thenReturn(bedrockListenerMock); | ||
|
||
WaterBucketListener waterBucketListenerMock = Mockito.mock(WaterBucketListener.class); | ||
when(mainMock.getWaterBucketListener()).thenReturn(waterBucketListenerMock); | ||
|
||
ScaleReverse scaleReverse = new ScaleReverse(); | ||
int totalRemovedCount = 30; | ||
int radiusLimit = 4; | ||
markedBlocks.add(blockMock); | ||
|
||
// Act | ||
scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "barrier"); | ||
|
||
// Assertions | ||
// Verify that CleanRemove() method of BarrierListener is called once | ||
verify(barrierListenerMock, times(1)).CleanRemove(anyInt(), anyBlockIterator()); | ||
} | ||
} | ||
|
||
@Test | ||
void testScaleReverseLogic_BedrockSource() { | ||
try (MockedStatic<JavaPlugin> mocked = Mockito.mockStatic(JavaPlugin.class)) { | ||
// Arrange | ||
Main mainMock = Mockito.mock(Main.class); | ||
mocked.when(() -> JavaPlugin.getPlugin(Main.class)).thenReturn(mainMock); | ||
|
||
BucketListener bucketListenerMock = Mockito.mock(BucketListener.class); | ||
when(mainMock.getBucketListener()).thenReturn(bucketListenerMock); | ||
|
||
BarrierListener barrierListenerMock = Mockito.mock(BarrierListener.class); | ||
when(mainMock.getBarrierListener()).thenReturn(barrierListenerMock); | ||
|
||
BedrockListener bedrockListenerMock = Mockito.mock(BedrockListener.class); | ||
when(mainMock.getBedrockListener()).thenReturn(bedrockListenerMock); | ||
|
||
WaterBucketListener waterBucketListenerMock = Mockito.mock(WaterBucketListener.class); | ||
when(mainMock.getWaterBucketListener()).thenReturn(waterBucketListenerMock); | ||
|
||
ScaleReverse scaleReverse = new ScaleReverse(); | ||
int totalRemovedCount = 10; | ||
int radiusLimit = 2; | ||
markedBlocks.add(blockMock); | ||
|
||
// Act | ||
scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "bedrock"); | ||
|
||
// Assertions | ||
// Verify that CleanRemove() method of BedrockListener is called once | ||
verify(bedrockListenerMock, times(1)).CleanRemove(anyInt(), anyBlockIterator()); | ||
} | ||
} | ||
|
||
@Test | ||
void testScaleReverseLogic_WauhSource() { | ||
try (MockedStatic<JavaPlugin> mocked = Mockito.mockStatic(JavaPlugin.class)) { | ||
// Arrange | ||
Main mainMock = Mockito.mock(Main.class); | ||
mocked.when(() -> JavaPlugin.getPlugin(Main.class)).thenReturn(mainMock); | ||
|
||
BucketListener bucketListenerMock = Mockito.mock(BucketListener.class); | ||
when(mainMock.getBucketListener()).thenReturn(bucketListenerMock); | ||
|
||
BarrierListener barrierListenerMock = Mockito.mock(BarrierListener.class); | ||
when(mainMock.getBarrierListener()).thenReturn(barrierListenerMock); | ||
|
||
BedrockListener bedrockListenerMock = Mockito.mock(BedrockListener.class); | ||
when(mainMock.getBedrockListener()).thenReturn(bedrockListenerMock); | ||
|
||
WaterBucketListener waterBucketListenerMock = Mockito.mock(WaterBucketListener.class); | ||
when(mainMock.getWaterBucketListener()).thenReturn(waterBucketListenerMock); | ||
|
||
ScaleReverse scaleReverse = new ScaleReverse(); | ||
int totalRemovedCount = 40; | ||
int radiusLimit = 5; | ||
markedBlocks.add(blockMock); | ||
|
||
// Act | ||
scaleReverse.ScaleReverseLogic(totalRemovedCount, radiusLimit, markedBlocks, "wauh"); | ||
|
||
// Assertions | ||
// Verify that CleanRemove() method of WauhBucketListener is called once | ||
verify(waterBucketListenerMock, times(1)).CleanRemove(anyInt(), anyBlockIterator()); | ||
} | ||
} | ||
} |