package com.nicewuerfel.blockown;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.EntityType;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class TestOwnedBlock {
private static World world1;
private static World world2;
private static Block[] blocks;
@Rule
public ExpectedException expected = ExpectedException.none();
@BeforeClass
public static void init() {
world1 = mock(World.class);
when(world1.getName()).thenReturn("w1");
world2 = mock(World.class);
when(world2.getName()).thenReturn("w2");
blocks = new Block[4];
blocks[0] = createBlock(world1, 12, 14, 16);
blocks[1] = createBlock(world1, 18, 20, 22);
blocks[2] = createBlock(world2, 12, 14, 16);
blocks[3] = createBlock(world2, 10, 8, 6);
}
private static Block createBlock(World world, int x, int y, int z) {
Block block = mock(Block.class);
when(block.getWorld()).thenReturn(world);
when(block.getX()).thenReturn(x);
when(block.getY()).thenReturn(y);
when(block.getZ()).thenReturn(z);
when(block.getType()).thenReturn(org.bukkit.Material.LOG);
return block;
}
@Test
public void testFactories() {
assertEquals(0, OwnedBlock.class.getConstructors().length);
OwnedBlock.newInstance(blocks[0]);
OwnedBlock.newInstance("Hi", 1, 2, 3);
OwnedBlock.newInstance("Hi", 1, 2, 3, null);
OwnedBlock.newInstance("Hi", 1, 2, 3, new Material(EntityType.BOAT));
expected.expect(NullPointerException.class);
OwnedBlock.newInstance(null);
OwnedBlock.newInstance(null, 1, 2, 3);
OwnedBlock.newInstance(null, 1, 2, 3, new Material(EntityType.BOAT));
}
@Test
public void testEquals() {
OwnedBlock[] ob = new OwnedBlock[6];
ob[0] = OwnedBlock.newInstance(blocks[0]);
ob[1] = OwnedBlock.newInstance(blocks[1]);
ob[2] = OwnedBlock.newInstance(blocks[2]);
ob[3] = OwnedBlock.newInstance(blocks[3]);
ob[4] = OwnedBlock.newInstance("w1", 12, 14, 16);
ob[5] = OwnedBlock.newInstance("w1", 12, 14, 16, new Material(org.bukkit.Material.DIRT));
// Reflexivity
assertTrue(ob[0].equals(ob[0]));
// Transitivity
assertTrue(ob[0].equals(ob[4]));
assertTrue(ob[4].equals(ob[5]));
assertTrue(ob[0].equals(ob[5]));
// Symmetry
assertTrue(ob[0].equals(ob[4]));
assertTrue(ob[4].equals(ob[0]));
assertTrue(ob[4].equals(ob[5]));
assertTrue(ob[5].equals(ob[4]));
// Integrity
assertFalse(ob[0].equals(ob[1]));
assertFalse(ob[1].equals(ob[0]));
assertFalse(ob[1].equals(ob[2]));
assertFalse(ob[2].equals(ob[1]));
assertFalse(ob[0].equals(ob[2]));
assertFalse(ob[2].equals(ob[0]));
}
@Test
public void testHashCode() {
OwnedBlock[] ob = new OwnedBlock[6];
ob[0] = OwnedBlock.newInstance(blocks[0]);
ob[1] = OwnedBlock.newInstance(blocks[1]);
ob[2] = OwnedBlock.newInstance("w1", 12, 14, 16);
ob[3] = OwnedBlock.newInstance("w1", 12, 14, 16, new Material(org.bukkit.Material.DIRT));
assertEquals(ob[0].hashCode(), ob[2].hashCode());
assertEquals(ob[0].hashCode(), ob[3].hashCode());
assertEquals(ob[1].hashCode(), ob[1].hashCode());
}
}