Skip to content

Commit

Permalink
Improve scale calculation logic and update unit tests
Browse files Browse the repository at this point in the history
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
SmartGecko44 committed Dec 5, 2023
1 parent d6dee13 commit b83dea1
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 75 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,11 @@
<version>3.12.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion src/main/java/org/gecko/wauh/logic/ScaleReverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void ScaleReverseLogic(int totalRemovedCount, int radiusLimit, Set<Block>

// Set BLOCKS_PER_ITERATION dynamically based on the total count
//TODO: Fix this stuff
int sqrtTotalBlocks = (int) (Math.sqrt(totalRemovedCount) * radiusLimit) / (2 ^ (int) Math.sqrt(radiusLimit));
int sqrtTotalBlocks = (int) (Math.sqrt(totalRemovedCount) * radiusLimit) / ((int) Math.pow(2, Math.sqrt(radiusLimit)));
int scaledBlocksPerIteration = Math.max(1, sqrtTotalBlocks);
// Update BLOCKS_PER_ITERATION based on the scaled value

Expand Down
245 changes: 171 additions & 74 deletions src/test/java/org/gecko/wauh/logic/ScaleReverseTest.java
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());
}
}
}

0 comments on commit b83dea1

Please sign in to comment.