Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
15 kB
1
Indexable
Never
package demo;

import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class StudentTest {
	
	main test = null;

	@BeforeEach
	void setUp() throws Exception {
		test = new main();
	}

	@AfterEach
	void tearDown() throws Exception {
		test = null;
	}

	/**
	 * test method: main.printCalender(String date)
	 * test date: 2022/03/15
	 */
	@Test
	void testA_1() {
		String input = "2022/03/15";
		String expect_output = 
				 "Sun Mon Tue Wed Thu Fri Sat\r\n" + 
				 "          1   2   3   4   5 \r\n" + 
				 "  6   7   8   9  10  11  12 \r\n" + 
				 " 13  14  15  16  17  18  19 \r\n" + 
				 " 20  21  22  23  24  25  26 \r\n" + 
				 " 27  28  29  30  31 \r\n";
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testA_1\n");
		System.out.println("請輸入欲查詢日期: ");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printCalender(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printCalender(String date)
	 * test date: 2022/02/15 
	 */
	@Test
	void testA_2() {
		String input = "2022/02/15";
		String expect_output = 
				 "Sun Mon Tue Wed Thu Fri Sat\r\n" + 
				 "          1   2   3   4   5 \r\n" + 
				 "  6   7   8   9  10  11  12 \r\n" + 
				 " 13  14  15  16  17  18  19 \r\n" + 
				 " 20  21  22  23  24  25  26 \r\n" + 
				 " 27  28 \r\n";
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testA_2\n");
		System.out.println("請輸入欲查詢日期: ");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printCalender(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}

	/**
	 * test method: main.printZodiac(int year)
	 * test year: 2022
	 */
	@Test
	void testB_1() {
		int input = 2022;
		String expect_output = 
				 "2022是壬寅年,屬虎\n";
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		//ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testB_1\n");
		//System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printZodiac(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printZodiac(int year)
	 * test year: 2021
	 */
	@Test
	void testB_2() {
		int input = 2021;
		String expect_output = 
				"2021是辛丑年,屬牛\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		//ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testB_2\n");
		//System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printZodiac(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printDays(String day)
	 * test day: 2022-03-15
	 */
	@Test
	void testC_1() {
		String input = "2022-03-15";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long time = 0;
		try {
			Date targerDay = sdf.parse(input);	
			long targetTime = targerDay.getTime();
			long todaytime = new Date().getTime();
			long t = todaytime - todaytime % (1000 * 60 * 60 * 24) - 28800000;
			time = Math.abs(targetTime - t);
			String today = sdf.format(new Date());
			//System.out.println("今天是" + today);
		} catch (ParseException e) {}
		String expect_output = 
				"距離" + input + "有" + (time) / 1000 / 60 / 60 / 24 + "天\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testC_1\n");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printDays(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printDays(String day)
	 * test day: 2022-03-18
	 */
	@Test
	void testC_2() {
		String input = "2022-03-18";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		long time = 0;
		try {
			Date targerDay = sdf.parse(input);	
			long targetTime = targerDay.getTime();
			long todaytime = new Date().getTime();
			long t = todaytime - todaytime % (1000 * 60 * 60 * 24) - 28800000;
			time = Math.abs(targetTime - t);
			String today = sdf.format(new Date());
			//System.out.println("今天是" + today);
		} catch (ParseException e) {}
		String expect_output = 
				"距離" + input + "有" + (time) / 1000 / 60 / 60 / 24 + "天\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testC_2\n");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printDays(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printAfterDays(int days)
	 * test days: 2
	 */
	@Test
	void testD_1() {
		int input = 2;
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DATE, input);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String expect_output = 
				"往後" + input + "天是" + sdf.format(today.getTime()) + "\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		//ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testD_1\n");
		//System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printAfterDays(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printAfterDays(int days)
	 * test days: 0 (today)
	 */
	@Test
	void testD_2() {
		int input = 0; //今天
		Calendar today = Calendar.getInstance();
		today.add(Calendar.DATE, input);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String expect_output = 
				"往後" + input + "天是" + sdf.format(today.getTime()) + "\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		//ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testD_2\n");
		//System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.printAfterDays(input);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	
	/**
	 * test method: main.printCalender(String date)
	 * test date: 2022/03/15
	 * test method: main.printZodiac(int year)
	 * test year: 2022
	 * test method: main.printDays(String day)
	 * test day: 2022-03-15
	 * test method: main.printAfterDays(int days)
	 * test days: 2
	 */
	@Test
	void testAll_1() {
		String inputc = "2022-03-15";
		String inputd = "2";
		String input = 
				//A
				"A\n" +
				"2022/03/15\r\n" +
				//B
				"B\n" +
				"2022\n" +
				//C
				"C\n" +
				inputc + "\n" +
				//D
				"D\n" +
				inputd + "\n" +
				//E
				"E\n";
		//A
		//B
		//C
		SimpleDateFormat sdfc = new SimpleDateFormat("yyyy-MM-dd");
		long timec = 0;
		try {
			Date targerDayc = sdfc.parse(inputc);	
			long targetTimec = targerDayc.getTime();
			long todaytimec = new Date().getTime();
			long tc = todaytimec - todaytimec % (1000 * 60 * 60 * 24) - 28800000;
			timec = Math.abs(targetTimec - tc);
			String todayc = sdfc.format(new Date());
			//System.out.println("今天是" + today);
		} catch (ParseException e) {}
		//D
		Calendar todayd = Calendar.getInstance();
		todayd.add(Calendar.DATE, Integer.parseInt(inputd));
		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
		//E
		String expect_output = 
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//A
				"請輸入欲查詢日期(年/月/日): \r\n"+
				
				"Sun Mon Tue Wed Thu Fri Sat\r\n" + 
				"          1   2   3   4   5 \r\n" + 
				"  6   7   8   9  10  11  12 \r\n" + 
				" 13  14  15  16  17  18  19 \r\n" + 
				" 20  21  22  23  24  25  26 \r\n" + 
				" 27  28  29  30  31 \r\n" +
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//B
				"請輸入欲查詢年: \r\n"+
				"2022是壬寅年,屬虎\n" +
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//C
				"請輸入您要算的日期 (格式:yyyy-MM-dd)\r\n" +
				"距離" + inputc + "有" + (timec) / 1000 / 60 / 60 / 24 + "天\r\n" +
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//D
				"請輸入往後推算的天數: \r\n" +
				
				"往後" + inputd + "天是" + sdfd.format(todayd.getTime()) + "\r\n" +
				//E
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				
				"離開系統\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testAll_1\n");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.main(null);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
	

	/**
	 * test method: main.printCalender(String date)
	 * test date: 2022/02/15
	 * test method: main.printZodiac(int year)
	 * test year: 2021
	 * test method: main.printDays(String day)
	 * test day: 2022-03-18
	 * test method: main.printAfterDays(int days)
	 * test days: 0 (today) 
	 */
	@Test
	void testAll_2() {
		String inputc = "2022-03-18";
		String inputd = "0";
		String input = 
				//A
				"A\n" +
				"2022/02/15\r\n" +
				//B
				"B\n" +
				"2021\n" +
				//C
				"C\n" +
				inputc + "\n" +
				//D
				"D\n" +
				inputd + "\n" +
				//E
				"E\n";
		//A
		//B
		//C
		SimpleDateFormat sdfc = new SimpleDateFormat("yyyy-MM-dd");
		long timec = 0;
		try {
			Date targerDayc = sdfc.parse(inputc);	
			long targetTimec = targerDayc.getTime();
			long todaytimec = new Date().getTime();
			long tc = todaytimec - todaytimec % (1000 * 60 * 60 * 24) - 28800000;
			timec = Math.abs(targetTimec - tc);
			String todayc = sdfc.format(new Date());
			//System.out.println("今天是" + today);
		} catch (ParseException e) {}
		//D
		Calendar todayd = Calendar.getInstance();
		todayd.add(Calendar.DATE, Integer.parseInt(inputd));
		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
		//E
		String expect_output = 
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//A
				"請輸入欲查詢日期(年/月/日): \r\n"+
				
				 "Sun Mon Tue Wed Thu Fri Sat\r\n" + 
				 "          1   2   3   4   5 \r\n" + 
				 "  6   7   8   9  10  11  12 \r\n" + 
				 " 13  14  15  16  17  18  19 \r\n" + 
				 " 20  21  22  23  24  25  26 \r\n" + 
				 " 27  28 \r\n" +
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//B
				"請輸入欲查詢年: \r\n"+
				"2021是辛丑年,屬牛\n" +
				
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//C
				"請輸入您要算的日期 (格式:yyyy-MM-dd)\r\n" +
				"距離" + inputc + "有" + (timec) / 1000 / 60 / 60 / 24 + "天\r\n" +
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				//D
				"請輸入往後推算的天數: \r\n" +
				
				"往後" + inputd + "天是" + sdfd.format(todayd.getTime()) + "\r\n" +
				//E
				"輸入指令號碼或 E(結束使用)?\r\n" + 
				"\r\n" + 
				"輸入指令: \r\n" + 
				"1) A 顯示該月月曆\r\n" + 
				"2) B 西元轉換干支、生肖\r\n" + 
				"3) C 計算天數\r\n" + 
				"4) D 計算日期\r\n" + 
				"5) E 結束使用\r\n" +
				
				"離開系統\r\n" ;
		ByteArrayOutputStream testOut = new ByteArrayOutputStream();
		ByteArrayInputStream testIn = new ByteArrayInputStream(input.getBytes());
		System.out.println("this is testAll_2\n");
		System.setIn(testIn);
		System.setOut(new PrintStream(testOut));
		test.main(null);
		String output = new String(testOut.toByteArray());
		assertEquals(expect_output, output);
	}
}