以下に示す Horse クラスがあります。Horse オブジェクトが Perso オブジェクトではないドライバーを使用せず、Vehicle クラスを継承し、それ自体ではないエンジンを持つことができないようにする必要があります。コードの下に投稿された Junit テストで、test4、test1、および test2 に失敗しています。問題は、セットエンジンとセットドライバーメソッドを具体的にどのように処理しているかにあると思います。この場合、メソッドを設定するために必要な特定の方法はありますか?
馬.java
public class Horse extends Vehicle implements Engine {
public Horse(Driver driver){
super(driver, null);
super.setEngine(this);
}
//@override
public void setEngine(Engine engine){
if(engine == null){
super.setEngine(this);
}
else if(engine instanceof Horse){
super.setEngine(engine);
}
else{
throw new IllegalStateException();}
}
//@override
public void setDriver(Driver driver){
if(driver instanceof Person){
super.setDriver(this);
}
else{
throw new IllegalStateException();
}
}
//@override
public int getForce(){
return 746;
}
public boolean equals(Object other)
{
if(other instanceof Horse){
if(((Horse)other).getForce() == this.getForce()){
return true;
}
}
return false;
}
public int compareTo(Engine o) {
int force = this.getForce() - o.getForce();
return force;
}
}
車両.java
abstract class Vehicle{
Engine engine;
Driver driver;
public Vehicle(Driver driver, Engine engine){
}
public Engine getEngine(){
return engine;
}
public void setEngine(Engine aEngine){
engine = aEngine;
}
public Driver getDriver(){
return driver;
}
public void setDriver(Driver aDriver){
driver = aDriver;
}
public boolean equals(Object other)
{
if(other instanceof Vehicle){
if(((Vehicle)other).getEngine().equals(this.getEngine())){
return true;
}
}
return false;
}
public int compareTo(Engine o) {
int force = o.getForce();
return force;
}
}
エンジン.java
public interface Engine extends Comparable<Engine>{
int compareTo(Engine o);
public int getForce();
}
ジュニットテスト
import static org.junit.Assert.*;
import org.junit.Test;
public class HorseTest {
@Test
public void test0_HorseIsAVehicleAndAnEngine() {
Horse horse = new Horse( new Person( 55 ) { } );
assertTrue( "Incorrect result", horse instanceof Vehicle );
assertTrue( "Incorrect result", horse instanceof Engine );
}
@Test(expected=IllegalArgumentException.class)
public void test1_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_NEW() {
new Horse( new Driver() { } );
}
@Test(expected=IllegalArgumentException.class)
public void test2_HorsesCannotBeRiddenByAnyDriverOtherThanPeople_SET() {
Horse horse = new Horse( new Person( 42 ) { } );
horse.setDriver( new Driver() { } );
}
@Test
public void test3_AHorseIsItsOwnEngine() {
Horse horse = new Horse( new Person( 99 ) { } );
Engine engine = horse.getEngine();
assertTrue( "Incorrect result", horse == engine );
}
@Test(expected=IllegalArgumentException.class)
public void test4_HorsesCannotHaveAnEngineThatIsNotItself() {
Engine anEngine = new Engine() {
@Override
public int compareTo(Engine o) {
return 0;
}
@Override
public int getForce() {
return 0;
}
};
Horse horse = new Horse( new Person( 24 ) { } );
horse.setEngine( anEngine );
}
@Test
public void test5_AHorseReceivingNullOrItselfAsEngineSetEngineToItself() {
Engine engine;
Horse horse = new Horse( new Person( 99 ) { } );
horse.setEngine( null );
engine = horse.getEngine();
assertTrue( "Incorrect result", horse == engine );
horse.setEngine( horse );
engine = horse.getEngine();
assertTrue( "Incorrect result", horse == engine );
}
@Test
public void test6_HorseHasEquals() {
Engine a, b;
boolean actual;
// equal to itself
a = new Horse( new Person( 21 ));
actual = a.equals( a );
assertTrue ( "Incorrect result", actual );
// equal to another horse (regardless of driver)
a = new Horse( new Person( 42 ));
b = new Horse( new Person( 24 ));
actual = a.equals( b );
assertTrue ( "Incorrect result", actual );
// not equal to null
actual = a.equals( null );
assertFalse( "Incorrect result", actual );
// not equal to some other object
actual = a.equals( "84" );
assertFalse( "Incorrect result", actual );
// not equal to some other object
actual = a.equals( 48 );
assertFalse( "Incorrect result", actual );
}
@Test
public void test7_HorseHasCompareTo() {
Horse a, b;
int actual;
// equal to itself
a = new Horse( new Person( 52 ));
actual = a.compareTo( a );
assertTrue( "Incorrect result", actual == 0 );
// equal to another horse (regardless of driver)
a = new Horse( new Person( 9 ));
b = new Horse( new Person( 8 ));
actual = a.compareTo( b );
assertTrue( "Incorrect result", actual == 0 );
}
}