Skip to content

Commit

Permalink
implement two equals methods, add Junit lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorafa committed Nov 18, 2021
1 parent 0cb5b16 commit c6bf3f8
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 0 deletions.
Binary file added lib/hamcrest-core-1.3-javadoc.jar
Binary file not shown.
Binary file added lib/hamcrest-core-1.3-sources.jar
Binary file not shown.
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.13.1-javadoc.jar
Binary file not shown.
Binary file added lib/junit-4.13.1-sources.jar
Binary file not shown.
Binary file added lib/junit-4.13.1.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions src/main/java/entity/BasePokemon.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entity;

import java.io.Serializable;
import java.util.Objects;

public class BasePokemon implements Serializable {
protected String name;
Expand Down Expand Up @@ -39,5 +40,17 @@ public int getSpeed() {
return basePokemonData.getSpeed();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BasePokemon that = (BasePokemon) o;
return getName().equals(that.getName()) && getBasePokemonData().equals(that.getBasePokemonData());
}

@Override
public int hashCode() {
return Objects.hash(getName(), getBasePokemonData());
}
}

13 changes: 13 additions & 0 deletions src/main/java/entity/BasePokemonData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package entity;

import java.io.Serializable;
import java.util.Objects;

public class BasePokemonData implements Serializable {
protected PokemonType pokemonType;
Expand Down Expand Up @@ -37,4 +38,16 @@ public int getSpeed() {
return speed;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BasePokemonData that = (BasePokemonData) o;
return getMaxHitPoint() == that.getMaxHitPoint() && getAttackPoint() == that.getAttackPoint() && getDefencePoint() == that.getDefencePoint() && getSpeed() == that.getSpeed() && getPokemonType() == that.getPokemonType();
}

@Override
public int hashCode() {
return Objects.hash(getPokemonType(), getMaxHitPoint(), getAttackPoint(), getDefencePoint(), getSpeed());
}
}
42 changes: 42 additions & 0 deletions src/test/java/entity/BasePokemonDataTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package entity;

import org.junit.Before;
import org.junit.Test;

import static entity.PokemonType.ELECTRICITY;
import static org.junit.Assert.*;

public class BasePokemonDataTest {
BasePokemonData basePokemonData;

@Before
public void setBasePokemon(){
basePokemonData = new BasePokemonData(ELECTRICITY, 1,1,1,1);
}

@Test(timeout = 50)
public void testGetPokemonType() {
assertEquals(ELECTRICITY, basePokemonData.getPokemonType());
}
@Test(timeout = 50)
public void testGetMaxHitPoint() {
assertEquals(1, basePokemonData.getMaxHitPoint());
}
@Test(timeout = 50)
public void testGetAttackPoint() {
assertEquals(1, basePokemonData.getAttackPoint());
}
@Test(timeout = 50)
public void testGetDefencePoint() {
assertEquals(1, basePokemonData.getDefencePoint());
}
@Test(timeout = 50)
public void testGetSpeed() {
assertEquals(1, basePokemonData.getSpeed());
}
@Test(timeout = 50)
public void testEqual(){
BasePokemonData bpd = new BasePokemonData(ELECTRICITY, 1,1,1,1);
assertTrue(basePokemonData.equals(bpd));
}
}
50 changes: 50 additions & 0 deletions src/test/java/entity/BasePokemonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package entity;

import org.junit.*;
import static entity.PokemonType.ELECTRICITY;
import static org.junit.Assert.*;

public class BasePokemonTest {
BasePokemon basePokemon;

@Before
public void setBasePokemon(){
BasePokemonData bpd = new BasePokemonData(ELECTRICITY, 1,1,1,1);
basePokemon = new BasePokemon("test",bpd);
}
@Test(timeout = 50)
public void testGetName() {
assertEquals("test", basePokemon.getName());
}
@Test(timeout = 50)
public void testGetBasePokemonData() {
BasePokemonData bpd = new BasePokemonData(ELECTRICITY, 1,1,1,1);
assertEquals(bpd, basePokemon.getBasePokemonData());
}
@Test(timeout = 50)
public void testGetPokemonType() {
assertEquals(ELECTRICITY, basePokemon.getPokemonType());
}
@Test(timeout = 50)
public void testGetMaxHitPoint() {
assertEquals(1, basePokemon.getMaxHitPoint());
}
@Test(timeout = 50)
public void testGetAttackPoint() {
assertEquals(1, basePokemon.getAttackPoint());
}
@Test(timeout = 50)
public void testGetDefencePoint() {
assertEquals(1, basePokemon.getDefencePoint());
}
@Test(timeout = 50)
public void testGetSpeed() {
assertEquals(1, basePokemon.getSpeed());
}
@Test(timeout = 50)
public void testEqual() {
BasePokemonData bpd = new BasePokemonData(ELECTRICITY, 1,1,1,1);
BasePokemon bp = new BasePokemon("test", bpd);
assertTrue(basePokemon.equals(bp));
}
}

0 comments on commit c6bf3f8

Please sign in to comment.