課題1テストコード

GameTest.java(11/22版完全差し替え)


import static org.junit.Assert.*;
import java.util.Scanner;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
public class GameTest {
	private String[] line;
	@Before
	public void setUp(){
		ByteArrayOutputStream bas = new ByteArrayOutputStream();
		PrintStream newout = new PrintStream(bas);
		System.setOut(newout);
		String command;
		command = "help\n";
		command += "list\n";
		command += "end\n";
		Scanner sc = new Scanner(command);
		Game game = new Game(3,sc);
		game.run();
		line = bas.toString().split(System.getProperty("line.separator"));
	}
	@Test
	public void test0(){
		assertEquals("0: null, 1: null, 2: null",line[0]);
	}
	@Test
	public void testHelp(){
		assertEquals("使い方: コマンド [引数]",line[1]);
		assertEquals("put: 位置 作物名 : 作物を指定の位置に植える",line[2]);
		assertEquals("age: 一日経過させる",line[3]);
		assertEquals("list: 作物一覧",line[4]);
		assertEquals("end : 終了",line[5]);
		assertEquals("0: null, 1: null, 2: null",line[6]);
	}
	@Test
	public void testLine(){
		assertEquals("[apple]",line[7]);
		assertEquals("0: null, 1: null, 2: null",line[8]);
	}
}

garden/DummySakumotsu.java


package garden;

class DummySakumotsu extends Sakumotsu {
	protected int calledParameter;
	protected boolean aged=false;
	public DummySakumotsu() {
		super("test");
	}
	class DummyCondition implements Condition {
		public DummyCondition(){}
		public String get(int n){ 
			calledParameter=n;
			return "abc";
			}
		@Override	public boolean isOverage(int age) {
			calledParameter=age;
			return true;
		}
	}
	@Override	public Condition getCondition() {
		return new DummyCondition();
	}
	@Override public void age(){
		super.age();
		aged = true;
	}
}

garden/SakumotsuTest.java


package garden;

import static org.junit.Assert.*;

import org.junit.Test;

public class SakumotsuTest {
	@Test
	public void testAge() {
		DummySakumotsu sakumotsu = new DummySakumotsu();
		sakumotsu.aged = false;
		sakumotsu.age();
		assertTrue(sakumotsu.aged); 
	}

	@Test
	public void testToString() {
		DummySakumotsu sakumotsu = new DummySakumotsu();
		assertEquals("testはabc",sakumotsu.toString());
	}

	@Test
	public void testIsOverage() {
		DummySakumotsu sakumotsu = new DummySakumotsu();
		assertTrue(sakumotsu.isOverage());
		assertEquals(0,sakumotsu.calledParameter);
	}

	@Test
	public void testGetCondition() {
		DummySakumotsu sakumotsu = new DummySakumotsu();
		assertEquals("garden.DummySakumotsu$DummyCondition",sakumotsu.getCondition().getClass().getName());
	}

	@Test
	public void testClone() {
		Sakumotsu sakumotsu = new DummySakumotsu();
		Sakumotsu newSakumotsu = sakumotsu.clone();
		assertNotSame(newSakumotsu,sakumotsu);
		assertEquals(sakumotsu.getClass(),newSakumotsu.getClass());
	}
}

garden/HatakeTest.java


package garden;

import static org.junit.Assert.*;

import org.junit.Test;

public class HatakeTest {
	@Test
	public void testToString() {
		Hatake hatake = new Hatake(1);
		assertEquals("0: null",hatake.toString());
	}
	@Test
	public void testPut() {
		Hatake hatake = new Hatake(1);
		hatake.put(0, new DummySakumotsu());
		assertEquals("0: testはabc",hatake.toString());
	}
	
	@Test
	public void testAge() {
		Hatake hatake = new Hatake(1);
		DummySakumotsu sakumotsu = new DummySakumotsu();
		hatake.put(0, sakumotsu);
		hatake.age();
		assertTrue(sakumotsu.aged);
		assertEquals("0: null",hatake.toString());
	}

}

garden/KajuConditionTest.java


package garden;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class KajuConditionTest {
  private KajuCondition kajuCondition;
  @Before
  public void setUp() throws Exception {
    kajuCondition = new KajuCondition();
  }
  @Test
  public void testGet() {
    assertEquals(kajuCondition.get(0),"種");
    assertEquals(kajuCondition.get(1),"育ってます");
    assertEquals(kajuCondition.get(2),"花が咲きました");
    assertEquals(kajuCondition.get(3),"実がなりました");
    assertEquals(kajuCondition.get(4),"葉が散りました");
    assertEquals(kajuCondition.get(5),"葉が繁りました");
    assertEquals(kajuCondition.get(6),"花が咲きました");
    assertEquals(kajuCondition.get(7),"実がなりました");
    assertEquals(kajuCondition.get(8),"枯れました");
  }
  @Test
  public void testIsOverage() {
    assertEquals(kajuCondition.isOverage(0),false);
    assertEquals(kajuCondition.isOverage(8),false);
    assertEquals(kajuCondition.isOverage(9),true);
  }

}

garden/AppleTest.java


package garden;
import static org.junit.Assert.*;

import org.junit.Test;


public class AppleTest {
  @Test
  public void testGetCondition() {
    Sakumotsu sakumotsu = new Apple();
    assertEquals("garden.KajuCondition",sakumotsu.getCondition().getClass().getName());
  }
  @Test
  public void test() {
    Sakumotsu sakumotsu = new Apple();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは種",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは育ってます",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは花が咲きました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは実がなりました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは葉が散りました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは葉が繁りました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは花が咲きました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは実がなりました",sakumotsu.toString());
    sakumotsu.age();
    assertFalse(sakumotsu.isOverage());
    assertEquals("りんごは枯れました",sakumotsu.toString());
    sakumotsu.age();
    assertTrue(sakumotsu.isOverage());

  }
  @Test
  public void testClone() {
    Sakumotsu sakumotsu = new Apple();
    Sakumotsu newSakumotsu = sakumotsu.clone();
    assertNotSame(newSakumotsu,sakumotsu);
    assertEquals(sakumotsu.getClass(),newSakumotsu.getClass());
  }
}

garden/TaneTest.java


package garden;

import static org.junit.Assert.*;

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

public class TaneTest {
	@Test
	public void testGetInstance() {
		Tane tane = new Tane();
		assertEquals("garden.Apple",tane.getInstance("apple").getClass().getName());
	}

	@Test
	public void testList() {
		Tane tane = new Tane();
		Set<String> set = new HashSet<String>();
		set.add("apple");
		assertEquals(set,tane.list());
	}

}