test/Yen1Test.java


package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import kadai.Yen1;
import kadai.Money1;
public class Yen1Test {
	private Money1 m1;
	@BeforeEach
	public void setup() {
		m1 = new Yen1(100);
	}
	@Test
	public void testGetPrefix() {
		assertEquals("",m1.getPrefix());
	}
	@Test
	public void testGetPostfix() {
		assertEquals("円",m1.getPostfix());
	}
	@Test
	public void testValue() {
		assertEquals(100,m1.getValue(),0.01);
	}
	@Test
	public void testToString() {
		assertEquals("100.0円",m1.toString());
	}
}

test/Dollar1Test.java


package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import kadai.Dollar1;
import kadai.Money1;
public class Dollar1Test {
	private Money1 m1;
	@BeforeEach
	public void setup() {
		m1 = new Dollar1(100);
	}
	@Test
	public void testGetPrefix() {
		assertEquals("$",m1.getPrefix());
	}
	@Test
	public void testGetPostfix() {
		assertEquals("",m1.getPostfix());
	}
	@Test
	public void testValue() {
		assertEquals(100,m1.getValue(),0.01);
	}
	@Test
	public void testToString() {
		assertEquals("$100.0",m1.toString());
	}
}

test/Yen2Test.java


package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import kadai.Money2;
import kadai.Yen2;
public class Yen2Test {
	@Test
	public void testSetYenRate() throws ClassNotFoundException {
		Money2 m = new Yen2(3);
		assertEquals("3.0円", m.toString());
		Yen2.setYenRate(80.0);
		Money2 m2 = m.getYen();
		assertEquals(Class.forName("kadai.Yen2"),m2.getClass());
		assertEquals("3.0円",m2.toString());
		Yen2.setYenRate(100.0);
		Money2 m3 = m.getYen();
		assertEquals("3.0円",m3.toString());
	}
	@Test
	public void testToString() {
		Money2 m = new Yen2(3);
		assertEquals("3.0円", m.toString());
	}
}

kadai/Dollar2Test.java


package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import kadai.Dollar2;
import kadai.Money2;
public class Dollar2Test {
	@Test
	public void testSetYenRate() throws ClassNotFoundException {
		Money2 m = new Dollar2(3);
		assertEquals("$3.0", m.toString());
		Dollar2.setYenRate(80.0);
		Money2 m2 = m.getYen();
		assertEquals(Class.forName("kadai.Yen2"),m2.getClass());
		assertEquals("240.0円",m2.toString());
		Dollar2.setYenRate(100.0);
		Money2 m3 = m.getYen();
		assertEquals("300.0円",m3.toString());
	}
	@Test
	public void testToString() {
		Money2 m = new Dollar2(3);
		assertEquals("$3.0", m.toString());
	}
}

kadai/Kadai3Test.java


package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import kadai.Dollar3;
import kadai.Euro;
import kadai.Money3;
import kadai.Yen3;
public class Kadai3Test {
  final double drate=115;
  final double erate=130;
  final double gcd = 5;
  Money3 m1 ;
  Money3 m2;
  Money3 m3;
  @BeforeEach
  public void setUp() {    
    m1 = new Yen3(drate*erate/gcd);
    m2 = new Dollar3(erate/gcd);
    m3 = new Euro(drate/gcd);
  }
  @Test
  public void testGetRate() {
    setDrate();
    assertAll("Rate",
        ()->assertEquals(1,m1.getRate(),0.001,"Yen Rate"),
        ()->assertEquals(drate,m2.getRate(),0.001,"Dollar Rate"),
        ()->assertEquals(drate,m3.getRate(),0.001,"Euro Rate"));
  }
  @Test
  public void testGetValue() {
    assertAll("getValue",
        ()->assertEquals(drate*erate/gcd,m1.getValue(),0.001,"Yen value"),
        ()->assertEquals(erate/gcd,m2.getValue(),0.001,"Dollar value"),
        ()->assertEquals(drate/gcd,m3.getValue(),0.001,"Euro value"));
  }
  @Test
  public void testEquals1(){
    setDrate();  
    assertAll("Equality",
        ()->assertTrue(m1.equals(m1),"Identity"),
        ()->assertFalse(m1.equals(null),"Null check"),
        ()->assertFalse(m1.equals(new Object(){}),"Other type"),
        ()->assertTrue(m1.equals(m2),"The same amount"),
        ()->assertTrue(m2.equals(m1),"Synmetric prop"),
        ()->assertFalse(m1.equals(m3),"Different amount"),
        ()->assertFalse(m2.equals(m3),"Different amount"));
  }
  private void setDrate() {
    Yen3.setYenRate(drate);
    Dollar3.setYenRate(drate);  
    Euro.setYenRate(drate);
  }
  @Test
  public void testEquals2(){
    setErate();  
    assertAll("Equality for Euro Rate",
        ()->assertTrue(m1.equals(m1)),
        ()->assertFalse(m1.equals(m2)),
        ()->assertTrue(m1.equals(m3)),
        ()->assertFalse(m2.equals(m3)));
  }
  private void setErate() {
    Yen3.setYenRate(erate);
    Dollar3.setYenRate(erate);  
    Euro.setYenRate(erate);
  }
  @Test
  public void testHashCode(){
    assertAll("Test Hashcode",
        ()->{
          setDrate();  
          assertAll("Hashcode for DRate",
              ()->assertTrue(m1.hashCode()==m2.hashCode(),"yen v.s. dollar"),
              ()->assertFalse(m1.hashCode()==m3.hashCode(),"yen v.s. euro"),
              ()->assertFalse(m2.hashCode()==m3.hashCode(),"dollar v.s. euro"));  
        },
        ()->{  
          setErate();  
          assertAll("Hashcoe for ERate",
              ()->assertFalse(m1.hashCode()==m2.hashCode(),"yen v.s. dollar"),
              ()->assertTrue(m1.hashCode()==m3.hashCode(),"yen v.s. euro"),
              ()->assertFalse(m2.hashCode()==m3.hashCode(),"dollar v.s. euro"));  
        }
        );
  }  
  @Test
  public void testCompareToD() {
    assertAll("Compare Dollar",
        ()->{
          Dollar3.setYenRate(110);
          assertAll("Dollar rate = 110",
              ()->assertTrue(m1.compareTo(m2)>0),
              ()->assertTrue(m2.compareTo(m1)<0));
        },
        ()->{
          Dollar3.setYenRate(115);
          assertAll("Dollar rate = 115",
              ()->assertTrue(m1.compareTo(m2)==0),
              ()->assertTrue(m2.compareTo(m1)==0));
        },
        ()->{
          Dollar3.setYenRate(120);
          assertAll("Dollar rate = 120",
              ()->assertTrue(m1.compareTo(m2)<0),    
              ()->assertTrue(m2.compareTo(m1)>0));    
        }
        );
  }
  @Test
  public void testCompareToE() {
    assertAll("Compare Dollar",
        ()->{
          Euro.setYenRate(120);
          assertAll("Euro rate = 120",
              ()->assertTrue(m1.compareTo(m3)>0),
              ()->assertTrue(m3.compareTo(m1)<0));
        },
        ()->{
          Euro.setYenRate(130);
          assertAll("Euro rate = 130",
              ()->assertTrue(m1.compareTo(m3)==0),
              ()->assertTrue(m3.compareTo(m1)==0));
        },
        ()->{
          Euro.setYenRate(140);
          assertAll("Euro rate = 140",
              ()->assertTrue(m1.compareTo(m3)<0),    
              ()->assertTrue(m3.compareTo(m1)>0));    
        }
        );
  }
}