課題1テストコード

kadai/Yen1Test.java


package kadai;
import static org.junit.Assert.*;
import org.junit.Test;
public class Yen1Test {
	@Test
	public void testToString() {
		Money1 m1 = new Yen1(100);
		assertEquals("100.0円",m1.toString());
	}
}

kadai/Dollar1Test.java


package kadai;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class Dollar1Test {
	@Test
	public void testToString() {
		Money1 m1 = new Dollar1(100);
		assertEquals("$100.0",m1.toString());
	}
}

kadai/Yen2Test.java


package kadai;
import static org.junit.Assert.assertEquals;
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 kadai;
import static org.junit.Assert.*;
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 kadai;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class Kadai3Test {
	Money3 m1 ;
	Money3 m2;
	Money3 m3;
	@Before
	public void setUp() {
		m1 = new Yen3(270);
		m2 = new Dollar3(3);
		m3 = new Euro(2);
	}
	@Test
	public void testGetRate() {
		Yen3.setYenRate(80);
		Dollar3.setYenRate(80);	
		Euro.setYenRate(80);	
		assertEquals(1,m1.getRate(),0.001);
		assertEquals(80,m2.getRate(),0.001);
		assertEquals(80,m3.getRate(),0.001);
	}
	@Test
	public void testGetValue() {
		assertEquals(270,m1.getValue(),0.001);
		assertEquals(3,m2.getValue(),0.001);
		assertEquals(2,m3.getValue(),0.001);
	}
	@Test
	public void testEquals1(){
		Yen3.setYenRate(90);
		Dollar3.setYenRate(90);	
		Euro.setYenRate(90);	
		assertTrue(m1.equals(m1));
		assertFalse(m1.equals(null));
		assertFalse(m1.equals(new Object(){}));
		assertTrue(m1.equals(m2));
		assertTrue(m2.equals(m1));
		assertFalse(m1.equals(m3));
		assertFalse(m2.equals(m3));
	}
	@Test
	public void testEquals2(){
		Yen3.setYenRate(135);
		Dollar3.setYenRate(135);	
		Euro.setYenRate(135);	
		assertTrue(m1.equals(m1));
		assertFalse(m1.equals(m2));
		assertTrue(m1.equals(m3));
		assertFalse(m2.equals(m3));
	}
	@Test
	public void testHashCode1(){
		Yen3.setYenRate(90);
		Dollar3.setYenRate(90);	
		Euro.setYenRate(90);	
		assertTrue(m1.hashCode()==m2.hashCode());
		assertFalse(m1.hashCode()==m3.hashCode());
		assertFalse(m2.hashCode()==m3.hashCode());	
	}
	@Test
	public void testHashCode2(){
		Yen3.setYenRate(135);
		Dollar3.setYenRate(135);	
		Euro.setYenRate(135);	
		assertFalse(m1.hashCode()==m2.hashCode());
		assertTrue(m1.hashCode()==m3.hashCode());
		assertFalse(m2.hashCode()==m3.hashCode());	
	}
	@Test
	public void testCompareToD() {
		Dollar3.setYenRate(80);
		assertTrue(m1.compareTo(m2)>0);
		assertTrue(m2.compareTo(m1)<0);
		Dollar3.setYenRate(90);
		assertTrue(m1.compareTo(m2)==0);
		assertTrue(m2.compareTo(m1)==0);
		Dollar3.setYenRate(100);
		assertTrue(m1.compareTo(m2)<0);		
		assertTrue(m2.compareTo(m1)>0);
	}
	@Test
	public void testCompareToE() {
		Euro.setYenRate(115);
		assertTrue(m1.compareTo(m3)>0);
		assertTrue(m3.compareTo(m1)<0);
		Euro.setYenRate(135);
		assertTrue(m1.compareTo(m3)==0);
		assertTrue(m3.compareTo(m1)==0);
		Euro.setYenRate(155);
		assertTrue(m1.compareTo(m3)<0);		
		assertTrue(m3.compareTo(m1)>0);
	}
}