bank plugin: Remove ContainerCalculation class

This commit removes the hashing feature from container calculation which
alone would make obsolete the ContainerCalculation class, so it
additionally inlines the `calculate()` method into the plugin class.

Additionally, this commit adds null handling for the items parameter of
`calculate()` to prevent NPEs.
This commit is contained in:
Jordan Atwood
2020-06-24 17:28:47 -07:00
parent db917454e8
commit 6ccaf8876c
4 changed files with 76 additions and 212 deletions

View File

@@ -25,6 +25,7 @@
*/
package net.runelite.client.plugins.bank;
import com.google.common.collect.ImmutableList;
import com.google.inject.Guice;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
@@ -37,6 +38,7 @@ import net.runelite.api.ItemContainer;
import net.runelite.api.ItemID;
import net.runelite.client.game.ItemManager;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
@@ -105,4 +107,31 @@ public class BankPluginTest
assertFalse(bankPlugin.valueSearch(itemId, "1000k"));
}
@Test
public void testCalculate()
{
Item coins = new Item(ItemID.COINS_995, Integer.MAX_VALUE);
Item whip = new Item(ItemID.ABYSSAL_WHIP, 1_000_000_000);
Item[] items = ImmutableList.of(
coins,
whip
).toArray(new Item[0]);
ItemComposition whipComp = mock(ItemComposition.class);
when(whipComp.getPrice())
.thenReturn(7); // 7 * .6 = 4, 4 * 1m overflows
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
.thenReturn(whipComp);
when(itemManager.getItemPrice(ItemID.ABYSSAL_WHIP))
.thenReturn(3); // 1b * 3 overflows
final ContainerPrices prices = bankPlugin.calculate(items);
assertNotNull(prices);
assertTrue(prices.getHighAlchPrice() > Integer.MAX_VALUE);
assertTrue(prices.getGePrice() > Integer.MAX_VALUE);
}
}

View File

@@ -1,93 +0,0 @@
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.bank;
import com.google.common.collect.ImmutableList;
import com.google.inject.Guice;
import com.google.inject.testing.fieldbinder.Bind;
import com.google.inject.testing.fieldbinder.BoundFieldModule;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.Item;
import net.runelite.api.ItemComposition;
import net.runelite.api.ItemID;
import net.runelite.client.game.ItemManager;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class ContainerCalculationTest
{
@Mock
@Bind
private Client client;
@Mock
@Bind
private ItemManager itemManager;
@Inject
private ContainerCalculation containerCalculation;
@Before
public void before()
{
Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this);
}
@Test
public void testCalculate()
{
Item coins = new Item(ItemID.COINS_995, Integer.MAX_VALUE);
Item whip = new Item(ItemID.ABYSSAL_WHIP, 1_000_000_000);
Item[] items = ImmutableList.of(
coins,
whip
).toArray(new Item[0]);
ItemComposition whipComp = mock(ItemComposition.class);
when(whipComp.getPrice())
.thenReturn(7); // 7 * .6 = 4, 4 * 1m overflows
when(itemManager.getItemComposition(ItemID.ABYSSAL_WHIP))
.thenReturn(whipComp);
when(itemManager.getItemPrice(ItemID.ABYSSAL_WHIP))
.thenReturn(3); // 1b * 3 overflows
final ContainerPrices prices = containerCalculation.calculate(items);
assertNotNull(prices);
assertTrue(prices.getHighAlchPrice() > Integer.MAX_VALUE);
assertTrue(prices.getGePrice() > Integer.MAX_VALUE);
}
}