Added test classes.

This commit is contained in:
tastybento 2019-01-30 15:30:24 -08:00
parent ad80d32a58
commit b9d1d9faad
5 changed files with 166 additions and 4 deletions

View File

@ -1,5 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/main/resources/locales=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8

View File

@ -12,11 +12,17 @@ public class BiomeRecipeAdapter implements AdapterInterface<BiomeRecipe, String>
@Override
public BiomeRecipe deserialize(Object object) {
if (object instanceof String && ((String)object).equals("null")) {
return null;
}
return RecipeManager.getBiomeRecipies((String)object).orElse(null);
}
@Override
public String serialize(Object object) {
if (object == null) {
return "null";
}
return ((BiomeRecipe)object).getName();
}

View File

@ -13,9 +13,11 @@ public class RectangleAdapter implements AdapterInterface<Rectangle, String> {
@Override
public Rectangle deserialize(Object object) {
if (object instanceof String) {
String[] s = ((String)object).split(",");
if (s.length == 4) {
return new Rectangle(Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]), Integer.valueOf(s[3]));
if (!((String)object).equals("null")) {
String[] s = ((String)object).split(",");
if (s.length == 4) {
return new Rectangle(Integer.valueOf(s[0]), Integer.valueOf(s[1]), Integer.valueOf(s[2]), Integer.valueOf(s[3]));
}
}
}
return null;
@ -24,7 +26,7 @@ public class RectangleAdapter implements AdapterInterface<Rectangle, String> {
@Override
public String serialize(Object object) {
Rectangle r = (Rectangle)object;
return r == null ? "" : r.x + ","+ r.y + "," + r.width + "," + r.height;
return r == null ? "null" : r.x + ","+ r.y + "," + r.width + "," + r.height;
}
}

View File

@ -0,0 +1,91 @@
/**
*
*/
package world.bentobox.greenhouses.data.adapters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.RecipeManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {RecipeManager.class} )
public class BiomeRecipeAdapterTest {
private BiomeRecipeAdapter bra;
private BiomeRecipe recipe;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
bra = new BiomeRecipeAdapter();
PowerMockito.mockStatic(RecipeManager.class);
recipe = mock(BiomeRecipe.class);
Optional<BiomeRecipe> optionalRecipe = Optional.of(recipe);
when(RecipeManager.getBiomeRecipies(Mockito.eq("recipe_name"))).thenReturn(optionalRecipe);
when(RecipeManager.getBiomeRecipies(Mockito.eq("nothing"))).thenReturn(Optional.empty());
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.BiomeRecipeAdapter#deserialize(java.lang.Object)}.
*/
@Test
public void testDeserialize() {
assertEquals(recipe, bra.deserialize("recipe_name"));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.BiomeRecipeAdapter#deserialize(java.lang.Object)}.
*/
@Test
public void testDeserializeNoRecipe() {
assertNull(bra.deserialize("nothing"));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.BiomeRecipeAdapter#deserialize(java.lang.Object)}.
*/
@Test
public void testDeserializeNull() {
assertNull(bra.deserialize("null"));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.BiomeRecipeAdapter#serialize(java.lang.Object)}.
*/
@Test
public void testSerialize() {
BiomeRecipe br = mock(BiomeRecipe.class);
when(br.getName()).thenReturn("recipe_name");
assertEquals("recipe_name", bra.serialize(br));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.BiomeRecipeAdapter#serialize(java.lang.Object)}.
*/
@Test
public void testSerializeNull() {
assertEquals("null", bra.serialize(null));
}
}

View File

@ -0,0 +1,62 @@
/**
*
*/
package world.bentobox.greenhouses.data.adapters;
import static org.junit.Assert.*;
import java.awt.Rectangle;
import org.junit.Before;
import org.junit.Test;
/**
* @author tastybento
*
*/
public class RectangleAdapterTest {
private RectangleAdapter ra;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
ra = new RectangleAdapter();
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.RectangleAdapter#Serialize(java.lang.Object)}.
*/
@Test
public void testSerialize() {
Rectangle rectangle = new Rectangle(10,20,30,40);;
assertEquals("10,20,30,40", ra.serialize(rectangle));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.RectangleAdapter#serialize(java.lang.Object)}.
*/
@Test
public void testSerializeNull() {
assertEquals("null", ra.serialize(null));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.RectangleAdapter#serialize(java.lang.Object)}.
*/
@Test
public void testDeserialize() {
Rectangle rectangle = new Rectangle(10,20,30,40);;
assertEquals(rectangle, ra.deserialize("10,20,30,40"));
}
/**
* Test method for {@link world.bentobox.greenhouses.data.adapters.RectangleAdapter#deserialize(java.lang.Object)}.
*/
@Test
public void testDeserializeNull() {
assertNull(ra.deserialize("null"));
}
}