From c5a37847706178f5ac4a9f2803cef6b28c3646a3 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Thu, 13 Jul 2017 11:00:46 +0800 Subject: [PATCH 1/8] payroll --- .../com/coderising/payroll/AddEmployee.java | 57 +++++++++ .../com/coderising/payroll/Affiliation.java | 5 + .../com/coderising/payroll/BankMethod.java | 21 ++++ .../coderising/payroll/BiWeeklySchedule.java | 20 ++++ .../payroll/CommissionedClassification.java | 36 ++++++ .../src/com/coderising/payroll/DateUtil.java | 85 +++++++++++++ .../com/coderising/payroll/DateUtilTest.java | 113 ++++++++++++++++++ .../src/com/coderising/payroll/Employee.java | 53 ++++++++ .../com/coderising/payroll/HoldMethod.java | 13 ++ .../payroll/HourlyClassification.java | 43 +++++++ .../com/coderising/payroll/MailMethod.java | 19 +++ .../coderising/payroll/MonthlySchedule.java | 17 +++ .../coderising/payroll/NonAffiliation.java | 11 ++ .../src/com/coderising/payroll/Paycheck.java | 50 ++++++++ .../payroll/PaymentClassification.java | 5 + .../com/coderising/payroll/PaymentMethod.java | 5 + .../coderising/payroll/PaymentSchedule.java | 8 ++ .../payroll/SalariedClassification.java | 16 +++ .../com/coderising/payroll/SalesReceipt.java | 20 ++++ .../com/coderising/payroll/ServiceCharge.java | 23 ++++ .../src/com/coderising/payroll/Test.java | 40 +++++++ .../src/com/coderising/payroll/TimeCard.java | 21 ++++ .../coderising/payroll/UnionAffiliation.java | 35 ++++++ .../coderising/payroll/WeeklySchedule.java | 17 +++ 24 files changed, 733 insertions(+) create mode 100644 students/1299310140/src/com/coderising/payroll/AddEmployee.java create mode 100644 students/1299310140/src/com/coderising/payroll/Affiliation.java create mode 100644 students/1299310140/src/com/coderising/payroll/BankMethod.java create mode 100644 students/1299310140/src/com/coderising/payroll/BiWeeklySchedule.java create mode 100644 students/1299310140/src/com/coderising/payroll/CommissionedClassification.java create mode 100644 students/1299310140/src/com/coderising/payroll/DateUtil.java create mode 100644 students/1299310140/src/com/coderising/payroll/DateUtilTest.java create mode 100644 students/1299310140/src/com/coderising/payroll/Employee.java create mode 100644 students/1299310140/src/com/coderising/payroll/HoldMethod.java create mode 100644 students/1299310140/src/com/coderising/payroll/HourlyClassification.java create mode 100644 students/1299310140/src/com/coderising/payroll/MailMethod.java create mode 100644 students/1299310140/src/com/coderising/payroll/MonthlySchedule.java create mode 100644 students/1299310140/src/com/coderising/payroll/NonAffiliation.java create mode 100644 students/1299310140/src/com/coderising/payroll/Paycheck.java create mode 100644 students/1299310140/src/com/coderising/payroll/PaymentClassification.java create mode 100644 students/1299310140/src/com/coderising/payroll/PaymentMethod.java create mode 100644 students/1299310140/src/com/coderising/payroll/PaymentSchedule.java create mode 100644 students/1299310140/src/com/coderising/payroll/SalariedClassification.java create mode 100644 students/1299310140/src/com/coderising/payroll/SalesReceipt.java create mode 100644 students/1299310140/src/com/coderising/payroll/ServiceCharge.java create mode 100644 students/1299310140/src/com/coderising/payroll/Test.java create mode 100644 students/1299310140/src/com/coderising/payroll/TimeCard.java create mode 100644 students/1299310140/src/com/coderising/payroll/UnionAffiliation.java create mode 100644 students/1299310140/src/com/coderising/payroll/WeeklySchedule.java diff --git a/students/1299310140/src/com/coderising/payroll/AddEmployee.java b/students/1299310140/src/com/coderising/payroll/AddEmployee.java new file mode 100644 index 0000000000..48341143d9 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/AddEmployee.java @@ -0,0 +1,57 @@ +package com.coderising.payroll; + +public class AddEmployee { + public static Employee addHourlyEmployee(String name,String address,double rate,String[] timeCardDates,int[] timeCardhours){ + Employee e = new Employee(name,address); + HourlyClassification classification = new HourlyClassification(rate); + for(int i = 0;i < timeCardDates.length;i++){ + TimeCard tc = new TimeCard(DateUtil.parseDate(timeCardDates[i]),timeCardhours[i]); + classification.addTimeCard(tc); + } + Affiliation affiliation = new NonAffiliation(); + PaymentMethod paymentMethod = new HoldMethod(); + PaymentSchedule paymentSchedule = new WeeklySchedule(); + + e.setClassification(classification); + e.setAffiliation(affiliation); + e.setPaymentMethod(paymentMethod); + e.setSchedule(paymentSchedule); + return e; + } + + public static Employee addSalariedEmployee(String name,String address,double salary,String[] serviceChargeDates,int[] serviceChargeAmount){ + Employee e = new Employee(name,address); + SalariedClassification sc = new SalariedClassification(salary); + UnionAffiliation ua = new UnionAffiliation(name); + for(int i = 0;i < serviceChargeDates.length;i++){ + ServiceCharge serviceCharge = new ServiceCharge(DateUtil.parseDate(serviceChargeDates[i]),serviceChargeAmount[i]); + ua.addServiceCharge(serviceCharge); + } + PaymentMethod pm = new MailMethod(address); + PaymentSchedule ps = new MonthlySchedule(); + + e.setClassification(sc); + e.setAffiliation(ua); + e.setPaymentMethod(pm); + e.setSchedule(ps); + return e; + } + + public static Employee addCommissionEmployee(String name,String address,String bank, String account,double salary,double rate,String[] salesReceiptDates,int[] salesReceiptAmount){ + Employee e = new Employee(name,address); + CommissionedClassification classification = new CommissionedClassification(salary,rate); + for(int i = 0;i < salesReceiptDates.length;i++){ + SalesReceipt sr = new SalesReceipt(DateUtil.parseDate(salesReceiptDates[i]),salesReceiptAmount[i]); + classification.addSalesReceipt(sr); + } + Affiliation affiliation = new NonAffiliation(); + PaymentMethod paymentMethod = new BankMethod(bank,account);//new HoldMethod(); + PaymentSchedule paymentSchedule = new BiWeeklySchedule(); + + e.setClassification(classification); + e.setAffiliation(affiliation); + e.setPaymentMethod(paymentMethod); + e.setSchedule(paymentSchedule); + return e; + } +} diff --git a/students/1299310140/src/com/coderising/payroll/Affiliation.java b/students/1299310140/src/com/coderising/payroll/Affiliation.java new file mode 100644 index 0000000000..e8f44acfa7 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/Affiliation.java @@ -0,0 +1,5 @@ +package com.coderising.payroll; + +public interface Affiliation { + public double calculateDeductions(Paycheck pc); +} diff --git a/students/1299310140/src/com/coderising/payroll/BankMethod.java b/students/1299310140/src/com/coderising/payroll/BankMethod.java new file mode 100644 index 0000000000..8e2fac62c0 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/BankMethod.java @@ -0,0 +1,21 @@ +package com.coderising.payroll; + +public class BankMethod implements PaymentMethod { + private String bank = ""; + private String account = ""; + + public BankMethod(String bank, String account) { + super(); + this.bank = bank; + this.account = account; + } + + @Override + public void pay(Paycheck pc) { + System.out.println("已将工资转入" + bank + "的" + account + "账户"); + System.out.println("应付" + pc.getGrossPay()); + System.out.println("扣除" + pc.getDeductions()); + System.out.println("实付" + pc.getNetPay()); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/BiWeeklySchedule.java b/students/1299310140/src/com/coderising/payroll/BiWeeklySchedule.java new file mode 100644 index 0000000000..28bfd31d52 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/BiWeeklySchedule.java @@ -0,0 +1,20 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class BiWeeklySchedule implements PaymentSchedule { + Date firstPayableFriday = DateUtil.parseDate("2017-6-2"); + + @Override + public boolean isPayDate(Date date) { + + long interval = DateUtil.getDaysBetween(firstPayableFriday,date); + return interval % 14 == 0; + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -13); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/CommissionedClassification.java b/students/1299310140/src/com/coderising/payroll/CommissionedClassification.java new file mode 100644 index 0000000000..cd70102ebd --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/CommissionedClassification.java @@ -0,0 +1,36 @@ +package com.coderising.payroll; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class CommissionedClassification implements PaymentClassification { + + double salary; + double rate; + Map receipts; + + public CommissionedClassification(double salary, double rate) { + super(); + this.salary = salary; + this.rate = rate; + this.receipts = new HashMap(); + } + + public void addSalesReceipt(SalesReceipt sr){ + receipts.put(sr.getSaleDate(), sr); + } + + @Override + public double calculatePay(Paycheck pc) { + double commission = 0; + for (SalesReceipt salesReceipt : receipts.values()) { + if(DateUtil.between(salesReceipt.getSaleDate(), + pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) { + commission += salesReceipt.getAmount() * rate; + } + } + return commission + salary; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/DateUtil.java b/students/1299310140/src/com/coderising/payroll/DateUtil.java new file mode 100644 index 0000000000..52a999620d --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/DateUtil.java @@ -0,0 +1,85 @@ +package com.coderising.payroll; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class DateUtil { + + private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + + private static Calendar calendar = Calendar.getInstance(); + + public static boolean between(Date date, Date startDate, + Date endDate) { +// boolean result = date.after(startDate) && date.before(endDate);//不包括边界 + boolean result = getDaysBetween(startDate,date) >= 0 && getDaysBetween(date,endDate) >= 0;//包括边界 + return result; + } + + public static boolean isFriday(Date date) { + calendar.setTime(date); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + return dayOfWeek == 6; + } + + public static Date add(Date date, int i) { + calendar.setTime(date); + calendar.add(Calendar.DATE, i); + return calendar.getTime(); + } + + public static boolean isLastDayOfMonth(Date date) { + calendar.setTime(date); + calendar.add(Calendar.MONTH, 1); + calendar.set(Calendar.DAY_OF_MONTH, 0); + return date.equals(calendar.getTime()); + } + + public static Date getFirstDay(Date date) { + calendar.setTime(date); + calendar.add(Calendar.MONTH, 0); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return calendar.getTime(); + } + + public static Date parseDate(String dateString){ + try + { + Date date = format.parse(dateString); + return date; + } + catch (ParseException e) + { + System.out.println(e.getMessage()); + } + return null; + } + + public static long getDaysBetween(Date startDate, Date endDate) { + return (endDate.getTime() - startDate.getTime()) / (1000*3600*24); + } + + public static int fridaysNum(Date startDate, Date endDate) { + int interval = (int) getDaysBetween(startDate,endDate); + int fridaysNumber = interval / 7; + + startDate = add(startDate,fridaysNumber * 7); + interval = (int) getDaysBetween(startDate,endDate); + calendar.setTime(startDate); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + for(int i = 0;i <= interval;i++){ + if(dayOfWeek == 6){ + fridaysNumber++; + break; + } + dayOfWeek++; + if(dayOfWeek == 8){ + dayOfWeek = 1; + } + } + return fridaysNumber; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/DateUtilTest.java b/students/1299310140/src/com/coderising/payroll/DateUtilTest.java new file mode 100644 index 0000000000..39c5099ee5 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/DateUtilTest.java @@ -0,0 +1,113 @@ +package com.coderising.payroll; + +import static org.junit.Assert.fail; + +import java.util.Date; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DateUtilTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testBetween() { + Date dateOne = DateUtil.parseDate("2017-6-29"); + Date dateTwo = DateUtil.parseDate("2017-6-30"); + Date dateThree = DateUtil.parseDate("2017-7-1"); + Date dateFour = DateUtil.parseDate("2017-7-2"); + Date dateFive = DateUtil.parseDate("2017-7-3"); + + Date startDate = DateUtil.parseDate("2017-6-30"); + Date endDate = DateUtil.parseDate("2017-7-2"); + + Assert.assertEquals(false,DateUtil.between(dateOne, startDate, endDate)); + Assert.assertEquals(true,DateUtil.between(dateTwo, startDate, endDate)); + Assert.assertEquals(true,DateUtil.between(dateThree, startDate, endDate)); + Assert.assertEquals(true,DateUtil.between(dateFour, startDate, endDate)); + Assert.assertEquals(false,DateUtil.between(dateFive, startDate, endDate)); + } + + @Test + public void testIsFriday() { + Assert.assertEquals(true,DateUtil.isFriday(DateUtil.parseDate("2017-7-7"))); + Assert.assertEquals(true,DateUtil.isFriday(DateUtil.parseDate("2017-7-14"))); + Assert.assertEquals(false,DateUtil.isFriday(DateUtil.parseDate("2017-7-6"))); + Assert.assertEquals(false,DateUtil.isFriday(DateUtil.parseDate("2017-7-8"))); + } + + @Test + public void testAdd() { + Date dateOne = DateUtil.parseDate("2017-7-2"); + Date dateTwo = DateUtil.parseDate("2017-7-30"); + + Assert.assertEquals("Sun Jul 02 00:00:00 CST 2017", DateUtil.add(dateOne, 0).toString()); + Assert.assertEquals("Tue Jul 04 00:00:00 CST 2017", DateUtil.add(dateOne, 2).toString()); + Assert.assertEquals("Fri Jun 30 00:00:00 CST 2017", DateUtil.add(dateOne, -2).toString()); + + Assert.assertEquals("Wed Aug 02 00:00:00 CST 2017", DateUtil.add(dateTwo, 3).toString()); + Assert.assertEquals("Sat Jul 29 00:00:00 CST 2017", DateUtil.add(dateTwo, -1).toString()); + } + + @Test + public void testIsLastDayOfMonth() { + Assert.assertEquals(true, DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-6-30"))); + Assert.assertEquals(false,DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-6-31"))); + Assert.assertEquals(true, DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-7-31"))); + Assert.assertEquals(false,DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-7-32"))); + Assert.assertEquals(true, DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-12-31"))); + Assert.assertEquals(true, DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-2-28"))); + Assert.assertEquals(false, DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-2-29"))); + } + + @Test + public void testGetFirstDay() { + Assert.assertEquals("Sat Jul 01 00:00:00 CST 2017", DateUtil.getFirstDay(DateUtil.parseDate("2017-7-1")).toString()); + Assert.assertEquals("Sat Jul 01 00:00:00 CST 2017", DateUtil.getFirstDay(DateUtil.parseDate("2017-7-15")).toString()); + Assert.assertEquals("Sat Jul 01 00:00:00 CST 2017", DateUtil.getFirstDay(DateUtil.parseDate("2017-7-31")).toString()); + Assert.assertEquals("Tue Aug 01 00:00:00 CST 2017", DateUtil.getFirstDay(DateUtil.parseDate("2017-7-32")).toString()); + } + + @Test + public void testParseDate() { + Assert.assertEquals("Sat Jul 01 00:00:00 CST 2017", DateUtil.parseDate("2017-7-1").toString()); + Assert.assertEquals("Sat Jul 01 00:00:00 CST 2017", DateUtil.parseDate("2017-07-01").toString()); + Assert.assertEquals("Mon Jul 03 00:00:00 CST 2017", DateUtil.parseDate("2017-6-33").toString()); + Assert.assertEquals("Tue Aug 01 00:00:00 CST 2017", DateUtil.parseDate("2017-7-32").toString()); + Assert.assertEquals("Mon Jul 31 00:00:00 CST 2017", DateUtil.parseDate("2017-8-0").toString()); + } + + @Test + public void testGetDaysBetween() { + Assert.assertEquals(0, DateUtil.getDaysBetween(DateUtil.parseDate("2017-7-1"),DateUtil.parseDate("2017-7-1"))); + Assert.assertEquals(1, DateUtil.getDaysBetween(DateUtil.parseDate("2017-7-1"),DateUtil.parseDate("2017-7-2"))); + Assert.assertEquals(-1, DateUtil.getDaysBetween(DateUtil.parseDate("2017-7-2"),DateUtil.parseDate("2017-7-1"))); + Assert.assertEquals(3, DateUtil.getDaysBetween(DateUtil.parseDate("2017-6-29"),DateUtil.parseDate("2017-7-2"))); + } + + @Test + public void testFridaysNum() { + Assert.assertEquals(1, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-7"),DateUtil.parseDate("2017-7-7"))); + Assert.assertEquals(0, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-2"),DateUtil.parseDate("2017-7-3"))); + Assert.assertEquals(0, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-5"),DateUtil.parseDate("2017-7-6"))); + Assert.assertEquals(1, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-6"),DateUtil.parseDate("2017-7-7"))); + Assert.assertEquals(1, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-7"),DateUtil.parseDate("2017-7-8"))); + Assert.assertEquals(0, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-8"),DateUtil.parseDate("2017-7-9"))); + Assert.assertEquals(1, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-6"),DateUtil.parseDate("2017-7-8"))); + Assert.assertEquals(2, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-7"),DateUtil.parseDate("2017-7-14"))); + Assert.assertEquals(2, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-6"),DateUtil.parseDate("2017-7-15"))); + Assert.assertEquals(2, DateUtil.fridaysNum(DateUtil.parseDate("2017-7-8"),DateUtil.parseDate("2017-7-21"))); + Assert.assertEquals(6, DateUtil.fridaysNum(DateUtil.parseDate("2017-6-30"),DateUtil.parseDate("2017-8-4"))); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/Employee.java b/students/1299310140/src/com/coderising/payroll/Employee.java new file mode 100644 index 0000000000..90cb38b343 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/Employee.java @@ -0,0 +1,53 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class Employee { + + String id; + String name; + String address; + + Affiliation affiliation; + PaymentClassification classification; + PaymentSchedule schedule; + PaymentMethod paymentMethod; + + public Employee(String name, String address){ + this.name = name; + this.address = address; + } + + public boolean isPayDay(Date d) { + return this.schedule.isPayDate(d); + } + + public Date getPayPeriodStartDate(Date d) { + return this.schedule.getPayPeriodStartDate(d); + } + + public void payDay(Paycheck pc){//重点,笼统说话 + double grossPay = this.classification.calculatePay(pc); + double deduction = this.affiliation.calculateDeductions(pc); + double netPay = grossPay - deduction; + pc.setGrossPay(grossPay); + pc.setDeductions(deduction); + pc.setNetPay(netPay); + this.paymentMethod.pay(pc); + } + + public void setClassification(PaymentClassification classification) { + this.classification = classification; + } + public void setSchedule(PaymentSchedule schedule) { + this.schedule = schedule; + } + public void setPaymentMethod(PaymentMethod paymentMethod) { + this.paymentMethod = paymentMethod; + } + public void setAffiliation(Affiliation affiliation) { + this.affiliation = affiliation; + } + +} + diff --git a/students/1299310140/src/com/coderising/payroll/HoldMethod.java b/students/1299310140/src/com/coderising/payroll/HoldMethod.java new file mode 100644 index 0000000000..74dea2d086 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/HoldMethod.java @@ -0,0 +1,13 @@ +package com.coderising.payroll; + +public class HoldMethod implements PaymentMethod { + + @Override + public void pay(Paycheck pc) { + System.out.println("工资保存在财务那,可随时支取"); + System.out.println("应付" + pc.getGrossPay()); + System.out.println("扣除" + pc.getDeductions()); + System.out.println("实付" + pc.getNetPay()); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/HourlyClassification.java b/students/1299310140/src/com/coderising/payroll/HourlyClassification.java new file mode 100644 index 0000000000..a94bb65bef --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/HourlyClassification.java @@ -0,0 +1,43 @@ +package com.coderising.payroll; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class HourlyClassification implements PaymentClassification { + + private double rate; + private Map timeCards; + + public HourlyClassification(double rate) { + super(); + this.rate = rate; + timeCards = new HashMap(); + } + + public void addTimeCard(TimeCard tc){ + this.timeCards.put(tc.getDate(), tc); + } + + private double calculatePayForTimeCard(TimeCard tc){ + int hours = tc.getHours(); + if(hours > 8){ + return 8 * rate + (hours - 8) * rate * 1.5; + }else{ + return hours * rate; + } + } + + @Override + public double calculatePay(Paycheck pc) { + double totalPay = 0; + for (TimeCard timeCard : timeCards.values()) { + if (DateUtil.between(timeCard.getDate(), + pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) { + totalPay += calculatePayForTimeCard(timeCard); + } + } + return totalPay; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/MailMethod.java b/students/1299310140/src/com/coderising/payroll/MailMethod.java new file mode 100644 index 0000000000..9ab07ff844 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/MailMethod.java @@ -0,0 +1,19 @@ +package com.coderising.payroll; + +public class MailMethod implements PaymentMethod { + private String address = ""; + + public MailMethod(String address) { + super(); + this.address = address; + } + + @Override + public void pay(Paycheck pc) { + System.out.println("已将支票邮寄到"+address); + System.out.println("应付" + pc.getGrossPay()); + System.out.println("扣除" + pc.getDeductions()); + System.out.println("实付" + pc.getNetPay()); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/MonthlySchedule.java b/students/1299310140/src/com/coderising/payroll/MonthlySchedule.java new file mode 100644 index 0000000000..f110a4f8c4 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/MonthlySchedule.java @@ -0,0 +1,17 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class MonthlySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + return DateUtil.isLastDayOfMonth(date); + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.getFirstDay(payPeriodEndDate); + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/NonAffiliation.java b/students/1299310140/src/com/coderising/payroll/NonAffiliation.java new file mode 100644 index 0000000000..f540ccaade --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/NonAffiliation.java @@ -0,0 +1,11 @@ +package com.coderising.payroll; + +public class NonAffiliation implements Affiliation { + + @Override + public double calculateDeductions(Paycheck pc) { + + return 0; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/Paycheck.java b/students/1299310140/src/com/coderising/payroll/Paycheck.java new file mode 100644 index 0000000000..2367d03a7d --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/Paycheck.java @@ -0,0 +1,50 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class Paycheck { + + private Date payPeriodStart; + private Date payPeriodEnd; + private double grossPay; + private double netPay; + private double deductions; + + public Paycheck(Date payPeriodStart, Date payPeriodEnd){ + this.payPeriodStart = payPeriodStart; + this.payPeriodEnd = payPeriodEnd; + } + + public void setGrossPay(double grossPay) { + this.grossPay = grossPay; + } + + public void setDeductions(double deductions) { + this.deductions = deductions; + } + + public void setNetPay(double netPay){ + this.netPay = netPay; + } + + public Date getPayPeriodEndDate() { + return this.payPeriodEnd; + } + + public Date getPayPeriodStartDate() { + return this.payPeriodStart; + } + + public double getGrossPay() { + return grossPay; + } + + public double getNetPay() { + return netPay; + } + + public double getDeductions() { + return deductions; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/PaymentClassification.java b/students/1299310140/src/com/coderising/payroll/PaymentClassification.java new file mode 100644 index 0000000000..8dcd324551 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/PaymentClassification.java @@ -0,0 +1,5 @@ +package com.coderising.payroll; + +public interface PaymentClassification { + public double calculatePay(Paycheck pc); +} diff --git a/students/1299310140/src/com/coderising/payroll/PaymentMethod.java b/students/1299310140/src/com/coderising/payroll/PaymentMethod.java new file mode 100644 index 0000000000..cd36151ec7 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/PaymentMethod.java @@ -0,0 +1,5 @@ +package com.coderising.payroll; + +public interface PaymentMethod { + public void pay(Paycheck pc); +} diff --git a/students/1299310140/src/com/coderising/payroll/PaymentSchedule.java b/students/1299310140/src/com/coderising/payroll/PaymentSchedule.java new file mode 100644 index 0000000000..f2aff3c46b --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/PaymentSchedule.java @@ -0,0 +1,8 @@ +package com.coderising.payroll; + +import java.util.Date; + +public interface PaymentSchedule { + public boolean isPayDate(Date date); + public Date getPayPeriodStartDate( Date payPeriodEndDate); +} diff --git a/students/1299310140/src/com/coderising/payroll/SalariedClassification.java b/students/1299310140/src/com/coderising/payroll/SalariedClassification.java new file mode 100644 index 0000000000..cb3ce5c9d1 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/SalariedClassification.java @@ -0,0 +1,16 @@ +package com.coderising.payroll; + +public class SalariedClassification implements PaymentClassification { + private double salary; + + public SalariedClassification(double salary) { + super(); + this.salary = salary; + } + + @Override + public double calculatePay(Paycheck pc) { + return salary; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/SalesReceipt.java b/students/1299310140/src/com/coderising/payroll/SalesReceipt.java new file mode 100644 index 0000000000..2d492f51da --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/SalesReceipt.java @@ -0,0 +1,20 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class SalesReceipt { + private Date saleDate; + private double amount; + + public SalesReceipt(Date saleDate, double amount) { + super(); + this.saleDate = saleDate; + this.amount = amount; + } + public Date getSaleDate() { + return saleDate; + } + public double getAmount() { + return amount; + } +} diff --git a/students/1299310140/src/com/coderising/payroll/ServiceCharge.java b/students/1299310140/src/com/coderising/payroll/ServiceCharge.java new file mode 100644 index 0000000000..1e1e2b2c23 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/ServiceCharge.java @@ -0,0 +1,23 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class ServiceCharge { + private Date date; + private int amount; + + public ServiceCharge(Date date, int amount) { + super(); + this.date = date; + this.amount = amount; + } + + public Date getDate() { + return date; + } + + public int getAmount() { + return amount; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/Test.java b/students/1299310140/src/com/coderising/payroll/Test.java new file mode 100644 index 0000000000..42cc3941aa --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/Test.java @@ -0,0 +1,40 @@ +package com.coderising.payroll; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +public class Test { + + public static void main(String[] args) { + List employeeList = getEmployees(); + Date date = DateUtil.parseDate("2017-6-30"); + for(Employee e : employeeList){ + if(e.isPayDay(date)){//可以加上是否已经发过工资的判断 + Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date),date); + e.payDay(pc); + //保存pc + } + } + } + + public static List getEmployees(){ + String[] timeCardDates = {"2017-6-23","2017-6-24","2017-6-28","2017-6-29","2017-6-30","2017-7-1"}; + int[] timeCardhours = {7,8,7,9,6,7}; + Employee e1 = AddEmployee.addHourlyEmployee("Hourly1", "a", 10, timeCardDates, timeCardhours); + + String[] serviceChargeDates = {"2017-5-31","2017-6-1","2017-6-15","2017-6-20","2017-6-30","2017-7-1"}; + int[] serviceChargeAmount = {5,3,2,4,5,5}; + Employee e2 = AddEmployee.addSalariedEmployee("Salaried1", "b", 6000, serviceChargeDates, serviceChargeAmount); + + String[] salesReceiptDates = {"2017-6-16","2017-6-17","2017-6-20","2017-6-25","2017-6-30","2017-7-1"}; + int[] salesReceiptAmount = {50000,90000,80000,50000,80000,90000}; + Employee e3 = AddEmployee.addCommissionEmployee("Commission1", "c", "中国农业银行", "0010", 2500, 0.01, salesReceiptDates, salesReceiptAmount); + + List result = new ArrayList(); + result.add(e1); + result.add(e2); + result.add(e3); + return result; + } +} diff --git a/students/1299310140/src/com/coderising/payroll/TimeCard.java b/students/1299310140/src/com/coderising/payroll/TimeCard.java new file mode 100644 index 0000000000..256915ede4 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/TimeCard.java @@ -0,0 +1,21 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class TimeCard { + private Date date; + private int hours; + + public TimeCard(Date date, int hours) { + super(); + this.date = date; + this.hours = hours; + } + + public Date getDate() { + return date; + } + public int getHours() { + return hours; + } +} diff --git a/students/1299310140/src/com/coderising/payroll/UnionAffiliation.java b/students/1299310140/src/com/coderising/payroll/UnionAffiliation.java new file mode 100644 index 0000000000..b95af11bdc --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/UnionAffiliation.java @@ -0,0 +1,35 @@ +package com.coderising.payroll; + +import java.util.ArrayList; +import java.util.List; + +public class UnionAffiliation implements Affiliation { + private String memberID = ""; + private int weeklyDue = 5; + private List serviceCharges = new ArrayList(); + + public UnionAffiliation(String memberID) { + super(); + this.memberID = memberID; + } + + public void addServiceCharge(ServiceCharge sc){ + this.serviceCharges.add(sc); + } + + @Override + public double calculateDeductions(Paycheck pc) { + int fridays = DateUtil.fridaysNum(pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate()); + int totalDue = fridays * this.weeklyDue; + int totalCharge = 0; + for (ServiceCharge sc : serviceCharges) { + if (DateUtil.between(sc.getDate(), pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate())) { + totalCharge += sc.getAmount(); + } + } + return totalDue + totalCharge; + } + +} diff --git a/students/1299310140/src/com/coderising/payroll/WeeklySchedule.java b/students/1299310140/src/com/coderising/payroll/WeeklySchedule.java new file mode 100644 index 0000000000..a59fdda127 --- /dev/null +++ b/students/1299310140/src/com/coderising/payroll/WeeklySchedule.java @@ -0,0 +1,17 @@ +package com.coderising.payroll; + +import java.util.Date; + +public class WeeklySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + return DateUtil.isFriday(date); + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate,-6); + } + +} From b79097c5e3b8778360f25e56b0347c5684e50aab Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Sat, 19 Aug 2017 21:19:47 +0800 Subject: [PATCH 2/8] junit --- .../src/com/coderising/junit/Assert.java | 19 +++++ .../junit/AssertionFailedError.java | 12 +++ .../src/com/coderising/junit/Calculator.java | 30 ++++++++ .../com/coderising/junit/CalculatorTest.java | 29 +++++++ .../src/com/coderising/junit/Test.java | 5 ++ .../src/com/coderising/junit/TestCase.java | 45 +++++++++++ .../src/com/coderising/junit/TestFailure.java | 21 ++++++ .../src/com/coderising/junit/TestSuite.java | 75 +++++++++++++++++++ 8 files changed, 236 insertions(+) create mode 100644 students/1299310140/src/com/coderising/junit/Assert.java create mode 100644 students/1299310140/src/com/coderising/junit/AssertionFailedError.java create mode 100644 students/1299310140/src/com/coderising/junit/Calculator.java create mode 100644 students/1299310140/src/com/coderising/junit/CalculatorTest.java create mode 100644 students/1299310140/src/com/coderising/junit/Test.java create mode 100644 students/1299310140/src/com/coderising/junit/TestCase.java create mode 100644 students/1299310140/src/com/coderising/junit/TestFailure.java create mode 100644 students/1299310140/src/com/coderising/junit/TestSuite.java diff --git a/students/1299310140/src/com/coderising/junit/Assert.java b/students/1299310140/src/com/coderising/junit/Assert.java new file mode 100644 index 0000000000..089a14bf55 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/Assert.java @@ -0,0 +1,19 @@ +package com.coderising.junit; + +public class Assert { + + public static void assertEquals(int expected,int actual){ + if(expected == actual) + return; + failNotEquals(expected,actual); + } + + private static void failNotEquals(int expected, int actual) { + String message = "expected:<" + expected + "> but was:<" + actual + ">"; + fail(message); + } + + private static void fail(String message) { + throw new AssertionFailedError(message); + } +} diff --git a/students/1299310140/src/com/coderising/junit/AssertionFailedError.java b/students/1299310140/src/com/coderising/junit/AssertionFailedError.java new file mode 100644 index 0000000000..599dffc70f --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/AssertionFailedError.java @@ -0,0 +1,12 @@ +package com.coderising.junit; + +public class AssertionFailedError extends Error { + + public AssertionFailedError(){ + + } + + public AssertionFailedError(String message){ + super(message); + } +} diff --git a/students/1299310140/src/com/coderising/junit/Calculator.java b/students/1299310140/src/com/coderising/junit/Calculator.java new file mode 100644 index 0000000000..a9bd36bb30 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/Calculator.java @@ -0,0 +1,30 @@ +package com.coderising.junit; + +public class Calculator { + + private int result = 0; + + public void add(int x){ + result += x; + } + + public void subtract(int x){ + result -= x; + } + + public int getResult(){ + return result; + } + + public static void main(String[] args){ + TestSuite ts = new TestSuite(CalculatorTest.class); + TestResult tr = new TestResult(); + ts.run(tr); + for(TestFailure testFailure:tr.getFailures()){ + System.out.println(testFailure.thrownException()); + } + for(TestFailure testFailure:tr.getErrors()){ + System.out.println(testFailure.thrownException()); + } + } +} diff --git a/students/1299310140/src/com/coderising/junit/CalculatorTest.java b/students/1299310140/src/com/coderising/junit/CalculatorTest.java new file mode 100644 index 0000000000..0a9c24f742 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/CalculatorTest.java @@ -0,0 +1,29 @@ +package com.coderising.junit; + +public class CalculatorTest extends TestCase { + + public CalculatorTest(String name) { + super(name); + } + + Calculator calculator = null; + + public void setUp(){ + calculator = new Calculator(); + } + + public void tearDown(){ + calculator = null; + } + + public void testAdd(){ + calculator.add(10); + Assert.assertEquals(10, calculator.getResult()); + } + + public void testSubtract(){ + calculator.add(10); + calculator.subtract(5); + Assert.assertEquals(10, calculator.getResult()); + } +} diff --git a/students/1299310140/src/com/coderising/junit/Test.java b/students/1299310140/src/com/coderising/junit/Test.java new file mode 100644 index 0000000000..ecd42d1409 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/Test.java @@ -0,0 +1,5 @@ +package com.coderising.junit; + +public interface Test { + public void run(TestResult tr); +} diff --git a/students/1299310140/src/com/coderising/junit/TestCase.java b/students/1299310140/src/com/coderising/junit/TestCase.java new file mode 100644 index 0000000000..314f20c9cc --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestCase.java @@ -0,0 +1,45 @@ +package com.coderising.junit; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public abstract class TestCase extends Assert implements Test { + + private String name; + + public TestCase(String name){ + this.name = name; + } + + @Override + public void run(TestResult tr){ + tr.run(this); + } + + public void doRun() throws Throwable{ + setUp(); + try{ + runTest(); + }finally{ + tearDown(); + } + } + + protected void runTest() throws Throwable{ + Method runMethod = this.getClass().getMethod(name, null); + try { + runMethod.invoke(this, new Class[0]); + } catch (InvocationTargetException e) { + e.fillInStackTrace(); + throw e.getTargetException(); + } + } + + protected void setUp(){ + + } + + protected void tearDown(){ + + } +} diff --git a/students/1299310140/src/com/coderising/junit/TestFailure.java b/students/1299310140/src/com/coderising/junit/TestFailure.java new file mode 100644 index 0000000000..28fdbc18a7 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestFailure.java @@ -0,0 +1,21 @@ +package com.coderising.junit; + +public class TestFailure { + + private Test failedTest; + private Throwable thrownException; + + public TestFailure(Test failedTest, Throwable thrownException) { + super(); + this.failedTest = failedTest; + this.thrownException = thrownException; + } + + public Test failedTest(){ + return this.failedTest; + } + + public Throwable thrownException(){ + return this.thrownException; + } +} diff --git a/students/1299310140/src/com/coderising/junit/TestSuite.java b/students/1299310140/src/com/coderising/junit/TestSuite.java new file mode 100644 index 0000000000..42f2e76f50 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestSuite.java @@ -0,0 +1,75 @@ +package com.coderising.junit; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class TestSuite implements Test { + + private List tests = new ArrayList(); + + @Override + public void run(TestResult tr) { + for(Iterator iterator = tests.iterator();iterator.hasNext();){ + Test test = iterator.next(); + test.run(tr); + } + } + + public void addTest(Test test){ + tests.add(test); + } + + public TestSuite(){ + + } + + public TestSuite(final Class theClass){ + Constructor constructor = null; + try { + constructor = theClass.getConstructor(String.class); + } catch (Exception e) { + e.printStackTrace(); + } + + List names = new ArrayList(); + Method[] method = theClass.getDeclaredMethods(); + for(int i = 0;i < method.length;i++){ + addTestMethod(method[i],names,constructor); + } + + if(tests.size() == 0){ + System.out.println("No tests found in" + theClass.getName()); + } + } + + private void addTestMethod(Method method, List names, + Constructor constructor) { + String name = method.getName(); + if(names.contains(name)){ + return; + } + + if(isPublicTestMethod(method)){ + names.add(name); + + try { + addTest((Test)constructor.newInstance(name)); + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + + private boolean isPublicTestMethod(Method method) { + String name = method.getName(); + Class[] parameters = method.getParameterTypes(); + Class returnType = method.getReturnType(); + return Modifier.isPublic(method.getModifiers()) && name.startsWith("test") + && parameters.length == 0 && returnType.equals(Void.TYPE); + } +} From 0a6ebbc259b05f78f0000f3616a77cdbe3864c50 Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Sun, 27 Aug 2017 08:50:51 +0800 Subject: [PATCH 3/8] TestListener --- .../src/com/coderising/junit/AllTest.java | 11 ++++ .../com/coderising/junit/BaseTestRunner.java | 51 +++++++++++++++ .../com/coderising/junit/CalculatorSuite.java | 10 +++ .../src/com/coderising/junit/Test.java | 3 + .../src/com/coderising/junit/TestCase.java | 5 ++ .../com/coderising/junit/TestListener.java | 12 ++++ .../src/com/coderising/junit/TestRunner.java | 62 +++++++++++++++++++ .../src/com/coderising/junit/TestSuite.java | 21 +++++++ 8 files changed, 175 insertions(+) create mode 100644 students/1299310140/src/com/coderising/junit/AllTest.java create mode 100644 students/1299310140/src/com/coderising/junit/BaseTestRunner.java create mode 100644 students/1299310140/src/com/coderising/junit/CalculatorSuite.java create mode 100644 students/1299310140/src/com/coderising/junit/TestListener.java create mode 100644 students/1299310140/src/com/coderising/junit/TestRunner.java diff --git a/students/1299310140/src/com/coderising/junit/AllTest.java b/students/1299310140/src/com/coderising/junit/AllTest.java new file mode 100644 index 0000000000..335335b863 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/AllTest.java @@ -0,0 +1,11 @@ +package com.coderising.junit; + +public class AllTest { + + public static Test suite(){ + TestSuite suite = new TestSuite("All Test"); + suite.addTestSuite(CalculatorTest.class); + suite.addTestSuite(CalculatorTest.class); + return suite; + } +} diff --git a/students/1299310140/src/com/coderising/junit/BaseTestRunner.java b/students/1299310140/src/com/coderising/junit/BaseTestRunner.java new file mode 100644 index 0000000000..271da28e12 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/BaseTestRunner.java @@ -0,0 +1,51 @@ +package com.coderising.junit; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public abstract class BaseTestRunner implements TestListener { + + public static final String SUITE_METHODNAME = "suite"; + + public Test getTest(String suiteClassName) { + Class testClass = null; + try { + testClass = loadSuiteClass(suiteClassName); + } catch (ClassNotFoundException e) { + + e.printStackTrace(); + } + + Method suiteMethod = null; + try { + suiteMethod = testClass.getMethod(SUITE_METHODNAME, new Class[0]); + } catch (NoSuchMethodException e) { + + e.printStackTrace(); + } catch (SecurityException e) { + + e.printStackTrace(); + } + + Test test = null; + try { + test = (Test) suiteMethod.invoke(null, new Class[0]); + } catch (IllegalAccessException e) { + + e.printStackTrace(); + } catch (IllegalArgumentException e) { + + e.printStackTrace(); + } catch (InvocationTargetException e) { + + e.printStackTrace(); + } + + return test; + } + + private Class loadSuiteClass(String suiteClassName) throws ClassNotFoundException { + + return Class.forName(suiteClassName); + } +} diff --git a/students/1299310140/src/com/coderising/junit/CalculatorSuite.java b/students/1299310140/src/com/coderising/junit/CalculatorSuite.java new file mode 100644 index 0000000000..a5205c0a5c --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/CalculatorSuite.java @@ -0,0 +1,10 @@ +package com.coderising.junit; + +public class CalculatorSuite { + + public static Test suite(){ + TestSuite suite = new TestSuite("Calculator All Test"); + suite.addTestSuite(CalculatorTest.class); + return suite; + } +} diff --git a/students/1299310140/src/com/coderising/junit/Test.java b/students/1299310140/src/com/coderising/junit/Test.java index ecd42d1409..5144d296a1 100644 --- a/students/1299310140/src/com/coderising/junit/Test.java +++ b/students/1299310140/src/com/coderising/junit/Test.java @@ -1,5 +1,8 @@ package com.coderising.junit; public interface Test { + public void run(TestResult tr); + + public int countTestCases(); } diff --git a/students/1299310140/src/com/coderising/junit/TestCase.java b/students/1299310140/src/com/coderising/junit/TestCase.java index 314f20c9cc..2c6f41b55a 100644 --- a/students/1299310140/src/com/coderising/junit/TestCase.java +++ b/students/1299310140/src/com/coderising/junit/TestCase.java @@ -42,4 +42,9 @@ protected void setUp(){ protected void tearDown(){ } + + @Override + public int countTestCases() { + return 1; + } } diff --git a/students/1299310140/src/com/coderising/junit/TestListener.java b/students/1299310140/src/com/coderising/junit/TestListener.java new file mode 100644 index 0000000000..0813b0e9ef --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestListener.java @@ -0,0 +1,12 @@ +package com.coderising.junit; + +public interface TestListener { + + public void addError(Test test, Throwable t); + + public void addFailure(Test test, AssertionFailedError t); + + public void startTest(Test test); + + public void endTest(Test test); +} diff --git a/students/1299310140/src/com/coderising/junit/TestRunner.java b/students/1299310140/src/com/coderising/junit/TestRunner.java new file mode 100644 index 0000000000..69b67bc0d5 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestRunner.java @@ -0,0 +1,62 @@ +package com.coderising.junit; + +import java.io.PrintStream; + +public class TestRunner extends BaseTestRunner { + + PrintStream write = System.out; + int column = 0; + + @Override + public void addError(Test test, Throwable t) { + write.print("E"); + } + + @Override + public void addFailure(Test test, AssertionFailedError t) { + write.print("F"); + } + + @Override + public void startTest(Test test) { + write.print("."); + if(column++ >= 40){ + write.println(); + column = 0; + } + } + + @Override + public void endTest(Test test) { + + } + + public static void main(String[] args) { + TestRunner testRunner = new TestRunner(); + try { + TestResult testResult = testRunner.start(args); + } catch (Exception e) { + + e.printStackTrace(); + } + } + + private TestResult start(String[] args) throws Exception { + if(args.length == 0){ + throw new Exception("Usage : TestRunner TestCaseName"); + } + + String testCase = args[0]; + Test suite = getTest(testCase); + + return doRun(suite); + } + + private TestResult doRun(Test suite) { + TestResult testResult = new TestResult(); + testResult.addListener(this); + suite.run(testResult); + return testResult; + } + +} diff --git a/students/1299310140/src/com/coderising/junit/TestSuite.java b/students/1299310140/src/com/coderising/junit/TestSuite.java index 42f2e76f50..48af3a92e9 100644 --- a/students/1299310140/src/com/coderising/junit/TestSuite.java +++ b/students/1299310140/src/com/coderising/junit/TestSuite.java @@ -11,6 +11,8 @@ public class TestSuite implements Test { private List tests = new ArrayList(); + private String name = ""; + @Override public void run(TestResult tr) { for(Iterator iterator = tests.iterator();iterator.hasNext();){ @@ -23,11 +25,20 @@ public void addTest(Test test){ tests.add(test); } + public void addTestSuite(Class theClass){ + addTest(new TestSuite(theClass)); + } + public TestSuite(){ } + public TestSuite(String name){ + this.name = name; + } + public TestSuite(final Class theClass){ + this.name = theClass.getName(); Constructor constructor = null; try { constructor = theClass.getConstructor(String.class); @@ -72,4 +83,14 @@ private boolean isPublicTestMethod(Method method) { return Modifier.isPublic(method.getModifiers()) && name.startsWith("test") && parameters.length == 0 && returnType.equals(Void.TYPE); } + + @Override + public int countTestCases() { + int count = 0; + for(Iterator iterator = tests.iterator();iterator.hasNext();){ + Test test = iterator.next(); + count += test.countTestCases(); + } + return count; + } } From 96083b124acd3b7a04dc0aa2d94aac5f41c99c0f Mon Sep 17 00:00:00 2001 From: '1299310140' <'13437282785@163.com'> Date: Sun, 27 Aug 2017 10:12:17 +0800 Subject: [PATCH 4/8] extension --- .../src/com/coderising/junit/AllTest.java | 31 +++++++++++++++++-- .../src/com/coderising/junit/Protectable.java | 5 +++ .../com/coderising/junit/RepeatedTest.java | 23 ++++++++++++++ .../com/coderising/junit/TestDecorator.java | 26 ++++++++++++++++ .../src/com/coderising/junit/TestSetUp.java | 30 ++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 students/1299310140/src/com/coderising/junit/Protectable.java create mode 100644 students/1299310140/src/com/coderising/junit/RepeatedTest.java create mode 100644 students/1299310140/src/com/coderising/junit/TestDecorator.java create mode 100644 students/1299310140/src/com/coderising/junit/TestSetUp.java diff --git a/students/1299310140/src/com/coderising/junit/AllTest.java b/students/1299310140/src/com/coderising/junit/AllTest.java index 335335b863..f9c715efbf 100644 --- a/students/1299310140/src/com/coderising/junit/AllTest.java +++ b/students/1299310140/src/com/coderising/junit/AllTest.java @@ -2,10 +2,35 @@ public class AllTest { +// public static Test suite(){ +// TestSuite suite = new TestSuite("All Test"); +// suite.addTestSuite(CalculatorTest.class); +// suite.addTestSuite(CalculatorTest.class); +// return suite; +// } + public static Test suite(){ TestSuite suite = new TestSuite("All Test"); - suite.addTestSuite(CalculatorTest.class); - suite.addTestSuite(CalculatorTest.class); - return suite; + suite.addTest(CalculatorSuite.suite()); +// suite.addTestSuite(CalculatorTest.class); + suite.addTest(new RepeatedTest(new TestSuite(CalculatorTest.class),3)); + return new OverallTestSetup(suite); + } + + static class OverallTestSetup extends TestSetUp{ + + public OverallTestSetup(Test test) { + super(test); + } + + @Override + public void setUp(){ + System.out.println("this is overall testsetup"); + } + + @Override + public void tearDown(){ + System.out.println("this is overall testteardown"); + } } } diff --git a/students/1299310140/src/com/coderising/junit/Protectable.java b/students/1299310140/src/com/coderising/junit/Protectable.java new file mode 100644 index 0000000000..d7238b5f39 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/Protectable.java @@ -0,0 +1,5 @@ +package com.coderising.junit; + +public interface Protectable { + public void protect() throws Throwable; +} diff --git a/students/1299310140/src/com/coderising/junit/RepeatedTest.java b/students/1299310140/src/com/coderising/junit/RepeatedTest.java new file mode 100644 index 0000000000..c281758586 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/RepeatedTest.java @@ -0,0 +1,23 @@ +package com.coderising.junit; + +public class RepeatedTest extends TestDecorator { + + private int fTimesRepeat; + + public RepeatedTest(Test test,int repeat) { + super(test); + if(repeat < 0) + throw new IllegalArgumentException("repeation count must be > 0"); + this.fTimesRepeat = repeat; + } + + public int countTestCases(){ + return super.countTestCases() * fTimesRepeat; + } + + public void run(TestResult testResult){ + for(int i = 0;i < fTimesRepeat;i++){ + super.run(testResult); + } + } +} diff --git a/students/1299310140/src/com/coderising/junit/TestDecorator.java b/students/1299310140/src/com/coderising/junit/TestDecorator.java new file mode 100644 index 0000000000..75f74cbcc7 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestDecorator.java @@ -0,0 +1,26 @@ +package com.coderising.junit; + +public class TestDecorator implements Test { + + protected Test test; + + public TestDecorator(Test test){ + this.test = test; + } + + @Override + public void run(TestResult tr) { + basicRun(tr); + } + + public void basicRun(TestResult tr) { + this.test.run(tr); + } + + @Override + public int countTestCases() { + + return test.countTestCases(); + } + +} diff --git a/students/1299310140/src/com/coderising/junit/TestSetUp.java b/students/1299310140/src/com/coderising/junit/TestSetUp.java new file mode 100644 index 0000000000..5ad6e22394 --- /dev/null +++ b/students/1299310140/src/com/coderising/junit/TestSetUp.java @@ -0,0 +1,30 @@ +package com.coderising.junit; + +public class TestSetUp extends TestDecorator { + + public TestSetUp(Test test) { + super(test); + } + + public void run(final TestResult testResult){ + Protectable p = new Protectable() { + @Override + public void protect() throws Throwable { + setUp(); + basicRun(testResult); + tearDown(); + } + }; + + testResult.runProtected(this, p); + } + + protected void tearDown() { + + } + + protected void setUp() { + + } + +} From 3b767d3dffe4818ec9ef09fb4ff4ebbadcade4ac Mon Sep 17 00:00:00 2001 From: wanghongdi56 <313001956@qq.com> Date: Thu, 14 Sep 2017 10:45:31 +0800 Subject: [PATCH 5/8] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E5=AD=A3=E5=BA=A6?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 第二季度所有作业 --- students/313001956/pom.xml | 32 + students/313001956/src/main/java/NewFile.xml | 2223 +++++++++++++++++ .../src/main/java/com/coderising/atm/ATM.java | 127 + .../java/com/coderising/atm/BankProxy.java | 24 + .../java/com/coderising/atm/CardReader.java | 45 + .../com/coderising/atm/CashDepensier.java | 15 + .../java/com/coderising/atm/DepositSlot.java | 10 + .../main/java/com/coderising/atm/Display.java | 11 + .../java/com/coderising/atm/KeyBoard.java | 54 + .../main/java/com/coderising/atm/Network.java | 5 + .../main/java/com/coderising/atm/Printer.java | 17 + .../java/com/coderising/atm/SuperKeypad.java | 131 + .../atm/transactions/DepositTx.java | 36 + .../atm/transactions/QueryBalanceTx.java | 28 + .../atm/transactions/Transaction.java | 19 + .../atm/transactions/TransactionBase.java | 29 + .../atm/transactions/TransferTx.java | 39 + .../atm/transactions/WithdrawTx.java | 41 + .../java/com/coderising/bank/ATMProxy.java | 41 + .../java/com/coderising/bank/Account.java | 39 + .../main/java/com/coderising/bank/Bank.java | 46 + .../bank/transactions/DepositTx.java | 27 + .../bank/transactions/QueryBalanceTx.java | 25 + .../bank/transactions/Transaction.java | 9 + .../bank/transactions/TransactionBase.java | 17 + .../bank/transactions/TransferTx.java | 25 + .../bank/transactions/WithdrawTx.java | 32 + .../java/com/coderising/dp/bridge/Circle.java | 23 + .../com/coderising/dp/bridge/Drawing.java | 7 + .../com/coderising/dp/bridge/DrawingGL1.java | 20 + .../com/coderising/dp/bridge/DrawingGL2.java | 20 + .../coderising/dp/bridge/GraphicLibrary1.java | 11 + .../coderising/dp/bridge/GraphicLibrary2.java | 11 + .../com/coderising/dp/bridge/Rectangle.java | 23 + .../java/com/coderising/dp/bridge/Shape.java | 5 + .../com/coderising/dp/bridge/testBrige.java | 28 + .../com/coderising/dp/builder/TagBuilder.java | 43 + .../coderising/dp/builder/TagBuilderTest.java | 39 + .../com/coderising/dp/builder/TagNode.java | 83 + .../com/coderising/dp/chain/ChainLogger.java | 11 + .../com/coderising/dp/chain/EmailLogger.java | 32 + .../com/coderising/dp/chain/FileLogger.java | 28 + .../java/com/coderising/dp/chain/Logger.java | 20 + .../com/coderising/dp/chain/StdoutLogger.java | 31 + .../com/coderising/dp/chain/chaintest.java | 19 + .../com/coderising/dp/command/Command.java | 6 + .../coderising/dp/command/CommandTest.java | 13 + .../java/com/coderising/dp/command/Cook.java | 12 + .../dp/command/OrderPeaKCommand.java | 17 + .../dp/command/OrderSteakCommand.java | 17 + .../com/coderising/dp/command/Waiter.java | 19 + .../com/coderising/dp/composite/Line.java | 11 + .../com/coderising/dp/composite/Picture.java | 23 + .../coderising/dp/composite/Rectangle.java | 11 + .../com/coderising/dp/composite/Shape.java | 5 + .../com/coderising/dp/composite/Square.java | 11 + .../com/coderising/dp/composite/Text.java | 11 + .../dp/composite/testComposite.java | 15 + .../dp/decorator/DecoratorTest.java | 11 + .../com/coderising/dp/decorator/Email.java | 6 + .../coderising/dp/decorator/EmailDeclare.java | 19 + .../dp/decorator/EmailDecorator.java | 13 + .../coderising/dp/decorator/EmailEcript.java | 14 + .../coderising/dp/decorator/EmailImpl.java | 12 + .../java/com/coderising/ood/ocp/DateUtil.java | 13 + .../com/coderising/ood/ocp/EmailLogWay.java | 7 + .../coderising/ood/ocp/EmailMessageWay.java | 8 + .../java/com/coderising/ood/ocp/ILogWay.java | 6 + .../com/coderising/ood/ocp/IMessageWay.java | 6 + .../java/com/coderising/ood/ocp/Logger.java | 17 + .../com/coderising/ood/ocp/LoggerTest.java | 21 + .../java/com/coderising/ood/ocp/MailUtil.java | 10 + .../com/coderising/ood/ocp/PrintLogWay.java | 9 + .../coderising/ood/ocp/PrintMessageWay.java | 9 + .../com/coderising/ood/ocp/SMSLogWay.java | 9 + .../com/coderising/ood/ocp/SMSMessageWay.java | 8 + .../java/com/coderising/ood/ocp/SMSUtil.java | 10 + .../com/coderising/ood/srp/Configuration.java | 25 + .../java/com/coderising/ood/srp/DBUtil.java | 28 + .../java/com/coderising/ood/srp/Mail.java | 34 + .../com/coderising/ood/srp/MailSender.java | 36 + .../java/com/coderising/ood/srp/Product.java | 22 + .../com/coderising/ood/srp/ProductServer.java | 36 + .../com/coderising/ood/srp/PromotionJob.java | 36 + .../coderising/ood/srp/PromotionJobTest.java | 29 + .../java/com/coderising/ood/srp/User.java | 34 + .../com/coderising/ood/srp/UserServer.java | 13 + .../coderising/ood/srp/product_promotion.txt | 4 + .../coderising/payroll/PayrollService.java | 69 + .../payroll/affiliation/NonAffiliation.java | 10 + .../payroll/affiliation/UnionAffiliation.java | 38 + .../CommissionedClassification.java | 38 + .../classification/HourlyClassification.java | 49 + .../SalariedClassification.java | 16 + .../payroll/domain/Affiliation.java | 5 + .../coderising/payroll/domain/Employee.java | 49 + .../coderising/payroll/domain/HoldMethod.java | 11 + .../coderising/payroll/domain/Paycheck.java | 35 + .../payroll/domain/PaydayTransaction.java | 23 + .../payroll/domain/PaymentClassification.java | 5 + .../payroll/domain/PaymentMethod.java | 5 + .../payroll/domain/PaymentSchedule.java | 8 + .../payroll/domain/SalesReceipt.java | 14 + .../payroll/domain/ServiceCharge.java | 16 + .../coderising/payroll/domain/TimeCard.java | 15 + .../payroll/schedule/BiweeklySchedule.java | 38 + .../payroll/schedule/MonthlySchedule.java | 22 + .../payroll/schedule/WeeklySchedule.java | 19 + .../transaction/AddEmployeeTransaction.java | 26 + .../AddHourlyEmployeeTransaction.java | 27 + .../com/coderising/payroll/util/DateUtil.java | 69 + .../src/main/java/org/v0_my/Assert.java | 225 ++ .../java/org/v0_my/AssertionFailedError.java | 12 + .../src/main/java/org/v0_my/Test.java | 7 + .../src/main/java/org/v0_my/TestCase.java | 75 + .../src/main/java/org/v0_my/TestFailure.java | 21 + .../src/main/java/org/v0_my/TestResult.java | 109 + .../src/main/java/org/v0_my/TestTuite.java | 140 ++ .../org/v0_my/extension/RepeatedTest.java | 28 + .../org/v0_my/extension/TestDecorator.java | 32 + .../java/org/v0_my/extension/TestSetup.java | 34 + .../java/org/v0_my/runner/TestBaseRunner.java | 44 + .../java/org/v0_my/runner/TestListener.java | 14 + .../java/org/v0_my/runner/TestRunner.java | 72 + .../main/java/org/v0_my/sample/AllTest.java | 20 + .../sample/caculator/CaculatorTestCase.java | 51 + .../v0_my/sample/caculator/Calculator.java | 22 + .../sample/caculator/CalculatorTuite.java | 12 + .../org/v0_my/sample/person/PersonTest.java | 38 + 129 files changed, 5840 insertions(+) create mode 100644 students/313001956/pom.xml create mode 100644 students/313001956/src/main/java/NewFile.xml create mode 100644 students/313001956/src/main/java/com/coderising/atm/ATM.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/BankProxy.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/CardReader.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/CashDepensier.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/DepositSlot.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/Display.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/KeyBoard.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/Network.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/Printer.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/SuperKeypad.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/DepositTx.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/QueryBalanceTx.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/Transaction.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/TransactionBase.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/TransferTx.java create mode 100644 students/313001956/src/main/java/com/coderising/atm/transactions/WithdrawTx.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/ATMProxy.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/Account.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/Bank.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/DepositTx.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/QueryBalanceTx.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/Transaction.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/TransactionBase.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/TransferTx.java create mode 100644 students/313001956/src/main/java/com/coderising/bank/transactions/WithdrawTx.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/Circle.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/Drawing.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL1.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL2.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary1.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary2.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/Rectangle.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/Shape.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/bridge/testBrige.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/builder/TagBuilder.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/builder/TagBuilderTest.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/builder/TagNode.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/ChainLogger.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/EmailLogger.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/FileLogger.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/Logger.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/StdoutLogger.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/chain/chaintest.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/Command.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/CommandTest.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/Cook.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/OrderPeaKCommand.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/OrderSteakCommand.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/command/Waiter.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Line.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Picture.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Rectangle.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Shape.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Square.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/Text.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/composite/testComposite.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/DecoratorTest.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/Email.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/EmailDeclare.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/EmailDecorator.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/EmailEcript.java create mode 100644 students/313001956/src/main/java/com/coderising/dp/decorator/EmailImpl.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/DateUtil.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/EmailLogWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/EmailMessageWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/ILogWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/IMessageWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/Logger.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/LoggerTest.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/MailUtil.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/PrintLogWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/PrintMessageWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/SMSLogWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/SMSMessageWay.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/ocp/SMSUtil.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/Configuration.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/DBUtil.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/MailSender.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/ProductServer.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/PromotionJob.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/PromotionJobTest.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/User.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/UserServer.java create mode 100644 students/313001956/src/main/java/com/coderising/ood/srp/product_promotion.txt create mode 100644 students/313001956/src/main/java/com/coderising/payroll/PayrollService.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/affiliation/NonAffiliation.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/affiliation/UnionAffiliation.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/classification/CommissionedClassification.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/classification/HourlyClassification.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/classification/SalariedClassification.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/Affiliation.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/Employee.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/HoldMethod.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/Paycheck.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/PaydayTransaction.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/PaymentClassification.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/PaymentMethod.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/PaymentSchedule.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/SalesReceipt.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/ServiceCharge.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/domain/TimeCard.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/schedule/BiweeklySchedule.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/schedule/MonthlySchedule.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/schedule/WeeklySchedule.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/transaction/AddEmployeeTransaction.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/transaction/AddHourlyEmployeeTransaction.java create mode 100644 students/313001956/src/main/java/com/coderising/payroll/util/DateUtil.java create mode 100644 students/313001956/src/main/java/org/v0_my/Assert.java create mode 100644 students/313001956/src/main/java/org/v0_my/AssertionFailedError.java create mode 100644 students/313001956/src/main/java/org/v0_my/Test.java create mode 100644 students/313001956/src/main/java/org/v0_my/TestCase.java create mode 100644 students/313001956/src/main/java/org/v0_my/TestFailure.java create mode 100644 students/313001956/src/main/java/org/v0_my/TestResult.java create mode 100644 students/313001956/src/main/java/org/v0_my/TestTuite.java create mode 100644 students/313001956/src/main/java/org/v0_my/extension/RepeatedTest.java create mode 100644 students/313001956/src/main/java/org/v0_my/extension/TestDecorator.java create mode 100644 students/313001956/src/main/java/org/v0_my/extension/TestSetup.java create mode 100644 students/313001956/src/main/java/org/v0_my/runner/TestBaseRunner.java create mode 100644 students/313001956/src/main/java/org/v0_my/runner/TestListener.java create mode 100644 students/313001956/src/main/java/org/v0_my/runner/TestRunner.java create mode 100644 students/313001956/src/main/java/org/v0_my/sample/AllTest.java create mode 100644 students/313001956/src/main/java/org/v0_my/sample/caculator/CaculatorTestCase.java create mode 100644 students/313001956/src/main/java/org/v0_my/sample/caculator/Calculator.java create mode 100644 students/313001956/src/main/java/org/v0_my/sample/caculator/CalculatorTuite.java create mode 100644 students/313001956/src/main/java/org/v0_my/sample/person/PersonTest.java diff --git a/students/313001956/pom.xml b/students/313001956/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/313001956/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/313001956/src/main/java/NewFile.xml b/students/313001956/src/main/java/NewFile.xml new file mode 100644 index 0000000000..8c9fd6a466 --- /dev/null +++ b/students/313001956/src/main/java/NewFile.xml @@ -0,0 +1,2223 @@ + + + + + + + 0 + 47 + ʾϢ + + + + + + 3426231013320298 + 342623101332029801 + + + 1962-04-13 + + + ͱ + 342623196204132713 +
޳Ȼ売
+ +
+ + + + 000118082607853555 + 000118082607853555 + 0001 + ΪҽԺ + 1684.8300 + 1601.2700 + 300.0000 + 1280.8000 + 0.0000 + 404.0300 + T50.901 + 2017-07-16 10:11:41 + 2017-07-18 8:26:33 + 21 + 2017-07-18 8:27:23 + 272.5300 + 0.0000 + 1 + 168.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231012461948 + 342623101246194801 + + + 1943-03-08 + + + ͱ + 342623194303088958 +
޳̴ֶȻ壴
+ һũ +
+ + + + 000207080039619556 + 000207080039619556 + 0002 + ΪҽҽԺ + 5733.1200 + 3768.3700 + 0.0000 + 5073.3000 + -200.1600 + 859.9800 + ZDZ486 + 2017-07-05 9:20:00 + 2017-07-17 9:44:01 + 2104 + 2017-07-18 7:40:27 + 933.1200 + 0.0000 + 1 + 573.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 1013110051 + 342623101311005013 + + + 1974-08-10 + + + ͱ + 342623197408102756 +
޳ͷ
+ һũ +
+ + + + 233521081028602568 + 233521081028602568 + 2335 + ϺߺҽԺ + 4933.0300 + 4737.6300 + 0.0000 + 4105.0000 + 0.0000 + 828.0300 + F20.901 + 2017-06-21 8:13:52 + 2017-07-18 8:17:45 + 21 + 2017-07-18 8:30:35 + 874.3300 + 53.0000 + 1 + 493.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231512130395 + 342623151213039502 + ȴ + Ů + 1951-01-21 + ԰ﱦ + Ů + һũ + 342623195101213022 +
ʯ԰Ȼ壳
+ һũ +
+ + + + 005616080011604122 + 005616080011604122 + 0056 + ΪػۿҽԺ + 1705.5200 + 1621.6800 + 0.0000 + 1295.5000 + 154.1400 + 255.8800 + ZDZ103 + 2017-07-16 7:59:54 + 2017-07-18 7:53:40 + 2104 + 2017-07-18 8:03:08 + 280.5200 + 0.0000 + 1 + 170.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232006190458 + 342623200619045801 + żӢ + Ů + 1938-10-26 + żӢ + Ů + ͱ + 342623193810264720 +
԰½Ȼ
+ һũ +
+ + + + 000115145258439815 + 000115145258439815 + 0001 + ΪҽԺ + 4978.6200 + 4842.1800 + 0.0000 + 4837.8000 + -854.9700 + 995.7900 + ZDZ015 + 2017-07-14 7:52:19 + 2017-07-18 9:18:41 + 2104 + 2017-07-18 9:17:18 + 338.6200 + 0.0000 + 1 + 497.8000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231801301806 + 342623180130180602 + 쾲÷ + Ů + 1973-04-24 + ι + Ů + һũ + 342623197304245349 +
ɽɽȻ壴
+ һũ +
+ + + + 004218092418328810 + 004218092418328810 + 0042 + ΪؼҽԺ + 1799.6000 + 1267.3000 + 0.0000 + 1439.7000 + 0.0000 + 359.9000 + N18.903 + 2017-04-13 9:22:06 + 2017-07-18 9:22:06 + 15 + 2017-07-18 9:25:42 + 239.9000 + 0.0000 + 1 + 180.0000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231616030563 + 342623161603056303 + Ӣ + Ů + 1948-03-27 + μҸ + Ů + ͱ + 342623194803273826 +
Ȼ声
+ һũ +
+ + + + 112418102447877740 + 112418102447877740 + 1124 + ҽƴѧҽԺԭ + 6931.3800 + 5664.5300 + 0.0000 + 4851.9000 + 0.0000 + 2079.4800 + Z47.002 + 2017-07-10 9:43:26 + 2017-07-18 8:47:45 + 21 + 2017-07-18 10:28:50 + 1772.5800 + 0.0000 + 1 + 693.1000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232402160473 + 342623240216047302 + ѧ + + 1948-10-17 + ѧ + + ͱ + 342623194810171393 +
ӰȻ声
+ һũ +
+ + + + 129218074258857075 + 129218074258857075 + 1292 + ɽҽԺ + 2659.0000 + 2436.6000 + 0.0000 + 1994.2000 + 0.0000 + 664.8000 + N40 01 + 2017-07-12 7:36:50 + 2017-07-14 7:36:50 + 21 + 2017-07-18 7:47:09 + 430.7000 + 0.0000 + 1 + 265.9000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232001040088 + 342623200104008801 + Ȼ + + 1955-04-28 + Ȼ + + ͱ + 342623195504287034 +
幵Ȼ声
+ һũ +
+ + + + 112711090227086225 + 112711090227086225 + 1127 + ߺеڶҽԺ + 11444.3100 + 10554.8100 + 0.0000 + 11087.4000 + 0.0000 + 356.9100 + D46.901 + 2017-07-11 9:03:27 + 2017-07-18 8:26:06 + 21 + 2017-07-18 8:42:49 + 2933.3100 + 1521.2000 + 1 + 1144.4000 + 1 + 410.8000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231816230821 + 342623181623082101 + α + + 1974-11-22 + α + + ͱ + 342623197411223612 +
ɽʯȻ売
+ һũ +
+ + + + 233517154520542487 + 233517154520542487 + 2335 + ϺߺҽԺ + 5757.5400 + 5495.3400 + 0.0000 + 4699.2000 + 0.0000 + 1058.3400 + F20.901 + 2017-06-17 15:48:22 + 2017-07-18 8:18:42 + 21 + 2017-07-18 8:31:55 + 1134.1400 + 0.0000 + 1 + 575.8000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231803431056 + 342623180343105601 + ² + + 1968-12-05 + ² + + ͱ + 342623196812053613 +
ɽСկȻ
+ һũ +
+ + + + 004212161554691195 + 004212161554691195 + 0042 + ΪؼҽԺ + 3227.7900 + 3079.2700 + 300.0000 + 2700.8000 + 0.0000 + 526.9900 + J98.505 + 2017-07-12 16:16:11 + 2017-07-18 11:33:18 + 21 + 2017-07-18 15:05:19 + 549.7900 + 0.0000 + 1 + 322.8000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231808180842 + 342623180818084204 + ´ + Ů + 1969-03-15 + + Ů + ͱ + 342623196903155324 +
ɽ򽨳ӾȻ声
+ һũ +
+ + + + 246614065803519157 + 246614065803519157 + 2466 + ΪƹҽԺ + 2323.0000 + 2296.0000 + 0.0000 + 2206.9000 + 0.0000 + 116.1000 + N93.801 + 2017-07-14 6:58:05 + 2017-07-18 7:44:54 + 21 + 2017-07-18 8:10:03 + 248.4000 + 0.0000 + 1 + 232.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231909090376 + 342623190909037604 + + Ů + 2005-05-24 + ѧ + Ů + һũ + 341422200505245923 +
ţȻ声
+ һũ +
+ + + + 000112075724118546 + 000112075724118546 + 0001 + ΪҽԺ + 2199.1900 + 2066.7700 + 0.0000 + 1829.9000 + -70.5700 + 439.8600 + ZDZ226 + 2017-07-12 8:03:36 + 2017-07-18 10:46:03 + 2104 + 2017-07-18 11:12:22 + 289.1900 + 0.0000 + 1 + 219.9000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231019210964 + 342623101921096404 + ׺ʹ + + 1991-10-20 + ձ + + ͱ + 342623199110200352 +
޳Ȼ壱
+ һũ +
+ + + + 112618093606289584 + 112618093606289584 + 1126 + ߺеҽԺ + 829.8000 + 476.4000 + 0.0000 + 622.3000 + 0.0000 + 207.5000 + F20.901 + 2017-07-14 9:34:54 + 2017-07-14 9:34:54 + 15 + 2017-07-18 9:36:51 + -209.5000 + 0.0000 + 1 + 83.0000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232508040208 + 342623250804020805 + + Ů + 1963-09-23 + + Ů + ͱ + 342623196309235022 +
Ȫ÷庫Ȼ壹
+ һũ +
+ + + + 004218071743845620 + 004218071743845620 + 0042 + ΪؼҽԺ + 735.0000 + 595.0000 + 0.0000 + 588.0000 + 0.0000 + 147.0000 + N18.903 + 2017-07-17 7:16:17 + 2017-07-18 7:16:17 + 15 + 2017-07-18 7:18:24 + -79.5000 + 0.0000 + 1 + 73.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426233102040322 + 342623310204032204 + + + 1953-07-15 + + + ͱ + 34262319530715681X +
ɽ罧彧Ȼ壶
+ һũ +
+ + + + 112715110644012483 + 112715110644012483 + 1127 + ߺеڶҽԺ + 3986.3200 + 3521.5700 + 0.0000 + 4108.6000 + -919.5800 + 797.3000 + ZDZ034 + 2017-07-15 11:06:22 + 2017-07-18 9:34:43 + 2103 + 2017-07-18 9:47:23 + -223.6800 + 0.0000 + 1 + 398.6000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232002170768 + 342623200217076801 + + + 1939-01-05 + + + ͱ + 342623193901054710 +
ʤǰȻ
+ һũ +
+ + + + 000118082021080533 + 000118082021080533 + 0001 + ΪҽԺ + 2955.1800 + 2945.1800 + 0.0000 + 2798.9000 + 0.0000 + 156.2800 + C64 01 + 2017-07-07 8:18:56 + 2017-07-18 8:18:56 + 15 + 2017-07-18 8:20:56 + 151.7800 + 0.0000 + 1 + 295.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232708120384 + 342623270812038402 + Ӣ + Ů + 1953-09-24 + ʯҵ + Ů + һũ + 34262319530924360X +
ͷʯȻ声
+ һũ +
+ + + + 000218082940310312 + 000218082940310312 + 0002 + ΪҽҽԺ + 3780.0400 + 3662.7600 + 0.0000 + 4578.0000 + -1364.9700 + 567.0100 + ZDZ420 + 2017-07-07 9:03:00 + 2017-07-18 8:04:01 + 2104 + 2017-07-18 8:46:05 + -719.9600 + 0.0000 + 1 + 378.0000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231028150665 + 342623102815066502 + ԬΪ + + 1939-02-19 + ԬΪ + + ͱ + 342623193902192736 +
޳ԬȻ壱
+ һũ +
+ + + + 000112104607382178 + 000112104607382178 + 0001 + ΪҽԺ + 2102.3600 + 2060.4800 + 0.0000 + 2760.2000 + -973.2300 + 315.3900 + ZDZ560 + 2017-07-12 10:52:45 + 2017-07-18 9:38:36 + 2104 + 2017-07-18 9:42:46 + -747.6400 + 0.0000 + 1 + 210.2000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232901381552 + 342623290138155202 + ʤ + + 1993-11-01 + + + ͱ + 342623199311015794 +
ëëȻ声
+ һũ +
+ + + + 112606081455041224 + 112606081455041224 + 1126 + ߺеҽԺ + 9096.1500 + 6881.7500 + 0.0000 + 8641.3000 + -898.6900 + 1353.5400 + ZDZ046 + 2017-06-06 8:13:19 + 2017-07-18 10:49:11 + 2107 + 2017-07-18 12:28:42 + 1330.1500 + 465.7000 + 1 + 909.6000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232005050137 + 342623200505013705 + Ҧ + Ů + 1939-01-09 + ʤ + Ů + ͱ + 342623193901094827 +
Ȼ声
+ һũ +
+ + + + 000118090426455871 + 000118090426455871 + 0001 + ΪҽԺ + 2867.3300 + 2716.2900 + 0.0000 + 2736.7000 + -442.8700 + 573.5000 + ZDZ136 + 2017-07-14 19:37:23 + 2017-07-18 8:32:28 + 2104 + 2017-07-18 9:05:34 + 117.3300 + 0.0000 + 1 + 286.7000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426233006190720 + 342623300619072002 + + + 1976-03-30 + μ + + ͱ + 342623197603307554 +
ʮȻ声
+ һũ +
+ + + + 233521080914049038 + 233521080914049038 + 2335 + ϺߺҽԺ + 5093.8400 + 4908.4400 + 0.0000 + 4216.3000 + 0.0000 + 877.5400 + F20.901 + 2017-06-21 8:12:37 + 2017-07-18 8:16:57 + 21 + 2017-07-18 8:27:03 + 886.9400 + 0.0000 + 1 + 509.4000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231802241569 + 342623180224156901 + ܱ + + 1972-01-10 + ܱ + + ͱ + 342623197201105335 +
ɽƹܴȻ
+ һũ +
+ + + + 246615063314827750 + 246615063314827750 + 2466 + ΪƹҽԺ + 2378.0000 + 2378.0000 + 100.0000 + 2259.1000 + 0.0000 + 118.9000 + J03.904 + 2017-07-14 8:05:24 + 2017-07-18 7:23:35 + 21 + 2017-07-18 8:14:24 + 256.7000 + 0.0000 + 1 + 237.8000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231603020437 + 342623160302043701 + Ӧ + + 1965-01-22 + Ӧ + + һũ + 34262319650122557X +
ׯȻ壳
+ һũ +
+ + + + 111219172205505280 + 111219172205505280 + 1112 + ҽƴѧһҽԺ + 36353.2800 + 11043.6600 + 0.0000 + 31299.1000 + 0.0000 + 5054.1800 + C43.601 + 2017-06-19 15:40:00 + 2017-07-18 0:00:00 + 21 + 2017-07-18 14:52:41 + 11723.6800 + 4034.2000 + 1 + 3635.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232212110409 + 342623221211040906 + + Ů + 1949-08-22 + ҦΪ + Ů + һũ + 342623194908222187 +
°Ȼ声
+ һũ +
+ + + + 112818090609587595 + 112818090609587595 + 1128 + ҽѧԺ߮ɽҽԺ + 1060.3100 + 268.5100 + 0.0000 + 742.1000 + 0.0000 + 318.2100 + C16.902 + 2017-07-11 9:04:09 + 2017-07-11 9:04:09 + 15 + 2017-07-18 9:06:45 + -575.7900 + 0.0000 + 1 + 106.0000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232705120425 + 342623270512042501 + ϲ + + 1936-10-22 + ϲ + + һũ + 342623193610223596 +
幨Ȼ声
+ һũ +
+ + + + 000112080308801349 + 000112080308801349 + 0001 + ΪҽԺ + 4868.9400 + 4681.9400 + 300.0000 + 4274.4000 + 0.0000 + 594.5400 + N28.101 + 2017-07-12 8:09:17 + 2017-07-18 14:55:18 + 21 + 2017-07-18 15:07:09 + 781.4400 + 0.0000 + 1 + 486.9000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231914050425 + 342623191405042502 + ϴ÷ + Ů + 1967-04-02 + + Ů + ͱ + 342623196704026829 +
ţٺȻ壸
+ һũ +
+ + + + 000118104220021677 + 000118104220021677 + 0001 + ΪҽԺ + 8555.7000 + 8472.3000 + 0.0000 + 8127.9000 + 0.0000 + 427.8000 + N18.902 + 2017-07-12 10:41:28 + 2017-07-18 10:41:28 + 15 + 2017-07-18 10:42:45 + 1054.3000 + 70.9000 + 1 + 855.6000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231515180628 + 342623151518062801 + + + 1947-09-16 + + + + 342623194709168536 +
ʯ̫ƽСȻ壴
+ һũ +
+ + + + 000112105012505391 + 000112105012505391 + 0001 + ΪҽԺ + 3150.0400 + 3001.0400 + 0.0000 + 4140.0000 + -1462.4700 + 472.5100 + ZDZ348 + 2017-07-12 10:56:18 + 2017-07-18 10:20:17 + 2104 + 2017-07-18 10:18:16 + -974.9600 + 0.0000 + 1 + 315.0000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231517110543 + 342623151711054303 + ˼ + + 1971-01-13 + ˿ά + + һũ + 342623197101138439 +
ʯɽ˼ҴȻ壱
+ һũ +
+ + + + 112418141245997147 + 112418141245997147 + 1124 + ҽƴѧҽԺԭ + 4761.6900 + 4266.0900 + 0.0000 + 3076.1000 + -695.3200 + 2380.9100 + ZDZ347 + 2017-07-10 10:14:32 + 2017-07-18 14:09:18 + 2102 + 2017-07-18 14:14:13 + 1161.6900 + 0.0000 + 1 + 476.1000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232702100504 + 342623270210050402 + ż + Ů + 1946-08-15 + ־ + Ů + һũ + 34262319460815344X +
Ȼ声
+ һũ +
+ + + + 888888180842361258 + 888888180842361258 + 888888 + ʡҽƻ + 23809.2000 + 11816.2000 + 4761.8000 + 11904.5000 + 0.0000 + 11904.7000 + M48.061 + 2017-05-09 0:00:00 + 2017-06-02 0:00:00 + 21 + 2017-07-18 8:43:52 + 9523.8000 + 0.0000 + 3 + 2380.9000 + 2 + 0.0000 + 2 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231906111237 + 342623190611123703 + ƽ + Ů + 1970-04-11 + + Ů + ͱ + 34262319700411614X +
ţ򶫺彯Ȼ壴
+ һũ +
+ + + + 113118085747515065 + 113118085747515065 + 1131 + ͭеҽԺ + 2108.7200 + 877.9000 + 0.0000 + 1581.5000 + 0.0000 + 527.2200 + F32.902 + 2017-01-03 8:51:29 + 2017-07-05 8:51:29 + 15 + 2017-07-18 8:58:53 + 238.1200 + 0.0000 + 1 + 210.9000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232514070385 + 342623251407038501 + ʤ + + 1974-09-23 + ʤ + + һũ + 342623197409235016 +
ȪȻ壸
+ һũ +
+ + + + 000118102549575673 + 000118102549575673 + 0001 + ΪҽԺ + 2968.4000 + 2891.6200 + 300.0000 + 2512.0000 + 0.0000 + 456.4000 + Z99.914 + 2017-07-07 9:05:38 + 2017-07-18 10:29:41 + 21 + 2017-07-18 10:27:20 + 453.2000 + 0.0000 + 1 + 296.8000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231614270998 + 342623161427099802 + ÷ + Ů + 1977-01-06 + ӵ + Ů + һũ + 342623197701064023 +
Ȼ声
+ һũ +
+ + + + 112618081402655014 + 112618081402655014 + 1126 + ߺеҽԺ + 1063.6000 + 3.6000 + 0.0000 + 797.7000 + 0.0000 + 265.9000 + F20.901 + 2017-03-14 8:12:04 + 2017-03-14 8:12:04 + 15 + 2017-07-18 8:14:58 + -127.7000 + 0.0000 + 1 + 106.4000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231027020500 + 342623102702050002 + ҵ + Ů + 1946-12-04 + + Ů + ͱ + 342623194612042726 +
޳ׯ山Ȼ壱
+ һũ +
+ + + + 000118100955337559 + 000118100955337559 + 0001 + ΪҽԺ + 1126.5700 + 977.2800 + 0.0000 + 962.0000 + 0.0000 + 164.5700 + N18.902 + 2017-07-17 8:35:20 + 2017-07-18 9:51:58 + 21 + 2017-07-18 10:11:09 + -22.7300 + 0.0000 + 1 + 112.7000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426233104080410 + 342623310408041002 + Ӣ + Ů + 1978-07-07 + + Ů + ͱ + 342623197807075740 +
ɽ彫Ȼ壶
+ һũ +
+ + + + 004210163614466214 + 004210163614466214 + 0042 + ΪؼҽԺ + 4153.7300 + 3954.2700 + 0.0000 + 3790.3000 + -259.7000 + 623.1300 + ZDZ053 + 2017-07-10 16:36:00 + 2017-07-18 7:56:49 + 2104 + 2017-07-18 8:02:39 + 478.7300 + 0.0000 + 1 + 415.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231021090716 + 342623102109071602 + + Ů + 1939-04-29 + ʤ + Ů + ͱ + 342623193904290022 +
޳׳Ȼ声
+ һũ +
+ + + + 000218090046744447 + 000218090046744447 + 0002 + ΪҽҽԺ + 4713.6100 + 4360.6200 + 0.0000 + 4221.3000 + -214.7900 + 707.1000 + ZDZ705 + 2017-07-11 8:23:00 + 2017-07-18 8:23:01 + 2104 + 2017-07-18 9:06:03 + 663.6100 + 0.0000 + 1 + 471.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232609090400 + 342623260909040001 + + + 1991-01-24 + ƽ + + һũ + 342623199101244855 +
յȻ壴
+ һũ +
+ + + + 234118100913563244 + 234118100913563244 + 2341 + ߺҽԺ + 12041.2000 + 9418.2000 + 500.0000 + 8661.2000 + 0.0000 + 3380.0000 + S99.901 + 2017-06-25 9:03:57 + 2017-07-01 9:03:57 + 34 + 2017-07-18 10:09:46 + 8474.0000 + 0.0000 + 2 + 1204.1000 + 2 + 3889.9000 + 2 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231602260770 + 342623160226077005 + + + 1977-08-20 + + + ͱ + 342623197708204199 +
ɽմȻ声
+ һũ +
+ + + + 004218144113217583 + 004218144113217583 + 0042 + ΪؼҽԺ + 2195.4000 + 1915.5000 + 0.0000 + 1847.6000 + 0.0000 + 347.8000 + N18.903 + 2017-04-29 9:22:06 + 2017-07-15 9:22:06 + 15 + 2017-07-18 14:44:22 + 267.3000 + 0.0000 + 1 + 219.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231906111238 + 342623190611123801 + + + 1953-03-22 + + + ͱ + 342623195303226155 +
ţ򶫺彯Ȼ壴
+ һũ +
+ + + + 888888180910373435 + 888888180910373435 + 888888 + ʡҽƻ + 28049.0000 + 24115.0000 + 0.0000 + 18479.6000 + 0.0000 + 9569.4000 + J60 01 + 2017-06-12 0:00:00 + 2017-07-07 0:00:00 + 21 + 2017-07-18 9:15:57 + 6764.5000 + 0.0000 + 3 + 2804.9000 + 2 + 0.0000 + 2 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232501030177 + 342623250103017704 + Ӣ + Ů + 1943-01-16 + + Ů + һũ + 342623194301163029 +
Ȫ役Ȼ壳
+ һũ +
+ + + + 005815202526443116 + 005815202526443116 + 0058 + Ϊ̩ҽԺ + 1549.0000 + 1475.2000 + 100.0000 + 1454.4000 + 0.0000 + 94.6000 + I10 05 + 2017-07-15 20:25:26 + 2017-07-18 9:08:12 + 21 + 2017-07-18 9:48:38 + 149.5000 + 0.0000 + 1 + 154.9000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426231909050079 + 342623190905007901 + + + 1947-03-16 + + + + 342623194703166273 +
ţȻ壱
+ һũ +
+ + + + 264912072701645298 + 264912072701645298 + 2649 + ΪҽԺ + 2446.0000 + 2414.0000 + 0.0000 + 2323.7000 + 0.0000 + 122.3000 + ZDZ001 + 2017-07-12 7:25:54 + 2017-07-18 9:57:12 + 21 + 2017-07-18 10:03:11 + 266.9000 + 0.0000 + 1 + 244.6000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232104250962 + 342623210425096203 + ƽ + + 1969-02-24 + ´ + + ͱ + 342623196902247710 +
ҦȻƣ
+ һũ +
+ + + + 000118093718673134 + 000118093718673134 + 0001 + ΪҽԺ + 1236.7200 + 1201.7200 + 0.0000 + 1145.1000 + 0.0000 + 91.6200 + N18.902 + 2017-01-05 9:35:57 + 2017-07-18 9:35:57 + 15 + 2017-07-18 9:37:53 + -84.6800 + 0.0000 + 1 + 123.7000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426233204070330 + 342623320407033001 + ï + + 1970-09-19 + ï + + ͱ + 342623197009196513 +
ɽɽȻ
+ һũ +
+ + + + 112624085058312777 + 112624085058312777 + 1126 + ߺеҽԺ + 12234.4000 + 10947.4000 + 0.0000 + 11541.5000 + -950.9200 + 1643.8200 + ZDZ046 + 2017-05-24 8:50:00 + 2017-07-18 10:10:21 + 2107 + 2017-07-18 12:42:17 + 2219.4000 + 803.1000 + 1 + 1223.4000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232602050151 + 342623260205015103 + Ө + Ů + 1957-09-10 + ά + Ů + ͱ + 342623195709104828 +
յƽȻ壱
+ һũ +
+ + + + 000111075141336280 + 000111075141336280 + 0001 + ΪҽԺ + 3333.6000 + 3183.1400 + 300.0000 + 2786.0000 + 0.0000 + 547.6000 + R00.201 + 2017-07-11 7:58:02 + 2017-07-18 8:45:01 + 21 + 2017-07-18 10:24:06 + 581.0000 + 0.0000 + 1 + 333.4000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426233201090836 + 342623320109083601 + + + 1968-06-11 + + + ͱ + 342623196806116518 +
Ȼ壵
+ һũ +
+ + + + 004318093638175330 + 004318093638175330 + 0043 + Ϊ޷ҽԺ + 1963.0000 + 1953.0000 + 0.0000 + 1864.9000 + 0.0000 + 98.1000 + I63.902 + 2017-07-09 0:00:00 + 2017-07-18 9:41:33 + 21 + 2017-07-18 9:44:44 + -5.6000 + 0.0000 + 1 + 196.3000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232402050175 + 342623240205017501 + + + 1936-01-03 + + + ͱ + 34262319360103139X +
ӰȻ
+ һũ +
+ + + + 888888181039278391 + 888888181039278391 + 888888 + ʡҽƻ + 10347.5000 + 9313.5000 + 0.0000 + 3434.8000 + 0.0000 + 6912.7000 + ZDZ023 + 2017-06-12 0:00:00 + 2017-06-16 0:00:00 + 99 + 2017-07-18 10:39:56 + 5878.0000 + 0.0000 + 3 + 1034.8000 + 2 + 0.0000 + 2 + 0 + 3 + 1 + 1 + + +
+ + + + 3426232804160253 + 342623280416025301 + ϲ + + 1937-08-26 + ϲ + + ͱ + 342623193708267917 +
߹Ȼƣ
+ һũ +
+ + + + 000118104842815812 + 000118104842815812 + 0001 + ΪҽԺ + 6654.8400 + 5699.6600 + 0.0000 + 5510.2000 + 0.0000 + 1144.6400 + N18.902 + 2017-01-17 10:47:19 + 2017-07-18 10:47:19 + 15 + 2017-07-18 10:49:09 + 1510.1400 + 0.0000 + 1 + 665.5000 + 1 + 0.0000 + 1 + 0 + 3 + 1 + 1 + + +
+
+
+ \ No newline at end of file diff --git a/students/313001956/src/main/java/com/coderising/atm/ATM.java b/students/313001956/src/main/java/com/coderising/atm/ATM.java new file mode 100644 index 0000000000..782dc48d13 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/ATM.java @@ -0,0 +1,127 @@ +package com.coderising.atm; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.atm.transactions.Transaction; +import com.coderising.atm.transactions.TransactionBase; + +public class ATM { + CardReader cardReader; + CashDepensier cashDepensier; + DepositSlot depositSlot; + Printer printer; + SuperKeypad superKeypad; + BankProxy bankProxy; + + public ATM(CardReader cardReader, CashDepensier cashDepensier, DepositSlot depositSlot, Printer printer, + SuperKeypad superKeypad, BankProxy bankProxy) { + this.cardReader = cardReader; + this.cashDepensier = cashDepensier; + this.depositSlot = depositSlot; + this.printer = printer; + this.superKeypad = superKeypad; + this.bankProxy = bankProxy; + } + + public boolean hashEnoughMoney(int amount) { + // TODO Auto-generated method stub + return cashDepensier.hashEnoughMoney(amount); + } + + public boolean dispenseMoney(int amount) { + // TODO Auto-generated method stub + return cashDepensier.dispenseMoney(amount); + } + + public int retriveMoney() { + // TODO Auto-generated method stub + return depositSlot.retriveMoney(); + } + + public SuperKeypad getSuperKeypad() { + return this.superKeypad; + } + + public static void main(String[] args) { + ATM atm = null; + try { + + atm = new ATM(new CardReader(), new CashDepensier(), new DepositSlot(), new Printer(), + new SuperKeypad(new Display(), new KeyBoard()), new BankProxy()); + int account = atm.cardReader.getAccount(); + atm.getSuperKeypad().displayMessage("??????ATM"); + Thread.sleep(2000); + // ?????? + atm.getSuperKeypad().displayMessage("?????" + account); + + String type = ""; + int password = 0; + List transactions = new ArrayList<>(); + TransactionBase tx = null; + while (true) { + type = atm.superKeypad.getTransactionType(); + if (type.equals("D") || type.equals("W") || type.equals("T") || type.equals("B")) { + password = verifyPassword(atm, account); + } + tx = atm.superKeypad.getTransaction(account, password, type); + if (tx.getType().equals("P")) { + atm.printer.print(transactions); + + break; + } else if (tx.getType().equals("E")) { + + break; + } else if (tx != null) { + + if (!tx.preProcess(atm)) { + Thread.sleep(2000); + continue; + } + if (!atm.bankProxy.process(tx)) { + Thread.sleep(2000); + continue; + } + if (!tx.postProcess(atm)) { + Thread.sleep(2000); + continue; + } + // atm.getSuperKeypad().displayMessage("????????"); + transactions.add(tx); + } + } + + } catch (Exception ex) { + atm.superKeypad.displayMessage(ex.getMessage()); + + } finally { + atm.cardReader.ejectCard(); + } + } + + private static int verifyPassword(ATM atm, int account) { + int password = 0; + int failCount = 0; + boolean verified = false; + atm.getSuperKeypad().displayMessage("??????????????"); + while (!verified) { + password = atm.superKeypad.getPassword(); + verified = atm.bankProxy.verify(account, password); + if (!verified) { + + failCount++; + if (failCount >= 3) { + break; + } + + atm.getSuperKeypad().displayMessage("?????????????????" + (3 - failCount) + "????"); + } + } + if (!verified) { + atm.bankProxy.FreezeAccount(account); + throw new RuntimeException("????????????3??,?????????????"); + } + return password; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/BankProxy.java b/students/313001956/src/main/java/com/coderising/atm/BankProxy.java new file mode 100644 index 0000000000..a88404de7a --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/BankProxy.java @@ -0,0 +1,24 @@ +package com.coderising.atm; + +import com.coderising.atm.transactions.Transaction; +import com.coderising.bank.ATMProxy; +import com.coderising.bank.Bank; + +public class BankProxy { + ATMProxy atmProxy = new ATMProxy(new Bank()); + + public boolean verify(int account, int password) { + + return atmProxy.verify(account, password); + } + + public boolean process(Transaction tx) { + String netPackage = tx.toNetworkPackage(); + atmProxy.process(netPackage, null); + return true; + } + + public void FreezeAccount(int account) { + + } +} diff --git a/students/313001956/src/main/java/com/coderising/atm/CardReader.java b/students/313001956/src/main/java/com/coderising/atm/CardReader.java new file mode 100644 index 0000000000..ea50a652bd --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/CardReader.java @@ -0,0 +1,45 @@ +package com.coderising.atm; + +public class CardReader { + //ATM atm; + + public CardReader() { + //this.atm = atm; + } + + public static final int CARDNUM = 12345678; + + public int getAccount() { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + if (!detectCard()) { + //System.out.println("???????"); + throw new RuntimeException("???????"); + } + + int cardnum = readCardNum(); + return cardnum; + + } + + private int readCardNum() { + // TODO Auto-generated method stub + return CARDNUM; + } + + // ????????????????????????????? + private boolean detectCard() { + return true; + } + + + public void ejectCard() { + // TODO Auto-generated method stub + + } +} + diff --git a/students/313001956/src/main/java/com/coderising/atm/CashDepensier.java b/students/313001956/src/main/java/com/coderising/atm/CashDepensier.java new file mode 100644 index 0000000000..477ae26c2c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/CashDepensier.java @@ -0,0 +1,15 @@ +package com.coderising.atm; + +public class CashDepensier { + + public boolean hashEnoughMoney(int amount) { + // TODO Auto-generated method stub + return amount<=1000; + } + + public boolean dispenseMoney(int amount) { + // TODO Auto-generated method stub + return amount<=1000; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/DepositSlot.java b/students/313001956/src/main/java/com/coderising/atm/DepositSlot.java new file mode 100644 index 0000000000..2acce638aa --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/DepositSlot.java @@ -0,0 +1,10 @@ +package com.coderising.atm; + +public class DepositSlot { + + public int retriveMoney() { + int rd = (int) (Math.random() * 10); + return rd * 100; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/Display.java b/students/313001956/src/main/java/com/coderising/atm/Display.java new file mode 100644 index 0000000000..96cc481c73 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/Display.java @@ -0,0 +1,11 @@ +package com.coderising.atm; + +public class Display { + public void displayMessage(String message) { + System.out.println(message); + } + + public void displayPassword(char message) { + System.out.print(message); + } +} diff --git a/students/313001956/src/main/java/com/coderising/atm/KeyBoard.java b/students/313001956/src/main/java/com/coderising/atm/KeyBoard.java new file mode 100644 index 0000000000..0123e4760b --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/KeyBoard.java @@ -0,0 +1,54 @@ +package com.coderising.atm; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Scanner; + +import javax.swing.JFrame; + +public class KeyBoard implements KeyListener { + + int charA = 0; + + public String getUserInput() { + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + try { + return br.readLine(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return ""; + } + + @Override + public void keyPressed(KeyEvent e) { + SuperKeypad sk = (SuperKeypad) e.getSource(); + charA = e.getKeyCode(); + sk.display.displayPassword('*'); + if (charA != 10) { + sk.appendStr((char) (int) charA); + } else { + sk.display.displayPassword((char) (int) charA); + sk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + sk.removeKeyListener(this); + sk.reGetPassword(); + } + } + + @Override + public void keyReleased(KeyEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/Network.java b/students/313001956/src/main/java/com/coderising/atm/Network.java new file mode 100644 index 0000000000..ea8a7e2107 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/Network.java @@ -0,0 +1,5 @@ +package com.coderising.atm; + +public class Network { + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/Printer.java b/students/313001956/src/main/java/com/coderising/atm/Printer.java new file mode 100644 index 0000000000..1931a40be4 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/Printer.java @@ -0,0 +1,17 @@ +package com.coderising.atm; + +import java.util.List; + +import com.coderising.atm.transactions.Transaction; +import com.coderising.atm.transactions.TransactionBase; + +public class Printer { + + public void print(List transactions) { + for (int i = 0; i < transactions.size(); i++) { + System.out.println(transactions.get(i).toNetworkPackage()); + } + + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/SuperKeypad.java b/students/313001956/src/main/java/com/coderising/atm/SuperKeypad.java new file mode 100644 index 0000000000..db5470611c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/SuperKeypad.java @@ -0,0 +1,131 @@ +package com.coderising.atm; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.concurrent.LinkedBlockingQueue; + +import javax.swing.JFrame; + +import com.coderising.atm.transactions.DepositTx; +import com.coderising.atm.transactions.QueryBalanceTx; +import com.coderising.atm.transactions.Transaction; +import com.coderising.atm.transactions.TransactionBase; +import com.coderising.atm.transactions.TransferTx; +import com.coderising.atm.transactions.WithdrawTx; + +public class SuperKeypad extends JFrame { + + Display display; + KeyBoard keyBoard; + private boolean whileKey = true; + private StringBuilder sb = new StringBuilder(); + + public SuperKeypad(Display display, KeyBoard keyBoard) { + this.display = display; + this.keyBoard = keyBoard; + + // initFrame(keyBoard); + } + + private void initFrame(KeyBoard keyBoard) { + this.setSize(0, 0); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setTitle("my jframe"); + this.setVisible(true); + + } + + public int getPassword() { + // ???? + // this.addKeyListener(keyBoard); + return Integer.valueOf(keyBoard.getUserInput()); + + } + + public String reGetPassword() { + // display.displayMessage(sb.toString()); + // ??????? + return sb.toString(); + } + + public void displayMessage(String message) { + display.displayMessage(message); + } + + public void setWhileKey(boolean whileKey) { + this.whileKey = whileKey; + } + + public void appendStr(char ch) { + sb.append(ch); + } + + public String getTransactionType() { + displayMessage("??????????? D : ??? W: ??? T: ??? B???????? P:??? E:???"); + String type = keyBoard.getUserInput(); + return type; + } + + public TransactionBase getTransaction(int account, int password, String type) { + TransactionBase tx = null; + + if (type.equals("D")) { + displayMessage("????????"); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + tx = new DepositTx(account, password, Transaction.DEPOSIT); + } else if (type.equals("W")) { + displayMessage("???????????"); + int amount = Integer.valueOf(keyBoard.getUserInput()); + tx = new WithdrawTx(account, password, Transaction.WITHDRAW, amount); + } else if (type.equals("T")) { + displayMessage("???????????"); + int toAccount = Integer.valueOf(keyBoard.getUserInput()); + displayMessage("???????????"); + int transferMoney = Integer.valueOf(keyBoard.getUserInput()); + tx = new TransferTx(account, password, Transaction.TRANFER, toAccount, transferMoney); + } else if (type.equals("B")) { + tx = new QueryBalanceTx(account, password, Transaction.BALANCE); + } else if (type.equals("P")) { + tx = new TransactionBase(account, password, Transaction.PRINT) { + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + }; + } else if (type.equals("E")) { + tx = new TransactionBase(account, password, Transaction.EXIT) { + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + }; + } else { + displayMessage("??????????????????"); + } + + return tx; + } +} diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/DepositTx.java b/students/313001956/src/main/java/com/coderising/atm/transactions/DepositTx.java new file mode 100644 index 0000000000..bdfdca9c1e --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/DepositTx.java @@ -0,0 +1,36 @@ +package com.coderising.atm.transactions; + +import com.coderising.atm.ATM; + +public class DepositTx extends TransactionBase { + + private int acctualMoney; + + public DepositTx(int account, int password, String type) { + super(account, password, type); + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + acctualMoney= atm.retriveMoney(); + + return true; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return true; + } + + public String toNetworkPackage() { + + return super.toNetworkPackage() +"|"+getAcctualMoney(); + } + + public int getAcctualMoney() { + return acctualMoney; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/QueryBalanceTx.java b/students/313001956/src/main/java/com/coderising/atm/transactions/QueryBalanceTx.java new file mode 100644 index 0000000000..fbe7a25663 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/QueryBalanceTx.java @@ -0,0 +1,28 @@ +package com.coderising.atm.transactions; + +import com.coderising.atm.ATM; + +public class QueryBalanceTx extends TransactionBase{ + + public QueryBalanceTx(int account, int password, String type) { + super(account, password, type); + // TODO Auto-generated constructor stub + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return true; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return true; + } + +public String toNetworkPackage() { + + return super.toNetworkPackage(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/Transaction.java b/students/313001956/src/main/java/com/coderising/atm/transactions/Transaction.java new file mode 100644 index 0000000000..1d5812d7d0 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/Transaction.java @@ -0,0 +1,19 @@ +package com.coderising.atm.transactions; + +import com.coderising.atm.ATM; + +public interface Transaction { + public static final String EXIT = "E"; + public static final String DEPOSIT = "D"; + public static final String WITHDRAW = "W"; + public static final String TRANFER = "T"; + public static final String BALANCE = "B"; + public static final String PRINT = "P"; + + + public boolean preProcess(ATM atm); + + public boolean postProcess(ATM atm); + + public String toNetworkPackage(); + } diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/TransactionBase.java b/students/313001956/src/main/java/com/coderising/atm/transactions/TransactionBase.java new file mode 100644 index 0000000000..a54b6804e3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/TransactionBase.java @@ -0,0 +1,29 @@ +package com.coderising.atm.transactions; + +public abstract class TransactionBase implements Transaction{ + int account; + int password; + String type; + + + public TransactionBase(int account,int password, String type) { + this.account=account; + this.password=password; + this.type=type; + } + + public int getAccount() { + return account; + } + + public String toNetworkPackage() { + // TODO Auto-generated method stub + return getType() +"|"+getAccount(); + } + + public String getType() { + // TODO Auto-generated method stub + return type; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/TransferTx.java b/students/313001956/src/main/java/com/coderising/atm/transactions/TransferTx.java new file mode 100644 index 0000000000..6e7c3e27c2 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/TransferTx.java @@ -0,0 +1,39 @@ +package com.coderising.atm.transactions; + +import com.coderising.atm.ATM; + +public class TransferTx extends TransactionBase { + int toAccount; + int transferMoney; + public TransferTx(int account, int password, String type, int toAccount, int transferMoney) { + super(account, password, type); + // TODO Auto-generated constructor stub + this.toAccount=toAccount; + this.transferMoney=transferMoney; + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return true; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return true; + } + +public String toNetworkPackage() { + + return super.toNetworkPackage() +"|"+getToAccount()+"|"+getTransferMoney(); + } + +public int getToAccount() { + return toAccount; +} + +public int getTransferMoney() { + return transferMoney; +} +} diff --git a/students/313001956/src/main/java/com/coderising/atm/transactions/WithdrawTx.java b/students/313001956/src/main/java/com/coderising/atm/transactions/WithdrawTx.java new file mode 100644 index 0000000000..4b2d9a244d --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/atm/transactions/WithdrawTx.java @@ -0,0 +1,41 @@ +package com.coderising.atm.transactions; + +import com.coderising.atm.ATM; + +public class WithdrawTx extends TransactionBase { + + int amount; + + public WithdrawTx(int account,int password,String type, int amount) { + super(account, password, type); + this.amount=amount; + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + boolean result= atm.hashEnoughMoney(getAmount()); + if (!result) { + atm.getSuperKeypad().displayMessage("ATM???????"); + + } + + return result ; + } + + private int getAmount() { + // TODO Auto-generated method stub + return amount; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return atm.dispenseMoney(getAmount()); + } + +public String toNetworkPackage() { + + return super.toNetworkPackage() +"|"+getAmount(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/bank/ATMProxy.java b/students/313001956/src/main/java/com/coderising/bank/ATMProxy.java new file mode 100644 index 0000000000..2b636de0ef --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/ATMProxy.java @@ -0,0 +1,41 @@ +package com.coderising.bank; + +import org.junit.experimental.theories.Theories; + +import com.coderising.atm.CardReader; +import com.coderising.bank.transactions.Transaction; + +public class ATMProxy { + private Bank bank; + + public ATMProxy(Bank bank) { + this.bank = bank; + } + + public void run() { + // ??????? + String data = ""; + String response = ""; + process(data, response); + } + + public void process(String data, String response) { + Transaction tx = toActObject(data); + int status = bank.process(tx); + // response.write(status); + } + + private Transaction toActObject(String data) { + // TODO Auto-generated method stub + return null; + } + + public boolean verify(int account, int password) { + + return this.bank.verify(account, password); + } + + public void FreezeAccount(int faccount) { + bank.FreezeAccount(faccount); + } +} diff --git a/students/313001956/src/main/java/com/coderising/bank/Account.java b/students/313001956/src/main/java/com/coderising/bank/Account.java new file mode 100644 index 0000000000..4f3f8fa62a --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/Account.java @@ -0,0 +1,39 @@ +package com.coderising.bank; + +public class Account { + + private int faccount; + private int password; + private int balance; + private boolean isfreeze; + + public Account(int faccount, int password) { + this.faccount = faccount; + this.password = password; + // TODO Auto-generated constructor stub + } + + public int getFaccount() { + return faccount; + } + + public int getPassword() { + return password; + } + + public int getBalance() { + return balance; + } + + public void setBalance(int balance) { + this.balance = balance; + } + + public void setIsfreeze(boolean isfreeze) { + this.isfreeze = isfreeze; + } + + public boolean getIsfreeze() { + return this.isfreeze; + } +} diff --git a/students/313001956/src/main/java/com/coderising/bank/Bank.java b/students/313001956/src/main/java/com/coderising/bank/Bank.java new file mode 100644 index 0000000000..801bd6a9a3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/Bank.java @@ -0,0 +1,46 @@ +package com.coderising.bank; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.atm.CardReader; +import com.coderising.bank.transactions.Transaction; + +public class Bank { + + List accounts; + + public Bank() { + accounts = new ArrayList<>(); + Account account = new Account(12345678, 1234); + account.setBalance(5000); + accounts.add(account); + } + + public int process(Transaction tx) { + // TODO Auto-generated method stub + return 0; + } + + public boolean verify(int account, int password) { + Account acc = getAccount(account); + if (acc != null) { + return acc.getPassword() == password; + } + return false; + } + + public Account getAccount(int faccount) { + for (Account acc : accounts) { + if (acc.getFaccount() == faccount) { + return acc; + } + } + + return null; + } + + public void FreezeAccount(int faccount) { + getAccount(faccount).setIsfreeze(true); + } +} diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/DepositTx.java b/students/313001956/src/main/java/com/coderising/bank/transactions/DepositTx.java new file mode 100644 index 0000000000..e6ce262ccb --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/DepositTx.java @@ -0,0 +1,27 @@ +package com.coderising.bank.transactions; + +import com.coderising.atm.ATM; + +public class DepositTx extends TransactionBase { + + public DepositTx(int account, int password) { + super(account, password); + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + int acctualMoney= atm.retriveMoney(); + + return false; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + + +} diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/QueryBalanceTx.java b/students/313001956/src/main/java/com/coderising/bank/transactions/QueryBalanceTx.java new file mode 100644 index 0000000000..4aaa570614 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/QueryBalanceTx.java @@ -0,0 +1,25 @@ +package com.coderising.bank.transactions; + +import com.coderising.atm.ATM; + +public class QueryBalanceTx extends TransactionBase{ + + public QueryBalanceTx(int account, int password) { + super(account, password); + // TODO Auto-generated constructor stub + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + +} diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/Transaction.java b/students/313001956/src/main/java/com/coderising/bank/transactions/Transaction.java new file mode 100644 index 0000000000..1172852e68 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/Transaction.java @@ -0,0 +1,9 @@ +package com.coderising.bank.transactions; + +import com.coderising.atm.ATM; + +public interface Transaction { + public boolean preProcess(ATM atm); + + public boolean postProcess(ATM atm); + } diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/TransactionBase.java b/students/313001956/src/main/java/com/coderising/bank/transactions/TransactionBase.java new file mode 100644 index 0000000000..2992310ceb --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/TransactionBase.java @@ -0,0 +1,17 @@ +package com.coderising.bank.transactions; + +public abstract class TransactionBase implements Transaction{ + int account; + int password; + + + public TransactionBase(int account,int password) { + this.account=account; + this.password=password; + } + + public int getAccount() { + return account; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/TransferTx.java b/students/313001956/src/main/java/com/coderising/bank/transactions/TransferTx.java new file mode 100644 index 0000000000..d273b76597 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/TransferTx.java @@ -0,0 +1,25 @@ +package com.coderising.bank.transactions; + +import com.coderising.atm.ATM; + +public class TransferTx extends TransactionBase { + + public TransferTx(int account, int password) { + super(account, password); + // TODO Auto-generated constructor stub + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return false; + } + + +} diff --git a/students/313001956/src/main/java/com/coderising/bank/transactions/WithdrawTx.java b/students/313001956/src/main/java/com/coderising/bank/transactions/WithdrawTx.java new file mode 100644 index 0000000000..2a15f6797e --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/bank/transactions/WithdrawTx.java @@ -0,0 +1,32 @@ +package com.coderising.bank.transactions; + +import com.coderising.atm.ATM; + +public class WithdrawTx extends TransactionBase { + + int amount; + + public WithdrawTx(int account,int password,int amount) { + super(account, password); + this.amount=amount; + } + + @Override + public boolean preProcess(ATM atm) { + // TODO Auto-generated method stub + return atm.hashEnoughMoney(getAmount()); + } + + private int getAmount() { + // TODO Auto-generated method stub + return amount; + } + + @Override + public boolean postProcess(ATM atm) { + // TODO Auto-generated method stub + return atm.dispenseMoney(getAmount()); + } + + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/Circle.java b/students/313001956/src/main/java/com/coderising/dp/bridge/Circle.java new file mode 100644 index 0000000000..138e25b4f8 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/Circle.java @@ -0,0 +1,23 @@ +package com.coderising.dp.bridge; + +public class Circle implements Shape { + + private Drawing drawing; + private int x; + private int y; + private int r; + + public Circle(Drawing drawing, int x, int y, int r) { + this.drawing = drawing; + this.x = x; + this.y = y; + this.r = r; + + } + + @Override + public void draw() { + drawing.drawCircle(x, y, r); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/Drawing.java b/students/313001956/src/main/java/com/coderising/dp/bridge/Drawing.java new file mode 100644 index 0000000000..f29acaa393 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/Drawing.java @@ -0,0 +1,7 @@ +package com.coderising.dp.bridge; + +public interface Drawing { + public void drawLine(int x1,int y1,int x2,int y2); + + public void drawCircle(int x,int y, int r); +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL1.java b/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL1.java new file mode 100644 index 0000000000..290853b1be --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL1.java @@ -0,0 +1,20 @@ +package com.coderising.dp.bridge; + +public class DrawingGL1 implements Drawing { + GraphicLibrary1 lib1; + + public DrawingGL1(GraphicLibrary1 lib1) { + this.lib1 = lib1; + } + + @Override + public void drawLine(int x1, int y1, int x2, int y2) { + lib1.draw_a_line(x1, y1, x2, y2); + } + + @Override + public void drawCircle(int x, int y, int r) { + lib1.draw_a_circle(x, y, r); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL2.java b/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL2.java new file mode 100644 index 0000000000..b24d6e0c65 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/DrawingGL2.java @@ -0,0 +1,20 @@ +package com.coderising.dp.bridge; + +public class DrawingGL2 implements Drawing { + GraphicLibrary2 lib2; + + public DrawingGL2(GraphicLibrary2 lib2) { + this.lib2 = lib2; + } + + @Override + public void drawLine(int x1, int y1, int x2, int y2) { + lib2.drawLine(x1, x2, y1, y2); + } + + @Override + public void drawCircle(int x, int y, int r) { + lib2.drawCircle(x, y, r); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary1.java b/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary1.java new file mode 100644 index 0000000000..deb40619a0 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary1.java @@ -0,0 +1,11 @@ +package com.coderising.dp.bridge; + +public class GraphicLibrary1 { + public void draw_a_line(int x1,int y1,int x2,int y2){ + System.out.println("GraphicLibrary1:draw rectangle"); + } + public void draw_a_circle(int x,int y, int r){ + System.out.println("GraphicLibrary1:draw circle"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary2.java b/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary2.java new file mode 100644 index 0000000000..84b306a3ff --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/GraphicLibrary2.java @@ -0,0 +1,11 @@ +package com.coderising.dp.bridge; + +public class GraphicLibrary2 { + public void drawLine(int x1,int x2,int y1,int y2){ + System.out.println("GraphicLibrary2:draw rectangle"); + } + public void drawCircle(int x,int y, int r){ + System.out.println("GraphicLibrary2:draw circle"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/Rectangle.java b/students/313001956/src/main/java/com/coderising/dp/bridge/Rectangle.java new file mode 100644 index 0000000000..8239628cb5 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/Rectangle.java @@ -0,0 +1,23 @@ +package com.coderising.dp.bridge; + +public class Rectangle implements Shape { + private Drawing drawing; + private int x1; + private int y1; + private int x2; + private int y2; + + public Rectangle(Drawing drawing, int x1, int x2, int y1, int y2) { + this.drawing = drawing; + this.x1 = x1; + this.x2 = x2; + this.y1 = y1; + this.y2 = y2; + } + + @Override + public void draw() { + drawing.drawLine(x1, y1, x2, y2); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/Shape.java b/students/313001956/src/main/java/com/coderising/dp/bridge/Shape.java new file mode 100644 index 0000000000..ec69f08fd4 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/Shape.java @@ -0,0 +1,5 @@ +package com.coderising.dp.bridge; + +public interface Shape { + public void draw(); +} diff --git a/students/313001956/src/main/java/com/coderising/dp/bridge/testBrige.java b/students/313001956/src/main/java/com/coderising/dp/bridge/testBrige.java new file mode 100644 index 0000000000..c01ee02ef1 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/bridge/testBrige.java @@ -0,0 +1,28 @@ +package com.coderising.dp.bridge; + +public class testBrige { + public static void main(String[] args) { + GraphicLibrary1 lib1 = new GraphicLibrary1(); + GraphicLibrary2 lib2 = new GraphicLibrary2(); + + DrawingGL1 drawingGL1 = new DrawingGL1(lib1); + DrawingGL2 drawingGL2 = new DrawingGL2(lib2); + int x = 8; + int y = 9; + int r = 6; + Circle circle = new Circle(drawingGL1, x, y, r); + circle.draw(); + + circle = new Circle(drawingGL2, x, y, r); + circle.draw(); + + int x1 = 5; + int y1 = 87; + int x2 = 8; + int y2 = 6; + Rectangle rectangle = new Rectangle(drawingGL1, x1, y1, x2, y2); + rectangle.draw(); + rectangle = new Rectangle(drawingGL2, x1, x2, y1, y2); + rectangle.draw(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilder.java b/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilder.java new file mode 100644 index 0000000000..07466f04cc --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilder.java @@ -0,0 +1,43 @@ +package com.coderising.dp.builder; + +public class TagBuilder { + + private TagNode root; + private TagNode currentNode; + private TagNode currentNodeParent; + + public TagBuilder(String rootTagName) { + this.root = new TagNode(rootTagName); + this.currentNode = this.root; + } + + public TagBuilder addChild(String childTagName) { + TagNode node = new TagNode(childTagName); + this.currentNode.add(node); + currentNodeParent = currentNode; + currentNode = node; + return this; + } + + public TagBuilder addSibling(String siblingTagName) { + TagNode node = new TagNode(siblingTagName); + this.currentNodeParent.add(node); + currentNode = node; + return this; + + } + + public TagBuilder setAttribute(String name, String value) { + this.currentNode.setAttribute(name, value); + return this; + } + + public TagBuilder setText(String value) { + this.currentNode.setValue(value); + return this; + } + + public String toXML() { + return this.root.toXML(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilderTest.java b/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilderTest.java new file mode 100644 index 0000000000..10731ec824 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/builder/TagBuilderTest.java @@ -0,0 +1,39 @@ +package com.coderising.dp.builder; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TagBuilderTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } +//java.lang.Runtime??java.awt.Desktop??java.awt.Toolkit + @Test + public void testToXML() { + + TagBuilder builder = new TagBuilder("order"); + + String xml = builder.addChild("line-items") + .addChild("line-item").setAttribute("pid", "P3677").setAttribute("qty", "3") + .addSibling("line-item").setAttribute("pid", "P9877").setAttribute("qty", "10") + .toXML(); + + String expected = "" + + "" + + "" + + "" + + "" + + ""; + + System.out.println(xml); + assertEquals(expected, xml); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/builder/TagNode.java b/students/313001956/src/main/java/com/coderising/dp/builder/TagNode.java new file mode 100644 index 0000000000..2ac26ad7b9 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/builder/TagNode.java @@ -0,0 +1,83 @@ +package com.coderising.dp.builder; + +import java.util.ArrayList; +import java.util.List; + +public class TagNode { + private String tagName; + private String tagValue; + private List children = new ArrayList<>(); + private List attributes = new ArrayList<>(); + + public TagNode(String name){ + this.tagName = name; + } + public void add(TagNode child){ + this.children.add(child); + } + public void setAttribute(String name, String value) { + Attribute attr = findAttribute(name); + if(attr != null){ + attr.value = value; + return; + } + + attributes.add(new Attribute(name,value)); + } + private Attribute findAttribute(String name){ + for(Attribute attr : attributes){ + if(attr.name.equals(name)){ + return attr; + } + } + return null; + } + public void setValue(String value) { + this.tagValue = value; + + } + public String getTagName() { + return tagName; + } + public List getChildren() { + return children; + } + + private static class Attribute{ + public Attribute(String name, String value) { + this.name = name; + this.value = value; + } + String name; + String value; + + } + public String toXML(){ + return toXML(this); + } + private String toXML(TagNode node){ + StringBuilder buffer = new StringBuilder(); + buffer.append("<").append(node.tagName); + if(node.attributes.size()> 0){ + for(int i=0;i"); + return buffer.toString(); + } + buffer.append(">"); + for(TagNode childNode : node.children){ + buffer.append(toXML(childNode)); + } + buffer.append(""); + + + return buffer.toString(); + } + private String toXML(Attribute attr){ + return attr.name+"=\""+attr.value + "\""; + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/ChainLogger.java b/students/313001956/src/main/java/com/coderising/dp/chain/ChainLogger.java new file mode 100644 index 0000000000..77a19a079c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/ChainLogger.java @@ -0,0 +1,11 @@ +package com.coderising.dp.chain; + +public class ChainLogger { + public static void main(String[] args) { + Logger logger = new StdoutLogger(Logger.DEBUG) + .setNext(new EmailLogger(Logger.NOTICE).setNext(new FileLogger(Logger.ERR))); + logger.message("?????????", Logger.DEBUG); + logger.message("???????????", Logger.NOTICE); + logger.message("????????????????", Logger.ERR); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/EmailLogger.java b/students/313001956/src/main/java/com/coderising/dp/chain/EmailLogger.java new file mode 100644 index 0000000000..7e4e6a2ca9 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/EmailLogger.java @@ -0,0 +1,32 @@ +package com.coderising.dp.chain; + +public class EmailLogger extends Logger{ + + + public EmailLogger(int type) { + this.type = type; + // TODO Auto-generated constructor stub + } + @Override + public Logger setNext(Logger logger) { + + this.nextLogger = logger; + // TODO Auto-generated method stub + return this; + } + + @Override + public void message(String message, int type) { + // TODO Auto-generated method stub + setMessage(message); + if (!hasType(type)) { + nextLogger.message(message, type); + } + } + + + + private void setMessage(String message) { + System.out.println(message + " type:" + type); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/FileLogger.java b/students/313001956/src/main/java/com/coderising/dp/chain/FileLogger.java new file mode 100644 index 0000000000..4d39c9ca35 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/FileLogger.java @@ -0,0 +1,28 @@ +package com.coderising.dp.chain; + +public class FileLogger extends Logger { + public FileLogger(int type) { + this.type = type; + // TODO Auto-generated constructor stub + } + + @Override + public Logger setNext(Logger logger) { + this.nextLogger = logger; + // TODO Auto-generated method stub + return this; + } + + @Override + public void message(String message, int type) { + // TODO Auto-generated method stub + setMessage(message); + if (!hasType(type)) { + nextLogger.message(message, type); + } + } + + private void setMessage(String message) { + System.out.println(message + " type:" + type); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/Logger.java b/students/313001956/src/main/java/com/coderising/dp/chain/Logger.java new file mode 100644 index 0000000000..8871d0e985 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/Logger.java @@ -0,0 +1,20 @@ +package com.coderising.dp.chain; + +public abstract class Logger { + protected Logger nextLogger; + protected int type; + + + public static final int DEBUG = 1; + public static final int NOTICE = 2; + public static final int ERR = 3; + + public abstract Logger setNext(Logger logger); + + public abstract void message(String message, int type); + + protected boolean hasType(int type) { + // TODO Auto-generated method stub + return this.type == type; + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/StdoutLogger.java b/students/313001956/src/main/java/com/coderising/dp/chain/StdoutLogger.java new file mode 100644 index 0000000000..e6c76cbeb3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/StdoutLogger.java @@ -0,0 +1,31 @@ +package com.coderising.dp.chain; + +public class StdoutLogger extends Logger { + + public StdoutLogger(int type) { + this.type = type; + // TODO Auto-generated constructor stub + } + + @Override + public Logger setNext(Logger logger) { + this.nextLogger = logger; + // TODO Auto-generated method stub + return this; + } + + @Override + public void message(String message, int type) { + // TODO Auto-generated method stub + setMessage(message); + if (!hasType(type)) { + nextLogger.message(message, type); + } + } + + + + private void setMessage(String message) { + System.out.println(message + " type:" + type); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/chain/chaintest.java b/students/313001956/src/main/java/com/coderising/dp/chain/chaintest.java new file mode 100644 index 0000000000..d80f31842e --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/chain/chaintest.java @@ -0,0 +1,19 @@ +package com.coderising.dp.chain; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class chaintest { + + @Test + public void test() { + //fail("Not yet implemented"); + Logger logger = new StdoutLogger(Logger.DEBUG) + .setNext(new EmailLogger(Logger.NOTICE).setNext(new FileLogger(Logger.ERR))); + logger.message("?????????", Logger.DEBUG); + logger.message("???????????", Logger.NOTICE); + logger.message("????????????????", Logger.ERR); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/Command.java b/students/313001956/src/main/java/com/coderising/dp/command/Command.java new file mode 100644 index 0000000000..f4b1b13d10 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/Command.java @@ -0,0 +1,6 @@ +package com.coderising.dp.command; + +public interface Command { + + void run(); +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/CommandTest.java b/students/313001956/src/main/java/com/coderising/dp/command/CommandTest.java new file mode 100644 index 0000000000..0f6295c9a7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/CommandTest.java @@ -0,0 +1,13 @@ +package com.coderising.dp.command; + +public class CommandTest { + public static void main(String[] args) { + Cook cook = new Cook(); + Waiter waiter = new Waiter(); + Command command1=new OrderSteakCommand(cook); + Command command2=new OrderPeaKCommand(cook); + waiter.addOrder(command1); + waiter.addOrder(command2); + waiter.sendOrder(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/Cook.java b/students/313001956/src/main/java/com/coderising/dp/command/Cook.java new file mode 100644 index 0000000000..4c56245b25 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/Cook.java @@ -0,0 +1,12 @@ +package com.coderising.dp.command; + +public class Cook { + + void cookSteak() { + System.out.println("steak is ok"); + } + + void cookPeak() { + System.out.println("peak is ok"); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/OrderPeaKCommand.java b/students/313001956/src/main/java/com/coderising/dp/command/OrderPeaKCommand.java new file mode 100644 index 0000000000..b823ba0a24 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/OrderPeaKCommand.java @@ -0,0 +1,17 @@ +package com.coderising.dp.command; + +public class OrderPeaKCommand implements Command { + + private Cook cook; + + public OrderPeaKCommand(Cook cook) { + // TODO Auto-generated constructor stub + this.cook = cook; + } + + @Override + public void run() { + // TODO Auto-generated method stub + cook.cookPeak(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/OrderSteakCommand.java b/students/313001956/src/main/java/com/coderising/dp/command/OrderSteakCommand.java new file mode 100644 index 0000000000..d5351e05a4 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/OrderSteakCommand.java @@ -0,0 +1,17 @@ +package com.coderising.dp.command; + +public class OrderSteakCommand implements Command { + + private Cook cook; + + public OrderSteakCommand(Cook cook) { + // TODO Auto-generated constructor stub + this.cook = cook; + } + + @Override + public void run() { + // TODO Auto-generated method stub + cook.cookSteak(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/command/Waiter.java b/students/313001956/src/main/java/com/coderising/dp/command/Waiter.java new file mode 100644 index 0000000000..0397ff71c0 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/command/Waiter.java @@ -0,0 +1,19 @@ +package com.coderising.dp.command; + +import java.util.ArrayList; +import java.util.List; + +public class Waiter { + + List list = new ArrayList<>(); + + public void addOrder(Command command) { + list.add(command); + } + + public void sendOrder() { + for (Command command : list) { + command.run(); + } + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Line.java b/students/313001956/src/main/java/com/coderising/dp/composite/Line.java new file mode 100644 index 0000000000..09fa13dda8 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Line.java @@ -0,0 +1,11 @@ +package com.coderising.dp.composite; + +public class Line implements Shape { + + @Override + public void draw() { + + System.out.println("Line"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Picture.java b/students/313001956/src/main/java/com/coderising/dp/composite/Picture.java new file mode 100644 index 0000000000..342452ac9f --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Picture.java @@ -0,0 +1,23 @@ +package com.coderising.dp.composite; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.validator.PublicClassValidator; + +public class Picture implements Shape { + List list = new ArrayList<>(); + + public void addList(Shape shape){ + list.add(shape); + } + + @Override + public void draw() { + System.out.println("Picture:"); + for (Shape shape : list) { + shape.draw(); + } + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Rectangle.java b/students/313001956/src/main/java/com/coderising/dp/composite/Rectangle.java new file mode 100644 index 0000000000..2f4fe47ee0 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Rectangle.java @@ -0,0 +1,11 @@ +package com.coderising.dp.composite; + +public class Rectangle implements Shape { + + @Override + public void draw() { + // TODO Auto-generated method stub + System.out.println("Rectangle"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Shape.java b/students/313001956/src/main/java/com/coderising/dp/composite/Shape.java new file mode 100644 index 0000000000..4562f10b12 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Shape.java @@ -0,0 +1,5 @@ +package com.coderising.dp.composite; + +public interface Shape { + public void draw(); +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Square.java b/students/313001956/src/main/java/com/coderising/dp/composite/Square.java new file mode 100644 index 0000000000..df9c8d9c3a --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Square.java @@ -0,0 +1,11 @@ +package com.coderising.dp.composite; + +public class Square implements Shape { + + @Override + public void draw() { + // TODO Auto-generated method stub + System.out.println("Square"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/Text.java b/students/313001956/src/main/java/com/coderising/dp/composite/Text.java new file mode 100644 index 0000000000..6fa91e9608 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/Text.java @@ -0,0 +1,11 @@ +package com.coderising.dp.composite; + +public class Text implements Shape { + + @Override + public void draw() { + // TODO Auto-generated method stub + System.out.println("Text"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/composite/testComposite.java b/students/313001956/src/main/java/com/coderising/dp/composite/testComposite.java new file mode 100644 index 0000000000..ac8ae44d24 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/composite/testComposite.java @@ -0,0 +1,15 @@ +package com.coderising.dp.composite; + +public class testComposite { + public static void main(String[] args) { + Picture root = new Picture(); + Picture node = new Picture(); + node.addList(new Text()); + node.addList(new Line()); + node.addList(new Square()); + root.addList(node); + root.addList(new Line()); + root.addList(new Rectangle()); + root.draw(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/DecoratorTest.java b/students/313001956/src/main/java/com/coderising/dp/decorator/DecoratorTest.java new file mode 100644 index 0000000000..8107e042fc --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/DecoratorTest.java @@ -0,0 +1,11 @@ +package com.coderising.dp.decorator; + +public class DecoratorTest { + public static void main(String[] args) { + Email e1 = new EmailEcript(new EmailDeclare(new EmailImpl("?????????????"))); + System.out.println(e1.getContent()); + + e1 = new EmailDeclare (new EmailEcript(new EmailImpl("?????????????"))); + System.out.println(e1.getContent()); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/Email.java b/students/313001956/src/main/java/com/coderising/dp/decorator/Email.java new file mode 100644 index 0000000000..064de1e837 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/Email.java @@ -0,0 +1,6 @@ +package com.coderising.dp.decorator; + +public interface Email { + public String getContent(); +} + diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDeclare.java b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDeclare.java new file mode 100644 index 0000000000..eef468dc0c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDeclare.java @@ -0,0 +1,19 @@ +package com.coderising.dp.decorator; + +import org.junit.experimental.theories.Theories; + +public class EmailDeclare extends EmailDecorator { + + + public EmailDeclare(Email email) { + super(email); + } + + @Override + public String getContent() { + return email.getContent() + +"\r\n" + +"???????????????????????????"; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDecorator.java b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDecorator.java new file mode 100644 index 0000000000..0c6665c8e9 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailDecorator.java @@ -0,0 +1,13 @@ +package com.coderising.dp.decorator; + +public abstract class EmailDecorator implements Email { + protected volatile Email email; + + public EmailDecorator(Email email) { + this.email = email; + } + + public String getContent() { + return email.getContent(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/EmailEcript.java b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailEcript.java new file mode 100644 index 0000000000..a6e91169c8 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailEcript.java @@ -0,0 +1,14 @@ +package com.coderising.dp.decorator; + +public class EmailEcript extends EmailDecorator { + + public EmailEcript(Email email) { + super(email); + } + + @Override + public String getContent() { + return "**" + email.getContent() + "**"; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/dp/decorator/EmailImpl.java b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailImpl.java new file mode 100644 index 0000000000..640aef6da3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/dp/decorator/EmailImpl.java @@ -0,0 +1,12 @@ +package com.coderising.dp.decorator; + +public class EmailImpl implements Email { + private String content; + + public EmailImpl(String content) { + this.content = content; + } + public String getContent() { + return content; + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/DateUtil.java b/students/313001956/src/main/java/com/coderising/ood/ocp/DateUtil.java new file mode 100644 index 0000000000..d35f8caa5c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/DateUtil.java @@ -0,0 +1,13 @@ +package com.coderising.ood.ocp; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DateUtil { + + public static String getCurrentDateAsString() { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return dateFormat.format(new Date()); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/EmailLogWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/EmailLogWay.java new file mode 100644 index 0000000000..a16ba7f283 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/EmailLogWay.java @@ -0,0 +1,7 @@ +package com.coderising.ood.ocp; + +public class EmailLogWay implements ILogWay { + public void excutelog(String logMsg) { + MailUtil.send(logMsg); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/EmailMessageWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/EmailMessageWay.java new file mode 100644 index 0000000000..28d846068f --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/EmailMessageWay.java @@ -0,0 +1,8 @@ +package com.coderising.ood.ocp; + +public class EmailMessageWay implements IMessageWay { + public String getmessage(String msg) { + String txtDate = DateUtil.getCurrentDateAsString(); + return txtDate + ": " + msg; + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/ILogWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/ILogWay.java new file mode 100644 index 0000000000..b90da40f6c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/ILogWay.java @@ -0,0 +1,6 @@ +package com.coderising.ood.ocp; + +public interface ILogWay { + + public void excutelog(String logMsg); +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/IMessageWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/IMessageWay.java new file mode 100644 index 0000000000..42a232eaa7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/IMessageWay.java @@ -0,0 +1,6 @@ +package com.coderising.ood.ocp; + +public interface IMessageWay { + + public String getmessage(String msg); +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/Logger.java b/students/313001956/src/main/java/com/coderising/ood/ocp/Logger.java new file mode 100644 index 0000000000..2680f6fac7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/Logger.java @@ -0,0 +1,17 @@ +package com.coderising.ood.ocp; + +public class Logger { + + private IMessageWay messageWay; + private ILogWay logWay; + + public Logger(IMessageWay messageWay, ILogWay logWay) { + this.messageWay = messageWay; + this.logWay = logWay; + } + + public void log(String msg) { + + logWay.excutelog(messageWay.getmessage(msg)); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/LoggerTest.java b/students/313001956/src/main/java/com/coderising/ood/ocp/LoggerTest.java new file mode 100644 index 0000000000..6a1cdd3b0f --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/LoggerTest.java @@ -0,0 +1,21 @@ +package com.coderising.ood.ocp; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class LoggerTest { + + @Test + public void testLog() { + Logger logger =new Logger(new EmailMessageWay(), new EmailLogWay()); + logger.log("hellow world"); + + logger =new Logger(new SMSMessageWay(), new SMSLogWay()); + logger.log("hellow world"); + + logger =new Logger(new PrintMessageWay(), new PrintLogWay()); + logger.log("hellow world"); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/MailUtil.java b/students/313001956/src/main/java/com/coderising/ood/ocp/MailUtil.java new file mode 100644 index 0000000000..eb891d72c2 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/MailUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class MailUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + System.out.println("Email:" + logMsg); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/PrintLogWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/PrintLogWay.java new file mode 100644 index 0000000000..ff8b180938 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/PrintLogWay.java @@ -0,0 +1,9 @@ +package com.coderising.ood.ocp; + +public class PrintLogWay implements ILogWay { + @Override + public void excutelog(String logMsg) { + // TODO Auto-generated method stub + System.out.println("Print:" + logMsg); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/PrintMessageWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/PrintMessageWay.java new file mode 100644 index 0000000000..678d9c2027 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/PrintMessageWay.java @@ -0,0 +1,9 @@ +package com.coderising.ood.ocp; + +public class PrintMessageWay implements IMessageWay { + @Override + public String getmessage(String msg) { + // TODO Auto-generated method stub + return msg; + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/SMSLogWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSLogWay.java new file mode 100644 index 0000000000..33db6557a7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSLogWay.java @@ -0,0 +1,9 @@ +package com.coderising.ood.ocp; + +public class SMSLogWay implements ILogWay { + @Override + public void excutelog(String logMsg) { + // TODO Auto-generated method stub + SMSUtil.send(logMsg); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/SMSMessageWay.java b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSMessageWay.java new file mode 100644 index 0000000000..b6975ed6a2 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSMessageWay.java @@ -0,0 +1,8 @@ +package com.coderising.ood.ocp; + +public class SMSMessageWay implements IMessageWay{ + @Override +public String getmessage(String msg) { + return msg; +} +} diff --git a/students/313001956/src/main/java/com/coderising/ood/ocp/SMSUtil.java b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSUtil.java new file mode 100644 index 0000000000..16f5e99c77 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/ocp/SMSUtil.java @@ -0,0 +1,10 @@ +package com.coderising.ood.ocp; + +public class SMSUtil { + + public static void send(String logMsg) { + // TODO Auto-generated method stub + System.out.println("SMS:" + logMsg); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/Configuration.java b/students/313001956/src/main/java/com/coderising/ood/srp/Configuration.java new file mode 100644 index 0000000000..e060caa01b --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/Configuration.java @@ -0,0 +1,25 @@ +package com.coderising.ood.srp; +import java.util.HashMap; +import java.util.Map; + +public class Configuration { + public static final String SMTP_SERVER = "smtp.server"; + public static final String ALT_SMTP_SERVER = "alt.smtp.server"; + public static final String EMAIL_ADMIN = "email.admin"; + + static Map configurations = new HashMap<>(); + static{ + configurations.put(SMTP_SERVER, "smtp.163.com"); + configurations.put(ALT_SMTP_SERVER, "smtp1.163.com"); + configurations.put(EMAIL_ADMIN, "admin@company.com"); + } + /** + * ??????????????? ?????????????????map ????? + * @param key + * @return + */ + public String getProperty(String key) { + + return configurations.get(key); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/313001956/src/main/java/com/coderising/ood/srp/DBUtil.java new file mode 100644 index 0000000000..c3b6f7aadd --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -0,0 +1,28 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class DBUtil { + + /** + * ???????????? ????????????? + * + * @param sql + * @return + */ + public static List query(String sql, Product product) { + + List userList = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + User user = new User(); + user.setName("User" + i); + user.setEmail("aa@bb.com"); + user.addProducts(product); + userList.add(user); + } + return userList; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/Mail.java b/students/313001956/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..470470ef7c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class Mail { + private User user; + private String subject; + private String message; + + public Mail(User user) { + this.user = user; + } + + public User getUser() { + return user; + } + + public String getMessage() { + return "??? " + user.getName() + ", ????????? " + buildDesc() + " ??????????????!"; + } + + public String getSubject() { + return "???????????????"; + } + + private String buildDesc() { + List products=user.getProducts(); + StringBuffer sb = new StringBuffer(); + for (Product p : products) { + sb.append(p.getProductDesc()); + } + return sb.toString(); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/MailSender.java b/students/313001956/src/main/java/com/coderising/ood/srp/MailSender.java new file mode 100644 index 0000000000..cfe2dba0fe --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/MailSender.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +public class MailSender { + + Configuration config; + + public MailSender(Configuration config) { + this.config = config; + } + + public void sendEmails(Mail mail) { + System.out.println("??????????"); + try { + sendEmail(mail, config.getProperty(Configuration.SMTP_SERVER)); + } catch (Exception e) { + try { + sendEmail(mail, config.getProperty(Configuration.ALT_SMTP_SERVER)); + + } catch (Exception e2) { + System.out.println("??????? SMTP????????????????: " + e2.getMessage()); + } + } + } + + public void sendEmail(Mail mail, String smtpHost) { + + // ????????????? + StringBuilder buffer = new StringBuilder(); + buffer.append("From:").append(config.getProperty(Configuration.EMAIL_ADMIN)).append("\n"); + buffer.append("To:").append(mail.getUser().getEmail()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + System.out.println(buffer.toString()); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/Product.java b/students/313001956/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..5137434bd9 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,22 @@ +package com.coderising.ood.srp; + +public class Product { + private String productID ; + private String productDesc; + + public String getProductDesc() { + return productDesc; + } + + public String getProductID() { + return productID; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } + + public void setProductID(String productID) { + this.productID = productID; + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/ProductServer.java b/students/313001956/src/main/java/com/coderising/ood/srp/ProductServer.java new file mode 100644 index 0000000000..6245a1621d --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/ProductServer.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class ProductServer { + private static final String FILE_NAME = "D:\\Java2017\\????\\ood-assignment\\src\\main\\java\\com\\coderising\\ood\\srp\\product_promotion.txt"; + + // ???????????? ???????????????????? ???? P8756 iPhone8 + public List getProductList() throws IOException // @02C + { + File file = new File(FILE_NAME); + BufferedReader br = null; + List prolist = new ArrayList<>(); + try { + br = new BufferedReader(new FileReader(file)); + String temp; + while ((temp = br.readLine()) != null) { + String[] arrStr = temp.split(" "); + Product p = new Product(); + p.setProductID(arrStr[0]); + p.setProductDesc(arrStr[1]); + prolist.add(p); + } + return prolist; + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJob.java b/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJob.java new file mode 100644 index 0000000000..fbbbd125d7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJob.java @@ -0,0 +1,36 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +public class PromotionJob { + private ProductServer productServer; + private UserServer userServer; + + public PromotionJob(ProductServer productServer, UserServer userServer) { + this.productServer = productServer; + this.userServer = userServer; + } + + public void run() throws IOException { + List productList = productServer.getProductList(); + Product product = productList.get(0); + System.out.println("???ID = " + product.getProductID() + "\n"); + System.out.println("??????? = " + product.getProductDesc() + "\n"); + + List users = userServer.getUserByProduct(product); + + MailSender mailSender = new MailSender(new Configuration()); + for (User user : users) { + mailSender.sendEmails(new Mail(user)); + } + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJobTest.java b/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJobTest.java new file mode 100644 index 0000000000..2f59d383a7 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/PromotionJobTest.java @@ -0,0 +1,29 @@ +package com.coderising.ood.srp; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.junit.Test; + +public class PromotionJobTest { + + @Test + public void testRun() throws IOException { + + Integer i1=4; + Integer i2=4; + Integer i3=400; + Integer i4=400; + + boolean b1=(i1==i2); + boolean b2=(i3==i4); + + System.out.println(b1); + System.out.println(b2); + + PromotionJob pJob = new PromotionJob(new ProductServer(), new UserServer()); + pJob.run(); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/User.java b/students/313001956/src/main/java/com/coderising/ood/srp/User.java new file mode 100644 index 0000000000..19ce2d0d1d --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/User.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +import java.util.ArrayList; +import java.util.List; + +public class User { + private String name; + private String email; + private List products = new ArrayList<>(); + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public List getProducts() { + return products; + } + + public void setName(String name) { + this.name = name; + } + + public void setEmail(String email) { + this.email = email; + } + + public void addProducts(Product product) { + this.products.add(product); + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/UserServer.java b/students/313001956/src/main/java/com/coderising/ood/srp/UserServer.java new file mode 100644 index 0000000000..ebd8ac5c37 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/UserServer.java @@ -0,0 +1,13 @@ +package com.coderising.ood.srp; + +import java.util.List; + +public class UserServer { + public List getUserByProduct(Product product) { + String sendMailQuery = "Select name from subscriptions " + "where product_id= '" + product.getProductID() + "' " + + "and send_mail=1 "; + System.out.println("load Query set"); + List userList = DBUtil.query(sendMailQuery, product); + return userList; + } +} diff --git a/students/313001956/src/main/java/com/coderising/ood/srp/product_promotion.txt b/students/313001956/src/main/java/com/coderising/ood/srp/product_promotion.txt new file mode 100644 index 0000000000..b7a974adb3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/ood/srp/product_promotion.txt @@ -0,0 +1,4 @@ +P8756 iPhone8 +P3946 XiaoMi10 +P8904 Oppo_R15 +P4955 Vivo_X20 \ No newline at end of file diff --git a/students/313001956/src/main/java/com/coderising/payroll/PayrollService.java b/students/313001956/src/main/java/com/coderising/payroll/PayrollService.java new file mode 100644 index 0000000000..d475fb41e3 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/PayrollService.java @@ -0,0 +1,69 @@ +package com.coderising.payroll; + +import java.util.ArrayList; +import java.util.List; + +import com.coderising.payroll.classification.CommissionedClassification; +import com.coderising.payroll.classification.HourlyClassification; +import com.coderising.payroll.classification.SalariedClassification; +import com.coderising.payroll.domain.Employee; +import com.coderising.payroll.domain.HoldMethod; +import com.coderising.payroll.domain.Paycheck; +import com.coderising.payroll.schedule.BiweeklySchedule; +import com.coderising.payroll.schedule.MonthlySchedule; +import com.coderising.payroll.schedule.WeeklySchedule; +import com.coderising.payroll.transaction.AddEmployeeTransaction; +import com.coderising.payroll.transaction.AddHourlyEmployeeTransaction; + +public class PayrollService { + + + public List getAllEmployees() { + List list = new ArrayList<>(); + list.add(addHourlyEmployee("????", "???", 0.6)); + list.add(addSalariedEmployee("????", "???", 1000, 0.6)); + list.add(addCommissionedEmployee("????", "???", 1500)); + + return list; + } + + + + public void savePaycheck(Paycheck pc) { + //?????????.... + + } + + public Employee addHourlyEmployee(String name, String address, double hourlyRate) { + Employee employee = new Employee(name, address); + employee.setClassification(new HourlyClassification(hourlyRate)); + employee.setPaymentMethod(new HoldMethod()); + employee.setSchedule(new WeeklySchedule()); + return employee; + } + + public Employee addSalariedEmployee(String name, String address, double salary, double saleRate) { + Employee employee = new Employee(name, address); + employee.setClassification(new CommissionedClassification(salary, saleRate)); + employee.setPaymentMethod(new HoldMethod()); + employee.setSchedule(new BiweeklySchedule()); + return employee; + } + + public Employee addCommissionedEmployee(String name, String address, double salary) { + Employee employee = new Employee(name, address); + employee.setClassification(new SalariedClassification(salary)); + employee.setPaymentMethod(new HoldMethod()); + employee.setSchedule(new MonthlySchedule()); + return employee; + } + + List list; + public void addEmployee(AddEmployeeTransaction transaction) { + list.add(transaction); + } + + public static void main(String[] args) { + new PayrollService().addEmployee(new AddHourlyEmployeeTransaction("", "", 0.1)); + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/affiliation/NonAffiliation.java b/students/313001956/src/main/java/com/coderising/payroll/affiliation/NonAffiliation.java new file mode 100644 index 0000000000..3cb6228aa4 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/affiliation/NonAffiliation.java @@ -0,0 +1,10 @@ +package com.coderising.payroll.affiliation; + +import com.coderising.payroll.domain.Affiliation; +import com.coderising.payroll.domain.Paycheck; + +public class NonAffiliation implements Affiliation{ + public double calculateDeductions(Paycheck pc){ + return 0.0; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/affiliation/UnionAffiliation.java b/students/313001956/src/main/java/com/coderising/payroll/affiliation/UnionAffiliation.java new file mode 100644 index 0000000000..0938e86803 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/affiliation/UnionAffiliation.java @@ -0,0 +1,38 @@ +package com.coderising.payroll.affiliation; + +import java.util.Date; +import java.util.Map; + +import com.coderising.payroll.domain.Affiliation; +import com.coderising.payroll.domain.Paycheck; +import com.coderising.payroll.domain.ServiceCharge; +import com.coderising.payroll.util.DateUtil; + +public class UnionAffiliation implements Affiliation { + + private String memberId; + private double weeklyDue; + + public UnionAffiliation(String memberId, double weeklyDue) { + this.memberId = memberId; + this.weeklyDue = weeklyDue; + } + + Map charges; + + @Override + public double calculateDeductions(Paycheck pc) { + int fridays = DateUtil.getFridayCountBetween(pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate()); + double totalDue = fridays * weeklyDue; + double totalCharge = 0; + for (ServiceCharge charge : charges.values()) { + if (DateUtil.between(charge.getDate(), pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) { + totalCharge += charge.getAmount(); + } + } + double deduction = totalDue + totalCharge; + + return deduction; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/classification/CommissionedClassification.java b/students/313001956/src/main/java/com/coderising/payroll/classification/CommissionedClassification.java new file mode 100644 index 0000000000..a574001137 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/classification/CommissionedClassification.java @@ -0,0 +1,38 @@ +package com.coderising.payroll.classification; + +import java.util.Date; +import java.util.Map; + +import com.coderising.payroll.domain.Paycheck; +import com.coderising.payroll.domain.PaymentClassification; +import com.coderising.payroll.domain.SalesReceipt; +import com.coderising.payroll.util.DateUtil; + +public class CommissionedClassification implements PaymentClassification { + double salary; + double rate; + + public CommissionedClassification(double salary, double rate) { + this.salary = salary; + this.rate = rate; + } + + Map receipts; + + public void AddSalesReceipt(SalesReceipt receipt){ + receipts.put(receipt.getSaleDate(), receipt); + } + + @Override + public double calculatePay(Paycheck pc) { + int count = 0; + for (SalesReceipt receipt : receipts.values()) { + if (DateUtil.between(receipt.getSaleDate(), pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) { + count += receipt.getAmount(); + } + } + + return salary + count* rate; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/classification/HourlyClassification.java b/students/313001956/src/main/java/com/coderising/payroll/classification/HourlyClassification.java new file mode 100644 index 0000000000..3588cdbef5 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/classification/HourlyClassification.java @@ -0,0 +1,49 @@ +package com.coderising.payroll.classification; + +import java.util.Date; +import java.util.Map; + +import javax.swing.plaf.PanelUI; + +import com.coderising.payroll.domain.Paycheck; +import com.coderising.payroll.domain.PaymentClassification; +import com.coderising.payroll.domain.SalesReceipt; +import com.coderising.payroll.domain.TimeCard; +import com.coderising.payroll.util.DateUtil; + +public class HourlyClassification implements PaymentClassification { + private double rate; + private Map timeCards; + + public HourlyClassification(double hourlyRate) { + this.rate = hourlyRate; + } + + public void addTimeCard(TimeCard tc) { + timeCards.put(tc.getDate(), tc); + } + + @Override + public double calculatePay(Paycheck pc) { + double totalPay=0; + for (TimeCard tc : timeCards.values()) { + if (DateUtil.between(tc.getDate(), pc.getPayPeriodStartDate(), pc.getPayPeriodEndDate())) { + totalPay += caculatePayforTimeCard(tc.getHours()) ; + } + } + + return totalPay; + } + + private double caculatePayforTimeCard(int hours) { + double pay = 0; + if (hours > 8) { + pay = rate * 8 + rate * 1.5 * (hours - 8); + } else { + pay = rate * hours; + } + + return pay; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/classification/SalariedClassification.java b/students/313001956/src/main/java/com/coderising/payroll/classification/SalariedClassification.java new file mode 100644 index 0000000000..796aae93f1 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/classification/SalariedClassification.java @@ -0,0 +1,16 @@ +package com.coderising.payroll.classification; + +import com.coderising.payroll.domain.Paycheck; +import com.coderising.payroll.domain.PaymentClassification; + +public class SalariedClassification implements PaymentClassification { + private double salary; + public SalariedClassification(double salary){ + this.salary = salary; + } + @Override + public double calculatePay(Paycheck pc) { + return salary; + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/Affiliation.java b/students/313001956/src/main/java/com/coderising/payroll/domain/Affiliation.java new file mode 100644 index 0000000000..74a6b404bc --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/Affiliation.java @@ -0,0 +1,5 @@ +package com.coderising.payroll.domain; + +public interface Affiliation { + public double calculateDeductions(Paycheck pc); +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/Employee.java b/students/313001956/src/main/java/com/coderising/payroll/domain/Employee.java new file mode 100644 index 0000000000..3ef8fe284a --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/Employee.java @@ -0,0 +1,49 @@ +package com.coderising.payroll.domain; + +import java.util.Date; + +public class Employee { + private String id; + private String name; + private String address; + private Affiliation affiliation; + + private PaymentClassification classification; + private PaymentSchedule schedule; + private PaymentMethod paymentMethod; + + public Employee(String name, String address) { + this.name = name; + this.address = address; + } + + public boolean isPayDay(Date d) { + return this.schedule.isPayDate(d); + } + + public Date getPayPeriodStartDate(Date d) { + return this.schedule.getPayPeriodStartDate(d); + } + + public void payDay(Paycheck pc) { + double grosspay = classification.calculatePay(pc); + pc.setGrossPay(grosspay); + double deductions = affiliation.calculateDeductions(pc); + pc.setDeductions(deductions); + pc.setNetPay(grosspay - deductions); + paymentMethod.pay(pc); + + } + + public void setClassification(PaymentClassification classification) { + this.classification = classification; + } + + public void setSchedule(PaymentSchedule schedule) { + this.schedule = schedule; + } + + public void setPaymentMethod(PaymentMethod paymentMethod) { + this.paymentMethod = paymentMethod; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/HoldMethod.java b/students/313001956/src/main/java/com/coderising/payroll/domain/HoldMethod.java new file mode 100644 index 0000000000..0ce19e2291 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/HoldMethod.java @@ -0,0 +1,11 @@ +package com.coderising.payroll.domain; + +public class HoldMethod implements PaymentMethod { + + @Override + public void pay(Paycheck pc) { + + + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/Paycheck.java b/students/313001956/src/main/java/com/coderising/payroll/domain/Paycheck.java new file mode 100644 index 0000000000..6f1ff99413 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/Paycheck.java @@ -0,0 +1,35 @@ +package com.coderising.payroll.domain; + +import java.util.Date; +import java.util.Map; + +public class Paycheck { + private Date payPeriodStart; + private Date payPeriodEnd; + private double grossPay; + private double netPay; + private double deductions; + private Map itsFields; + public Paycheck(Date payPeriodStart, Date payPeriodEnd){ + this.payPeriodStart = payPeriodStart; + this.payPeriodEnd = payPeriodEnd; + } + public void setGrossPay(double grossPay) { + this.grossPay = grossPay; + + } + public void setDeductions(double deductions) { + this.deductions = deductions; + } + public void setNetPay(double netPay){ + this.netPay = netPay; + } + public Date getPayPeriodEndDate() { + + return this.payPeriodEnd; + } + public Date getPayPeriodStartDate() { + + return this.payPeriodStart; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/PaydayTransaction.java b/students/313001956/src/main/java/com/coderising/payroll/domain/PaydayTransaction.java new file mode 100644 index 0000000000..ddd6fc817e --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/PaydayTransaction.java @@ -0,0 +1,23 @@ +package com.coderising.payroll.domain; + +import java.util.Date; +import java.util.List; + +import com.coderising.payroll.PayrollService; + +public class PaydayTransaction { + private Date date; + private PayrollService payrollService; + + public void execute() { + List employees = payrollService.getAllEmployees(); + for (Employee e : employees) { + if (e.isPayDay(date)) { + Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date), date); + e.payDay(pc); + payrollService.savePaycheck(pc); + } + + } + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentClassification.java b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentClassification.java new file mode 100644 index 0000000000..b6f2120bdb --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentClassification.java @@ -0,0 +1,5 @@ +package com.coderising.payroll.domain; + +public interface PaymentClassification { + public double calculatePay(Paycheck pc); +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentMethod.java b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentMethod.java new file mode 100644 index 0000000000..f07cc5354b --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentMethod.java @@ -0,0 +1,5 @@ +package com.coderising.payroll.domain; + +public interface PaymentMethod { + public void pay(Paycheck pc); +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentSchedule.java b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentSchedule.java new file mode 100644 index 0000000000..96788f4f80 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/PaymentSchedule.java @@ -0,0 +1,8 @@ +package com.coderising.payroll.domain; + +import java.util.Date; + +public interface PaymentSchedule { + public boolean isPayDate(Date date); + public Date getPayPeriodStartDate( Date payPeriodEndDate); +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/SalesReceipt.java b/students/313001956/src/main/java/com/coderising/payroll/domain/SalesReceipt.java new file mode 100644 index 0000000000..a7b0ba41ad --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/SalesReceipt.java @@ -0,0 +1,14 @@ +package com.coderising.payroll.domain; + +import java.util.Date; + +public class SalesReceipt { + private Date saleDate; + private double amount; + public Date getSaleDate() { + return saleDate; + } + public double getAmount() { + return amount; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/ServiceCharge.java b/students/313001956/src/main/java/com/coderising/payroll/domain/ServiceCharge.java new file mode 100644 index 0000000000..88cf1d56dc --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/ServiceCharge.java @@ -0,0 +1,16 @@ +package com.coderising.payroll.domain; + +import java.util.Date; + +public class ServiceCharge { + private Date date; + private int amount; + + public int getAmount() { + return amount; + } + + public Date getDate() { + return date; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/domain/TimeCard.java b/students/313001956/src/main/java/com/coderising/payroll/domain/TimeCard.java new file mode 100644 index 0000000000..ebf6e17a4c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/domain/TimeCard.java @@ -0,0 +1,15 @@ +package com.coderising.payroll.domain; + +import java.util.Date; + +public class TimeCard { + private Date date; + private int hours; + + public Date getDate() { + return date; + } + public int getHours() { + return hours; + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/schedule/BiweeklySchedule.java b/students/313001956/src/main/java/com/coderising/payroll/schedule/BiweeklySchedule.java new file mode 100644 index 0000000000..1ad42c1d19 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/schedule/BiweeklySchedule.java @@ -0,0 +1,38 @@ +package com.coderising.payroll.schedule; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import com.coderising.payroll.domain.PaymentSchedule; +import com.coderising.payroll.util.DateUtil; + +public class BiweeklySchedule implements PaymentSchedule { + Date firstPayableFriday = DateUtil.parseDate("2017-6-2"); + + @Override + public boolean isPayDate(Date date) { + long daysBetween = DateUtil.getDaysBetween(firstPayableFriday, date); + + return daysBetween % 14 == 0; + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -13); + } + + public static void main(String[] args) throws Exception { + BiweeklySchedule schedule = new BiweeklySchedule(); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Date d = sdf.parse("2017-07-21"); + + System.out.println(schedule.isPayDate(d)); + + System.out.println(DateUtil.isFriday(d)); + + System.out.println(schedule.getPayPeriodStartDate(d)); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/schedule/MonthlySchedule.java b/students/313001956/src/main/java/com/coderising/payroll/schedule/MonthlySchedule.java new file mode 100644 index 0000000000..0f85b15a83 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/schedule/MonthlySchedule.java @@ -0,0 +1,22 @@ +package com.coderising.payroll.schedule; + +import java.util.Calendar; +import java.util.Date; + +import com.coderising.payroll.domain.PaymentSchedule; +import com.coderising.payroll.util.DateUtil; + +public class MonthlySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + return DateUtil.isLastDayOfMonth(date); + + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.getFirstDay(payPeriodEndDate); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/schedule/WeeklySchedule.java b/students/313001956/src/main/java/com/coderising/payroll/schedule/WeeklySchedule.java new file mode 100644 index 0000000000..54a22ab7db --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/schedule/WeeklySchedule.java @@ -0,0 +1,19 @@ +package com.coderising.payroll.schedule; + +import java.util.Date; + +import com.coderising.payroll.domain.PaymentSchedule; +import com.coderising.payroll.util.DateUtil; + +public class WeeklySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + return DateUtil.isFriday(date); + } + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -6); + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/transaction/AddEmployeeTransaction.java b/students/313001956/src/main/java/com/coderising/payroll/transaction/AddEmployeeTransaction.java new file mode 100644 index 0000000000..f1727e1fd5 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/transaction/AddEmployeeTransaction.java @@ -0,0 +1,26 @@ +package com.coderising.payroll.transaction; + +import com.coderising.payroll.domain.Employee; +import com.coderising.payroll.domain.HoldMethod; +import com.coderising.payroll.domain.PaymentClassification; +import com.coderising.payroll.domain.PaymentMethod; +import com.coderising.payroll.domain.PaymentSchedule; + +public abstract class AddEmployeeTransaction { + private String name; + private String address; + public AddEmployeeTransaction(String name,String address){ + this.name = name; + this.address = address; + } + public abstract PaymentSchedule getPaymentSchedule(); + public abstract PaymentClassification getPaymentClassification(); + + public void execute(){ + Employee employee=new Employee(name, address); + employee.setClassification(getPaymentClassification()); + employee.setPaymentMethod(new HoldMethod()); + employee.setSchedule(getPaymentSchedule()); + //????????, ?? + } +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/transaction/AddHourlyEmployeeTransaction.java b/students/313001956/src/main/java/com/coderising/payroll/transaction/AddHourlyEmployeeTransaction.java new file mode 100644 index 0000000000..4db5905352 --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/transaction/AddHourlyEmployeeTransaction.java @@ -0,0 +1,27 @@ +package com.coderising.payroll.transaction; + +import com.coderising.payroll.classification.HourlyClassification; +import com.coderising.payroll.domain.PaymentClassification; +import com.coderising.payroll.domain.PaymentSchedule; +import com.coderising.payroll.schedule.WeeklySchedule; + +public class AddHourlyEmployeeTransaction extends AddEmployeeTransaction { + private double rate; + + public AddHourlyEmployeeTransaction(String name, String address, double hourlyRate) { + super(name, address); + this.rate = hourlyRate; + } + + @Override + public PaymentSchedule getPaymentSchedule() { + return new WeeklySchedule(); + } + + @Override + public PaymentClassification getPaymentClassification() { + return new HourlyClassification(rate); + + } + +} diff --git a/students/313001956/src/main/java/com/coderising/payroll/util/DateUtil.java b/students/313001956/src/main/java/com/coderising/payroll/util/DateUtil.java new file mode 100644 index 0000000000..1a9d28cc5c --- /dev/null +++ b/students/313001956/src/main/java/com/coderising/payroll/util/DateUtil.java @@ -0,0 +1,69 @@ +package com.coderising.payroll.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class DateUtil { + public static long getDaysBetween(Date d1, Date d2) { + + return (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24); + } + + public static Date parseDate(String txtDate) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + try { + return sdf.parse(txtDate); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return null; + } + + } + + public static boolean isFriday(Date d) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + return calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY; + } + + public static Date add(Date d, int days) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.add(Calendar.DAY_OF_MONTH, days); + return calendar.getTime(); + } + + public static boolean isLastDayOfMonth(Date d) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.add(Calendar.MONTH, 1); + calendar.set(Calendar.DAY_OF_MONTH, 0); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(d).equals(sdf.format(calendar.getTime())); + } + + public static Date getFirstDay(Date d) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.set(Calendar.DAY_OF_MONTH, 1); + return calendar.getTime(); + } + + public static void main(String[] args) throws Exception { + System.out.println(DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-5-30"))); + + System.out.println(DateUtil.getFirstDay(DateUtil.parseDate("2017-6-30"))); + System.out.println(DateUtil.isFriday(DateUtil.parseDate("2017-7-28"))); + } + + public static boolean between(Date d, Date date1, Date date2) { + return d.after(date1) && d.before(date2); + } + + public static int getFridayCountBetween(Date date1, Date date2) { + return (int) (getDaysBetween(date1, date2) / 7); + } +} diff --git a/students/313001956/src/main/java/org/v0_my/Assert.java b/students/313001956/src/main/java/org/v0_my/Assert.java new file mode 100644 index 0000000000..c786200504 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/Assert.java @@ -0,0 +1,225 @@ +package org.v0_my; + +/** + * A set of assert methods. + */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} diff --git a/students/313001956/src/main/java/org/v0_my/AssertionFailedError.java b/students/313001956/src/main/java/org/v0_my/AssertionFailedError.java new file mode 100644 index 0000000000..51c2c0559f --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/AssertionFailedError.java @@ -0,0 +1,12 @@ +package org.v0_my; + +public class AssertionFailedError extends Error { +public AssertionFailedError(){ + +} + + public AssertionFailedError(String message) { + // TODO Auto-generated constructor stub + super(message); + } +} diff --git a/students/313001956/src/main/java/org/v0_my/Test.java b/students/313001956/src/main/java/org/v0_my/Test.java new file mode 100644 index 0000000000..e7f7cc5159 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/Test.java @@ -0,0 +1,7 @@ +package org.v0_my; + +public interface Test { + public void run(TestResult testResult); + + public Integer getCaseCount(); +} diff --git a/students/313001956/src/main/java/org/v0_my/TestCase.java b/students/313001956/src/main/java/org/v0_my/TestCase.java new file mode 100644 index 0000000000..ca569bf942 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/TestCase.java @@ -0,0 +1,75 @@ +package org.v0_my; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +public abstract class TestCase extends Assert implements Test { + private String name; + + public TestCase(String name) { + this.name = name; + // TODO Auto-generated constructor stub + } + + @Override + public void run(TestResult testResult) { + testResult.run(this); + } + + public void doRun(TestResult testResult) throws Throwable { + setUp(); + try { + runTest(); + + } finally { + tearDown(); + } + } + + protected void setUp() { + + } + + protected void runTest() throws Throwable { + Class clazz = this.getClass(); + Method method=null; + try { + method = clazz.getDeclaredMethod(name); + + } catch (NoSuchMethodException | SecurityException e1) { + // TODO Auto-generated catch block + // e1.printStackTrace(); + } + + if(!Modifier.isPublic(method.getModifiers())){ + fail("method "+name+" is not a public menthod!"); + } + + try { + method.invoke(this); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // TODO Auto-generated catch block + // e.printStackTrace(); + + Throwable ta = e.getCause(); + // if (ta instanceof AssertionFailedError) { + throw ta; + // } + + } + + } + + protected void tearDown() { + + } + + @Override + public Integer getCaseCount() { + return 1; + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/TestFailure.java b/students/313001956/src/main/java/org/v0_my/TestFailure.java new file mode 100644 index 0000000000..b26b440396 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/TestFailure.java @@ -0,0 +1,21 @@ +package org.v0_my; + +public class TestFailure { + + private Test test; + private Throwable throwable; + + public TestFailure(Test test, Throwable throwable) { + // TODO Auto-generated constructor stub + this.test = test; + this.throwable = throwable; + } + + public Test getTestCase() { + return this.test; + } + + public Throwable getThrowable() { + return this.throwable; + } +} diff --git a/students/313001956/src/main/java/org/v0_my/TestResult.java b/students/313001956/src/main/java/org/v0_my/TestResult.java new file mode 100644 index 0000000000..c25d5b6bf3 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/TestResult.java @@ -0,0 +1,109 @@ +package org.v0_my; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.v0_my.runner.TestListener; + +public class TestResult { + + private List fails; + private List errs; + private int testCount; + private List listeners; + + public TestResult() { + // TODO Auto-generated constructor stub + fails = new ArrayList<>(); + errs = new ArrayList<>(); + listeners = new ArrayList<>(); + testCount = 0; + } + + public int failsCount() { + return fails.size(); + } + + public void run(final TestCase test) { + // TODO Auto-generated method stub + startTest(test); + try { + test.doRun(this); + } catch (AssertionFailedError e) { + addFail(test, e); + } catch (Throwable e) { + addErr(test, e); + } finally { + endTest(test); + } + } + + private void endTest(Test test) { + // TODO Auto-generated method stub + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.endTest(test); + } + } + + private void startTest(Test test) { + // TODO Auto-generated method stub + testCount += test.getCaseCount(); + + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.startTest(test); + } + } + + private void addErr(final Test test, Throwable e) { + errs.add(new TestFailure(test, e)); + + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.addErr(test, e); + } + } + + private void addFail(final Test test, AssertionFailedError e) { + fails.add(new TestFailure(test, e)); + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.addFail(test, e); + } + } + + public Iterator fails() { + return fails.iterator(); + } + + public Iterator errs() { + return errs.iterator(); + } + + public int errsCount() { + // TODO Auto-generated method stub + return errs.size(); + } + + public int runCount() { + return testCount; + } + + public boolean isSuccesful() { + return failsCount() == 0 && errsCount() == 0; + } + + public void addListener(TestListener listener) { + listeners.add(listener); + } + + public void removeListener(TestListener listener) { + listeners.remove(listener); + } + + private Iterator listeners() { + return listeners.iterator(); + } +} diff --git a/students/313001956/src/main/java/org/v0_my/TestTuite.java b/students/313001956/src/main/java/org/v0_my/TestTuite.java new file mode 100644 index 0000000000..bdc551bf15 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/TestTuite.java @@ -0,0 +1,140 @@ +package org.v0_my; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class TestTuite extends Assert implements Test { + List caselist = new ArrayList<>(10); + List namelist = new ArrayList<>(); + private String name; + + public TestTuite(String name) { + this.name = name; + } + + public TestTuite(final Class clazz) { + // TODO Auto-generated constructor stub + if (!Modifier.isPublic(clazz.getModifiers())) { + fail("the class " + clazz.getName() + " must be public"); + } + Constructor constructor = getConstructor(clazz); + Method[] arrmethod = clazz.getDeclaredMethods(); + for (Method m : arrmethod) { + addTest(m, constructor); + } + + if (arrmethod.length == 0) { + fail("there is no method to test!"); + } + } + + private Constructor getConstructor(Class clazz) { + // TODO Auto-generated method stub + Class[] parameterTypes = new Class[] { String.class }; + Constructor constructor = null; + try { + constructor = clazz.getConstructor(parameterTypes); + } catch (NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return constructor; + } + + public void addTest(Method m, Constructor constructor) { + if (!isTestMethod(m)) { + return; + } + String method_name = m.getName(); + if (namelist.contains(method_name)) { + return; + } + namelist.add(method_name); + Object[] initargs = new Object[] { method_name }; + try { + Test t = (Test) constructor.newInstance(initargs); + addTest(t); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void addTest(Test t) { + caselist.add(t); + } + + public void addTest(Class clazz) { + Object obj = null; + try { + obj = clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + + Method method = null; + try { + Class[] args = new Class[0]; + method=clazz.getMethod("tuite", args); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + Test test = (Test) method.invoke(obj); + addTest(test); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + private boolean isTestMethod(Method m) { + + return m.getName().startsWith("test") && Modifier.isPublic(m.getModifiers()) + && m.getParameterTypes().length == 0 && m.getReturnType().equals(Void.TYPE); + } + + @Override + public void run(TestResult testResult) { + // TODO Auto-generated method stub + for (Test tc : caselist) { + tc.run(testResult); + + } + } + + @Override + public Integer getCaseCount() { + // TODO Auto-generated method stub + int count = 0; + for (Iterator iterator = caselist.iterator(); iterator.hasNext();) { + Test test = iterator.next(); + count += test.getCaseCount(); + } + return count; + } + + public void addTestTuite(Class clazz) { + addTest(new TestTuite(clazz)); + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/extension/RepeatedTest.java b/students/313001956/src/main/java/org/v0_my/extension/RepeatedTest.java new file mode 100644 index 0000000000..f63d9f4938 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/extension/RepeatedTest.java @@ -0,0 +1,28 @@ +package org.v0_my.extension; + +import org.v0_my.Test; +import org.v0_my.TestResult; + +public class RepeatedTest extends TestDecorator { + + private int num; + + public RepeatedTest(Test t, int num) { + super(t); + if (num < 0) { + throw new IllegalArgumentException("num must be >0"); + } + this.num = num; + // TODO Auto-generated constructor stub + } + + @Override + public void run(TestResult testResult) { + // TODO Auto-generated method stub + for (int i = 0; i < num; i++) { + super.run(testResult); + } + + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/extension/TestDecorator.java b/students/313001956/src/main/java/org/v0_my/extension/TestDecorator.java new file mode 100644 index 0000000000..f597621c9e --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/extension/TestDecorator.java @@ -0,0 +1,32 @@ +package org.v0_my.extension; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import org.v0_my.Test; +import org.v0_my.TestResult; + +public class TestDecorator implements Test { + + Test test; + + public TestDecorator(Test test) { + // TODO Auto-generated constructor stub + this.test = test; + } + + + + @Override + public void run(TestResult testResult) { + test.run(testResult); + } + + @Override + public Integer getCaseCount() { + // TODO Auto-generated method stub + return test.getCaseCount(); + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/extension/TestSetup.java b/students/313001956/src/main/java/org/v0_my/extension/TestSetup.java new file mode 100644 index 0000000000..833635d4dc --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/extension/TestSetup.java @@ -0,0 +1,34 @@ +package org.v0_my.extension; + +import org.v0_my.Test; +import org.v0_my.TestResult; + +public class TestSetup extends TestDecorator { + + public TestSetup(Test t) { + super(t); + + // TODO Auto-generated constructor stub + } + + @Override + public void run(TestResult testResult) { + // TODO Auto-generated method stub + setup(); + super.run(testResult); + teardown(); + } + + private void teardown() { + // TODO Auto-generated method stub + System.out.println(); + System.out.println("this is alltest teardown."); + } + + private void setup() { + // TODO Auto-generated method stub + + System.out.println("this is alltest setup"); + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/runner/TestBaseRunner.java b/students/313001956/src/main/java/org/v0_my/runner/TestBaseRunner.java new file mode 100644 index 0000000000..5557d164ec --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/runner/TestBaseRunner.java @@ -0,0 +1,44 @@ +package org.v0_my.runner; + +import java.io.PrintStream; +import java.text.NumberFormat; + +import org.v0_my.AssertionFailedError; +import org.v0_my.Test; + +public class TestBaseRunner implements TestListener { + private PrintStream pStream = System.out; + int column = 0; + + @Override + public synchronized void endTest(Test test) { + // TODO Auto-generated method stub + // pStream.println(); + } + + @Override + public synchronized void startTest(Test test) { + // TODO Auto-generated method stub + pStream.print("."); + if (++column > 20) { + pStream.println(); + column = 0; + } + } + + @Override + public synchronized void addErr(Test test, Throwable e) { + // TODO Auto-generated method stub + pStream.print("E"); + } + + @Override + public synchronized void addFail(Test test, AssertionFailedError e) { + // TODO Auto-generated method stub + pStream.print("F"); + } +public String formatNum(long num) { + return NumberFormat.getInstance().format((double)(num/1000)); +} + +} diff --git a/students/313001956/src/main/java/org/v0_my/runner/TestListener.java b/students/313001956/src/main/java/org/v0_my/runner/TestListener.java new file mode 100644 index 0000000000..e834267a4d --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/runner/TestListener.java @@ -0,0 +1,14 @@ +package org.v0_my.runner; + +import org.v0_my.AssertionFailedError; +import org.v0_my.Test; + +public interface TestListener { + public void endTest(Test test); + + public void startTest(Test test); + + public void addErr(final Test test, Throwable e) ; + + public void addFail(final Test test, AssertionFailedError e); +} diff --git a/students/313001956/src/main/java/org/v0_my/runner/TestRunner.java b/students/313001956/src/main/java/org/v0_my/runner/TestRunner.java new file mode 100644 index 0000000000..ef63109496 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/runner/TestRunner.java @@ -0,0 +1,72 @@ +package org.v0_my.runner; + +import java.io.PrintStream; +import java.util.Iterator; + +import org.v0_my.Test; +import org.v0_my.TestFailure; +import org.v0_my.TestResult; +import org.v0_my.sample.AllTest; + +public class TestRunner extends TestBaseRunner { + + private PrintStream ps = System.out; + + public static void main(String[] args) { + + Test test = AllTest.tuite(); + TestResult tr = new TestResult(); + TestRunner runner = new TestRunner(); + tr.addListener(runner); + long t1 = System.currentTimeMillis(); + test.run(tr); + long t2 = System.currentTimeMillis(); + runner.ps.println(); + runner.ps.println("Time:" + runner.formatNum(t2 - t1) + "s"); + + runner.printResult(tr); + + } + + private void printResult(TestResult tr) { + printFails(tr); + printErrs(tr); + printSummary(tr); + } + + private void printSummary(TestResult tr) { + // TODO Auto-generated method stub + String result = tr.isSuccesful() ? "Success" : "Fail"; + ps.println(result); + ps.println("Test run:" + tr.runCount() + ",Failures:" + tr.failsCount() + ",Errs:" + tr.errsCount()); + } + + private void printErrs(TestResult tr) { + // TODO Auto-generated method stub + int count = tr.errsCount(); + String s = count > 1 ? "there were " + count + " errs" : "there was " + count + " err"; + ps.println(s); + int i = 1; + for (Iterator iter = tr.errs(); iter.hasNext();) { + TestFailure failure = iter.next(); + ps.print((i++) + ")"); + ps.println(failure.getTestCase()); + ps.println(failure.getThrowable()); + } + } + + private void printFails(TestResult tr) { + // TODO Auto-generated method stub + int count = tr.failsCount(); + String s = count > 1 ? "there were " + count + " fails" : "there was " + count + " fail"; + ps.println(s); + int i = 1; + for (Iterator iter = tr.fails(); iter.hasNext();) { + TestFailure failure = iter.next(); + ps.print((i++) + ")"); + ps.println(failure.getTestCase()); + ps.println(failure.getThrowable()); + } + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/sample/AllTest.java b/students/313001956/src/main/java/org/v0_my/sample/AllTest.java new file mode 100644 index 0000000000..1468d14090 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/sample/AllTest.java @@ -0,0 +1,20 @@ +package org.v0_my.sample; + +import org.v0_my.Test; +import org.v0_my.TestTuite; +import org.v0_my.extension.RepeatedTest; +import org.v0_my.sample.caculator.CalculatorTuite; +import org.v0_my.sample.person.PersonTest; + +import junit.extensions.TestSetup; + +public class AllTest { +public static Test tuite() { + TestTuite tuite=new TestTuite("AllTest"); + tuite.addTest(CalculatorTuite.tuite()); + //tuite.addTestTuite(PersonTest.class); + //return tuite; + tuite.addTest(new RepeatedTest(new TestTuite(PersonTest.class), 1)); + return new org.v0_my.extension.TestSetup(tuite); +} +} diff --git a/students/313001956/src/main/java/org/v0_my/sample/caculator/CaculatorTestCase.java b/students/313001956/src/main/java/org/v0_my/sample/caculator/CaculatorTestCase.java new file mode 100644 index 0000000000..ee3e47be0c --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/sample/caculator/CaculatorTestCase.java @@ -0,0 +1,51 @@ +package org.v0_my.sample.caculator; + +import org.junit.experimental.theories.Theories; +import org.v0_my.TestCase; + +public class CaculatorTestCase extends TestCase { + + Calculator calculator = null; + + public CaculatorTestCase(String name) { + super(name); + // TODO Auto-generated constructor stub + } + + public void setUp() { + // TODO Auto-generated method stub + calculator = new Calculator(); + } + + public void tearDown() { + // TODO Auto-generated method stub + calculator = null; + } + + public void testAdd() { + + calculator.add(10); + int a=0; + int b=1/a; + assertEquals(10, calculator.getResult()); + System.out.println("testadd"); + } + + public void testSubtract() { + calculator.add(10); + calculator.subtract(5); + assertEquals(4, calculator.getResult()); + System.out.println("testSubtract"); + } + + public static void main(String[] args) { + // TestCase testCase = new CaculatorTestCase("testAdd"); + // testCase.run(); +// Test tt = new TestTuite(CaculatorTestCase.class); +// TestResult testResult=new TestResult(); +// tt.run(testResult); +// System.out.println(testResult.getFailtestCount()); +// System.out.println(testResult.getErrtestCount()); + } + +} diff --git a/students/313001956/src/main/java/org/v0_my/sample/caculator/Calculator.java b/students/313001956/src/main/java/org/v0_my/sample/caculator/Calculator.java new file mode 100644 index 0000000000..19ecae3af4 --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/sample/caculator/Calculator.java @@ -0,0 +1,22 @@ +package org.v0_my.sample.caculator; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} diff --git a/students/313001956/src/main/java/org/v0_my/sample/caculator/CalculatorTuite.java b/students/313001956/src/main/java/org/v0_my/sample/caculator/CalculatorTuite.java new file mode 100644 index 0000000000..9f846339ed --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/sample/caculator/CalculatorTuite.java @@ -0,0 +1,12 @@ +package org.v0_my.sample.caculator; + +import org.v0_my.Test; +import org.v0_my.TestTuite; + +public class CalculatorTuite { +public static Test tuite() { + TestTuite tuite=new TestTuite("CalculatorTuite"); + tuite.addTestTuite(CaculatorTestCase.class); + return tuite; +} +} diff --git a/students/313001956/src/main/java/org/v0_my/sample/person/PersonTest.java b/students/313001956/src/main/java/org/v0_my/sample/person/PersonTest.java new file mode 100644 index 0000000000..eb8ae4585c --- /dev/null +++ b/students/313001956/src/main/java/org/v0_my/sample/person/PersonTest.java @@ -0,0 +1,38 @@ +package org.v0_my.sample.person; + +import org.v0_my.TestCase; + +public class PersonTest extends TestCase { + + Person p = null; + protected void setUp() { + p = new Person("andy",30); + } + public PersonTest(String name) { + super(name); + } + public void testAge(){ + this.assertEquals(31, p.getAge()); + } + public void testName(){ + this.assertEquals("andy1", p.getName()); + } +} +class Person{ + private String name; + private int age; + + public Person(String name, int age) { + + this.name = name; + this.age = age; + } + public String getName() { + return name; + } + public int getAge() { + return age; + } + + +} From adc9847736074bbf2959d869cd20af3341472e66 Mon Sep 17 00:00:00 2001 From: maneng Date: Fri, 15 Sep 2017 22:26:59 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/643449856/litejunit/pom.xml | 32 +++ .../src/main/java/org/litejunit/Assert.java | 225 +++++++++++++++ .../org/litejunit/AssertionFailedError.java | 11 + .../src/main/java/org/litejunit/Test.java | 7 + .../src/main/java/org/litejunit/TestCase.java | 69 +++++ .../main/java/org/litejunit/TestFailure.java | 20 ++ .../main/java/org/litejunit/TestResult.java | 104 +++++++ .../main/java/org/litejunit/TestTuite.java | 128 +++++++++ .../org/litejunit/extension/RepeatedTest.java | 26 ++ .../litejunit/extension/TestDecorator.java | 26 ++ .../org/litejunit/extension/TestSetup.java | 27 ++ .../org/litejunit/runner/TestBaseRunner.java | 37 +++ .../org/litejunit/runner/TestListener.java | 14 + .../java/org/litejunit/runner/TestRunner.java | 69 +++++ .../java/org/litejunit/sample/AllTest.java | 19 ++ .../sample/caculator/CaculatorTestCase.java | 39 +++ .../sample/caculator/Calculator.java | 22 ++ .../sample/caculator/CalculatorTuite.java | 12 + .../litejunit/src/main/java/org/v3/After.java | 12 + .../src/main/java/org/v3/AfterClass.java | 13 + .../src/main/java/org/v3/Assert.java | 269 ++++++++++++++++++ .../src/main/java/org/v3/Before.java | 13 + .../src/main/java/org/v3/BeforeClass.java | 11 + .../main/java/org/v3/ComparisonFailure.java | 124 ++++++++ .../src/main/java/org/v3/Ignore.java | 31 ++ .../litejunit/src/main/java/org/v3/Test.java | 62 ++++ .../java/org/v3/notification/Failure.java | 79 +++++ .../java/org/v3/notification/RunListener.java | 53 ++++ .../java/org/v3/notification/RunNotifier.java | 141 +++++++++ .../notification/StoppedByUserException.java | 11 + .../java/org/v3/requests/ClassRequest.java | 48 ++++ .../main/java/org/v3/runner/Description.java | 127 +++++++++ .../main/java/org/v3/runner/JUnitCore.java | 167 +++++++++++ .../src/main/java/org/v3/runner/Request.java | 86 ++++++ .../src/main/java/org/v3/runner/Result.java | 98 +++++++ .../java/org/v3/runner/ResultPrinter.java | 72 +++++ .../src/main/java/org/v3/runner/RunWith.java | 24 ++ .../src/main/java/org/v3/runner/Runner.java | 24 ++ .../org/v3/runners/BeforeAndAfterRunner.java | 75 +++++ .../org/v3/runners/InitializationError.java | 25 ++ .../v3/runners/TestClassMethodsRunner.java | 78 +++++ .../java/org/v3/runners/TestClassRunner.java | 53 ++++ .../java/org/v3/runners/TestIntrospector.java | 74 +++++ .../java/org/v3/runners/TestMethodRunner.java | 86 ++++++ .../java/org/v3/runners/TextListener.java | 103 +++++++ .../main/java/org/v3/sample/Calculator.java | 22 ++ .../java/org/v3/sample/CalculatorTest.java | 52 ++++ .../com/coderising/ood/srp/Configuration.java | 14 +- .../java/com/coderising/ood/srp/DBUtil.java | 21 ++ .../java/com/coderising/ood/srp/FileUtil.java | 35 +++ .../java/com/coderising/ood/srp/Mail.java | 87 ++++++ .../java/com/coderising/ood/srp/MailUtil.java | 84 +++++- .../java/com/coderising/ood/srp/Product.java | 34 +++ .../com/coderising/ood/srp/PromotionMail.java | 148 +--------- students/643449856/payrolla/pom.xml | 32 +++ .../java/com/Affiliation/NonAffiliation.java | 20 ++ .../com/Affiliation/UnionAffiliation.java | 18 ++ .../CommissionedClassification.java | 46 +++ .../Classification/HourlyClassification.java | 59 ++++ .../SalariedClassification.java | 25 ++ .../java/com/PaymentMethod/BankMethod.java | 21 ++ .../java/com/PaymentMethod/HoldMethod.java | 24 ++ .../java/com/PaymentMethod/MailMethod.java | 30 ++ .../src/main/java/com/PayrollService.java | 82 ++++++ .../java/com/Schedule/BiweeklySchedule.java | 49 ++++ .../java/com/Schedule/MonthlySchedule.java | 27 ++ .../java/com/Schedule/WeeklySchedule.java | 27 ++ .../src/main/java/com/pojo/Affiliation.java | 15 + .../src/main/java/com/pojo/Employee.java | 118 ++++++++ .../src/main/java/com/pojo/Paycheck.java | 84 ++++++ .../java/com/pojo/PaymentClassification.java | 18 ++ .../src/main/java/com/pojo/PaymentMethod.java | 14 + .../main/java/com/pojo/PaymentSchedule.java | 21 ++ .../src/main/java/com/pojo/SalesReceipt.java | 22 ++ .../src/main/java/com/pojo/TimeCard.java | 23 ++ .../transaction/AddEmployeeTransaction.java | 42 +++ .../AddHourlyEmployeeTransaction.java | 33 +++ .../src/main/java/com/util/DateUtil.java | 67 +++++ 78 files changed, 4106 insertions(+), 154 deletions(-) create mode 100644 students/643449856/litejunit/pom.xml create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/Assert.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/AssertionFailedError.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/Test.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/TestCase.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/TestFailure.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/TestResult.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/TestTuite.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/extension/RepeatedTest.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/extension/TestDecorator.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/extension/TestSetup.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/runner/TestBaseRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/runner/TestListener.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/runner/TestRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/sample/AllTest.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CaculatorTestCase.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/Calculator.java create mode 100644 students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CalculatorTuite.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/After.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/AfterClass.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/Assert.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/Before.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/BeforeClass.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/ComparisonFailure.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/Ignore.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/Test.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/notification/Failure.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/notification/RunListener.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/notification/RunNotifier.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/notification/StoppedByUserException.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/requests/ClassRequest.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/Description.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/JUnitCore.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/Request.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/Result.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/ResultPrinter.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/RunWith.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runner/Runner.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/BeforeAndAfterRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/InitializationError.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/TestClassMethodsRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/TestClassRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/TestIntrospector.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/TestMethodRunner.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/runners/TextListener.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/sample/Calculator.java create mode 100644 students/643449856/litejunit/src/main/java/org/v3/sample/CalculatorTest.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java create mode 100644 students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java create mode 100644 students/643449856/payrolla/pom.xml create mode 100644 students/643449856/payrolla/src/main/java/com/Affiliation/NonAffiliation.java create mode 100644 students/643449856/payrolla/src/main/java/com/Affiliation/UnionAffiliation.java create mode 100644 students/643449856/payrolla/src/main/java/com/Classification/CommissionedClassification.java create mode 100644 students/643449856/payrolla/src/main/java/com/Classification/HourlyClassification.java create mode 100644 students/643449856/payrolla/src/main/java/com/Classification/SalariedClassification.java create mode 100644 students/643449856/payrolla/src/main/java/com/PaymentMethod/BankMethod.java create mode 100644 students/643449856/payrolla/src/main/java/com/PaymentMethod/HoldMethod.java create mode 100644 students/643449856/payrolla/src/main/java/com/PaymentMethod/MailMethod.java create mode 100644 students/643449856/payrolla/src/main/java/com/PayrollService.java create mode 100644 students/643449856/payrolla/src/main/java/com/Schedule/BiweeklySchedule.java create mode 100644 students/643449856/payrolla/src/main/java/com/Schedule/MonthlySchedule.java create mode 100644 students/643449856/payrolla/src/main/java/com/Schedule/WeeklySchedule.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/Affiliation.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/Employee.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/Paycheck.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/PaymentClassification.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/PaymentMethod.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/PaymentSchedule.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/SalesReceipt.java create mode 100644 students/643449856/payrolla/src/main/java/com/pojo/TimeCard.java create mode 100644 students/643449856/payrolla/src/main/java/com/transaction/AddEmployeeTransaction.java create mode 100644 students/643449856/payrolla/src/main/java/com/transaction/AddHourlyEmployeeTransaction.java create mode 100644 students/643449856/payrolla/src/main/java/com/util/DateUtil.java diff --git a/students/643449856/litejunit/pom.xml b/students/643449856/litejunit/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/643449856/litejunit/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/Assert.java b/students/643449856/litejunit/src/main/java/org/litejunit/Assert.java new file mode 100644 index 0000000000..9908821a5f --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/Assert.java @@ -0,0 +1,225 @@ +package org.litejunit; + +/** + * A set of assert methods. + */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/AssertionFailedError.java b/students/643449856/litejunit/src/main/java/org/litejunit/AssertionFailedError.java new file mode 100644 index 0000000000..6ab9b29ba7 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/AssertionFailedError.java @@ -0,0 +1,11 @@ +package org.litejunit; + +public class AssertionFailedError extends Error { +public AssertionFailedError(){ + +} + + public AssertionFailedError(String message) { + super(message); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/Test.java b/students/643449856/litejunit/src/main/java/org/litejunit/Test.java new file mode 100644 index 0000000000..862dc01c41 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/Test.java @@ -0,0 +1,7 @@ +package org.litejunit; + +public interface Test { + public void run(TestResult testResult); + + public Integer getCaseCount(); +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/TestCase.java b/students/643449856/litejunit/src/main/java/org/litejunit/TestCase.java new file mode 100644 index 0000000000..310293885a --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/TestCase.java @@ -0,0 +1,69 @@ +package org.litejunit; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +public abstract class TestCase extends Assert implements Test { + private String name; + + public TestCase(String name) { + this.name = name; + } + + @Override + public void run(TestResult testResult) { + testResult.run(this); + } + + public void doRun(TestResult testResult) throws Throwable { + setUp(); + try { + runTest(); + + } finally { + tearDown(); + } + } + + protected void setUp() { + + } + + protected void runTest() throws Throwable { + Class clazz = this.getClass(); + Method method=null; + try { + method = clazz.getDeclaredMethod(name); + + } catch (NoSuchMethodException | SecurityException e1) { + // e1.printStackTrace(); + } + + if(!Modifier.isPublic(method.getModifiers())){ + fail("method "+name+" is not a public menthod!"); + } + + try { + method.invoke(this); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // e.printStackTrace(); + + Throwable ta = e.getCause(); + throw ta; + // } + + } + + } + + protected void tearDown() { + + } + + @Override + public Integer getCaseCount() { + return 1; + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/TestFailure.java b/students/643449856/litejunit/src/main/java/org/litejunit/TestFailure.java new file mode 100644 index 0000000000..5933d1f479 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/TestFailure.java @@ -0,0 +1,20 @@ +package org.litejunit; + +public class TestFailure { + + private Test test; + private Throwable throwable; + + public TestFailure(Test test, Throwable throwable) { + this.test = test; + this.throwable = throwable; + } + + public Test getTestCase() { + return this.test; + } + + public Throwable getThrowable() { + return this.throwable; + } +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/TestResult.java b/students/643449856/litejunit/src/main/java/org/litejunit/TestResult.java new file mode 100644 index 0000000000..96f76e380c --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/TestResult.java @@ -0,0 +1,104 @@ +package org.litejunit; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.litejunit.runner.TestListener; + +public class TestResult { + + private List fails; + private List errs; + private int testCount; + private List listeners; + + public TestResult() { + fails = new ArrayList<>(); + errs = new ArrayList<>(); + listeners = new ArrayList<>(); + testCount = 0; + } + + public int failsCount() { + return fails.size(); + } + + public void run(final TestCase test) { + startTest(test); + try { + test.doRun(this); + } catch (AssertionFailedError e) { + addFail(test, e); + } catch (Throwable e) { + addErr(test, e); + } finally { + endTest(test); + } + } + + private void endTest(Test test) { + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.endTest(test); + } + } + + private void startTest(Test test) { + testCount += test.getCaseCount(); + + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.startTest(test); + } + } + + private void addErr(final Test test, Throwable e) { + errs.add(new TestFailure(test, e)); + + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.addErr(test, e); + } + } + + private void addFail(final Test test, AssertionFailedError e) { + fails.add(new TestFailure(test, e)); + for (Iterator iter = listeners(); iter.hasNext();) { + TestListener listener = iter.next(); + listener.addFail(test, e); + } + } + + public Iterator fails() { + return fails.iterator(); + } + + public Iterator errs() { + return errs.iterator(); + } + + public int errsCount() { + return errs.size(); + } + + public int runCount() { + return testCount; + } + + public boolean isSuccesful() { + return failsCount() == 0 && errsCount() == 0; + } + + public void addListener(TestListener listener) { + listeners.add(listener); + } + + public void removeListener(TestListener listener) { + listeners.remove(listener); + } + + private Iterator listeners() { + return listeners.iterator(); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/TestTuite.java b/students/643449856/litejunit/src/main/java/org/litejunit/TestTuite.java new file mode 100644 index 0000000000..187433a27b --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/TestTuite.java @@ -0,0 +1,128 @@ +package org.litejunit; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class TestTuite extends Assert implements Test { + List caselist = new ArrayList<>(10); + List namelist = new ArrayList<>(); + private String name; + + public TestTuite(String name) { + this.name = name; + } + + public TestTuite(final Class clazz) { + if (!Modifier.isPublic(clazz.getModifiers())) { + fail("the class " + clazz.getName() + " must be public"); + } + Constructor constructor = getConstructor(clazz); + Method[] arrmethod = clazz.getDeclaredMethods(); + for (Method m : arrmethod) { + addTest(m, constructor); + } + + if (arrmethod.length == 0) { + fail("there is no method to test!"); + } + } + + private Constructor getConstructor(Class clazz) { + Class[] parameterTypes = new Class[] { String.class }; + Constructor constructor = null; + try { + constructor = clazz.getConstructor(parameterTypes); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } + return constructor; + } + + public void addTest(Method m, Constructor constructor) { + if (!isTestMethod(m)) { + return; + } + String method_name = m.getName(); + if (namelist.contains(method_name)) { + return; + } + namelist.add(method_name); + Object[] initargs = new Object[] { method_name }; + try { + Test t = (Test) constructor.newInstance(initargs); + addTest(t); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + } + + public void addTest(Test t) { + caselist.add(t); + } + + public void addTest(Class clazz) { + Object obj = null; + try { + obj = clazz.newInstance(); + } catch (InstantiationException | IllegalAccessException e1) { + e1.printStackTrace(); + } + + Method method = null; + try { + Class[] args = new Class[0]; + method=clazz.getMethod("tuite", args); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + + try { + Test test = (Test) method.invoke(obj); + addTest(test); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + } + + private boolean isTestMethod(Method m) { + + return m.getName().startsWith("test") && Modifier.isPublic(m.getModifiers()) + && m.getParameterTypes().length == 0 && m.getReturnType().equals(Void.TYPE); + } + + @Override + public void run(TestResult testResult) { + for (Test tc : caselist) { + tc.run(testResult); + + } + } + + @Override + public Integer getCaseCount() { + int count = 0; + for (Iterator iterator = caselist.iterator(); iterator.hasNext();) { + Test test = iterator.next(); + count += test.getCaseCount(); + } + return count; + } + + public void addTestTuite(Class clazz) { + addTest(new TestTuite(clazz)); + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/extension/RepeatedTest.java b/students/643449856/litejunit/src/main/java/org/litejunit/extension/RepeatedTest.java new file mode 100644 index 0000000000..dbce472e8b --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/extension/RepeatedTest.java @@ -0,0 +1,26 @@ +package org.litejunit.extension; + +import org.litejunit.Test; +import org.litejunit.TestResult; + +public class RepeatedTest extends TestDecorator { + + private int num; + + public RepeatedTest(Test t, int num) { + super(t); + if (num < 0) { + throw new IllegalArgumentException("num must>0"); + } + this.num = num; + } + + @Override + public void run(TestResult testResult) { + for (int i = 0; i < num; i++) { + super.run(testResult); + } + + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestDecorator.java b/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestDecorator.java new file mode 100644 index 0000000000..571e1074a7 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestDecorator.java @@ -0,0 +1,26 @@ +package org.litejunit.extension; + +import org.litejunit.Test; +import org.litejunit.TestResult; + +public class TestDecorator implements Test { + + Test test; + + public TestDecorator(Test test) { + this.test = test; + } + + + + @Override + public void run(TestResult testResult) { + test.run(testResult); + } + + @Override + public Integer getCaseCount() { + return test.getCaseCount(); + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestSetup.java b/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestSetup.java new file mode 100644 index 0000000000..136da5050d --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/extension/TestSetup.java @@ -0,0 +1,27 @@ +package org.litejunit.extension; + +import org.litejunit.Test; +import org.litejunit.TestResult; + +public class TestSetup extends TestDecorator { + + public TestSetup(Test t) { + super(t); + + } + + @Override + public void run(TestResult testResult) { + setup(); + super.run(testResult); + teardown(); + } + + private void teardown() { + } + + private void setup() { + + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestBaseRunner.java b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestBaseRunner.java new file mode 100644 index 0000000000..48ca6a24af --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestBaseRunner.java @@ -0,0 +1,37 @@ +package org.litejunit.runner; + +import java.io.PrintStream; +import java.text.NumberFormat; + +import org.litejunit.AssertionFailedError; +import org.litejunit.Test; + +public class TestBaseRunner implements TestListener { + private PrintStream pStream = System.out; + int column = 0; + + @Override + public synchronized void endTest(Test test) { + } + + @Override + public synchronized void startTest(Test test) { + pStream.print("."); + if (++column > 20) { + pStream.println(); + column = 0; + } + } + + @Override + public synchronized void addErr(Test test, Throwable e) { + } + + @Override + public synchronized void addFail(Test test, AssertionFailedError e) { + } +public String formatNum(long num) { + return NumberFormat.getInstance().format((double)(num/1000)); +} + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestListener.java b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestListener.java new file mode 100644 index 0000000000..8612b9863a --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestListener.java @@ -0,0 +1,14 @@ +package org.litejunit.runner; + +import org.litejunit.AssertionFailedError; +import org.litejunit.Test; + +public interface TestListener { + public void endTest(Test test); + + public void startTest(Test test); + + public void addErr(final Test test, Throwable e) ; + + public void addFail(final Test test, AssertionFailedError e); +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestRunner.java b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestRunner.java new file mode 100644 index 0000000000..422b01183d --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/runner/TestRunner.java @@ -0,0 +1,69 @@ +package org.litejunit.runner; + +import java.io.PrintStream; +import java.util.Iterator; + +import org.litejunit.Test; +import org.litejunit.TestFailure; +import org.litejunit.TestResult; +import org.litejunit.sample.AllTest; + +public class TestRunner extends TestBaseRunner { + + private PrintStream ps = System.out; + + public static void main(String[] args) { + + Test test = AllTest.tuite(); + TestResult tr = new TestResult(); + TestRunner runner = new TestRunner(); + tr.addListener(runner); + long t1 = System.currentTimeMillis(); + test.run(tr); + long t2 = System.currentTimeMillis(); + runner.ps.println(); + runner.ps.println("Time:" + runner.formatNum(t2 - t1) + "s"); + + runner.printResult(tr); + + } + + private void printResult(TestResult tr) { + printFails(tr); + printErrs(tr); + printSummary(tr); + } + + private void printSummary(TestResult tr) { + String result = tr.isSuccesful() ? "Success" : "Fail"; + ps.println(result); + ps.println("Test run:" + tr.runCount() + ",Failures:" + tr.failsCount() + ",Errs:" + tr.errsCount()); + } + + private void printErrs(TestResult tr) { + int count = tr.errsCount(); + String s = count > 1 ? "there were " + count + " errs" : "there was " + count + " err"; + ps.println(s); + int i = 1; + for (Iterator iter = tr.errs(); iter.hasNext();) { + TestFailure failure = iter.next(); + ps.print((i++) + ")"); + ps.println(failure.getTestCase()); + ps.println(failure.getThrowable()); + } + } + + private void printFails(TestResult tr) { + int count = tr.failsCount(); + String s = count > 1 ? "there were " + count + " fails" : "there was " + count + " fail"; + ps.println(s); + int i = 1; + for (Iterator iter = tr.fails(); iter.hasNext();) { + TestFailure failure = iter.next(); + ps.print((i++) + ")"); + ps.println(failure.getTestCase()); + ps.println(failure.getThrowable()); + } + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/sample/AllTest.java b/students/643449856/litejunit/src/main/java/org/litejunit/sample/AllTest.java new file mode 100644 index 0000000000..edcb87452b --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/sample/AllTest.java @@ -0,0 +1,19 @@ +package org.litejunit.sample; + +import org.litejunit.Test; +import org.litejunit.TestTuite; +import org.litejunit.extension.RepeatedTest; +import org.litejunit.sample.caculator.CalculatorTuite; +import org.litejunit.sample.person.PersonTest; + + +public class AllTest { +public static Test tuite() { + TestTuite tuite=new TestTuite("AllTest"); + tuite.addTest(CalculatorTuite.tuite()); + //tuite.addTestTuite(PersonTest.class); + //return tuite; + tuite.addTest(new RepeatedTest(new TestTuite(PersonTest.class), 1)); + return new org.litejunit.extension.TestSetup(tuite); +} +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CaculatorTestCase.java b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CaculatorTestCase.java new file mode 100644 index 0000000000..f183f4dc3e --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CaculatorTestCase.java @@ -0,0 +1,39 @@ +package org.litejunit.sample.caculator; + +import org.litejunit.TestCase; + +public class CaculatorTestCase extends TestCase { + + Calculator calculator = null; + + public CaculatorTestCase(String name) { + super(name); + } + + public void setUp() { + calculator = new Calculator(); + } + + public void tearDown() { + calculator = null; + } + + public void testAdd() { + + calculator.add(10); + int a=0; + int b=1/a; + assertEquals(10, calculator.getResult()); + System.out.println("testadd"); + } + + public void testSubtract() { + calculator.add(10); + calculator.subtract(5); + assertEquals(4, calculator.getResult()); + System.out.println("testSubtract"); + } + + + +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/Calculator.java b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/Calculator.java new file mode 100644 index 0000000000..d0ed088aca --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/Calculator.java @@ -0,0 +1,22 @@ +package org.litejunit.sample.caculator; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CalculatorTuite.java b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CalculatorTuite.java new file mode 100644 index 0000000000..0be67ab224 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/litejunit/sample/caculator/CalculatorTuite.java @@ -0,0 +1,12 @@ +package org.litejunit.sample.caculator; + +import org.litejunit.Test; +import org.litejunit.TestTuite; + +public class CalculatorTuite { +public static Test tuite() { + TestTuite tuite=new TestTuite("CalculatorTuite"); + tuite.addTestTuite(CaculatorTestCase.class); + return tuite; +} +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/After.java b/students/643449856/litejunit/src/main/java/org/v3/After.java new file mode 100644 index 0000000000..85dc08ab38 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/After.java @@ -0,0 +1,12 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface After { +} + diff --git a/students/643449856/litejunit/src/main/java/org/v3/AfterClass.java b/students/643449856/litejunit/src/main/java/org/v3/AfterClass.java new file mode 100644 index 0000000000..e101051002 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/AfterClass.java @@ -0,0 +1,13 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface AfterClass { +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/Assert.java b/students/643449856/litejunit/src/main/java/org/v3/Assert.java new file mode 100644 index 0000000000..dce024757e --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/Assert.java @@ -0,0 +1,269 @@ +package org.v3; + +/** + * A set of assertion methods useful for writing tests. Only failed assertions are recorded. + * These methods can be used directly: Assert.assertEquals(...), however, they + * read better if they are referenced through static import:
+ * + * import static org.junit.Assert.*;
+ * ...
+ *   assertEquals(...);
+ *
+ */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws an + * AssertionError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + + /** + * Asserts that a condition is true. If it isn't it throws an + * AssertionError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + + /** + * Asserts that a condition is false. If it isn't it throws an + * AssertionError with the given message. + */ + static public void assertFalse(String message, boolean condition) { + assertTrue(message, !condition); + } + + /** + * Asserts that a condition is false. If it isn't it throws an + * AssertionError. + */ + static public void assertFalse(boolean condition) { + assertFalse(null, condition); + } + + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionError(message); + } + + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + + /** + * Asserts that two objects are equal. If they are not, an + * AssertionError is thrown with the given message. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + if (expected instanceof String && actual instanceof String) + throw new ComparisonFailure(message, (String)expected, (String)actual); + else + failNotEquals(message, expected, actual); + } + + /** + * Asserts that two objects are equal. If they are not, an + * AssertionError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * AssertionError is thrown with the given message. + */ + public static void assertEquals(String message, Object[] expecteds, Object[] actuals) { + if (expecteds == actuals) + return; + String header = message == null ? "" : message + ": "; + if (expecteds == null) + fail(header + "expected array was null"); + if (actuals == null) + fail(header + "actual array was null"); + if (actuals.length != expecteds.length) + fail(header + "array lengths differed, expected.length=" + expecteds.length + " actual.length=" + actuals.length); + + for (int i= 0; i < expecteds.length; i++) { + Object o1= expecteds[i]; + Object o2= actuals[i]; + if (o1.getClass().isArray() && o2.getClass().isArray()) { + Object[] expected= (Object[]) o1; + Object[] actual= (Object[]) o2; + assertEquals(header + "arrays first differed at element " + i + ";", expected, actual); + } else + assertEquals(header + "arrays first differed at element [" + i + "];", o1, o2); + } + } + + /** + * Asserts that two object arrays are equal. If they are not, an + * AssertionError is thrown. + */ + public static void assertEquals(Object[] expecteds, Object[] actuals) { + assertEquals(null, expecteds, actuals); + } + + /** + * Asserts that two doubles are equal to within a positive delta. If they + * are not, an AssertionError is thrown with the given message. If the + * expected value is infinity then the delta value is ignored. NaNs are + * considered equal: + * assertEquals(Double.NaN, Double.NaN, *) passes + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + if (Double.compare(expected, actual) == 0) + return; + if (!(Math.abs(expected - actual) <= delta)) + failNotEquals(message, new Double(expected), new Double(actual)); + } + + /** + * Asserts that two doubles are equal to within a positive delta. If they + * are not, an AssertionError is thrown. If the + * expected value is infinity then the delta value is ignored.NaNs are + * considered equal: + * assertEquals(Double.NaN, Double.NaN, *) passes + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + + /** + * Asserts that two floats are equal to within a positive delta. If they + * are not, an AssertionError is thrown with the given message. If the + * expected value is infinity then the delta value is ignored.NaNs are + * considered equal: + * assertEquals(Float.NaN, Float.NaN, *) passes + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + if (Float.compare(expected, actual) == 0) + return; + if (!(Math.abs(expected - actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + + /** + * Asserts that two floats are equal to within a positive delta. If they + * are not, an AssertionError is thrown. If the + * expected value is infinity then the delta value is ignored.NaNs are + * considered equal: + * assertEquals(Float.NaN, Float.NaN, *) passes + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + + /** + * Asserts that an object isn't null. If it is an AssertionError is + * thrown with the given message. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + + /** + * Asserts that an object isn't null. If it is an AssertionError is + * thrown. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + + /** + * Asserts that an object is null. If it is not, an AssertionError is + * thrown with the given message. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + + /** + * Asserts that an object is null. If it isn't an AssertionError is + * thrown. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + + /** + * Asserts that two objects refer to the same object. If they are not, an + * AssertionError is thrown with the given message. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + + /** + * Asserts that two objects refer to the same object. If they are not the + * same, an AssertionError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + /** + * Asserts that two objects do not refer to the same object. If they do + * refer to the same object, an AssertionError is thrown with the given + * message. + */ + static public void assertNotSame(String message, Object expected, Object actual) { + if (expected == actual) + failSame(message); + } + + /** + * Asserts that two objects do not refer to the same object. If they do + * refer to the same object, an AssertionError is thrown. + */ + static public void assertNotSame(Object expected, Object actual) { + assertNotSame(null, expected, actual); + } + + static private void failSame(String message) { + String formatted= ""; + if (message != null) + formatted= message + " "; + fail(formatted + "expected not same"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message + " "; + fail(formatted + "expected same:<" + expected + "> was not:<" + actual + ">"); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + fail(format(message, expected, actual)); + } + + static String format(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message + " "; + return formatted + "expected:<" + expected + "> but was:<" + actual + ">"; + } + +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/Before.java b/students/643449856/litejunit/src/main/java/org/v3/Before.java new file mode 100644 index 0000000000..55b92777e3 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/Before.java @@ -0,0 +1,13 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Before { +} + diff --git a/students/643449856/litejunit/src/main/java/org/v3/BeforeClass.java b/students/643449856/litejunit/src/main/java/org/v3/BeforeClass.java new file mode 100644 index 0000000000..3b06fe066b --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/BeforeClass.java @@ -0,0 +1,11 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface BeforeClass { +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/ComparisonFailure.java b/students/643449856/litejunit/src/main/java/org/v3/ComparisonFailure.java new file mode 100644 index 0000000000..359cc48bef --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/ComparisonFailure.java @@ -0,0 +1,124 @@ +package org.v3; + +/** + * Thrown when an assertEquals(String, String) fails. Create and throw + * a ComparisonFailure manually if you want to show users the difference between two complex + * strings. + * + * Inspired by a patch from Alex Chaffee (alex@purpletech.com) + */ +public class ComparisonFailure extends AssertionError { + private static final int MAX_CONTEXT_LENGTH= 20; + private static final long serialVersionUID= 1L; + + private String fExpected; + private String fActual; + + /** + * Constructs a comparison failure. + * @param message the identifying message or null + * @param expected the expected string value + * @param actual the actual string value + */ + public ComparisonFailure (String message, String expected, String actual) { + super (message); + fExpected= expected; + fActual= actual; + } + + /** + * Returns "..." in place of common prefix and "..." in + * place of common suffix between expected and actual. + * + * @see Throwable#getMessage() + */ + @Override + public String getMessage() { + return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage()); + } + + /** + * Returns the actual value + * @return the actual string value + */ + public String getActual() { + return fActual; + } + /** + * Returns the expected value + * @return the expected string value + */ + public String getExpected() { + return fExpected; + } + + private static class ComparisonCompactor { + private static final String ELLIPSIS= "..."; + private static final String DELTA_END= "]"; + private static final String DELTA_START= "["; + + private int fContextLength; + private String fExpected; + private String fActual; + private int fPrefix; + private int fSuffix; + + public ComparisonCompactor(int contextLength, String expected, String actual) { + fContextLength= contextLength; + fExpected= expected; + fActual= actual; + } + + public String compact(String message) { + if (fExpected == null || fActual == null || areStringsEqual()) + return Assert.format(message, fExpected, fActual); + + findCommonPrefix(); + findCommonSuffix(); + String expected= compactString(fExpected); + String actual= compactString(fActual); + return Assert.format(message, expected, actual); + } + + private String compactString(String source) { + String result= DELTA_START + source.substring(fPrefix, source.length() - fSuffix + 1) + DELTA_END; + if (fPrefix > 0) + result= computeCommonPrefix() + result; + if (fSuffix > 0) + result= result + computeCommonSuffix(); + return result; + } + + private void findCommonPrefix() { + fPrefix= 0; + int end= Math.min(fExpected.length(), fActual.length()); + for (; fPrefix < end; fPrefix++) { + if (fExpected.charAt(fPrefix) != fActual.charAt(fPrefix)) + break; + } + } + + private void findCommonSuffix() { + int expectedSuffix= fExpected.length() - 1; + int actualSuffix= fActual.length() - 1; + for (; actualSuffix >= fPrefix && expectedSuffix >= fPrefix; actualSuffix--, expectedSuffix--) { + if (fExpected.charAt(expectedSuffix) != fActual.charAt(actualSuffix)) + break; + } + fSuffix= fExpected.length() - expectedSuffix; + } + + private String computeCommonPrefix() { + return (fPrefix > fContextLength ? ELLIPSIS : "") + fExpected.substring(Math.max(0, fPrefix - fContextLength), fPrefix); + } + + private String computeCommonSuffix() { + int end= Math.min(fExpected.length() - fSuffix + 1 + fContextLength, fExpected.length()); + return fExpected.substring(fExpected.length() - fSuffix + 1, end) + (fExpected.length() - fSuffix + 1 < fExpected.length() - fContextLength ? ELLIPSIS : ""); + } + + private boolean areStringsEqual() { + return fExpected.equals(fActual); + } + } +} \ No newline at end of file diff --git a/students/643449856/litejunit/src/main/java/org/v3/Ignore.java b/students/643449856/litejunit/src/main/java/org/v3/Ignore.java new file mode 100644 index 0000000000..4677b35a2e --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/Ignore.java @@ -0,0 +1,31 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Sometimes you want to temporarily disable a test. Methods annotated with @Test + * that are also annotated with @Ignore will not be executed as tests. Native JUnit 4 test runners + * should report the number of ignored tests along with the number of tests that ran and the + * number of tests that failed. + *

+ * For example:
+ * + *   @Ignore @Test public void something() { ...
+ *
+ * @Ignore takes an optional default parameter if you want to record why a test is being ignored:
+ * + *   @Ignore("not ready yet") @Test public void something() { ...
+ *
+ * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Ignore { + /** + * The optional reason why the test is ignored. + */ + String value() default ""; +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/Test.java b/students/643449856/litejunit/src/main/java/org/v3/Test.java new file mode 100644 index 0000000000..0902695239 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/Test.java @@ -0,0 +1,62 @@ +package org.v3; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * The Test annotation tells JUnit that the public void method + * to which it is attached can be run as a test case. To run the method, + * JUnit first constructs a fresh instance of the class then invokes the + * annotated method. Any exceptions thrown by the test will be reported + * by JUnit as a failure. If no exceptions are thrown, the test is assumed + * to have succeeded. + *

+ * A simple test looks like this:
+ * + * public class Example {
+ *   @Test public void method() {
+ *     System.out.println("Hello");
+ *   }
+ * } + *
+ *

+ * The Test annotation supports two optional parameters. + * The first, expected, declares that a test method should throw + * an exception. If it doesn't throw an exception or if it throws a different exception + * than the one declared, the test fails. For example, the following test succeeds:
+ * + *   @Test(expected=IndexOutOfBoundsException.class) public void outOfBounds() {
+ *     new ArrayList<Object>().get(1);
+ *   }
+ *
+ *

+ * The second optional parameter, timeout, causes a test to fail if it takes longer than a specified + * amount of clock time (measured in milliseconds). The following test fails:
+ * + *   @Test(timeout=100) public void infinity() {
+ *     for(;;);
+ *   }
+ *
+ */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface Test { + static class None extends Throwable { + private static final long serialVersionUID= 1L; + private None() { + } + } + + /** + * Optionally specify expected, a Throwable, to cause a test method to succeed iff + * an exception of the specified class is thrown by the method. + */ + Class expected() default None.class; + + /** + * Optionally specify timeout in milliseconds to cause a test method to fail if it + * takes longer than that number of milliseconds.*/ + long timeout() default 0L; +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/notification/Failure.java b/students/643449856/litejunit/src/main/java/org/v3/notification/Failure.java new file mode 100644 index 0000000000..29f9cf11c1 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/notification/Failure.java @@ -0,0 +1,79 @@ +package org.v3.notification; + +import org.v3.runner.Description; + +import java.io.PrintWriter; +import java.io.StringWriter; + + + +/** + * A Failure holds a description of the failed test and the + * exception that was thrown while running it. In most cases the Description + * will be of a single test. However, if problems are encountered while constructing the + * test (for example, if a @BeforeClass method is not static), it may describe + * something other than a single test. + */ +public class Failure { + private final Description fDescription; + private Throwable fThrownException; + + /** + * Constructs a Failure with the given description and exception. + * @param description a Description of the test that failed + * @param thrownException the exception that was thrown while running the test + */ + public Failure(Description description, Throwable thrownException) { + fThrownException = thrownException; + fDescription= description; + } + + /** + * @return a user-understandable label for the test + */ + public String getTestHeader() { + return fDescription.getDisplayName(); + } + + /** + * @return the raw description of the context of the failure. + */ + public Description getDescription() { + return fDescription; + } + + /** + * @return the exception thrown + */ + + public Throwable getException() { + return fThrownException; + } + + @Override + public String toString() { + StringBuffer buffer= new StringBuffer(); + buffer.append(getTestHeader() + ": "+fThrownException.getMessage()); + return buffer.toString(); + } + + /** + * Convenience method + * @return the printed form of the exception + */ + public String getTrace() { + StringWriter stringWriter= new StringWriter(); + PrintWriter writer= new PrintWriter(stringWriter); + getException().printStackTrace(writer); + StringBuffer buffer= stringWriter.getBuffer(); + return buffer.toString(); + } + + /** + * Convenience method + * @return the message of the thrown exception + */ + public String getMessage() { + return getException().getMessage(); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/notification/RunListener.java b/students/643449856/litejunit/src/main/java/org/v3/notification/RunListener.java new file mode 100644 index 0000000000..2da301b9c4 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/notification/RunListener.java @@ -0,0 +1,53 @@ +package org.v3.notification; + + +import org.v3.runner.Description; +import org.v3.runner.Result; + +public class RunListener { + + /** + * Called before any tests have been run. + * @param description describes the tests to be run + */ + public void testRunStarted(Description description) throws Exception { + } + + /** + * Called when all tests have finished + * @param result the summary of the test run, including all the tests that failed + */ + public void testRunFinished(Result result) throws Exception { + } + + /** + * Called when an atomic test is about to be started. + * @param description the description of the test that is about to be run (generally a class and method name) + */ + public void testStarted(Description description) throws Exception { + } + + /** + * Called when an atomic test has finished, whether the test succeeds or fails. + * @param description the description of the test that just ran + */ + public void testFinished(Description description) throws Exception { + } + + /** + * Called when an atomic test fails. + * @param failure describes the test that failed and the exception that was thrown + */ + public void testFailure(Failure failure) throws Exception { + } + + /** + * Called when a test will not be run, generally because a test method is annotated with @Ignored. + * @param description describes the test that will not be run + */ + public void testIgnored(Description description) throws Exception { + } + +} + + diff --git a/students/643449856/litejunit/src/main/java/org/v3/notification/RunNotifier.java b/students/643449856/litejunit/src/main/java/org/v3/notification/RunNotifier.java new file mode 100644 index 0000000000..3566b1f46f --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/notification/RunNotifier.java @@ -0,0 +1,141 @@ +package org.v3.notification; + +import org.v3.runner.Description; +import org.v3.runner.Result; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + + + + +/** + * If you write custom runners, you may need to notify JUnit of your progress running tests. + * Do this by invoking the RunNotifier passed to your implementation of + * Runner.run(RunNotifier notifier). Future evolution of this class is likely to + * move fireTestRunStarted() and fireTestRunFinished() + * to a separate class since they should only be called once per run. + */ +public class RunNotifier { + private List fListeners= new ArrayList(); + private boolean fPleaseStop= false; + + /** Internal use only + */ + public void addListener(RunListener listener) { + fListeners.add(listener); + } + + /** Internal use only + */ + public void removeListener(RunListener listener) { + fListeners.remove(listener); + } + + private abstract class SafeNotifier { + void run() { + for (Iterator all= fListeners.iterator(); all.hasNext();) { + try { + notifyListener(all.next()); + } catch (Exception e) { + all.remove(); // Remove the offending listener first to avoid an infinite loop + fireTestFailure(new Failure(Description.TEST_MECHANISM, e)); + } + } + } + + abstract protected void notifyListener(RunListener each) throws Exception; + } + + /** + * Do not invoke. + */ + public void fireTestRunStarted(final Description description) { + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testRunStarted(description); + }; + }.run(); + } + + /** + * Do not invoke. + */ + public void fireTestRunFinished(final Result result) { + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testRunFinished(result); + }; + }.run(); + } + + /** + * Invoke to tell listeners that an atomic test is about to start. + * @param description the description of the atomic test (generally a class and method name) + * @throws StoppedByUserException thrown if a user has requested that the test run stop + */ + public void fireTestStarted(final Description description) throws StoppedByUserException { + if (fPleaseStop) + throw new StoppedByUserException(); + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testStarted(description); + }; + }.run(); + } + + /** + * Invoke to tell listeners that an atomic test failed. + * @param failure the description of the test that failed and the exception thrown + */ + public void fireTestFailure(final Failure failure) { + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testFailure(failure); + }; + }.run(); + } + + /** + * Invoke to tell listeners that an atomic test was ignored. + * @param description the description of the ignored test + */ + public void fireTestIgnored(final Description description) { + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testIgnored(description); + }; + }.run(); + } + + /** + * Invoke to tell listeners that an atomic test finished. Always invoke fireTestFinished() + * if you invoke fireTestStarted() as listeners are likely to expect them to come in pairs. + * @param description the description of the test that finished + */ + public void fireTestFinished(final Description description) { + new SafeNotifier() { + @Override + protected void notifyListener(RunListener each) throws Exception { + each.testFinished(description); + }; + }.run(); + } + + /** + * Ask that the tests run stop before starting the next test. Phrased politely because + * the test currently running will not be interrupted. It seems a little odd to put this + * functionality here, but the RunNotifier is the only object guaranteed + * to be shared amongst the many runners involved. + */ + public void pleaseStop() { + fPleaseStop= true; + } +} \ No newline at end of file diff --git a/students/643449856/litejunit/src/main/java/org/v3/notification/StoppedByUserException.java b/students/643449856/litejunit/src/main/java/org/v3/notification/StoppedByUserException.java new file mode 100644 index 0000000000..a820b0ab44 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/notification/StoppedByUserException.java @@ -0,0 +1,11 @@ +package org.v3.notification; + +/** + * Thrown when a user has requested that the test run stop. Writers of + * test running GUIs should be prepared to catch a StoppedByUserException. + * + * @see org.junit.runner.notification.RunNotifier + */ +public class StoppedByUserException extends RuntimeException { + private static final long serialVersionUID= 1L; +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/requests/ClassRequest.java b/students/643449856/litejunit/src/main/java/org/v3/requests/ClassRequest.java new file mode 100644 index 0000000000..4ac6356a71 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/requests/ClassRequest.java @@ -0,0 +1,48 @@ +/** + * + */ +package org.v3.requests; + +import java.lang.reflect.Constructor; + +import org.v3.runner.Request; +import org.v3.runner.Runner; +import org.v3.runners.TestClassRunner; + + +public class ClassRequest extends Request { + private final Class fTestClass; + + public ClassRequest(Class each) { + fTestClass= each; + } + + @Override + public Runner getRunner() { + Class runnerClass= getRunnerClass(fTestClass); + try { + Constructor constructor= runnerClass.getConstructor(Class.class); // TODO good error message if no such constructor + Runner runner= (Runner) constructor + .newInstance(new Object[] { fTestClass }); + return runner; + } catch (Exception e) { + return null; + //return Request.errorReport(fTestClass, e).getRunner(); + } + } + + Class getRunnerClass(Class testClass) { + /*RunWith annotation= testClass.getAnnotation(RunWith.class); + if (annotation != null) { + return annotation.value(); + } else if (isPre4Test(testClass)) { + return OldTestClassRunner.class; + } else {*/ + return TestClassRunner.class; + /*}*/ + } + + /*boolean isPre4Test(Class testClass) { + return junit.framework.TestCase.class.isAssignableFrom(testClass); + }*/ +} \ No newline at end of file diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/Description.java b/students/643449856/litejunit/src/main/java/org/v3/runner/Description.java new file mode 100644 index 0000000000..991d9471af --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/Description.java @@ -0,0 +1,127 @@ +package org.v3.runner; + +import java.util.ArrayList; + +/** + * A Description describes a test which is to be run or has been run. Descriptions can + * be atomic (a single test) or compound (containing children tests). Descriptions are used + * to provide feedback about the tests that are about to run (for example, the tree view + * visible in many IDEs) or tests that have been run (for example, the failures view).

+ * Descriptions are implemented as a single class rather than a Composite because + * they are entirely informational. They contain no logic aside from counting their tests.

+ * In the past, we used the raw junit.framework.TestCases and junit.framework.TestSuites + * to display the tree of tests. This was no longer viable in JUnit 4 because atomic tests no longer have a superclass below Object. + * We needed a way to pass a class and name together. Description emerged from this. + * + * @see org.junit.runner.Request + * @see org.junit.runner.Runner + */ +public class Description { + + /** + * Create a Description named name. + * Generally, you will add children to this Description. + * @param name The name of the Description + * @return A Description named name + */ + public static Description createSuiteDescription(String name) { + return new Description(name); + } + + /** + * Create a Description of a single test named name in the class clazz. + * Generally, this will be a leaf Description. + * @param clazz The class of the test + * @param name The name of the test (a method name for test annotated with @Test) + * @return A Description named name + */ + public static Description createTestDescription(Class clazz, String name) { + return new Description(String.format("%s(%s)", name, clazz.getName())); + } + + /** + * Create a generic Description that says there are tests in testClass. + * This is used as a last resort when you cannot precisely describe the individual tests in the class. + * @param testClass A Class containing tests + * @return A Description of testClass + */ + public static Description createSuiteDescription(Class testClass) { + return new Description(testClass.getName()); + } + + public static Description TEST_MECHANISM = new Description("Test mechanism"); + private final ArrayList fChildren= new ArrayList(); + private final String fDisplayName; + + //TODO we seem to be using the static factories exclusively + private Description(final String displayName) { + fDisplayName= displayName; + } + + /** + * @return a user-understandable label + */ + public String getDisplayName() { + return fDisplayName; + } + + /** + * Add description as a child of the receiver. + * @param description The soon-to-be child. + */ + public void addChild(Description description) { + getChildren().add(description); + } + + /** + * @return the receiver's children, if any + */ + public ArrayList getChildren() { + return fChildren; + } + + /** + * @return true if the receiver is a suite + */ + public boolean isSuite() { + return !isTest(); + } + + /** + * @return true if the receiver is an atomic test + */ + public boolean isTest() { + return getChildren().isEmpty(); + } + + /** + * @return the total number of atomic tests in the receiver + */ + public int testCount() { + if (isTest()) + return 1; + int result= 0; + for (Description child : getChildren()) + result+= child.testCount(); + return result; + } + + @Override + public int hashCode() { + return getDisplayName().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Description)) + return false; + Description d = (Description) obj; + return getDisplayName().equals(d.getDisplayName()) + && getChildren().equals(d.getChildren()); + } + + @Override + public String toString() { + return getDisplayName(); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/JUnitCore.java b/students/643449856/litejunit/src/main/java/org/v3/runner/JUnitCore.java new file mode 100644 index 0000000000..adb7469365 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/JUnitCore.java @@ -0,0 +1,167 @@ +package org.v3.runner; + +import java.util.ArrayList; +import java.util.List; + +import org.v3.notification.RunListener; +import org.v3.notification.RunNotifier; +import org.v3.runners.InitializationError; +import org.v3.runners.TestClassRunner; +import org.v3.runners.TextListener; + + + +/** + * JUnitCore is a facade for running tests. It supports running JUnit 4 tests, + * JUnit 3.8.2 tests, and mixtures. To run tests from the command line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 .... + * For one-shot test runs, use the static method runClasses(Class... classes) + * . If you want to add special listeners, + * create an instance of JUnitCore first and use it to run the tests. + * + * @see org.junit.runner.Result + * @see org.junit.runner.notification.RunListener + * @see org.junit.runner.Request + */ +public class JUnitCore { + + private RunNotifier notifier; + + /** + * Create a new JUnitCore to run tests. + */ + public JUnitCore() { + notifier= new RunNotifier(); + } + + /** + * Run the tests contained in the classes named in the args. + * If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. + * Write feedback while tests are running and write + * stack traces for all failed tests after the tests all complete. + * @param args names of classes in which to find tests to run + */ + /*public static void main(String... args) { + Class clz = null; + try { + clz = Class.forName(args[0]); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return; + } + + Request request = Request.aClass(clz); + + new JUnitCore().run(request); + + Result result= new JUnitCore().runMain(args); + killAllThreads(result); + }*/ + public static void runClass(Class clz){ + try { + TestClassRunner runner = new TestClassRunner(clz); + JUnitCore core = new JUnitCore(); + core.addListener(new TextListener()); + Result result = core.run(runner); + + } catch (InitializationError e) { + + e.printStackTrace(); + } + + } + /*private static void killAllThreads(Result result) { + System.exit(result.wasSuccessful() ? 0 : 1); + }*/ + + /** + * Run the tests contained in classes. Write feedback while the tests + * are running and write stack traces for all failed tests after all tests complete. This is + * similar to main(), but intended to be used programmatically. + * @param classes Classes in which to find tests + * @return a Result describing the details of the test run and the failed tests. + */ + /*public static Result runClasses(Class... classes) { + return new JUnitCore().run(classes); + }*/ + + /** + * Do not use. Testing purposes only. + */ + /*public Result runMain(String... args) { + + List classes= new ArrayList(); + for (String each : args) + try { + classes.add(Class.forName(each)); + } catch (ClassNotFoundException e) { + System.out.println("Could not find class: " + each); + } + RunListener listener= new TextListener(); + addListener(listener); + return run(classes.toArray(new Class[0])); + }*/ + + + + /** + * Run all the tests in classes. + * @param classes the classes containing tests + * @return a Result describing the details of the test run and the failed tests. + */ + /*public Result run(Class... classes) { + return run(Request.classes("All", classes)); + }*/ + + /** + * Run all the tests contained in request. + * @param request the request describing tests + * @return a Result describing the details of the test run and the failed tests. + */ + /*public Result run(Request request) { + return run(request.getRunner()); + }*/ + + /** + * Run all the tests contained in JUnit 3.8.x test. Here for backward compatibility. + * @param test the old-style test + * @return a Result describing the details of the test run and the failed tests. + */ + /*public Result run(junit.framework.Test test) { + return run(new OldTestClassRunner(test)); + } + */ + /** + * Do not use. Testing purposes only. + */ + public Result run(Runner runner) { + Result result= new Result(); + RunListener listener= result.createListener(); + addListener(listener); + + try { + notifier.fireTestRunStarted(runner.getDescription()); + runner.run(notifier); + notifier.fireTestRunFinished(result); + } finally { + removeListener(listener); + } + return result; + } + + /** + * Add a listener to be notified as the tests run. + * @param listener the listener + * @see org.junit.runner.notification.RunListener + */ + public void addListener(RunListener listener) { + notifier.addListener(listener); + } + + /** + * Remove a listener. + * @param listener the listener to remove + */ + public void removeListener(RunListener listener) { + notifier.removeListener(listener); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/Request.java b/students/643449856/litejunit/src/main/java/org/v3/runner/Request.java new file mode 100644 index 0000000000..0c99aba8f7 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/Request.java @@ -0,0 +1,86 @@ +package org.v3.runner; + +import org.v3.requests.ClassRequest; + + + +/** + * A Request is an abstract description of tests to be run. Older versions of + * JUnit did not need such a concept--tests to be run were described either by classes containing + * tests or a tree of Tests. However, we want to support filtering and sorting, + * so we need a more abstract specification than the tests themselves and a richer + * specification than just the classes. + *

+ * The flow when JUnit runs tests is that a Request specifies some tests to be run -> + * a Runner is created for each class implied by the Request -> the Runner + * returns a detailed Description which is a tree structure of the tests to be run. + */ +public abstract class Request { + /** + * Create a Request that, when processed, will run a single test. + * This is done by filtering out all other tests. This method is used to support rerunning + * single tests. + * @param clazz the class of the test + * @param methodName the name of the test + * @return a Request that will cause a single test be run + */ + /*public static Request method(Class clazz, String methodName) { + Description method= Description.createTestDescription(clazz, methodName); + return Request.aClass(clazz).filterWith(method); + }*/ + + /** + * Create a Request that, when processed, will run all the tests + * in a class. The odd name is necessary because class is a reserved word. + * @param clazz the class containing the tests + * @return a Request that will cause all tests in the class to be run + */ + public static Request aClass(Class clazz) { + return new ClassRequest(clazz); + } + + /** + * Create a Request that, when processed, will run all the tests + * in a set of classes. + * @param collectionName a name to identify this suite of tests + * @param classes the classes containing the tests + * @return a Request that will cause all tests in the classes to be run + */ + /*public static Request classes(String collectionName, Class... classes) { + return new ClassesRequest(collectionName, classes); + }*/ + + /*public static Request errorReport(Class klass, Throwable cause) { + return new ErrorReportingRequest(klass, cause); + }*/ + + public abstract Runner getRunner(); + + /*public Request filterWith(Filter filter) { + return new FilterRequest(this, filter); + } + + public Request filterWith(final Description desiredDescription) { + return filterWith(new Filter() { + @Override + public boolean shouldRun(Description description) { + // TODO: test for equality even if we have children? + if (description.isTest()) + return desiredDescription.equals(description); + for (Description each : description.getChildren()) + if (shouldRun(each)) + return true; + return false; + } + + @Override + public String describe() { + return String.format("Method %s", desiredDescription.getDisplayName()); + } + }); + } + + public Request sortWith(Comparator comparator) { + return new SortingRequest(this, comparator); + }*/ +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/Result.java b/students/643449856/litejunit/src/main/java/org/v3/runner/Result.java new file mode 100644 index 0000000000..d31f986b68 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/Result.java @@ -0,0 +1,98 @@ +package org.v3.runner; + +import java.util.ArrayList; +import java.util.List; + +import org.v3.notification.Failure; +import org.v3.notification.RunListener; + + +/** + * A Result collects and summarizes information from running multiple + * tests. Since tests are expected to run correctly, successful tests are only noted in + * the count of tests that ran. + */ +public class Result { + private int fCount= 0; + private int fIgnoreCount= 0; + private List fFailures= new ArrayList(); + private long fRunTime= 0; + private long fStartTime; + + /** + * @return the number of tests run + */ + public int getRunCount() { + return fCount; + } + + /** + * @return the number of tests that failed during the run + */ + public int getFailureCount() { + return fFailures.size(); + } + + /** + * @return the number of milliseconds it took to run the entire suite to run + */ + public long getRunTime() { + return fRunTime; + } + + /** + * @return the Failures describing tests that failed and the problems they encountered + */ + public List getFailures() { + return fFailures; + } + + /** + * @return the number of tests ignored during the run + */ + public int getIgnoreCount() { + return fIgnoreCount; + } + + /** + * @return true if all tests succeeded + */ + public boolean wasSuccessful() { + return getFailureCount() == 0; + } + + private class Listener extends RunListener { + @Override + public void testRunStarted(Description description) throws Exception { + fStartTime= System.currentTimeMillis(); + } + + @Override + public void testRunFinished(Result result) throws Exception { + long endTime= System.currentTimeMillis(); + fRunTime+= endTime - fStartTime; + } + + @Override + public void testStarted(Description description) throws Exception { + fCount++; + } + + @Override + public void testFailure(Failure failure) throws Exception { + fFailures.add(failure); + } + + @Override + public void testIgnored(Description description) throws Exception { + fIgnoreCount++; + } + } + + /** + * Internal use only. + */ + public RunListener createListener() { + return new Listener(); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/ResultPrinter.java b/students/643449856/litejunit/src/main/java/org/v3/runner/ResultPrinter.java new file mode 100644 index 0000000000..af5f10dda7 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/ResultPrinter.java @@ -0,0 +1,72 @@ +package org.v3.runner; + +import java.io.PrintStream; +import java.text.NumberFormat; + +import org.v3.notification.Failure; + +public class ResultPrinter { + + private final PrintStream fWriter; + + public ResultPrinter(){ + fWriter = System.out; + } + + public void print(Result result) { + printHeader(result.getRunTime()); + printFailures(result); + printFooter(result); + } + protected void printHeader(long runTime) { + getWriter().println(); + getWriter().println("Time: " + elapsedTimeAsString(runTime)); + } + + protected void printFailures(Result result) { + if (result.getFailureCount() == 0) + return; + if (result.getFailureCount() == 1) + getWriter().println("There was " + result.getFailureCount() + " failure:"); + else + getWriter().println("There were " + result.getFailureCount() + " failures:"); + int i= 1; + for (Failure each : result.getFailures()) + printFailure(each, i++); + } + + protected void printFailure(Failure failure, int count) { + printFailureHeader(failure, count); + printFailureTrace(failure); + } + + protected void printFailureHeader(Failure failure, int count) { + getWriter().println(count + ") " + failure.getTestHeader()); + } + + protected void printFailureTrace(Failure failure) { + getWriter().print(failure.getTrace()); + } + + protected void printFooter(Result result) { + if (result.wasSuccessful()) { + getWriter().println(); + getWriter().print("OK"); + getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")"); + + } else { + getWriter().println(); + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: " + result.getRunCount() + ", Failures: " + result.getFailureCount()); + } + getWriter().println(); + } + protected String elapsedTimeAsString(long runTime) { + return NumberFormat.getInstance().format((double) runTime / 1000); + } + private PrintStream getWriter() { + return fWriter; + } + + +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/RunWith.java b/students/643449856/litejunit/src/main/java/org/v3/runner/RunWith.java new file mode 100644 index 0000000000..57f5f4f640 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/RunWith.java @@ -0,0 +1,24 @@ +package org.v3.runner; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +//TODO add simple exampel +/** + * When you annotate a class with @RunWith, JUnit will invoke + * the class it references to run the tests in that class instead of the runner + * built into JUnit. We added this feature late in development. While it + * seems powerful we expect the runner API to change as we learn how people + * really use it. Some of the classes that are currently internal will likely be refined + * and become public. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface RunWith { + /** + * @return a Runner class (must have a constructor that takes a single Class to run) + */ + Class value(); +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runner/Runner.java b/students/643449856/litejunit/src/main/java/org/v3/runner/Runner.java new file mode 100644 index 0000000000..1b1afc80a8 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runner/Runner.java @@ -0,0 +1,24 @@ +package org.v3.runner; + +import org.v3.notification.RunNotifier; + + +public abstract class Runner { + /** + * @return a Description showing the tests to be run by the receiver + */ + public abstract Description getDescription(); + + /** + * Run the tests for this runner. + * @param notifier will be notified of events while tests are being run--tests being started, finishing, and failing + */ + public abstract void run(RunNotifier notifier); + + /** + * @return the number of tests to be run by the receiver + */ + public int testCount() { + return getDescription().testCount(); + } +} \ No newline at end of file diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/BeforeAndAfterRunner.java b/students/643449856/litejunit/src/main/java/org/v3/runners/BeforeAndAfterRunner.java new file mode 100644 index 0000000000..e23a1e7cca --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/BeforeAndAfterRunner.java @@ -0,0 +1,75 @@ +package org.v3.runners; + +import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.List; + +public abstract class BeforeAndAfterRunner { + private static class FailedBefore extends Exception { + private static final long serialVersionUID= 1L; + } + + private final Class beforeAnnotation; + + private final Class afterAnnotation; + + private TestIntrospector testIntrospector; + + private Object test; + + public BeforeAndAfterRunner(Class testClass, + Class beforeAnnotation, + Class afterAnnotation, + Object test) { + this.beforeAnnotation= beforeAnnotation; + this.afterAnnotation= afterAnnotation; + this.testIntrospector= new TestIntrospector(testClass); + this.test= test; + } + + public void runProtected() { + try { + runBefores(); + runUnprotected(); + } catch (FailedBefore e) { + } finally { + runAfters(); + } + } + + protected abstract void runUnprotected(); + + protected abstract void addFailure(Throwable targetException); + + // Stop after first failed @Before + private void runBefores() throws FailedBefore { + try { + List befores= testIntrospector.getTestMethods(beforeAnnotation); + for (Method before : befores) + invokeMethod(before); + } catch (InvocationTargetException e) { + addFailure(e.getTargetException()); + throw new FailedBefore(); + } catch (Throwable e) { + addFailure(e); + throw new FailedBefore(); + } + } + + private void runAfters() { + List afters= testIntrospector.getTestMethods(afterAnnotation); + for (Method after : afters) + try { + invokeMethod(after); + } catch (InvocationTargetException e) { + addFailure(e.getTargetException()); + } catch (Throwable e) { + addFailure(e); // Untested, but seems impossible + } + } + + private void invokeMethod(Method method) throws Exception { + method.invoke(test); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/InitializationError.java b/students/643449856/litejunit/src/main/java/org/v3/runners/InitializationError.java new file mode 100644 index 0000000000..a7b4bf87fc --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/InitializationError.java @@ -0,0 +1,25 @@ +package org.v3.runners; + +import java.util.Arrays; +import java.util.List; + +public class InitializationError extends Exception { + private static final long serialVersionUID= 1L; + private final List fErrors; + + public InitializationError(List errors) { + fErrors= errors; + } + + public InitializationError(Throwable... errors) { + this(Arrays.asList(errors)); + } + + public InitializationError(String string) { + this(new Exception(string)); + } + + public List getCauses() { + return fErrors; + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassMethodsRunner.java b/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassMethodsRunner.java new file mode 100644 index 0000000000..0ff73d816d --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassMethodsRunner.java @@ -0,0 +1,78 @@ +package org.v3.runners; + +import java.lang.reflect.Method; +import java.util.List; + +import org.v3.Test; +import org.v3.notification.Failure; +import org.v3.notification.RunNotifier; +import org.v3.runner.Description; +import org.v3.runner.Runner; + + + +public class TestClassMethodsRunner extends Runner { + private final List testMethods; + private final Class testClass; + + public TestClassMethodsRunner(Class klass) { + testClass= klass; + testMethods= new TestIntrospector(testClass).getTestMethods(Test.class); + } + + @Override + public void run(RunNotifier notifier) { + /*if (testMethods.isEmpty()) + testAborted(notifier, getDescription());*/ + for (Method method : testMethods) + invokeTestMethod(method, notifier); + } + + + + @Override + public Description getDescription() { + Description spec= Description.createSuiteDescription(getName()); + List testMethods= this.testMethods; + for (Method method : testMethods) + spec.addChild(methodDescription(method)); + return spec; + } + + protected String getName() { + return getTestClass().getName(); + } + + protected Object createTest() throws Exception { + return getTestClass().getConstructor().newInstance(); + } + + protected void invokeTestMethod(Method method, RunNotifier notifier) { + Object test; + try { + test= createTest(); + } catch (Exception e) { + //testAborted(notifier, methodDescription(method)); + return; + } + createMethodRunner(test, method, notifier).run(); + } + + protected TestMethodRunner createMethodRunner(Object test, Method method, RunNotifier notifier) { + return new TestMethodRunner(test, method, notifier, methodDescription(method)); + } + + protected String testName(Method method) { + return method.getName(); + } + + protected Description methodDescription(Method method) { + return Description.createTestDescription(getTestClass(), testName(method)); + } + + + + protected Class getTestClass() { + return testClass; + } +} \ No newline at end of file diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassRunner.java b/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassRunner.java new file mode 100644 index 0000000000..c79ec0ffdb --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/TestClassRunner.java @@ -0,0 +1,53 @@ +package org.v3.runners; + +import org.v3.AfterClass; +import org.v3.BeforeClass; +import org.v3.notification.Failure; +import org.v3.notification.RunNotifier; +import org.v3.runner.Description; +import org.v3.runner.Runner; + +public class TestClassRunner extends Runner { + protected final Runner enclosedRunner; + private final Class testClass; + + public TestClassRunner(Class klass) throws InitializationError { + this(klass, new TestClassMethodsRunner(klass)); + } + + public TestClassRunner(Class klass, Runner runner) throws InitializationError { + testClass= klass; + enclosedRunner= runner; + + } + + + + @Override + public void run(final RunNotifier notifier) { + BeforeAndAfterRunner runner = new BeforeAndAfterRunner(getTestClass(), + BeforeClass.class, AfterClass.class, null) { + @Override + protected void runUnprotected() { + enclosedRunner.run(notifier); + } + + // TODO: looks very similar to other method of BeforeAfter, now + @Override + protected void addFailure(Throwable targetException) { + notifier.fireTestFailure(new Failure(getDescription(), targetException)); + } + }; + + runner.runProtected(); + } + + @Override + public Description getDescription() { + return enclosedRunner.getDescription(); + } + + protected Class getTestClass() { + return testClass; + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/TestIntrospector.java b/students/643449856/litejunit/src/main/java/org/v3/runners/TestIntrospector.java new file mode 100644 index 0000000000..ba51b21c9b --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/TestIntrospector.java @@ -0,0 +1,74 @@ +package org.v3.runners; + + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.v3.Before; +import org.v3.BeforeClass; +import org.v3.Ignore; +import org.v3.Test; +import org.v3.Test.None; + + + +public class TestIntrospector { + private final Class< ?> testClass; + + public TestIntrospector(Class testClass) { + this.testClass= testClass; + } + + public List getTestMethods(Class annotationClass) { + List results= new ArrayList(); + + //for (Class eachClass : getSuperClasses(testClass)) { + Method[] methods= testClass.getDeclaredMethods(); + for (Method method : methods) { + Annotation annotation= method.getAnnotation(annotationClass); + if (annotation != null && ! isShadowed(method, results)) + results.add(method); + } + //} + if (runsTopToBottom(annotationClass)) + Collections.reverse(results); + return results; + } + + public boolean isIgnored(Method eachMethod) { + return eachMethod.getAnnotation(Ignore.class) != null; + } + + private boolean runsTopToBottom(Class< ? extends Annotation> annotation) { + return annotation.equals(Before.class) || annotation.equals(BeforeClass.class); + } + + private boolean isShadowed(Method method, List results) { + for (Method m : results) { + if (m.getName().equals(method.getName())) + return true; + } + return false; + } + + + + long getTimeout(Method method) { + Test annotation= method.getAnnotation(Test.class); + long timeout= annotation.timeout(); + return timeout; + } + + Class expectedException(Method method) { + Test annotation= method.getAnnotation(Test.class); + if (annotation.expected() == None.class) + return null; + else + return annotation.expected(); + } + +} + diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/TestMethodRunner.java b/students/643449856/litejunit/src/main/java/org/v3/runners/TestMethodRunner.java new file mode 100644 index 0000000000..777c98cb1c --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/TestMethodRunner.java @@ -0,0 +1,86 @@ +package org.v3.runners; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + + +import org.v3.After; +import org.v3.Before; +import org.v3.notification.Failure; +import org.v3.notification.RunNotifier; +import org.v3.runner.Description; + + + +public class TestMethodRunner extends BeforeAndAfterRunner { + private final Object test; + private final Method method; + private final RunNotifier notifier; + private final TestIntrospector testIntrospector; + private final Description description; + + public TestMethodRunner(Object test, Method method, RunNotifier notifier, Description description) { + super(test.getClass(), Before.class, After.class, test); + this.test= test; + this.method= method; + this.notifier= notifier; + testIntrospector= new TestIntrospector(test.getClass()); + this.description= description; + } + + public void run() { + /*if (testIntrospector.isIgnored(method)) { + notifier.fireTestIgnored(description); + return; + }*/ + notifier.fireTestStarted(description); + try { + /*long timeout= testIntrospector.getTimeout(method); + if (timeout > 0) + runWithTimeout(timeout); + else*/ + runMethod(); + } finally { + notifier.fireTestFinished(description); + } + } + + + + private void runMethod() { + runProtected(); + } + + @Override + protected void runUnprotected() { + try { + executeMethodBody(); + /*if (expectsException()) + addFailure(new AssertionError("Expected exception: " + expectedException().getName()));*/ + } catch (InvocationTargetException e) { + addFailure(e); + /*Throwable actual= e.getTargetException(); + if (!expectsException()) + addFailure(actual); + else if (isUnexpected(actual)) { + String message= "Unexpected exception, expected<" + expectedException().getName() + "> but was<" + + actual.getClass().getName() + ">"; + addFailure(new Exception(message, actual)); + }*/ + } catch (Throwable e) { + addFailure(e); + } + } + + protected void executeMethodBody() throws IllegalAccessException, InvocationTargetException { + method.invoke(test); + } + + @Override + protected void addFailure(Throwable e) { + notifier.fireTestFailure(new Failure(description, e)); + } + + +} + diff --git a/students/643449856/litejunit/src/main/java/org/v3/runners/TextListener.java b/students/643449856/litejunit/src/main/java/org/v3/runners/TextListener.java new file mode 100644 index 0000000000..b52d6fc024 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/runners/TextListener.java @@ -0,0 +1,103 @@ +package org.v3.runners; + +import java.io.PrintStream; +import java.text.NumberFormat; + +import org.v3.notification.Failure; +import org.v3.notification.RunListener; +import org.v3.runner.Description; +import org.v3.runner.Result; + + + +public class TextListener extends RunListener { + + private final PrintStream writer; + + public TextListener() { + this(System.out); + } + + public TextListener(PrintStream writer) { + this.writer= writer; + } + + @Override + public void testRunFinished(Result result) { + printHeader(result.getRunTime()); + printFailures(result); + printFooter(result); + } + + @Override + public void testStarted(Description description) { + writer.append('.'); + } + + @Override + public void testFailure(Failure failure) { + writer.append('E'); + } + + @Override + public void testIgnored(Description description) { + writer.append('I'); + } + + /* + * Internal methods + */ + + private PrintStream getWriter() { + return writer; + } + + protected void printHeader(long runTime) { + getWriter().println(); + getWriter().println("Time: " + elapsedTimeAsString(runTime)); + } + + protected void printFailures(Result result) { + if (result.getFailureCount() == 0) + return; + if (result.getFailureCount() == 1) + getWriter().println("There was " + result.getFailureCount() + " failure:"); + else + getWriter().println("There were " + result.getFailureCount() + " failures:"); + int i= 1; + for (Failure each : result.getFailures()) + printFailure(each, i++); + } + + protected void printFailure(Failure failure, int count) { + printFailureHeader(failure, count); + printFailureTrace(failure); + } + + protected void printFailureHeader(Failure failure, int count) { + getWriter().println(count + ") " + failure.getTestHeader()); + } + + protected void printFailureTrace(Failure failure) { + getWriter().print(failure.getTrace()); + } + + protected void printFooter(Result result) { + if (result.wasSuccessful()) { + getWriter().println(); + getWriter().print("OK"); + getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")"); + + } else { + getWriter().println(); + getWriter().println("FAILURES!!!"); + getWriter().println("Tests run: " + result.getRunCount() + ", Failures: " + result.getFailureCount()); + } + getWriter().println(); + } + + + protected String elapsedTimeAsString(long runTime) { + return NumberFormat.getInstance().format((double) runTime / 1000); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/sample/Calculator.java b/students/643449856/litejunit/src/main/java/org/v3/sample/Calculator.java new file mode 100644 index 0000000000..7bd3759dc1 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/sample/Calculator.java @@ -0,0 +1,22 @@ +package org.v3.sample; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} diff --git a/students/643449856/litejunit/src/main/java/org/v3/sample/CalculatorTest.java b/students/643449856/litejunit/src/main/java/org/v3/sample/CalculatorTest.java new file mode 100644 index 0000000000..0dbbaf7cc5 --- /dev/null +++ b/students/643449856/litejunit/src/main/java/org/v3/sample/CalculatorTest.java @@ -0,0 +1,52 @@ +package org.v3.sample; + +import org.v3.After; +import org.v3.AfterClass; +import org.v3.Before; +import org.v3.BeforeClass; +import org.v3.Test; +import org.v3.runner.JUnitCore; +import static org.v3.Assert.assertEquals; + +/** + * @author neng + * + */ +public class CalculatorTest { + + Calculator calculator =null; + @Before + public void prepare(){ + calculator = new Calculator(); + } + @After + public void clean(){ + calculator = null; + } + @Test + public void testAdd(){ + + calculator.add(10); + assertEquals(15,calculator.getResult()); + } + @Test + public void testSubtract(){ + calculator.add(10); + calculator.subtract(5); + assertEquals(5,calculator.getResult()); + } + @BeforeClass + public static void prepareGlobalResouce(){ + System.err.println("准备所有的资源"); + } + @AfterClass + public static void cleanGlobalResouce(){ + System.err.println("清楚所有的资源"); + } + + + public static void main(String[] args){ + JUnitCore.runClass(CalculatorTest.class); + + } +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java index f328c1816a..16fab6a29c 100644 --- a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Configuration.java @@ -4,7 +4,17 @@ public class Configuration { - static Map configurations = new HashMap<>(); + protected String smtpHost = null; + protected String altSmtpHost = null; + protected String fromAddress = null; + + public Configuration() { + this.smtpHost = ConfigurationKeys.SMTP_SERVER; + this.altSmtpHost = ConfigurationKeys.ALT_SMTP_SERVER; + this.fromAddress = ConfigurationKeys.EMAIL_ADMIN; + } + + static Map configurations = new HashMap(); static{ configurations.put(ConfigurationKeys.SMTP_SERVER, "smtp.163.com"); configurations.put(ConfigurationKeys.ALT_SMTP_SERVER, "smtp1.163.com"); @@ -16,7 +26,7 @@ public class Configuration { * @return */ public String getProperty(String key) { - + return configurations.get(key); } diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java index 82e9261d18..20f1e74cfe 100644 --- a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/DBUtil.java @@ -4,6 +4,8 @@ import java.util.List; public class DBUtil { + + private static String sendMailQuery = null; /** * 应该从数据库读, 但是简化为直接生成。 @@ -22,4 +24,23 @@ public static List query(String sql){ return userList; } + + public static String setLoadQuery(String productID) throws Exception { + + + sendMailQuery = "Select name from subscriptions " + + "where product_id= '" + productID +"' " + + "and send_mail=1 "; + + + System.out.println("loadQuery set"); + return sendMailQuery; + } + + + static List loadMailingList() throws Exception { + return DBUtil.query(sendMailQuery); + } + + } diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java new file mode 100644 index 0000000000..0ebb9c014e --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/FileUtil.java @@ -0,0 +1,35 @@ +package com.coderising.ood.srp; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * Created by nengneng on 2017/6/19. + */ +public class FileUtil { + + + static void readFile(File file,Product product) throws IOException // @02C + { + BufferedReader br = null; + try { + br = new BufferedReader(new FileReader(file)); + String temp = br.readLine(); + String[] data = temp.split(" "); + + product.setProductID(data[0]); + product.setProductDesc(data[1]); + + System.out.println("产品ID = " + product.getProductID() + "\n"); + System.out.println("产品描述 = " + product.getProductDesc() + "\n"); + + } catch (IOException e) { + throw new IOException(e.getMessage()); + } finally { + br.close(); + } + } + +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java new file mode 100644 index 0000000000..e7ad19ce67 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Mail.java @@ -0,0 +1,87 @@ +package com.coderising.ood.srp; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + +/** + * Created by nengneng on 2017/6/19. + */ +public class Mail { + + private String fromAddress; + private String toAddress; + private String subject; + private String message; + + + private static final String NAME_KEY = "NAME"; + private static final String EMAIL_KEY = "EMAIL"; + + public Mail(){ + + } + + public Mail(String fromAddress, String toAddress, String subject, String message) { + this.fromAddress = fromAddress; + this.toAddress = toAddress; + this.subject = subject; + this.message = message; + } + + public static String getNameKey() { + return NAME_KEY; + } + + public static String getEmailKey() { + return EMAIL_KEY; + } + + public String getFromAddress() { + return fromAddress; + } + + public void setFromAddress(String fromAddress) { + this.fromAddress = fromAddress; + } + + public String getToAddress() { + return toAddress; + } + + public void setToAddress(String toAddress) { + this.toAddress = toAddress; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + private void setMessage(HashMap userInfo, Product product) throws IOException + { + String name = (String) userInfo.get(NAME_KEY); + this.subject = "您关注的产品降价了"; + this.message = "尊敬的 "+name+", 您关注的产品 " + product.getProductDesc() + " 降价了,欢迎购买!" ; + } + + + + + + + + +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java index 9f9e749af7..453707bbf0 100644 --- a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/MailUtil.java @@ -1,18 +1,84 @@ package com.coderising.ood.srp; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; + public class MailUtil { - public static void sendEmail(String toAddress, String fromAddress, String subject, String message, String smtpHost, - boolean debug) { + + + + public static void sendEmail(Mail mail,String smtpHost,HashMap userInfo, + Product product, boolean debug) throws IOException { + mail.setMessage(userInfo, product); + String email = encapsulation(mail); + System.out.println(email.toString()); + } + + private static String encapsulation(Mail mail) { //假装发了一封邮件 StringBuilder buffer = new StringBuilder(); - buffer.append("From:").append(fromAddress).append("\n"); - buffer.append("To:").append(toAddress).append("\n"); - buffer.append("Subject:").append(subject).append("\n"); - buffer.append("Content:").append(message).append("\n"); - System.out.println(buffer.toString()); - + buffer.append("From:").append(mail.getFromAddress()).append("\n"); + buffer.append("To:").append(mail.getToAddress()).append("\n"); + buffer.append("Subject:").append(mail.getSubject()).append("\n"); + buffer.append("Content:").append(mail.getMessage()).append("\n"); + return buffer.toString(); + } - + public void configureEMail(HashMap userInfo,Mail mail) throws IOException + { + mail.getToAddress() = (String) userInfo.get(Mail.getEmailKey()); + if (toAddress.length() > 0) + setMessage(userInfo.toString()); + } + + + protected void sendEMails(boolean debug, List mailingList) throws IOException + { + + System.out.println("开始发送邮件"); + + + if (mailingList != null) { + + send(mailingList); + + } + + else { + System.out.println("没有邮件发送"); + + } + + } + + public void send(List mailingList,Mail mail){ + Iterator iter = mailingList.iterator(); + while (iter.hasNext()) { + Mail.configureEMail((HashMap) iter.next()); + try + { + if (toAddress.length() > 0) + MailUtil.sendEmail(mail, smtpHost, debug); + } + catch (Exception e) + { + + try { + MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); + + } catch (Exception e2) + { + System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); + } + } + } + + + } + + } diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java new file mode 100644 index 0000000000..c13f0cc294 --- /dev/null +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/Product.java @@ -0,0 +1,34 @@ +package com.coderising.ood.srp; + +/** + * Created by nengneng on 2017/6/19. + */ +public class Product { + + private String productID; + private String productDesc; + + public Product(String productID, String productDesc) { + this.productID = productID; + this.productDesc = productDesc; + } + + public Product() { + } + + public String getProductID() { + return productID; + } + + public void setProductID(String productID) { + this.productID = productID; + } + + public String getProductDesc() { + return productDesc; + } + + public void setProductDesc(String productDesc) { + this.productDesc = productDesc; + } +} diff --git a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java index 781587a846..7670f87c29 100644 --- a/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java +++ b/students/643449856/ood-assignment/src/main/java/com/coderising/ood/srp/PromotionMail.java @@ -10,27 +10,11 @@ import java.util.List; public class PromotionMail { - - - protected String sendMailQuery = null; - - - protected String smtpHost = null; - protected String altSmtpHost = null; - protected String fromAddress = null; - protected String toAddress = null; - protected String subject = null; - protected String message = null; - - protected String productID = null; - protected String productDesc = null; - private static Configuration config; - private static final String NAME_KEY = "NAME"; - private static final String EMAIL_KEY = "EMAIL"; + public static void main(String[] args) throws Exception { @@ -43,24 +27,13 @@ public static void main(String[] args) throws Exception { } - public PromotionMail(File file, boolean mailDebug) throws Exception { + public PromotionMail(File file, Product product,boolean mailDebug) throws Exception { //读取配置文件, 文件中只有一行用空格隔开, 例如 P8756 iPhone8 - readFile(file); - - + FileUtil.readFile(file, product); config = new Configuration(); - - setSMTPHost(); - setAltSMTPHost(); - - - setFromAddress(); - - - setLoadQuery(); - - sendEMails(mailDebug, loadMailingList()); + DBUtil.setLoadQuery(product.getProductID()); + sendEMails(mailDebug, DBUtil.loadMailingList()); } @@ -68,132 +41,21 @@ public PromotionMail(File file, boolean mailDebug) throws Exception { - protected void setProductID(String productID) - { - this.productID = productID; - - } - protected String getproductID() - { - return productID; - } - protected void setLoadQuery() throws Exception { - - sendMailQuery = "Select name from subscriptions " - + "where product_id= '" + productID +"' " - + "and send_mail=1 "; - - - System.out.println("loadQuery set"); - } - - protected void setSMTPHost() - { - smtpHost = config.getProperty(ConfigurationKeys.SMTP_SERVER); - } - - protected void setAltSMTPHost() - { - altSmtpHost = config.getProperty(ConfigurationKeys.ALT_SMTP_SERVER); - } - - protected void setFromAddress() - { - fromAddress = config.getProperty(ConfigurationKeys.EMAIL_ADMIN); - } - protected void setMessage(HashMap userInfo) throws IOException - { - - String name = (String) userInfo.get(NAME_KEY); - - subject = "您关注的产品降价了"; - message = "尊敬的 "+name+", 您关注的产品 " + productDesc + " 降价了,欢迎购买!" ; - - - } - protected void readFile(File file) throws IOException // @02C - { - BufferedReader br = null; - try { - br = new BufferedReader(new FileReader(file)); - String temp = br.readLine(); - String[] data = temp.split(" "); - - setProductID(data[0]); - setProductDesc(data[1]); - - System.out.println("产品ID = " + productID + "\n"); - System.out.println("产品描述 = " + productDesc + "\n"); - - } catch (IOException e) { - throw new IOException(e.getMessage()); - } finally { - br.close(); - } - } - private void setProductDesc(String desc) { - this.productDesc = desc; - } - protected void configureEMail(HashMap userInfo) throws IOException - { - toAddress = (String) userInfo.get(EMAIL_KEY); - if (toAddress.length() > 0) - setMessage(userInfo); - } - protected List loadMailingList() throws Exception { - return DBUtil.query(this.sendMailQuery); - } - - - protected void sendEMails(boolean debug, List mailingList) throws IOException - { - System.out.println("开始发送邮件"); - - if (mailingList != null) { - Iterator iter = mailingList.iterator(); - while (iter.hasNext()) { - configureEMail((HashMap) iter.next()); - try - { - if (toAddress.length() > 0) - MailUtil.sendEmail(toAddress, fromAddress, subject, message, smtpHost, debug); - } - catch (Exception e) - { - - try { - MailUtil.sendEmail(toAddress, fromAddress, subject, message, altSmtpHost, debug); - - } catch (Exception e2) - { - System.out.println("通过备用 SMTP服务器发送邮件失败: " + e2.getMessage()); - } - } - } - - - } - - else { - System.out.println("没有邮件发送"); - - } - } } diff --git a/students/643449856/payrolla/pom.xml b/students/643449856/payrolla/pom.xml new file mode 100644 index 0000000000..cac49a5328 --- /dev/null +++ b/students/643449856/payrolla/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + com.coderising + ood-assignment + 0.0.1-SNAPSHOT + jar + + ood-assignment + http://maven.apache.org + + + UTF-8 + + + + + + junit + junit + 4.12 + + + + + + aliyunmaven + http://maven.aliyun.com/nexus/content/groups/public/ + + + diff --git a/students/643449856/payrolla/src/main/java/com/Affiliation/NonAffiliation.java b/students/643449856/payrolla/src/main/java/com/Affiliation/NonAffiliation.java new file mode 100644 index 0000000000..07d0b186a4 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Affiliation/NonAffiliation.java @@ -0,0 +1,20 @@ +package com.Affiliation; + +import com.pojo.Affiliation; +import com.pojo.Paycheck; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:20 + * + * 没有联系 + */ +public class NonAffiliation implements Affiliation { + + + @Override + public double calculateDeductions(Paycheck pc) { + return 0.0; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Affiliation/UnionAffiliation.java b/students/643449856/payrolla/src/main/java/com/Affiliation/UnionAffiliation.java new file mode 100644 index 0000000000..cb22e697f3 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Affiliation/UnionAffiliation.java @@ -0,0 +1,18 @@ +package com.Affiliation; + +import com.pojo.Affiliation; +import com.pojo.Paycheck; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:21 + */ +public class UnionAffiliation implements Affiliation { + + + @Override + public double calculateDeductions(Paycheck pc) { + return 0; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Classification/CommissionedClassification.java b/students/643449856/payrolla/src/main/java/com/Classification/CommissionedClassification.java new file mode 100644 index 0000000000..6979d8a513 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Classification/CommissionedClassification.java @@ -0,0 +1,46 @@ +package com.Classification; + +import com.pojo.Paycheck; +import com.pojo.PaymentClassification; +import com.util.DateUtil; +import com.pojo.SalesReceipt; + +import java.util.Date; +import java.util.Map; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:22 + * + * 销售 + */ +public class CommissionedClassification implements PaymentClassification { + + double salary; + double rate; + + Map receipts; + + + + + public CommissionedClassification(double salary , double rate){ + this.salary = salary; + this.rate = rate; + } + + + + @Override + public double calculatePay(Paycheck pc) { + double commission = 0.0; + for(SalesReceipt sr : receipts.values()){ + if(DateUtil.between(sr.getSaleDate(), pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate())){ + commission += sr.getAmount() * rate; + } + } + return salary + commission; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Classification/HourlyClassification.java b/students/643449856/payrolla/src/main/java/com/Classification/HourlyClassification.java new file mode 100644 index 0000000000..d3de75ce52 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Classification/HourlyClassification.java @@ -0,0 +1,59 @@ +package com.Classification; + +import com.pojo.Paycheck; +import com.pojo.PaymentClassification; +import com.pojo.TimeCard; +import com.util.DateUtil; + +import java.util.Date; +import java.util.Map; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:39 + *

+ * 小时工 + */ +public class HourlyClassification implements PaymentClassification { + + + private double rate; + + private Map timeCards; + + + + public HourlyClassification(double hourlyRate) { + this.rate = hourlyRate; + } + + @Override + + public double calculatePay(Paycheck pc) { + + double totalPay = 0; + for (TimeCard t : timeCards.values()) { + if(DateUtil.between(t.getDate(), pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate())){ + totalPay += calculatePayForTimeCard(t); + } + } + return 0; + } + + public void addTimeCard(TimeCard tc){ + timeCards.put(tc.getDate(), tc); + } + + + private double calculatePayForTimeCard(TimeCard tc) { + int hours = tc.getHours(); + + if(hours > 8){ + return 8*rate + (hours-8) * rate * 1.5; + } else{ + return 8*rate; + } + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Classification/SalariedClassification.java b/students/643449856/payrolla/src/main/java/com/Classification/SalariedClassification.java new file mode 100644 index 0000000000..6abda7048d --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Classification/SalariedClassification.java @@ -0,0 +1,25 @@ +package com.Classification; + +import com.pojo.Paycheck; +import com.pojo.PaymentClassification; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:57 + * + * 雇佣工 + */ +public class SalariedClassification implements PaymentClassification { + + private double salary; + public SalariedClassification(double salary){ + this.salary = salary; + } + + + @Override + public double calculatePay(Paycheck pc) { + return salary; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/PaymentMethod/BankMethod.java b/students/643449856/payrolla/src/main/java/com/PaymentMethod/BankMethod.java new file mode 100644 index 0000000000..92622b437a --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/PaymentMethod/BankMethod.java @@ -0,0 +1,21 @@ +package com.PaymentMethod; + +import com.pojo.Paycheck; +import com.pojo.PaymentMethod; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:18 + * + * 从银行直接取 + * + */ +public class BankMethod implements PaymentMethod { + + + @Override + public void pay(Paycheck pc) { + + } +} diff --git a/students/643449856/payrolla/src/main/java/com/PaymentMethod/HoldMethod.java b/students/643449856/payrolla/src/main/java/com/PaymentMethod/HoldMethod.java new file mode 100644 index 0000000000..b0120bab7e --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/PaymentMethod/HoldMethod.java @@ -0,0 +1,24 @@ +package com.PaymentMethod; + +import com.pojo.Paycheck; +import com.pojo.PaymentMethod; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:18 + * + * 从银行直接取 + * + */ +public class HoldMethod implements PaymentMethod { + + + @Override + public void pay(Paycheck pc) { + System.out.println("工资保存在财务那,可随时支取"); + System.out.println("应付" + pc.getGrossPay()); + System.out.println("扣除" + pc.getDeductions()); + System.out.println("实付" + pc.getNetPay()); + } +} diff --git a/students/643449856/payrolla/src/main/java/com/PaymentMethod/MailMethod.java b/students/643449856/payrolla/src/main/java/com/PaymentMethod/MailMethod.java new file mode 100644 index 0000000000..dffcedcf03 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/PaymentMethod/MailMethod.java @@ -0,0 +1,30 @@ +package com.PaymentMethod; + +import com.pojo.Paycheck; +import com.pojo.PaymentMethod; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:18 + * + * 从银行直接取 + * + */ +public class MailMethod implements PaymentMethod { + private String address = ""; + + + public MailMethod(String address) { + super(); + this.address = address; + } + + @Override + public void pay(Paycheck pc) { + System.out.println("已将支票邮寄到"+address); + System.out.println("应付" + pc.getGrossPay()); + System.out.println("扣除" + pc.getDeductions()); + System.out.println("实付" + pc.getNetPay()); + } +} diff --git a/students/643449856/payrolla/src/main/java/com/PayrollService.java b/students/643449856/payrolla/src/main/java/com/PayrollService.java new file mode 100644 index 0000000000..349c7bbf1d --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/PayrollService.java @@ -0,0 +1,82 @@ +package com; + +import com.Classification.CommissionedClassification; +import com.Classification.HourlyClassification; +import com.Classification.SalariedClassification; +import com.PaymentMethod.BankMethod; +import com.PaymentMethod.MailMethod; +import com.Schedule.BiweeklySchedule; +import com.Schedule.MonthlySchedule; +import com.Schedule.WeeklySchedule; +import com.pojo.Employee; +import com.PaymentMethod.HoldMethod; +import com.pojo.Paycheck; + +import java.util.List; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 21:11 + */ +public class PayrollService { + + + public List getAllEmployees(){ + return null; + } + + public void savePaycheck(Paycheck pc){ + + } + + /** + * 添加小时工 + * @param name + * @param address + * @param hourlyRate + * @return + */ + public Employee addHourlyEmployee(String name, String address, double hourlyRate){ + Employee e = new Employee(name, address); + e.setClassification(new HourlyClassification(hourlyRate)); + e.setSchedule(new WeeklySchedule()); + e.setPaymentMethod(new HoldMethod()); + return e; + } + + + /** + * 添加固定工资员工 + * @param name + * @param address + * @param salary + * @return + */ + public Employee addSalariedEmployee(String name, String address, double salary){ + Employee e = new Employee(name, address); + e.setClassification(new SalariedClassification(salary)); + e.setSchedule(new MonthlySchedule()); + e.setPaymentMethod(new BankMethod()); + return e; + } + + + /** + * 添加销售员工 + * @param name + * @param address + * @param salary + * @param saleRate + * @return + */ + public Employee addCommissionedEmployee(String name, String address, double salary, double saleRate){ + Employee e = new Employee(name, address); + e.setClassification(new CommissionedClassification(salary, saleRate)); + e.setSchedule(new BiweeklySchedule()); + e.setPaymentMethod(new MailMethod()); + return e; + } + + +} diff --git a/students/643449856/payrolla/src/main/java/com/Schedule/BiweeklySchedule.java b/students/643449856/payrolla/src/main/java/com/Schedule/BiweeklySchedule.java new file mode 100644 index 0000000000..b256862b19 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Schedule/BiweeklySchedule.java @@ -0,0 +1,49 @@ +package com.Schedule; + +import com.pojo.PaymentSchedule; +import com.util.DateUtil; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:29 + * + * 每隔一周支付 + */ +public class BiweeklySchedule implements PaymentSchedule { + + + /** + * 上一次支付的日期 + */ + Date firstPayableFriday = DateUtil.parseDate("2017-6-2"); + + + @Override + public boolean isPayDay(Date date) { + long interval = DateUtil.getDaysBetween(firstPayableFriday, date); + return interval % 14 == 0; + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -13); + } + + + public static void main(String [] args) throws Exception{ + BiweeklySchedule schedule = new BiweeklySchedule(); + + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); + Date d = sdf.parse("2017-06-30"); + + System.out.println(schedule.isPayDay(d)); + + System.out.println(DateUtil.isFriday(d)); + + System.out.println(schedule.getPayPeriodStartDate(d)); + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Schedule/MonthlySchedule.java b/students/643449856/payrolla/src/main/java/com/Schedule/MonthlySchedule.java new file mode 100644 index 0000000000..f3535d6246 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Schedule/MonthlySchedule.java @@ -0,0 +1,27 @@ +package com.Schedule; + +import com.pojo.PaymentSchedule; +import com.util.DateUtil; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:33 + * + * 每月的最后一天支付 + */ +public class MonthlySchedule implements PaymentSchedule { + + + @Override + public boolean isPayDay(Date date) { + return DateUtil.isLastDayOfMonth(date); + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.getFirstDay(payPeriodEndDate); + } +} diff --git a/students/643449856/payrolla/src/main/java/com/Schedule/WeeklySchedule.java b/students/643449856/payrolla/src/main/java/com/Schedule/WeeklySchedule.java new file mode 100644 index 0000000000..46945e649a --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/Schedule/WeeklySchedule.java @@ -0,0 +1,27 @@ +package com.Schedule; + +import com.pojo.PaymentSchedule; +import com.util.DateUtil; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:35 + * + * 每周五支付 + */ +public class WeeklySchedule implements PaymentSchedule { + + + @Override + public boolean isPayDay(Date date) { + return DateUtil.isFriday(date); + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -6); + } +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/Affiliation.java b/students/643449856/payrolla/src/main/java/com/pojo/Affiliation.java new file mode 100644 index 0000000000..1ba6f8b642 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/Affiliation.java @@ -0,0 +1,15 @@ +package com.pojo; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:07 + *

+ * 从属关系 + */ + + +public interface Affiliation { + + public double calculateDeductions(Paycheck pc); +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/Employee.java b/students/643449856/payrolla/src/main/java/com/pojo/Employee.java new file mode 100644 index 0000000000..18ff043561 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/Employee.java @@ -0,0 +1,118 @@ +package com.pojo; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 17:22 + */ +public class Employee { + + private String id; + private String name; + private String address; + private Affiliation affiliation; + + + private PaymentClassification classification; + private PaymentSchedule schedule; + private PaymentMethod paymentMethod; + + public Employee(String name, String address){ + this.name = name; + this.address = address; + } + + + /** + * 是不是支付日 + * @param d + * @return + */ + public boolean isPayDay(Date d) { + return this.schedule.isPayDay(d); + } + + + /** + * 得到支付开始的日期 + * @param d + * @return + */ + public Date getPayPeriodStartDate(Date d) { + return this.schedule.getPayPeriodStartDate(d); + } + + + /** + * 计算工资 + * @param pc + */ + public void payDay(Paycheck pc){ + double grossPay = classification.calculatePay(pc); + double deductions = affiliation.calculateDeductions(pc); + double netPay = grossPay - deductions; + pc.setGrossPay(grossPay); + pc.setDeductions(deductions); + pc.setNetPay(netPay); + paymentMethod.pay(pc); + } + + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Affiliation getAffiliation() { + return affiliation; + } + + public void setAffiliation(Affiliation affiliation) { + this.affiliation = affiliation; + } + + public PaymentClassification getClassification() { + return classification; + } + + public void setClassification(PaymentClassification classification) { + this.classification = classification; + } + + public PaymentSchedule getSchedule() { + return schedule; + } + + public void setSchedule(PaymentSchedule schedule) { + this.schedule = schedule; + } + + public PaymentMethod getPaymentMethod() { + return paymentMethod; + } + + public void setPaymentMethod(PaymentMethod paymentMethod) { + this.paymentMethod = paymentMethod; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/Paycheck.java b/students/643449856/payrolla/src/main/java/com/pojo/Paycheck.java new file mode 100644 index 0000000000..de903ca166 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/Paycheck.java @@ -0,0 +1,84 @@ +package com.pojo; + +import java.util.Date; +import java.util.Map; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:07 + * 薪水 + */ +public class Paycheck { + + private Date payPeriodStart; + private Date payPeriodEnd; + private double grossPay; // 应发工资 + private double netPay; // 实付工资 + private double deductions; // 扣除工资 + private Map itsFields; + + public Paycheck(Date payPeriodStart, Date payPeriodEnd){ + this.payPeriodStart = payPeriodStart; + this.payPeriodEnd = payPeriodEnd; + } + + public void setGrossPay(double grossPay) { + this.grossPay = grossPay; + + } + + public void setDeductions(double deductions) { + this.deductions = deductions; + } + + public void setNetPay(double netPay){ + this.netPay = netPay; + } + + public Date getPayPeriodEndDate() { + + return this.payPeriodEnd; + } + public Date getPayPeriodStartDate() { + + return this.payPeriodStart; + } + + + public Date getPayPeriodStart() { + return payPeriodStart; + } + + public void setPayPeriodStart(Date payPeriodStart) { + this.payPeriodStart = payPeriodStart; + } + + public Date getPayPeriodEnd() { + return payPeriodEnd; + } + + public void setPayPeriodEnd(Date payPeriodEnd) { + this.payPeriodEnd = payPeriodEnd; + } + + public double getGrossPay() { + return grossPay; + } + + public double getNetPay() { + return netPay; + } + + public double getDeductions() { + return deductions; + } + + public Map getItsFields() { + return itsFields; + } + + public void setItsFields(Map itsFields) { + this.itsFields = itsFields; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/PaymentClassification.java b/students/643449856/payrolla/src/main/java/com/pojo/PaymentClassification.java new file mode 100644 index 0000000000..0c83ad56ab --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/PaymentClassification.java @@ -0,0 +1,18 @@ +package com.pojo; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:17 + * + * 付款分类 + */ +public interface PaymentClassification { + + /** + * 计算应该支付的工资 + * @param pc + * @return + */ + public double calculatePay(Paycheck pc); +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/PaymentMethod.java b/students/643449856/payrolla/src/main/java/com/pojo/PaymentMethod.java new file mode 100644 index 0000000000..d805f0413e --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/PaymentMethod.java @@ -0,0 +1,14 @@ +package com.pojo; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:15 + * 付款方法 + */ +public interface PaymentMethod { + + public void pay(Paycheck pc); + + +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/PaymentSchedule.java b/students/643449856/payrolla/src/main/java/com/pojo/PaymentSchedule.java new file mode 100644 index 0000000000..7d3c7a78d8 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/PaymentSchedule.java @@ -0,0 +1,21 @@ +package com.pojo; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:14 + *

+ * 付款日期表 + */ +public interface PaymentSchedule { + + /** + * 是否是付款日 + * @param date + * @return + */ + public boolean isPayDay(Date date); + public Date getPayPeriodStartDate( Date payPeriodEndDate); +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/SalesReceipt.java b/students/643449856/payrolla/src/main/java/com/pojo/SalesReceipt.java new file mode 100644 index 0000000000..f9700bca80 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/SalesReceipt.java @@ -0,0 +1,22 @@ +package com.pojo; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:13 + *

+ * 销售数据 + */ +public class SalesReceipt { + + private Date saleDate; + private double amount; + public Date getSaleDate() { + return saleDate; + } + public double getAmount() { + return amount; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/pojo/TimeCard.java b/students/643449856/payrolla/src/main/java/com/pojo/TimeCard.java new file mode 100644 index 0000000000..b581e0bf87 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/pojo/TimeCard.java @@ -0,0 +1,23 @@ +package com.pojo; + +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:13 + * 时间卡片 + */ +public class TimeCard { + + + private Date date; + private int hours; + + public Date getDate() { + return date; + } + public int getHours() { + return hours; + } +} diff --git a/students/643449856/payrolla/src/main/java/com/transaction/AddEmployeeTransaction.java b/students/643449856/payrolla/src/main/java/com/transaction/AddEmployeeTransaction.java new file mode 100644 index 0000000000..bef6a08e20 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/transaction/AddEmployeeTransaction.java @@ -0,0 +1,42 @@ +package com.transaction; + +import com.PaymentMethod.HoldMethod; +import com.pojo.Employee; +import com.pojo.PaymentClassification; +import com.pojo.PaymentMethod; +import com.pojo.PaymentSchedule; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 21:24 + */ +public abstract class AddEmployeeTransaction { + + private String name; + private String address; + public AddEmployeeTransaction(String name,String address){ + this.name = name; + this.address = address; + } + + + public abstract PaymentClassification getClassification(); + + public abstract PaymentSchedule getSchedule(); + + + public void execute(){ + PaymentClassification pc = getClassification(); + PaymentSchedule ps = getSchedule(); + PaymentMethod pm = new HoldMethod(); + Employee e = new Employee(name, address); + e.setClassification(pc); + e.setSchedule(ps); + e.setPaymentMethod(pm); + } + + + + +} diff --git a/students/643449856/payrolla/src/main/java/com/transaction/AddHourlyEmployeeTransaction.java b/students/643449856/payrolla/src/main/java/com/transaction/AddHourlyEmployeeTransaction.java new file mode 100644 index 0000000000..1442672485 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/transaction/AddHourlyEmployeeTransaction.java @@ -0,0 +1,33 @@ +package com.transaction; + +import com.Classification.HourlyClassification; +import com.Schedule.WeeklySchedule; +import com.pojo.PaymentClassification; +import com.pojo.PaymentSchedule; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 21:25 + */ +public class AddHourlyEmployeeTransaction extends AddEmployeeTransaction { + + + private double rate; + + AddHourlyEmployeeTransaction(String name, String address, double hourlyRate) { + super(name, address); + this.rate = hourlyRate; + } + + @Override + public PaymentClassification getClassification() { + return new HourlyClassification(rate); + } + + @Override + public PaymentSchedule getSchedule() { + + return new WeeklySchedule(); + } +} \ No newline at end of file diff --git a/students/643449856/payrolla/src/main/java/com/util/DateUtil.java b/students/643449856/payrolla/src/main/java/com/util/DateUtil.java new file mode 100644 index 0000000000..3a270aa0f4 --- /dev/null +++ b/students/643449856/payrolla/src/main/java/com/util/DateUtil.java @@ -0,0 +1,67 @@ +package com.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * Created by nengneng + * Date: 2017/9/15 + * Time: 20:25 + * + * + */ +public class DateUtil { + + + public static long getDaysBetween(Date d1, Date d2){ + + return (d2.getTime() - d1.getTime())/(24*60*60*1000); + } + + public static Date parseDate(String txtDate){ + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); + try { + return sdf.parse(txtDate); + } catch (ParseException e) { + e.printStackTrace(); + return null; + } + } + public static boolean isFriday(Date d){ + Calendar calendar = Calendar.getInstance(); + return calendar.get(Calendar.DAY_OF_WEEK) == 5; + } + + public static Date add(Date d, int days){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.add(Calendar.DATE, days); + return calendar.getTime(); + } + + public static boolean isLastDayOfMonth(Date d){ + Calendar calendar=Calendar.getInstance(); + calendar.setTime(d); + return calendar.get(Calendar.DATE)==calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + } + public static Date getFirstDay(Date d){ + Calendar calendar=Calendar.getInstance(); + calendar.setTime(d); + int day = calendar.get(Calendar.DATE); + calendar.add(Calendar.DATE, -(day-1)); + return calendar.getTime(); + } + public static void main(String [] args) throws Exception{ + System.out.println(DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-6-29"))); + + System.out.println(DateUtil.getFirstDay(DateUtil.parseDate("2017-6-30"))); + } + + public static boolean between(Date d, Date date1, Date date2){ + return d.after(date1) && d.before(date2); + } + + +} From 40795b635aaa7861b4c86e0b38e3486395964364 Mon Sep 17 00:00:00 2001 From: jyz2017 <734473301@qq.com> Date: Sat, 16 Sep 2017 22:58:07 +0800 Subject: [PATCH 7/8] =?UTF-8?q?jyz=E8=96=AA=E6=B0=B4=E6=94=AF=E4=BB=98?= =?UTF-8?q?=EF=BC=8Cjunit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework/jyz/coding2017/EmailHost.java | 1 + .../myjunit/calculator/Calculator.java | 22 ++ .../myjunit/calculator/CalculatorTest.java | 41 +++ students/734473301/myjunit/junit/Assert.java | 243 ++++++++++++++++++ .../myjunit/junit/AssertionFailedError.java | 13 + students/734473301/myjunit/junit/Test.java | 9 + .../734473301/myjunit/junit/TestCase.java | 75 ++++++ .../734473301/myjunit/junit/TestFailure.java | 30 +++ .../734473301/myjunit/junit/TestResult.java | 104 ++++++++ .../734473301/myjunit/junit/TestSuite.java | 98 +++++++ .../734473301/payroll/PayrollService.java | 68 +++++ .../payroll/affiliation/NonAffiliation.java | 10 + .../payroll/affiliation/UnionAffiliation.java | 14 + .../CommissionedClassification.java | 39 +++ .../classification/HourlyClassification.java | 48 ++++ .../SalariedClassification.java | 19 ++ .../734473301/payroll/domain/Affiliation.java | 10 + .../734473301/payroll/domain/Employee.java | 58 +++++ .../734473301/payroll/domain/HoldMethod.java | 11 + .../734473301/payroll/domain/Paycheck.java | 48 ++++ .../payroll/domain/PaydayTransaction.java | 44 ++++ .../payroll/domain/PaymentClassification.java | 10 + .../payroll/domain/PaymentMethod.java | 9 + .../payroll/domain/PaymentSchedule.java | 19 ++ .../payroll/domain/SalesReceipt.java | 23 ++ .../734473301/payroll/domain/TimeCard.java | 23 ++ .../payroll/schedule/BiweeklySchedule.java | 42 +++ .../payroll/schedule/MonthlySchedule.java | 25 ++ .../payroll/schedule/WeeklySchedule.java | 22 ++ students/734473301/payroll/util/DateUtil.java | 58 +++++ 30 files changed, 1236 insertions(+) create mode 100644 students/734473301/myjunit/calculator/Calculator.java create mode 100644 students/734473301/myjunit/calculator/CalculatorTest.java create mode 100644 students/734473301/myjunit/junit/Assert.java create mode 100644 students/734473301/myjunit/junit/AssertionFailedError.java create mode 100644 students/734473301/myjunit/junit/Test.java create mode 100644 students/734473301/myjunit/junit/TestCase.java create mode 100644 students/734473301/myjunit/junit/TestFailure.java create mode 100644 students/734473301/myjunit/junit/TestResult.java create mode 100644 students/734473301/myjunit/junit/TestSuite.java create mode 100644 students/734473301/payroll/PayrollService.java create mode 100644 students/734473301/payroll/affiliation/NonAffiliation.java create mode 100644 students/734473301/payroll/affiliation/UnionAffiliation.java create mode 100644 students/734473301/payroll/classification/CommissionedClassification.java create mode 100644 students/734473301/payroll/classification/HourlyClassification.java create mode 100644 students/734473301/payroll/classification/SalariedClassification.java create mode 100644 students/734473301/payroll/domain/Affiliation.java create mode 100644 students/734473301/payroll/domain/Employee.java create mode 100644 students/734473301/payroll/domain/HoldMethod.java create mode 100644 students/734473301/payroll/domain/Paycheck.java create mode 100644 students/734473301/payroll/domain/PaydayTransaction.java create mode 100644 students/734473301/payroll/domain/PaymentClassification.java create mode 100644 students/734473301/payroll/domain/PaymentMethod.java create mode 100644 students/734473301/payroll/domain/PaymentSchedule.java create mode 100644 students/734473301/payroll/domain/SalesReceipt.java create mode 100644 students/734473301/payroll/domain/TimeCard.java create mode 100644 students/734473301/payroll/schedule/BiweeklySchedule.java create mode 100644 students/734473301/payroll/schedule/MonthlySchedule.java create mode 100644 students/734473301/payroll/schedule/WeeklySchedule.java create mode 100644 students/734473301/payroll/util/DateUtil.java diff --git a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java index 4299d466fe..dd12981982 100644 --- a/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java +++ b/students/734473301/coding2017jyz/src/homework/jyz/coding2017/EmailHost.java @@ -10,6 +10,7 @@ public class EmailHost { private String altHost; private String hostAdmin; + /** * 构建主机 * @param config 主机配置 diff --git a/students/734473301/myjunit/calculator/Calculator.java b/students/734473301/myjunit/calculator/Calculator.java new file mode 100644 index 0000000000..e9835f3872 --- /dev/null +++ b/students/734473301/myjunit/calculator/Calculator.java @@ -0,0 +1,22 @@ +package com.jyz.myjunit.calculator; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} diff --git a/students/734473301/myjunit/calculator/CalculatorTest.java b/students/734473301/myjunit/calculator/CalculatorTest.java new file mode 100644 index 0000000000..256a867371 --- /dev/null +++ b/students/734473301/myjunit/calculator/CalculatorTest.java @@ -0,0 +1,41 @@ +package com.jyz.myjunit.calculator; + + +import com.jyz.myjunit.junit.TestCase; +import com.jyz.myjunit.junit.TestResult; +import com.jyz.myjunit.junit.TestSuite; + +public class CalculatorTest extends TestCase { + public CalculatorTest(String name) { + super(name); + + } + Calculator calculator =null; + + public void setUp(){ + calculator = new Calculator(); + } + public void tearDown(){ + calculator = null; + } + + public void testAdd(){ + + calculator.add(10); + assertEquals(1,calculator.getResult()); + } + public void testSubtract(){ + calculator.add(10); + calculator.subtract(5); + //throw new RuntimeException("this is a test"); + assertEquals(5,calculator.getResult()); + } + + public static void main(String[] args) throws ClassNotFoundException { + + TestSuite ts = new TestSuite(Class.forName("com.jyz.myjunit.calculator.CalculatorTest")); + TestResult result = new TestResult(); + ts.run(result); + System.out.println(result); + } +} diff --git a/students/734473301/myjunit/junit/Assert.java b/students/734473301/myjunit/junit/Assert.java new file mode 100644 index 0000000000..5d2c11214f --- /dev/null +++ b/students/734473301/myjunit/junit/Assert.java @@ -0,0 +1,243 @@ +package com.jyz.myjunit.junit; + +/** + * A set of assert methods. + */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + * @deprecated use assertTrue + */ + /*static public void assert(String message, boolean condition) { + if (!condition) + fail(message); + }*/ + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + * @deprecated use assertTrue + * + */ + /*static public void assert(boolean condition) { + assert(null, condition); + } +*/ + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} \ No newline at end of file diff --git a/students/734473301/myjunit/junit/AssertionFailedError.java b/students/734473301/myjunit/junit/AssertionFailedError.java new file mode 100644 index 0000000000..7dd67f0beb --- /dev/null +++ b/students/734473301/myjunit/junit/AssertionFailedError.java @@ -0,0 +1,13 @@ +package com.jyz.myjunit.junit; + +/** + * Thrown when an assertion failed. + */ +public class AssertionFailedError extends Error { + + public AssertionFailedError () { + } + public AssertionFailedError (String message) { + super (message); + } +} \ No newline at end of file diff --git a/students/734473301/myjunit/junit/Test.java b/students/734473301/myjunit/junit/Test.java new file mode 100644 index 0000000000..ecf2f9ef7c --- /dev/null +++ b/students/734473301/myjunit/junit/Test.java @@ -0,0 +1,9 @@ +package com.jyz.myjunit.junit; + +/** + * Created by jyz on 2017/9/16. + */ +public interface Test { + void run(TestResult tr); + int countTestCases(); +} diff --git a/students/734473301/myjunit/junit/TestCase.java b/students/734473301/myjunit/junit/TestCase.java new file mode 100644 index 0000000000..9071ad9813 --- /dev/null +++ b/students/734473301/myjunit/junit/TestCase.java @@ -0,0 +1,75 @@ +package com.jyz.myjunit.junit; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +/** + * Created by jyz on 2017/9/16. + */ +public abstract class TestCase extends Assert implements Test { + private String name;// 该用例的方法名字 + + public TestCase(String name) { + this.name = name; + } + + @Override + public String toString() { + return "TestCase{" + + "methond ='" + name + '\'' + + '}'; + } + + public void doRun() throws Throwable{ + setUp(); + try{ + runTest(); + } + finally{ + tearDown(); + } + } + + @Override + public int countTestCases(){ + return 1; + } + + protected void setUp() { + + } + + protected void tearDown() { + + } + + @Override + public void run(TestResult tr) { + tr.run(this); + } + + protected void runTest() throws Throwable{ + Method runMethod= null; + try { + runMethod= getClass().getMethod(name, null); + } catch (NoSuchMethodException e) { + fail("Method \""+name+"\" not found"); + } + if (!Modifier.isPublic(runMethod.getModifiers())) { + fail("Method \""+name+"\" should be public"); + } + + try { + runMethod.invoke(this); + } + catch (InvocationTargetException e) { + e.fillInStackTrace(); + throw e.getTargetException(); + } + catch (IllegalAccessException e) { + e.fillInStackTrace(); + throw e; + } + } +} diff --git a/students/734473301/myjunit/junit/TestFailure.java b/students/734473301/myjunit/junit/TestFailure.java new file mode 100644 index 0000000000..f6da0b3086 --- /dev/null +++ b/students/734473301/myjunit/junit/TestFailure.java @@ -0,0 +1,30 @@ +package com.jyz.myjunit.junit; + +/** + * Created by jyz on 2017/9/16. + */ +public class TestFailure { + protected Test failedTest; + protected Throwable throwException; + + public TestFailure(Test failedTest, Throwable throwException) { + this.failedTest = failedTest; + this.throwException = throwException; + } + + public Test getFailedTest() { + return failedTest; + } + + public Throwable getThrowException() { + return throwException; + } + + @Override + public String toString() { + return "TestFailure{" + + "failedTest=" + failedTest + + ", throwException=" + throwException + + '}'; + } +} diff --git a/students/734473301/myjunit/junit/TestResult.java b/students/734473301/myjunit/junit/TestResult.java new file mode 100644 index 0000000000..02d72ec0ea --- /dev/null +++ b/students/734473301/myjunit/junit/TestResult.java @@ -0,0 +1,104 @@ +package com.jyz.myjunit.junit; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * 用来保存测试结果 + */ +public class TestResult { + protected List failures; + protected List errors; + + protected int testCount; + private boolean stop; + + public TestResult() { + failures= new ArrayList<>(); + errors= new ArrayList<>(); + + testCount= 0; + stop= false; + } + + public void addError(Test test, Throwable t) { + errors.add(new TestFailure(test, t)); + } + + public void addFailure(Test test, AssertionFailedError t) { + failures.add(new TestFailure(test, t)); + } + + public void startTest(Test test) { + int count= test.countTestCases(); + testCount+= count; + } + public void endTest(Test test) { + } + + /** + * Runs a TestCase. + */ + protected void run(final TestCase test) { + startTest(test); + try { + test.doRun(); + } + catch (AssertionFailedError e) { + addFailure(test, e); + } + catch (Throwable e) { + addError(test, e); + } + + endTest(test); + } + /** + * Gets the number of run tests. + */ + public int runCount() { + return testCount; + } + + + public boolean shouldStop() { + return stop; + } + + public void stop() { + stop= true; + } + + public int errorCount() { + return errors.size(); + } + + public Iterator errors() { + return errors.iterator(); + } + + public int failureCount() { + return failures.size(); + } + + public Iterator failures() { + return failures.iterator(); + } + /** + * Returns whether the entire test was successful or not. + */ + public boolean wasSuccessful() { + return this.failureCount() == 0 && this.errorCount() == 0; + } + + @Override + public String toString() { + return "TestResult{" + + "\n failures=" + failures + + ",\n errors=" + errors + + ",\n testCount=" + testCount + + ",\n success ="+wasSuccessful()+"\n}"; + } +} \ No newline at end of file diff --git a/students/734473301/myjunit/junit/TestSuite.java b/students/734473301/myjunit/junit/TestSuite.java new file mode 100644 index 0000000000..12e995b64f --- /dev/null +++ b/students/734473301/myjunit/junit/TestSuite.java @@ -0,0 +1,98 @@ +package com.jyz.myjunit.junit; + + +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + + +/** + * 测试套件 + * 测试套件通过反射获取一个 测试用例类 的所有test开头的方法;每个方法对应一个实例 + * 遍历这些实例 + * Created by jyz on 2017/9/16. + */ +public class TestSuite extends Assert implements Test{ + private List tests= new ArrayList<>(10); + private String name; + + + /** + * 反射获取该类所有测试方法 + * @param theClass + */ + public TestSuite(final Class theClass) { + this.name= theClass.getName(); + Constructor constructor = null; + try { + constructor= theClass.getConstructor(String.class); + Method[] methods= theClass.getDeclaredMethods(); + for (Method method : methods) { + if(isPublicTestMethod(method)){ + System.out.println("添加测试方法:"+method.getName()); + addTest((Test) constructor.newInstance(method.getName())); + } + } + } catch (Exception e) { + addTest(warning("在获取测试方法时出现异常!")); + e.printStackTrace(); + } + if (tests.size() == 0){ + addTest(warning("没有发现可以使用的测试用例!")); + } + + } + + + private boolean isPublicTestMethod(Method m) { + return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); + } + private boolean isTestMethod(Method m) { + String name= m.getName(); + Class[] parameters= m.getParameterTypes(); + Class returnType= m.getReturnType(); + return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); + } + + public void addTest(Test test) { + tests.add(test); + } + + private Test warning(final String message) { + return new TestCase("warning") { + @Override + public void doRun() { + fail(message); + } + }; + } + + @Override + public void run(TestResult result) { + + for (Iterator e = tests(); e.hasNext(); ) { + if (result.shouldStop() ){ + break; + } + Test test= (Test)e.next(); + test.run(result); + // result.run(test); + } + + } + + public int countTestCases() { + int count= 0; + for (Iterator e= tests(); e.hasNext(); ) { + Test test= e.next(); + count= count + test.countTestCases(); + } + return count; + } + public Iterator tests() { + return tests.iterator(); + } +} diff --git a/students/734473301/payroll/PayrollService.java b/students/734473301/payroll/PayrollService.java new file mode 100644 index 0000000000..c333a1b40d --- /dev/null +++ b/students/734473301/payroll/PayrollService.java @@ -0,0 +1,68 @@ +package com.jyz.payroll; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.jyz.payroll.affiliation.NonAffiliation; +import com.jyz.payroll.classification.CommissionedClassification; +import com.jyz.payroll.classification.HourlyClassification; +import com.jyz.payroll.classification.SalariedClassification; +import com.jyz.payroll.domain.Employee; +import com.jyz.payroll.domain.HoldMethod; +import com.jyz.payroll.domain.Paycheck; +import com.jyz.payroll.domain.TimeCard; +import com.jyz.payroll.schedule.BiweeklySchedule; +import com.jyz.payroll.schedule.MonthlySchedule; +import com.jyz.payroll.schedule.WeeklySchedule; + +public class PayrollService { + private List allEmployees = new ArrayList<>(); + + public List getAllEmployees(){ + return allEmployees; + } + public void savePaycheck(Paycheck pc){ + System.out.println("保存此次支付记录:"+pc); + } + + public Employee addHourlyEmployee(String name, String address, double hourlyRate){ + + Employee e = new Employee(name, address); + HourlyClassification hourlyClassification = new HourlyClassification(hourlyRate); + + hourlyClassification.addTimeCard(new TimeCard(new Date(),8)); + + e.setClassification(hourlyClassification); + e.setSchedule(new WeeklySchedule()); + e.setPaymentMethod(new HoldMethod()); + e.setAffiliation(new NonAffiliation()); + allEmployees.add(e); + //保存员工到数据库.. 略 + return e; + } + + public Employee addSalariedEmployee(String name, String address, double salary){ + Employee e = new Employee(name, address); + e.setClassification(new SalariedClassification(salary)); + e.setSchedule(new MonthlySchedule()); + e.setPaymentMethod(new HoldMethod()); + e.setAffiliation(new NonAffiliation()); + //保存员工到数据库.. 略 + allEmployees.add(e); + return e; + } + + public Employee addCommissionedEmployee(String name, String address, double salary, double saleRate){ + Employee e = new Employee(name, address); + e.setClassification(new CommissionedClassification(salary, saleRate)); + e.setSchedule(new BiweeklySchedule()); + e.setPaymentMethod(new HoldMethod()); + e.setAffiliation(new NonAffiliation()); + //保存员工到数据库.. 略 + allEmployees.add(e); + return e; + } + + +} diff --git a/students/734473301/payroll/affiliation/NonAffiliation.java b/students/734473301/payroll/affiliation/NonAffiliation.java new file mode 100644 index 0000000000..1b42201113 --- /dev/null +++ b/students/734473301/payroll/affiliation/NonAffiliation.java @@ -0,0 +1,10 @@ +package com.jyz.payroll.affiliation; + +import com.jyz.payroll.domain.Affiliation; +import com.jyz.payroll.domain.Paycheck; + +public class NonAffiliation implements Affiliation{ + public double calculateDeductions(Paycheck pc){ + return 9.0; + } +} diff --git a/students/734473301/payroll/affiliation/UnionAffiliation.java b/students/734473301/payroll/affiliation/UnionAffiliation.java new file mode 100644 index 0000000000..5ab02ac21e --- /dev/null +++ b/students/734473301/payroll/affiliation/UnionAffiliation.java @@ -0,0 +1,14 @@ +package com.jyz.payroll.affiliation; + +import com.jyz.payroll.domain.Affiliation; +import com.jyz.payroll.domain.Paycheck; + +public class UnionAffiliation implements Affiliation { + + @Override + public double calculateDeductions(Paycheck pc) { + + return 0; + } + +} diff --git a/students/734473301/payroll/classification/CommissionedClassification.java b/students/734473301/payroll/classification/CommissionedClassification.java new file mode 100644 index 0000000000..7db6f16a4b --- /dev/null +++ b/students/734473301/payroll/classification/CommissionedClassification.java @@ -0,0 +1,39 @@ +package com.jyz.payroll.classification; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import com.jyz.payroll.domain.Paycheck; +import com.jyz.payroll.domain.PaymentClassification; +import com.jyz.payroll.domain.SalesReceipt; +import com.jyz.payroll.util.DateUtil; + +/** + * 销售人员计算薪水 + */ +public class CommissionedClassification implements PaymentClassification { + double salary; + double rate; + public CommissionedClassification(double salary , double rate){ + this.salary = salary; + this.rate = rate; + receipts = new HashMap<>(); + SalesReceipt sr = new SalesReceipt(new Date(),50000); + receipts.put(new Date(),sr); + } + Map receipts; + + @Override + public double calculatePay(Paycheck pc) { + double commission = 0.0; + for(SalesReceipt sr : receipts.values()){ + if(DateUtil.between(sr.getSaleDate(), pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate())){ + commission += sr.getAmount() * rate; + } + } + return salary + commission; + } + +} diff --git a/students/734473301/payroll/classification/HourlyClassification.java b/students/734473301/payroll/classification/HourlyClassification.java new file mode 100644 index 0000000000..ee563836c9 --- /dev/null +++ b/students/734473301/payroll/classification/HourlyClassification.java @@ -0,0 +1,48 @@ +package com.jyz.payroll.classification; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import com.jyz.payroll.domain.Paycheck; +import com.jyz.payroll.domain.PaymentClassification; +import com.jyz.payroll.domain.TimeCard; +import com.jyz.payroll.util.DateUtil; +/** + * 小时工的薪水计算 + */ + +public class HourlyClassification implements PaymentClassification { + private double rate; + private Map timeCards = new HashMap<>(); + + public HourlyClassification(double hourlyRate) { + this.rate = hourlyRate; + } + public void addTimeCard(TimeCard tc){ + timeCards.put(tc.getDate(), tc); + } + + @Override + public double calculatePay(Paycheck pc) { + double totalPay = 0; + for(TimeCard tc : timeCards.values()){ + if(DateUtil.between(tc.getDate(), pc.getPayPeriodStartDate(), + pc.getPayPeriodEndDate())){ + totalPay += calculatePayForTimeCard(tc); + } + } + return totalPay; + + } + private double calculatePayForTimeCard(TimeCard tc) { + int hours = tc.getHours(); + + if(hours > 8){ + return 8*rate + (hours-8) * rate * 1.5; + } else{ + return 8*rate; + } + } +} + diff --git a/students/734473301/payroll/classification/SalariedClassification.java b/students/734473301/payroll/classification/SalariedClassification.java new file mode 100644 index 0000000000..48db5e6b22 --- /dev/null +++ b/students/734473301/payroll/classification/SalariedClassification.java @@ -0,0 +1,19 @@ +package com.jyz.payroll.classification; + +import com.jyz.payroll.domain.Paycheck; +import com.jyz.payroll.domain.PaymentClassification; + +/** + * 月薪族 + */ +public class SalariedClassification implements PaymentClassification { + private double salary; + public SalariedClassification(double salary){ + this.salary = salary; + } + @Override + public double calculatePay(Paycheck pc) { + return salary; + } + +} diff --git a/students/734473301/payroll/domain/Affiliation.java b/students/734473301/payroll/domain/Affiliation.java new file mode 100644 index 0000000000..a1ea246762 --- /dev/null +++ b/students/734473301/payroll/domain/Affiliation.java @@ -0,0 +1,10 @@ +package com.jyz.payroll.domain; + +public interface Affiliation { + /** + * 计算扣除 + * @param pc + * @return + */ + double calculateDeductions(Paycheck pc); +} diff --git a/students/734473301/payroll/domain/Employee.java b/students/734473301/payroll/domain/Employee.java new file mode 100644 index 0000000000..7bbadd103d --- /dev/null +++ b/students/734473301/payroll/domain/Employee.java @@ -0,0 +1,58 @@ +package com.jyz.payroll.domain; + +import java.util.Date; +// 雇员类 +public class Employee { + private String id; + private String name; + private String address; + private Affiliation affiliation; + + + private PaymentClassification classification; // 计算薪水 + private PaymentSchedule schedule; // 支付周期 + + private PaymentMethod paymentMethod; // 该雇员的支付 方式 + // 支票邮寄到他们指定的邮政地址,也可以保存在财务那里随时支取,或者要求直接存入他们指定的银行账户 + + public Employee(String name, String address){ + this.name = name; + this.address = address; + } + public boolean isPayDay(Date d) { + return this.schedule.isPayDate(d); + } + + public Date getPayPeriodStartDate(Date d) { + return this.schedule.getPayPeriodStartDate(d); + } + + // 重复发薪校验 + public void payDay(Paycheck pc){ + double grossPay = classification.calculatePay(pc); + double deductions = affiliation.calculateDeductions(pc); + double netPay = grossPay - deductions; + pc.setGrossPay(grossPay); + pc.setDeductions(deductions); + pc.setNetPay(netPay); + System.out.println("员工:"+name); + paymentMethod.pay(pc); + } + + + + public void setClassification(PaymentClassification classification) { + this.classification = classification; + } + public void setSchedule(PaymentSchedule schedule) { + this.schedule = schedule; + } + public void setPaymentMethod(PaymentMethod paymentMethod) { + this.paymentMethod = paymentMethod; + } + + public void setAffiliation(Affiliation affiliation) { + this.affiliation = affiliation; + } +} + diff --git a/students/734473301/payroll/domain/HoldMethod.java b/students/734473301/payroll/domain/HoldMethod.java new file mode 100644 index 0000000000..9ca345cce2 --- /dev/null +++ b/students/734473301/payroll/domain/HoldMethod.java @@ -0,0 +1,11 @@ +package com.jyz.payroll.domain; + +public class HoldMethod implements PaymentMethod { + + @Override + public void pay(Paycheck pc) { + + System.out.println("支付记录:"+pc); + } + +} diff --git a/students/734473301/payroll/domain/Paycheck.java b/students/734473301/payroll/domain/Paycheck.java new file mode 100644 index 0000000000..61ab731817 --- /dev/null +++ b/students/734473301/payroll/domain/Paycheck.java @@ -0,0 +1,48 @@ +package com.jyz.payroll.domain; + +import java.util.Date; +import java.util.Map; + +public class Paycheck { + private Date payPeriodStart;// 上一次支付时间 + private Date payPeriodEnd;// 这一次支付时间 + private double grossPay; //总共薪水 + private double netPay; //实际支付 + private double deductions; //应该扣除的钱 + private Map itsFields; + + public Paycheck(Date payPeriodStart, Date payPeriodEnd){ + this.payPeriodStart = payPeriodStart; + this.payPeriodEnd = payPeriodEnd; + } + + public void setGrossPay(double grossPay) { + this.grossPay = grossPay; + + } + + public void setDeductions(double deductions) { + this.deductions = deductions; + } + public void setNetPay(double netPay){ + this.netPay = netPay; + } + + public Date getPayPeriodEndDate() { + + return this.payPeriodEnd; + } + public Date getPayPeriodStartDate() { + + return this.payPeriodStart; + } + + @Override + public String toString() { + return "{" + + "总薪水= " + grossPay + + ", 实际支付= " + netPay + + ", 扣除部分= " + deductions + + '}'; + } +} diff --git a/students/734473301/payroll/domain/PaydayTransaction.java b/students/734473301/payroll/domain/PaydayTransaction.java new file mode 100644 index 0000000000..0e574ebab9 --- /dev/null +++ b/students/734473301/payroll/domain/PaydayTransaction.java @@ -0,0 +1,44 @@ +package com.jyz.payroll.domain; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import com.jyz.payroll.PayrollService; + + +public class PaydayTransaction { + private Date date; + private PayrollService payrollService; + + public void execute(){ + List employees = payrollService.getAllEmployees(); + for(Employee e : employees){ + if(e.isPayDay(date)){ + Paycheck pc = new Paycheck(e.getPayPeriodStartDate(date),date); + e.payDay(pc); + payrollService.savePaycheck(pc); + System.out.println(""); + } + } + } + + + public static void main(String args[]) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH"); + PayrollService ps = new PayrollService(); + PaydayTransaction pt = new PaydayTransaction(); + pt.date = sdf.parse("2017-09-21 05"); + pt.payrollService = ps; + // 小时工 + ps.addHourlyEmployee("小时工","上海",200); + //销售人员 + ps.addCommissionedEmployee("销售","广州",500,0.2); + ps.addSalariedEmployee("月薪族","北京",10000); + pt.execute(); + + + } +} + diff --git a/students/734473301/payroll/domain/PaymentClassification.java b/students/734473301/payroll/domain/PaymentClassification.java new file mode 100644 index 0000000000..97ec105da9 --- /dev/null +++ b/students/734473301/payroll/domain/PaymentClassification.java @@ -0,0 +1,10 @@ +package com.jyz.payroll.domain; + +public interface PaymentClassification { + /** + * 计算薪水 + * @param pc + * @return + */ + double calculatePay(Paycheck pc); +} diff --git a/students/734473301/payroll/domain/PaymentMethod.java b/students/734473301/payroll/domain/PaymentMethod.java new file mode 100644 index 0000000000..97b932a8c9 --- /dev/null +++ b/students/734473301/payroll/domain/PaymentMethod.java @@ -0,0 +1,9 @@ +package com.jyz.payroll.domain; + +public interface PaymentMethod { + /** + * 支付方式 + * @param pc + */ + void pay(Paycheck pc); +} diff --git a/students/734473301/payroll/domain/PaymentSchedule.java b/students/734473301/payroll/domain/PaymentSchedule.java new file mode 100644 index 0000000000..85c561b7c5 --- /dev/null +++ b/students/734473301/payroll/domain/PaymentSchedule.java @@ -0,0 +1,19 @@ +package com.jyz.payroll.domain; + +import java.util.Date; + +public interface PaymentSchedule { + /** + * 今天是不是发薪水的日子 + * @param date + * @return + */ + boolean isPayDate(Date date); + + /** + * 上一次发薪水的日子 + * @param payPeriodEndDate + * @return + */ + Date getPayPeriodStartDate(Date payPeriodEndDate); +} diff --git a/students/734473301/payroll/domain/SalesReceipt.java b/students/734473301/payroll/domain/SalesReceipt.java new file mode 100644 index 0000000000..7d72733879 --- /dev/null +++ b/students/734473301/payroll/domain/SalesReceipt.java @@ -0,0 +1,23 @@ +package com.jyz.payroll.domain; + +import java.util.Date; + +/** + * 销售凭条 + */ +public class SalesReceipt { + private Date saleDate; + private double amount; + + public SalesReceipt(Date saleDate, double amount) { + this.saleDate = saleDate; + this.amount = amount; + } + + public Date getSaleDate() { + return saleDate; + } + public double getAmount() { + return amount; + } +} diff --git a/students/734473301/payroll/domain/TimeCard.java b/students/734473301/payroll/domain/TimeCard.java new file mode 100644 index 0000000000..1cf5d56173 --- /dev/null +++ b/students/734473301/payroll/domain/TimeCard.java @@ -0,0 +1,23 @@ +package com.jyz.payroll.domain; + +import java.util.Date; + +/** + * 时间卡,小时工用 + */ +public class TimeCard { + private Date date; + private int hours; + + public TimeCard(Date date, int hours) { + this.date = date; + this.hours = hours; + } + + public Date getDate() { + return date; + } + public int getHours() { + return hours; + } +} diff --git a/students/734473301/payroll/schedule/BiweeklySchedule.java b/students/734473301/payroll/schedule/BiweeklySchedule.java new file mode 100644 index 0000000000..32db8ba2ea --- /dev/null +++ b/students/734473301/payroll/schedule/BiweeklySchedule.java @@ -0,0 +1,42 @@ +package com.jyz.payroll.schedule; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.jyz.payroll.domain.PaymentSchedule; +import com.jyz.payroll.util.DateUtil; + +/** + * 双周支付一次薪水 + */ +public class BiweeklySchedule implements PaymentSchedule { + Date firstPayableFriday = DateUtil.parseDate("2017-09-07"); + + @Override + public boolean isPayDate(Date date) { + + long interval = DateUtil.getDaysBetween(firstPayableFriday, date); + System.out.println("距离上次支付多少天:" + interval); + return interval % 14 == 0; + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -13); + + } + + public static void main(String [] args) throws Exception{ + BiweeklySchedule schedule = new BiweeklySchedule(); + + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); + Date d = sdf.parse("2017-06-30"); + + System.out.println(schedule.isPayDate(d)); + + System.out.println(DateUtil.isFriday(d)); + + System.out.println(schedule.getPayPeriodStartDate(d)); + } + +} diff --git a/students/734473301/payroll/schedule/MonthlySchedule.java b/students/734473301/payroll/schedule/MonthlySchedule.java new file mode 100644 index 0000000000..1839cd7518 --- /dev/null +++ b/students/734473301/payroll/schedule/MonthlySchedule.java @@ -0,0 +1,25 @@ +package com.jyz.payroll.schedule; + +import java.util.Date; + +import com.jyz.payroll.domain.PaymentSchedule; +import com.jyz.payroll.util.DateUtil; + +/** + * 月薪族支付 + */ +public class MonthlySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + //return DateUtil.isLastDayOfMonth(date); + System.out.println("月薪族测试,总是每月的最后一天"); + return true; + } + + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.getFirstDay(payPeriodEndDate); + } + +} diff --git a/students/734473301/payroll/schedule/WeeklySchedule.java b/students/734473301/payroll/schedule/WeeklySchedule.java new file mode 100644 index 0000000000..33264f81dc --- /dev/null +++ b/students/734473301/payroll/schedule/WeeklySchedule.java @@ -0,0 +1,22 @@ +package com.jyz.payroll.schedule; + +import java.util.Date; + +import com.jyz.payroll.domain.PaymentSchedule; +import com.jyz.payroll.util.DateUtil; + +/** + * 每周五支付 + */ +public class WeeklySchedule implements PaymentSchedule { + + @Override + public boolean isPayDate(Date date) { + return DateUtil.isFriday(date); + } + @Override + public Date getPayPeriodStartDate(Date payPeriodEndDate) { + return DateUtil.add(payPeriodEndDate, -6); + } + +} diff --git a/students/734473301/payroll/util/DateUtil.java b/students/734473301/payroll/util/DateUtil.java new file mode 100644 index 0000000000..8ac5925122 --- /dev/null +++ b/students/734473301/payroll/util/DateUtil.java @@ -0,0 +1,58 @@ +package com.jyz.payroll.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class DateUtil { + public static long getDaysBetween(Date d1, Date d2){ + + return (d2.getTime() - d1.getTime())/(24*60*60*1000); + } + + public static Date parseDate(String txtDate){ + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); + try { + return sdf.parse(txtDate); + } catch (ParseException e) { + e.printStackTrace(); + return null; + } + } + public static boolean isFriday(Date d){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + System.out.println("今天周几:"+calendar.get(Calendar.DAY_OF_WEEK)); + return calendar.get(Calendar.DAY_OF_WEEK) == 5; + } + + public static Date add(Date d, int days){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime(d); + calendar.add(Calendar.DATE, days); + return calendar.getTime(); + } + + public static boolean isLastDayOfMonth(Date d){ + Calendar calendar=Calendar.getInstance(); + calendar.setTime(d); + return calendar.get(Calendar.DATE)==calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + } + public static Date getFirstDay(Date d){ + Calendar calendar=Calendar.getInstance(); + calendar.setTime(d); + int day = calendar.get(Calendar.DATE); + calendar.add(Calendar.DATE, -(day-1)); + return calendar.getTime(); + } + public static void main(String [] args) throws Exception{ + System.out.println(DateUtil.isLastDayOfMonth(DateUtil.parseDate("2017-6-29"))); + + System.out.println(DateUtil.getFirstDay(DateUtil.parseDate("2017-6-30"))); + } + + public static boolean between(Date d, Date date1, Date date2){ + return d.after(date1) && d.before(date2); + } +} From e0c6e1fba242872ddbf24a736fbb9f4438df3d57 Mon Sep 17 00:00:00 2001 From: sheng <1158154002@qq.com> Date: Sun, 17 Sep 2017 21:40:54 +0800 Subject: [PATCH 8/8] junit3.x --- .../com/coderising/dp/chain/EmailLogger.java | 15 ++ .../com/coderising/dp/chain/FileLogger.java | 15 ++ .../java/com/coderising/dp/chain/Logger.java | 28 ++ .../com/coderising/dp/chain/StdoutLogger.java | 13 + .../java/com/coderising/dp/chain/Test.java | 15 ++ .../com/coderising/dp/command/Command.java | 5 + .../java/com/coderising/dp/command/Cook.java | 11 + .../dp/command/OrderPorkCommand.java | 13 + .../dp/command/OrderSteakCommand.java | 15 ++ .../com/coderising/dp/command/Waiter.java | 33 +++ .../litejunit/extension/RepeatedTest.java | 32 +++ .../litejunit/extension/TestDecorator.java | 40 +++ .../litejunit/extension/TestSetup.java | 39 +++ .../coderising/litejunit/sample/AllTest.java | 42 +++ .../litejunit/sample/PersonTest.java | 38 +++ .../sample/calculator/Calculator.java | 22 ++ .../sample/calculator/CalculatorSuite.java | 12 + .../sample/calculator/CalculatorTest.java | 59 +++++ .../com/coderising/litejunit/v/Assert.java | 225 ++++++++++++++++ .../litejunit/v/AssertionFailedError.java | 13 + .../java/com/coderising/litejunit/v/Test.java | 6 + .../com/coderising/litejunit/v/TestCase.java | 63 +++++ .../coderising/litejunit/v/TestFailure.java | 39 +++ .../coderising/litejunit/v/TestListener.java | 12 + .../com/coderising/litejunit/v/TestSuite.java | 77 ++++++ .../litejunit/v/runner/BaseTestRunner.java | 45 ++++ .../litejunit/v/textui/TestRunner.java | 156 +++++++++++ .../com/coderising/litejunit/v1/Assert.java | 225 ++++++++++++++++ .../litejunit/v1/AssertionFailedError.java | 13 + .../coderising/litejunit/v1/Calculator.java | 22 ++ .../litejunit/v1/CalculatorTest.java | 69 +++++ .../com/coderising/litejunit/v1/Test.java | 6 + .../com/coderising/litejunit/v1/TestCase.java | 59 +++++ .../coderising/litejunit/v1/TestFailure.java | 39 +++ .../coderising/litejunit/v1/TestSuite.java | 133 ++++++++++ .../com/coderising/litejunit/v2/Assert.java | 243 ++++++++++++++++++ .../litejunit/v2/AssertionFailedError.java | 13 + .../coderising/litejunit/v2/Protectable.java | 14 + .../com/coderising/litejunit/v2/Test.java | 6 + .../com/coderising/litejunit/v2/TestCase.java | 64 +++++ .../coderising/litejunit/v2/TestFailure.java | 39 +++ .../coderising/litejunit/v2/TestListener.java | 15 ++ .../coderising/litejunit/v2/TestSuite.java | 133 ++++++++++ .../litejunit/v2/runner/BaseTestRunner.java | 85 ++++++ .../litejunit/v2/textui/TestRunner.java | 196 ++++++++++++++ 45 files changed, 2457 insertions(+) create mode 100644 students/1158154002/src/main/java/com/coderising/dp/chain/EmailLogger.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/chain/FileLogger.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/chain/Logger.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/chain/StdoutLogger.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/chain/Test.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/command/Command.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/command/Cook.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/command/OrderPorkCommand.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/command/OrderSteakCommand.java create mode 100644 students/1158154002/src/main/java/com/coderising/dp/command/Waiter.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/extension/RepeatedTest.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/extension/TestDecorator.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/extension/TestSetup.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/sample/AllTest.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/sample/PersonTest.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/Calculator.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorSuite.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorTest.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/Assert.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/AssertionFailedError.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/Test.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/TestCase.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/TestFailure.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/TestListener.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/TestSuite.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/runner/BaseTestRunner.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v/textui/TestRunner.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/Assert.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/AssertionFailedError.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/Calculator.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/CalculatorTest.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/Test.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/TestCase.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/TestFailure.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v1/TestSuite.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/Assert.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/AssertionFailedError.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/Protectable.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/Test.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/TestCase.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/TestFailure.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/TestListener.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/TestSuite.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/runner/BaseTestRunner.java create mode 100644 students/1158154002/src/main/java/com/coderising/litejunit/v2/textui/TestRunner.java diff --git a/students/1158154002/src/main/java/com/coderising/dp/chain/EmailLogger.java b/students/1158154002/src/main/java/com/coderising/dp/chain/EmailLogger.java new file mode 100644 index 0000000000..8a249c048a --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/chain/EmailLogger.java @@ -0,0 +1,15 @@ +package com.coderising.dp.chain; + +public class EmailLogger extends Logger{ + + EmailLogger(int level) { + super(level); + } + + @Override + protected void write(String message) { + System.out.println("EmailLogger "+message); + } + + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/chain/FileLogger.java b/students/1158154002/src/main/java/com/coderising/dp/chain/FileLogger.java new file mode 100644 index 0000000000..22b8a4cd9b --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/chain/FileLogger.java @@ -0,0 +1,15 @@ +package com.coderising.dp.chain; + +public class FileLogger extends Logger { + + FileLogger(int level) { + super(level); + } + + @Override + protected void write(String message) { + System.out.println("FileLogger " + message); + } + + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/chain/Logger.java b/students/1158154002/src/main/java/com/coderising/dp/chain/Logger.java new file mode 100644 index 0000000000..1265063c31 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/chain/Logger.java @@ -0,0 +1,28 @@ +package com.coderising.dp.chain; + +public abstract class Logger { + public static int DEBUG = 1; + public static int NOTICE = 2; + public static int ERR = 3; + protected int level; + + Logger(int level) { + this.level=level; + } + protected Logger nextLogger; + public Logger setNextLogger(Logger nextLogger) { + this.nextLogger = nextLogger; + return this; + } + + public void message(String message,int level){ + if (this.level<=level) { + write(message); + } + if (nextLogger!=null) { + nextLogger.message( message,level); + } + } + abstract protected void write(String message); + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/chain/StdoutLogger.java b/students/1158154002/src/main/java/com/coderising/dp/chain/StdoutLogger.java new file mode 100644 index 0000000000..0bf96e8d9c --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/chain/StdoutLogger.java @@ -0,0 +1,13 @@ +package com.coderising.dp.chain; + +public class StdoutLogger extends Logger{ + + StdoutLogger(int level) { + super(level); + } + + @Override + protected void write(String message) { + System.out.println("StdoutLogger "+message); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/chain/Test.java b/students/1158154002/src/main/java/com/coderising/dp/chain/Test.java new file mode 100644 index 0000000000..13ff6419e1 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/chain/Test.java @@ -0,0 +1,15 @@ +package com.coderising.dp.chain; + +public class Test { + + public static void main(String[] args) { + Logger logger = new StdoutLogger(Logger.DEBUG) + .setNextLogger(new EmailLogger(Logger.NOTICE).setNextLogger(new FileLogger(Logger.ERR))); + logger.message("进入函数计算", Logger.DEBUG); + + logger.message("第一步已经完成", Logger.NOTICE); + + logger.message("一个致命的错误发生了", Logger.ERR); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/command/Command.java b/students/1158154002/src/main/java/com/coderising/dp/command/Command.java new file mode 100644 index 0000000000..bd54aeec59 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/command/Command.java @@ -0,0 +1,5 @@ +package com.coderising.dp.command; + +public interface Command { + void order(); +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/command/Cook.java b/students/1158154002/src/main/java/com/coderising/dp/command/Cook.java new file mode 100644 index 0000000000..710d752521 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/command/Cook.java @@ -0,0 +1,11 @@ +package com.coderising.dp.command; + +public class Cook { + void cookSteak(){ + System.out.println("Steak is ok"); + } + + void cookPork(){ + System.out.println("Pork is ok"); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/command/OrderPorkCommand.java b/students/1158154002/src/main/java/com/coderising/dp/command/OrderPorkCommand.java new file mode 100644 index 0000000000..b0e1c61191 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/command/OrderPorkCommand.java @@ -0,0 +1,13 @@ +package com.coderising.dp.command; + +public class OrderPorkCommand implements Command{ + private Cook cook; + public OrderPorkCommand(Cook cook) { + this.cook=cook; + } + @Override + public void order() { + cook.cookPork(); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/command/OrderSteakCommand.java b/students/1158154002/src/main/java/com/coderising/dp/command/OrderSteakCommand.java new file mode 100644 index 0000000000..e56b2649b5 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/command/OrderSteakCommand.java @@ -0,0 +1,15 @@ +package com.coderising.dp.command; + +public class OrderSteakCommand implements Command { + private Cook cook; + + public OrderSteakCommand(Cook cook) { + this.cook = cook; + } + + @Override + public void order() { + cook.cookSteak(); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/dp/command/Waiter.java b/students/1158154002/src/main/java/com/coderising/dp/command/Waiter.java new file mode 100644 index 0000000000..0c9e94852c --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/dp/command/Waiter.java @@ -0,0 +1,33 @@ +package com.coderising.dp.command; + +import java.util.ArrayList; +import java.util.List; + +public class Waiter { + private List commands = new ArrayList<>(); + + public void addOrder(Command command) { + commands.add(command); + } + + public void sendOrders(){ + for (Command command : commands) { + command.order(); + } + } + + public static void main(String[] args) { + Cook cook=new Cook(); + + Waiter waiter=new Waiter(); + + Command command1=new OrderSteakCommand(cook); + Command command2=new OrderPorkCommand(cook); + + waiter.addOrder(command1); + waiter.addOrder(command2); + + waiter.sendOrders(); + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/extension/RepeatedTest.java b/students/1158154002/src/main/java/com/coderising/litejunit/extension/RepeatedTest.java new file mode 100644 index 0000000000..f13faf5b43 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/extension/RepeatedTest.java @@ -0,0 +1,32 @@ +package com.coderising.litejunit.extension; + +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestResult; + +/** + * A Decorator that runs a test repeatedly. + * + */ +public class RepeatedTest extends TestDecorator { + private int fTimesRepeat; + + public RepeatedTest(Test test, int repeat) { + super(test); + if (repeat < 0) + throw new IllegalArgumentException("Repetition count must be > 0"); + fTimesRepeat= repeat; + } + public int countTestCases() { + return super.countTestCases()*fTimesRepeat; + } + public void run(TestResult result) { + for (int i= 0; i < fTimesRepeat; i++) { + if (result.shouldStop()) + break; + super.run(result); + } + } + public String toString() { + return super.toString()+"(repeated)"; + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestDecorator.java b/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestDecorator.java new file mode 100644 index 0000000000..3b51d7bff1 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestDecorator.java @@ -0,0 +1,40 @@ +package com.coderising.litejunit.extension; + +import com.coderising.litejunit.v2.Assert; +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestResult; + +/** + * A Decorator for Tests. Use TestDecorator as the base class + * for defining new test decorators. Test decorator subclasses + * can be introduced to add behaviour before or after a test + * is run. + * + */ +public class TestDecorator extends Assert implements Test { + protected Test test; + + public TestDecorator(Test test) { + this.test= test; + } + /** + * The basic run behaviour. + */ + public void basicRun(TestResult result) { + test.run(result); + } + public int countTestCases() { + return test.countTestCases(); + } + public void run(TestResult result) { + basicRun(result); + } + + public String toString() { + return test.toString(); + } + + public Test getTest() { + return test; + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestSetup.java b/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestSetup.java new file mode 100644 index 0000000000..54f95f6f79 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/extension/TestSetup.java @@ -0,0 +1,39 @@ +package com.coderising.litejunit.extension; + +import com.coderising.litejunit.v2.Protectable; +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestResult; + +/** + * A Decorator to set up and tear down additional fixture state. + * Subclass TestSetup and insert it into your tests when you want + * to set up additional state once before the tests are run. + */ +public class TestSetup extends TestDecorator { + + public TestSetup(Test test) { + super(test); + } + public void run(final TestResult result) { + Protectable p= new Protectable() { + public void protect() throws Exception { + setUp(); + basicRun(result); + tearDown(); + } + }; + result.runProtected(this, p); + } + /** + * Sets up the fixture. Override to set up additional fixture + * state. + */ + protected void setUp() throws Exception { + } + /** + * Tears down the fixture. Override to tear down the additional + * fixture state. + */ + protected void tearDown() throws Exception { + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/sample/AllTest.java b/students/1158154002/src/main/java/com/coderising/litejunit/sample/AllTest.java new file mode 100644 index 0000000000..8cf38f176b --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/sample/AllTest.java @@ -0,0 +1,42 @@ +package com.coderising.litejunit.sample; + +import com.coderising.litejunit.extension.RepeatedTest; +import com.coderising.litejunit.extension.TestSetup; +import com.coderising.litejunit.sample.calculator.CalculatorSuite; +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestSuite; + +public class AllTest { +// public static Test suite(){ +// +// TestSuite suite= new TestSuite("All Test"); +// suite.addTest(CalculatorSuite.suite()); +// //suite.addTestSuite(PersonTest.class); +// return suite; +// +// } + + public static Test suite(){ + + TestSuite suite= new TestSuite("All Test"); + suite.addTest(CalculatorSuite.suite()); + suite.addTest(new RepeatedTest(new TestSuite(PersonTest.class), 2)); + return new OverallTestSetup(suite); + } + + + static class OverallTestSetup extends TestSetup{ + + public OverallTestSetup(Test test) { + super(test); + + } + protected void setUp() throws Exception { + System.out.println("this is overall testsetup"); + } + protected void tearDown() throws Exception { + System.out.println("this is overall teardown"); + } + + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/sample/PersonTest.java b/students/1158154002/src/main/java/com/coderising/litejunit/sample/PersonTest.java new file mode 100644 index 0000000000..bcb8b1c971 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/sample/PersonTest.java @@ -0,0 +1,38 @@ +package com.coderising.litejunit.sample; + +import com.coderising.litejunit.v2.TestCase; + +public class PersonTest extends TestCase { + + Person p = null; + protected void setUp() { + p = new Person("andy",30); + } + public PersonTest(String name) { + super(name); + } + public void testAge(){ + this.assertEquals(30, p.getAge()); + } + public void testName(){ + this.assertEquals("andy", p.getName()); + } +} +class Person{ + private String name; + private int age; + + public Person(String name, int age) { + + this.name = name; + this.age = age; + } + public String getName() { + return name; + } + public int getAge() { + return age; + } + + +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/Calculator.java b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/Calculator.java new file mode 100644 index 0000000000..cede0aab15 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/Calculator.java @@ -0,0 +1,22 @@ +package com.coderising.litejunit.sample.calculator; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorSuite.java b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorSuite.java new file mode 100644 index 0000000000..df3159bf91 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorSuite.java @@ -0,0 +1,12 @@ +package com.coderising.litejunit.sample.calculator; + +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestSuite; + +public class CalculatorSuite { + public static Test suite(){ + TestSuite suite= new TestSuite("Calculator All Test"); + suite.addTestSuite(CalculatorTest.class); + return suite; + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorTest.java b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorTest.java new file mode 100644 index 0000000000..eb14934330 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/sample/calculator/CalculatorTest.java @@ -0,0 +1,59 @@ +package com.coderising.litejunit.sample.calculator; + +import com.coderising.litejunit.v2.TestCase; + +public class CalculatorTest extends TestCase { + public CalculatorTest(String name) { + super(name); + + } + Calculator calculator =null; + public void setUp(){ + calculator = new Calculator(); + } + public void tearDown(){ + calculator = null; + } + public void testAdd(){ + + calculator.add(10); + assertEquals(10,calculator.getResult()); + } + public void testSubtract(){ + calculator.add(10); + calculator.subtract(5); + //throw new RuntimeException("this is a test"); + assertEquals(5,calculator.getResult()); + } + + public static void main(String[] args){ + /*{ + TestCase tc1 = new CalculatorTest("testAdd"){ + protected void runTest() { + testAdd(); + } + }; + + TestCase tc2 = new CalculatorTest("testSubtract"){ + protected void runTest() { + testSubtract(); + } + }; + tc1.run(); + tc2.run(); + } + + + TestSuite ts = new TestSuite(); + ts.addTest(new CalculatorTest("testAdd")); + ts.addTest(new CalculatorTest("testSubtract")); + + + { + TestCase tc1 = new CalculatorTest("test1"); + TestCase tc2 = new CalculatorTest("test2"); + tc1.run(); + tc2.run(); + }*/ + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/Assert.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/Assert.java new file mode 100644 index 0000000000..0b68ef790c --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/Assert.java @@ -0,0 +1,225 @@ +package com.coderising.litejunit.v; + +/** + * A set of assert methods. + */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/AssertionFailedError.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/AssertionFailedError.java new file mode 100644 index 0000000000..3c45ba40d5 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/AssertionFailedError.java @@ -0,0 +1,13 @@ +package com.coderising.litejunit.v; + +/** + * Thrown when an assertion failed. + */ +public class AssertionFailedError extends Error { + + public AssertionFailedError () { + } + public AssertionFailedError (String message) { + super (message); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/Test.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/Test.java new file mode 100644 index 0000000000..f5639552c3 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/Test.java @@ -0,0 +1,6 @@ +package com.coderising.litejunit.v; + +public interface Test { + public abstract int countTestCases(); + public void run(TestResult tr); +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/TestCase.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestCase.java new file mode 100644 index 0000000000..49ac03b69f --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestCase.java @@ -0,0 +1,63 @@ +package com.coderising.litejunit.v; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public abstract class TestCase extends Assert implements Test { + private String name; + public TestCase(String name) { + this.name=name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void run(TestResult tr) { + tr.run(this); + } + + public void doRun() { + setUp(); + try { + runTest(); + } finally { + tearDown(); + } + } + + protected void setUp(){ + + } + + private void runTest(){ + try { + Method method=this.getClass().getMethod(this.getName(),new Class[0]); + method.invoke(this, null); + } catch (NoSuchMethodException | SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + } + + + @Override + public int countTestCases() { + return 1; + } + + protected void tearDown(){ + + } + +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/TestFailure.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestFailure.java new file mode 100644 index 0000000000..e7a6fc1606 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestFailure.java @@ -0,0 +1,39 @@ +package com.coderising.litejunit.v; + +/** + * A TestFailure collects a failed test together with + * the caught exception. + * @see TestResult + */ +public class TestFailure { + protected Test failedTest; + protected Throwable thrownException; + + /** + * Constructs a TestFailure with the given test and exception. + */ + public TestFailure(Test failedTest, Throwable thrownException) { + this.failedTest= failedTest; + this.thrownException= thrownException; + } + /** + * Gets the failed test. + */ + public Test failedTest() { + return failedTest; + } + /** + * Gets the thrown exception. + */ + public Throwable thrownException() { + return thrownException; + } + /** + * Returns a short description of the failure. + */ + public String toString() { + StringBuffer buffer= new StringBuffer(); + buffer.append(failedTest+": "+thrownException.getMessage()); + return buffer.toString(); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/TestListener.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestListener.java new file mode 100644 index 0000000000..b98409a549 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestListener.java @@ -0,0 +1,12 @@ +package com.coderising.litejunit.v; + +public interface TestListener { + + void addError(Test test,Throwable t); + + void addFailure(Test test,AssertionFailedError t); + + void endTest(Test test); + + void startTest(Test test); +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/TestSuite.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestSuite.java new file mode 100644 index 0000000000..ca694254b0 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/TestSuite.java @@ -0,0 +1,77 @@ +package com.coderising.litejunit.v; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; + +public class TestSuite extends Assert implements Test { + List tests = new ArrayList<>(); + + public TestSuite(final Class clazz) { + Constructor cons = null; + cons = getConstructor(clazz, cons); + + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + if (isPublicTestMethod(method)) { + tests.add(addTestMethod(cons, method.getName())); + } + } + } + + private Constructor getConstructor(final Class clazz, Constructor cons) { + try { + cons = clazz.getConstructor(String.class); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } + return cons; + } + + private Test addTestMethod(Constructor cons, String name) { + try { + return (Test) cons.newInstance(name); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + e.printStackTrace(); + } + return null; + } + + private boolean isPublicTestMethod(Method method) { + return isPublic(method) && isTest(method); + } + + private boolean isTest(Method method) { + return method.getName().startsWith("test") && method.getParameterCount() == 0 + && method.getReturnType().equals(Void.TYPE); + } + + private boolean isPublic(Method method) { + return Modifier.isPublic(method.getModifiers()); + } + + @Override + public int countTestCases() { + int count=0; + for (Test test : tests) { + count+=test.countTestCases(); + } + + return count; + } + + @Override + public void run(TestResult tr) { + for (Test test : tests) { + test.run(tr); + } + + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/runner/BaseTestRunner.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/runner/BaseTestRunner.java new file mode 100644 index 0000000000..1217e45024 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/runner/BaseTestRunner.java @@ -0,0 +1,45 @@ +package com.coderising.litejunit.v.runner; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import com.coderising.litejunit.v.AssertionFailedError; +import com.coderising.litejunit.v.Test; +import com.coderising.litejunit.v.TestListener; + +public abstract class BaseTestRunner implements TestListener { + public static final String METHOD_NAME = "suite"; + + public Test getTest(String testCase) { + if (testCase.length() <= 0) { + return null; + } + Class clazz = null; + try { + clazz = loadSuitClass(testCase); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + try { + Method method = clazz.getMethod(METHOD_NAME, new Class[0]); + return (Test) method.invoke(null, null); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (SecurityException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + + return null; + } + + private static Class loadSuitClass(String suitClassName) throws ClassNotFoundException { + return Class.forName(suitClassName); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v/textui/TestRunner.java b/students/1158154002/src/main/java/com/coderising/litejunit/v/textui/TestRunner.java new file mode 100644 index 0000000000..9c7342bc30 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v/textui/TestRunner.java @@ -0,0 +1,156 @@ +package com.coderising.litejunit.v.textui; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.text.NumberFormat; +import java.util.Iterator; + +import com.coderising.litejunit.v.AssertionFailedError; +import com.coderising.litejunit.v.Test; +import com.coderising.litejunit.v.TestFailure; +import com.coderising.litejunit.v.TestResult; +import com.coderising.litejunit.v.runner.BaseTestRunner; + +public class TestRunner extends BaseTestRunner { + PrintStream writer=System.out; + int column=0; + + public static void main(String[] args) { + TestRunner testRunner = new TestRunner(); + try { + TestResult result= testRunner.start(args); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + private TestResult start(String[] args) throws Exception { + if (args.length == 0) { + throw new Exception("参数错误!"); + } + String testCase = args[0]; + Test test = getTest(testCase); + return this.doRun(test); + } + + private TestResult doRun(Test suite){ + TestResult result=new TestResult(); + result.addListener(this); + long startTime= System.currentTimeMillis(); + suite.run(result); + long endTime= System.currentTimeMillis(); + long runTime= endTime-startTime; + writer().println(); + writer().println("Time: "+elapsedTimeAsString(runTime)); + print(result); + + writer().println(); + return result; + } + + private void print(TestResult result) { + printErrors(result); + printFailures(result); + printHeader(result); + + } + + /** + * Prints the errors to the standard output + */ + public void printErrors(TestResult result) { + if (result.errorCount() != 0) { + if (result.errorCount() == 1) + writer().println("There was "+result.errorCount()+" error:"); + else + writer().println("There were "+result.errorCount()+" errors:"); + + int i= 1; + for (Iterator e= result.errors(); e.hasNext(); i++) { + TestFailure failure= e.next(); + writer().println(i+") "+failure.failedTest()); + writer().print(getFilteredTrace(failure.thrownException())); + } + } + } + private static String getFilteredTrace(Throwable t) { + StringWriter stringWriter= new StringWriter(); + PrintWriter writer= new PrintWriter(stringWriter); + t.printStackTrace(writer); + StringBuffer buffer= stringWriter.getBuffer(); + String trace= buffer.toString(); + return trace; + } + + /** + * Prints failures to the standard output + */ + public void printFailures(TestResult result) { + if (result.failureCount() != 0) { + if (result.failureCount() == 1) + writer().println("There was " + result.failureCount() + " failure:"); + else + writer().println("There were " + result.failureCount() + " failures:"); + int i = 1; + for (Iterator e= result.failures(); e.hasNext(); i++) { + TestFailure failure= (TestFailure) e.next(); + writer().print(i + ") " + failure.failedTest()); + Throwable t= failure.thrownException(); + writer().print(getFilteredTrace(failure.thrownException())); + } + } + } + /** + * Prints the header of the report + */ + public void printHeader(TestResult result) { + if (result.wasSuccessful()) { + writer().println(); + writer().print("OK"); + writer().println (" (" + result.runCount() + " tests)"); + + } else { + writer().println(); + writer().println("FAILURES!!!"); + writer().println("Tests run: "+result.runCount()+ + ", Failures: "+result.failureCount()+ + ", Errors: "+result.errorCount()); + } + } + + + private String elapsedTimeAsString(long runTime) { + return NumberFormat.getInstance().format((double)runTime/1000); + } + + @Override + public void addError(Test test, Throwable t) { + writer().print("E"); + } + + @Override + public void addFailure(Test test, AssertionFailedError t) { + writer().print("F"); + } + + @Override + public void endTest(Test test) { + + } + + @Override + public void startTest(Test test) { + writer().print("."); + if (column++ >= 40) { + writer().println(); + column= 0; + } + } + + private PrintStream writer(){ + return writer; + } + +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/Assert.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Assert.java new file mode 100644 index 0000000000..c94f7bdb8a --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Assert.java @@ -0,0 +1,225 @@ +package com.coderising.litejunit.v1; + +/** + * A set of assert methods. + */ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/AssertionFailedError.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/AssertionFailedError.java new file mode 100644 index 0000000000..0d109969de --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/AssertionFailedError.java @@ -0,0 +1,13 @@ +package com.coderising.litejunit.v1; + +/** + * Thrown when an assertion failed. + */ +public class AssertionFailedError extends Error { + + public AssertionFailedError () { + } + public AssertionFailedError (String message) { + super (message); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/Calculator.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Calculator.java new file mode 100644 index 0000000000..828e011938 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Calculator.java @@ -0,0 +1,22 @@ +package com.coderising.litejunit.v1; + +public class Calculator { + + private int result = 0; + public void add(int x){ + result += x; + } + public void subtract(int x){ + result -=x; + } + + public int getResult(){ + return this.result; + } + public static void main(String[] args){ + Calculator calculator = new Calculator(); + calculator.add(10); + calculator.subtract(5); + System.out.println(calculator.getResult()); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/CalculatorTest.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/CalculatorTest.java new file mode 100644 index 0000000000..180ba461e0 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/CalculatorTest.java @@ -0,0 +1,69 @@ +package com.coderising.litejunit.v1; + +public class CalculatorTest extends TestCase { + public CalculatorTest(String name) { + super(name); + + } + + Calculator calculator = null; + + public void setUp() { + calculator = new Calculator(); + } + + public void tearDown() { + calculator = null; + } + + public void testAdd() { + + calculator.add(10); + assertEquals(10, calculator.getResult()); + } + + public void testSubtract() { + calculator.add(10); + calculator.subtract(5); + assertEquals(5, calculator.getResult()); + } + + public static void main(String[] args) { + TestSuite ts = new TestSuite(CalculatorTest.class); + TestResult tr = new TestResult(); + ts.run(tr); + System.out.println(tr.wasSuccessful()); + for (TestFailure failure : tr.failures) { + System.err.println(failure); + } + + /*{ + TestCase tc1 = new CalculatorTest("testAdd"){ + protected void runTest() { + testAdd(); + } + }; + + TestCase tc2 = new CalculatorTest("testSubtract"){ + protected void runTest() { + testSubtract(); + } + }; + tc1.run(); + tc2.run(); + } + + + TestSuite ts = new TestSuite(); + ts.addTest(new CalculatorTest("testAdd")); + ts.addTest(new CalculatorTest("testSubtract")); + + + { + TestCase tc1 = new CalculatorTest("test1"); + TestCase tc2 = new CalculatorTest("test2"); + tc1.run(); + tc2.run(); + }*/ + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/Test.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Test.java new file mode 100644 index 0000000000..4b9e496e33 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/Test.java @@ -0,0 +1,6 @@ +package com.coderising.litejunit.v1; + +public interface Test { + public abstract int countTestCases(); + public void run(TestResult tr); +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestCase.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestCase.java new file mode 100644 index 0000000000..62a505af92 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestCase.java @@ -0,0 +1,59 @@ +package com.coderising.litejunit.v1; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +public abstract class TestCase extends Assert implements Test { + private String name; + + public TestCase(String name) { + this.name = name; + } + + public int countTestCases() { + return 1; + } + + protected void runTest() throws Throwable{ + Method runMethod= null; + try { + runMethod= getClass().getMethod(name, null); + } catch (NoSuchMethodException e) { + fail("Method \""+name+"\" not found"); + } + if (!Modifier.isPublic(runMethod.getModifiers())) { + fail("Method \""+name+"\" should be public"); + } + + try { + runMethod.invoke(this, new Class[0]); + } + catch (InvocationTargetException e) { + e.fillInStackTrace(); + throw e.getTargetException(); + } + catch (IllegalAccessException e) { + e.fillInStackTrace(); + throw e; + } + } + + protected void setUp() { + } + + protected void tearDown() { + } + + public void run(TestResult tr) { + tr.run(this); + } + public void doRun() throws Throwable{ + setUp(); + try{ + runTest(); + } + finally{ + tearDown(); + } + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestFailure.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestFailure.java new file mode 100644 index 0000000000..de314e894b --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestFailure.java @@ -0,0 +1,39 @@ +package com.coderising.litejunit.v1; + +/** + * A TestFailure collects a failed test together with + * the caught exception. + * @see TestResult + */ +public class TestFailure { + protected Test failedTest; + protected Throwable thrownException; + + /** + * Constructs a TestFailure with the given test and exception. + */ + public TestFailure(Test failedTest, Throwable thrownException) { + this.failedTest= failedTest; + this.thrownException= thrownException; + } + /** + * Gets the failed test. + */ + public Test failedTest() { + return failedTest; + } + /** + * Gets the thrown exception. + */ + public Throwable thrownException() { + return thrownException; + } + /** + * Returns a short description of the failure. + */ + public String toString() { + StringBuffer buffer= new StringBuffer(); + buffer.append(failedTest+": "+thrownException.getMessage()); + return buffer.toString(); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestSuite.java b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestSuite.java new file mode 100644 index 0000000000..bd01327df0 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v1/TestSuite.java @@ -0,0 +1,133 @@ +package com.coderising.litejunit.v1; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + +public class TestSuite extends Assert implements Test { + private List tests = new ArrayList<>(10); + private String name; + + public TestSuite() { + + } + + public TestSuite(final Class theClass) { + this.name = theClass.getName(); + Constructor constructor = null; + try { + constructor = getConstructor(theClass); + } catch (NoSuchMethodException e) { + addTest(warning("Class " + theClass.getName() + " has no public constructor TestCase(String name)")); + return; + } + + if (!Modifier.isPublic(theClass.getModifiers())) { + addTest(warning("Class " + theClass.getName() + " is not public")); + return; + } + + Vector names = new Vector<>(); + Method[] methods = theClass.getDeclaredMethods(); + for (int i = 0; i < methods.length; i++) { + addTestMethod(methods[i], names, constructor); + } + + if (tests.size() == 0) + addTest(warning("No tests found in " + theClass.getName())); + } + + private Constructor getConstructor(Class theClass) throws NoSuchMethodException { + Class[] args = { String.class }; + return theClass.getConstructor(args); + } + + private void addTestMethod(Method m, Vector names, Constructor constructor) { + String name = m.getName(); + if (names.contains(name)) + return; + if (isPublicTestMethod(m)) { + names.addElement(name); + + Object[] args = new Object[] { name }; + try { + addTest((Test) constructor.newInstance(args)); + } catch (InstantiationException e) { + addTest(warning("Cannot instantiate test case: " + name + " (" + exceptionToString(e) + ")")); + } catch (InvocationTargetException e) { + addTest(warning( + "Exception in constructor: " + name + " (" + exceptionToString(e.getTargetException()) + ")")); + } catch (IllegalAccessException e) { + addTest(warning("Cannot access test case: " + name + " (" + exceptionToString(e) + ")")); + } + + } else { // almost a test method + if (isTestMethod(m)) + addTest(warning("Test method isn't public: " + m.getName())); + } + } + + private boolean isPublicTestMethod(Method m) { + return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); + } + + private boolean isTestMethod(Method m) { + String name = m.getName(); + Class[] parameters = m.getParameterTypes(); + Class returnType = m.getReturnType(); + return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); + } + + public void addTest(Test test) { + tests.add(test); + } + + private Test warning(final String message) { + return new TestCase("warning") { + public void doRun() { + fail(message); + } + }; + } + + private String exceptionToString(Throwable t) { + StringWriter stringWriter = new StringWriter(); + PrintWriter writer = new PrintWriter(stringWriter); + t.printStackTrace(writer); + return stringWriter.toString(); + + } + + @Override + public void run(TestResult result) { + for (Iterator e = tests(); e.hasNext();) { + if (result.shouldStop()) { + break; + } + Test test = (Test) e.next(); + test.run(result); + } + + } + + public int countTestCases() { + int count = 0; + + for (Iterator e = tests(); e.hasNext();) { + Test test = e.next(); + count = count + test.countTestCases(); + } + return count; + } + + public Iterator tests() { + return tests.iterator(); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/Assert.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Assert.java new file mode 100644 index 0000000000..b0a6191fec --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Assert.java @@ -0,0 +1,243 @@ +package com.coderising.litejunit.v2; + +/** +* A set of assert methods. +*/ + +public class Assert { + /** + * Protect constructor since it is a static only class + */ + protected Assert() { + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + * @deprecated use assertTrue + */ + /*static public void assert(String message, boolean condition) { + if (!condition) + fail(message); + }*/ + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + * @deprecated use assertTrue + * + */ + /*static public void assert(boolean condition) { + assert(null, condition); + } +*/ + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError with the given message. + */ + static public void assertTrue(String message, boolean condition) { + if (!condition) + fail(message); + } + /** + * Asserts that a condition is true. If it isn't it throws + * an AssertionFailedError. + */ + static public void assertTrue(boolean condition) { + assertTrue(null, condition); + } + /** + * Fails a test with the given message. + */ + static public void fail(String message) { + throw new AssertionFailedError(message); + } + /** + * Fails a test with no message. + */ + static public void fail() { + fail(null); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(String message, Object expected, Object actual) { + if (expected == null && actual == null) + return; + if (expected != null && expected.equals(actual)) + return; + failNotEquals(message, expected, actual); + } + /** + * Asserts that two objects are equal. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertEquals(Object expected, Object actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, double expected, double actual, double delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Double.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Double(expected), new Double(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) // Because comparison with NaN always returns false + failNotEquals(message, new Double(expected), new Double(actual)); + } + /** + * Asserts that two doubles are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(double expected, double actual, double delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(String message, float expected, float actual, float delta) { + // handle infinity specially since subtracting to infinite values gives NaN and the + // the following test fails + if (Float.isInfinite(expected)) { + if (!(expected == actual)) + failNotEquals(message, new Float(expected), new Float(actual)); + } else if (!(Math.abs(expected-actual) <= delta)) + failNotEquals(message, new Float(expected), new Float(actual)); + } + /** + * Asserts that two floats are equal concerning a delta. If the expected + * value is infinity then the delta value is ignored. + */ + static public void assertEquals(float expected, float actual, float delta) { + assertEquals(null, expected, actual, delta); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(String message, long expected, long actual) { + assertEquals(message, new Long(expected), new Long(actual)); + } + /** + * Asserts that two longs are equal. + */ + static public void assertEquals(long expected, long actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(String message, boolean expected, boolean actual) { + assertEquals(message, new Boolean(expected), new Boolean(actual)); + } + /** + * Asserts that two booleans are equal. + */ + static public void assertEquals(boolean expected, boolean actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(String message, byte expected, byte actual) { + assertEquals(message, new Byte(expected), new Byte(actual)); + } + /** + * Asserts that two bytes are equal. + */ + static public void assertEquals(byte expected, byte actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(String message, char expected, char actual) { + assertEquals(message, new Character(expected), new Character(actual)); + } + /** + * Asserts that two chars are equal. + */ + static public void assertEquals(char expected, char actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(String message, short expected, short actual) { + assertEquals(message, new Short(expected), new Short(actual)); + } + /** + * Asserts that two shorts are equal. + */ + static public void assertEquals(short expected, short actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(String message, int expected, int actual) { + assertEquals(message, new Integer(expected), new Integer(actual)); + } + /** + * Asserts that two ints are equal. + */ + static public void assertEquals(int expected, int actual) { + assertEquals(null, expected, actual); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(Object object) { + assertNotNull(null, object); + } + /** + * Asserts that an object isn't null. + */ + static public void assertNotNull(String message, Object object) { + assertTrue(message, object != null); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(Object object) { + assertNull(null, object); + } + /** + * Asserts that an object is null. + */ + static public void assertNull(String message, Object object) { + assertTrue(message, object == null); + } + /** + * Asserts that two objects refer to the same object. If they are not + * an AssertionFailedError is thrown. + */ + static public void assertSame(String message, Object expected, Object actual) { + if (expected == actual) + return; + failNotSame(message, expected, actual); + } + /** + * Asserts that two objects refer to the same object. If they are not + * the same an AssertionFailedError is thrown. + */ + static public void assertSame(Object expected, Object actual) { + assertSame(null, expected, actual); + } + + static private void failNotEquals(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected:<"+expected+"> but was:<"+actual+">"); + } + + static private void failNotSame(String message, Object expected, Object actual) { + String formatted= ""; + if (message != null) + formatted= message+" "; + fail(formatted+"expected same"); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/AssertionFailedError.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/AssertionFailedError.java new file mode 100644 index 0000000000..fbfdb42df3 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/AssertionFailedError.java @@ -0,0 +1,13 @@ +package com.coderising.litejunit.v2; + +/** + * Thrown when an assertion failed. + */ +public class AssertionFailedError extends Error { + + public AssertionFailedError () { + } + public AssertionFailedError (String message) { + super (message); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/Protectable.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Protectable.java new file mode 100644 index 0000000000..1b5b177e5f --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Protectable.java @@ -0,0 +1,14 @@ +package com.coderising.litejunit.v2; + +/** + * A Protectable can be run and can throw a Throwable. + * + * @see TestResult + */ +public interface Protectable { + + /** + * Run the the following method protected. + */ + public abstract void protect() throws Throwable; +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/Test.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Test.java new file mode 100644 index 0000000000..0dcdcb0726 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/Test.java @@ -0,0 +1,6 @@ +package com.coderising.litejunit.v2; + +public interface Test { + public abstract int countTestCases(); + public void run(TestResult tr); +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestCase.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestCase.java new file mode 100644 index 0000000000..b282ff5985 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestCase.java @@ -0,0 +1,64 @@ +package com.coderising.litejunit.v2; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + + + +public abstract class TestCase extends Assert implements Test { + private String name; + + + public TestCase(String name) { + this.name = name; + } + + + public int countTestCases() { + return 1; + } + + protected void runTest() throws Throwable{ + Method runMethod= null; + try { + runMethod= getClass().getMethod(name, null); + } catch (NoSuchMethodException e) { + fail("Method \""+name+"\" not found"); + } + if (!Modifier.isPublic(runMethod.getModifiers())) { + fail("Method \""+name+"\" should be public"); + } + + try { + runMethod.invoke(this, new Class[0]); + } + catch (InvocationTargetException e) { + e.fillInStackTrace(); + throw e.getTargetException(); + } + catch (IllegalAccessException e) { + e.fillInStackTrace(); + throw e; + } + } + + protected void setUp() { + } + + protected void tearDown() { + } + + public void run(TestResult tr) { + tr.run(this); + } + public void doRun() throws Throwable{ + setUp(); + try{ + runTest(); + } + finally{ + tearDown(); + } + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestFailure.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestFailure.java new file mode 100644 index 0000000000..ddacec71fd --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestFailure.java @@ -0,0 +1,39 @@ +package com.coderising.litejunit.v2; + +/** + * A TestFailure collects a failed test together with + * the caught exception. + * @see TestResult + */ +public class TestFailure { + protected Test failedTest; + protected Throwable thrownException; + + /** + * Constructs a TestFailure with the given test and exception. + */ + public TestFailure(Test failedTest, Throwable thrownException) { + this.failedTest= failedTest; + this.thrownException= thrownException; + } + /** + * Gets the failed test. + */ + public Test failedTest() { + return failedTest; + } + /** + * Gets the thrown exception. + */ + public Throwable thrownException() { + return thrownException; + } + /** + * Returns a short description of the failure. + */ + public String toString() { + StringBuffer buffer= new StringBuffer(); + buffer.append(failedTest+": "+thrownException.getMessage()); + return buffer.toString(); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestListener.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestListener.java new file mode 100644 index 0000000000..7e765d4066 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestListener.java @@ -0,0 +1,15 @@ +package com.coderising.litejunit.v2; + +/** + * A Listener for test progress + */ +public interface TestListener { + + public void addError(Test test, Throwable t); + + public void addFailure(Test test, AssertionFailedError t); + + public void endTest(Test test); + + public void startTest(Test test); +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestSuite.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestSuite.java new file mode 100644 index 0000000000..11a6094ed1 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/TestSuite.java @@ -0,0 +1,133 @@ +package com.coderising.litejunit.v2; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; + +import java.util.Iterator; +import java.util.List; +import java.util.Vector; + + +public class TestSuite extends Assert implements Test { + private List tests= new ArrayList<>(10); + private String name; + public TestSuite(){ + + } + public TestSuite(final Class theClass) { + this.name= theClass.getName(); + Constructor constructor= null; + try { + constructor= getConstructor(theClass); + } catch (NoSuchMethodException e) { + addTest(warning("Class "+theClass.getName()+" has no public constructor TestCase(String name)")); + return; + } + + if (!Modifier.isPublic(theClass.getModifiers())) { + addTest(warning("Class "+theClass.getName()+" is not public")); + return; + } + + Vector names= new Vector<>(); + Method[] methods= theClass.getDeclaredMethods(); + for (int i= 0; i < methods.length; i++) { + addTestMethod(methods[i], names, constructor); + } + + if (tests.size() == 0) + addTest(warning("No tests found in "+theClass.getName())); + } + + public TestSuite(String name) { + this.name = name; + } + private Constructor getConstructor(Class theClass) throws NoSuchMethodException { + Class[] args= { String.class }; + return theClass.getConstructor(args); + } + private void addTestMethod(Method m, Vector names, Constructor constructor) { + String name= m.getName(); + if (names.contains(name)) + return; + if (isPublicTestMethod(m)) { + names.addElement(name); + + Object[] args= new Object[]{name}; + try { + addTest((Test)constructor.newInstance(args)); + } catch (InstantiationException e) { + addTest(warning("Cannot instantiate test case: "+name+" ("+exceptionToString(e)+")")); + } catch (InvocationTargetException e) { + addTest(warning("Exception in constructor: "+name+" ("+exceptionToString(e.getTargetException())+")")); + } catch (IllegalAccessException e) { + addTest(warning("Cannot access test case: "+name+" ("+exceptionToString(e)+")")); + } + + } else { // almost a test method + if (isTestMethod(m)) + addTest(warning("Test method isn't public: "+m.getName())); + } + } + private boolean isPublicTestMethod(Method m) { + return isTestMethod(m) && Modifier.isPublic(m.getModifiers()); + } + private boolean isTestMethod(Method m) { + String name= m.getName(); + Class[] parameters= m.getParameterTypes(); + Class returnType= m.getReturnType(); + return parameters.length == 0 && name.startsWith("test") && returnType.equals(Void.TYPE); + } + public void addTest(Test test) { + tests.add(test); + } + public void addTestSuite(Class testClass) { + addTest(new TestSuite(testClass)); + } + private Test warning(final String message) { + return new TestCase("warning") { + public void doRun() { + fail(message); + } + }; + } + private String exceptionToString(Throwable t) { + StringWriter stringWriter= new StringWriter(); + PrintWriter writer= new PrintWriter(stringWriter); + t.printStackTrace(writer); + return stringWriter.toString(); + + } + + + + @Override + public void run(TestResult result) { + for (Iterator e= tests(); e.hasNext(); ) { + if (result.shouldStop() ){ + break; + } + Test test= (Test)e.next(); + test.run(result); + } + + } + + public int countTestCases() { + int count= 0; + + for (Iterator e= tests(); e.hasNext(); ) { + Test test= e.next(); + count= count + test.countTestCases(); + } + return count; + } + public Iterator tests() { + return tests.iterator(); + } +} \ No newline at end of file diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/runner/BaseTestRunner.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/runner/BaseTestRunner.java new file mode 100644 index 0000000000..bbd63e10a0 --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/runner/BaseTestRunner.java @@ -0,0 +1,85 @@ +package com.coderising.litejunit.v2.runner; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.NumberFormat; + +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestListener; +import com.coderising.litejunit.v2.TestSuite; + + +public abstract class BaseTestRunner implements TestListener { + public static final String SUITE_METHODNAME= "suite"; + /** + * Returns a filtered stack trace + */ + public static String getFilteredTrace(Throwable t) { + StringWriter stringWriter= new StringWriter(); + PrintWriter writer= new PrintWriter(stringWriter); + t.printStackTrace(writer); + StringBuffer buffer= stringWriter.getBuffer(); + String trace= buffer.toString(); + return trace; + //return BaseTestRunner.filterStack(trace); + } + + public Test getTest(String suiteClassName) { + if (suiteClassName.length() <= 0) { + return null; + } + Class testClass= null; + try { + testClass= loadSuiteClass(suiteClassName); + } catch (ClassNotFoundException e) { + String clazz= e.getMessage(); + if (clazz == null) + clazz= suiteClassName; + runFailed("Class not found \""+clazz+"\""); + return null; + } catch(Exception e) { + runFailed("Error: "+e.toString()); + return null; + } + Method suiteMethod= null; + try { + suiteMethod= testClass.getMethod(SUITE_METHODNAME, new Class[0]); + } catch(Exception e) { + // try to extract a test suite automatically + //clearStatus(); + return new TestSuite(testClass); + } + Test test= null; + try { + test= (Test)suiteMethod.invoke(null, new Class[0]); // static method + if (test == null) + return test; + } + catch (InvocationTargetException e) { + runFailed("Failed to invoke suite():" + e.getTargetException().toString()); + return null; + } + catch (IllegalAccessException e) { + runFailed("Failed to invoke suite():" + e.toString()); + return null; + } + + //clearStatus(); + return test; + } + protected Class loadSuiteClass(String suiteClassName) throws ClassNotFoundException { + + //TODO + return Class.forName(suiteClassName); + + + //return getLoader().load(suiteClassName); + } + protected abstract void runFailed(String message); + + public String elapsedTimeAsString(long runTime) { + return NumberFormat.getInstance().format((double)runTime/1000); + } +} diff --git a/students/1158154002/src/main/java/com/coderising/litejunit/v2/textui/TestRunner.java b/students/1158154002/src/main/java/com/coderising/litejunit/v2/textui/TestRunner.java new file mode 100644 index 0000000000..01b4c20a4c --- /dev/null +++ b/students/1158154002/src/main/java/com/coderising/litejunit/v2/textui/TestRunner.java @@ -0,0 +1,196 @@ +package com.coderising.litejunit.v2.textui; + +import java.io.PrintStream; +import java.util.Iterator; + +import com.coderising.litejunit.v2.AssertionFailedError; +import com.coderising.litejunit.v2.Test; +import com.coderising.litejunit.v2.TestFailure; +import com.coderising.litejunit.v2.TestResult; +import com.coderising.litejunit.v2.TestSuite; +import com.coderising.litejunit.v2.runner.BaseTestRunner; + + +public class TestRunner extends BaseTestRunner { + PrintStream writer= System.out; + int column= 0; + + /** + * Constructs a TestRunner. + */ + public TestRunner() { + } + + + /** + * Always use the StandardTestSuiteLoader. Overridden from + * BaseTestRunner. + */ + /*public TestSuiteLoader getLoader() { + return new StandardTestSuiteLoader(); + }*/ + + public synchronized void addError(Test test, Throwable t) { + writer().print("E"); + } + + public synchronized void addFailure(Test test, AssertionFailedError t) { + writer().print("F"); + } + + public TestResult doRun(Test suite) { + TestResult result= new TestResult(); + result.addListener(this); + long startTime= System.currentTimeMillis(); + suite.run(result); + long endTime= System.currentTimeMillis(); + long runTime= endTime-startTime; + writer().println(); + writer().println("Time: "+elapsedTimeAsString(runTime)); + print(result); + + writer().println(); + + + return result; + } + + + + public synchronized void startTest(Test test) { + writer().print("."); + if (column++ >= 40) { + writer().println(); + column= 0; + } + } + + public void endTest(Test test) { + } + + public static void main(String args[]) { + TestRunner testRunner= new TestRunner(); + try { + TestResult r= testRunner.start(args); + if (!r.wasSuccessful()) + System.exit(-1); + System.exit(0); + } catch(Exception e) { + System.err.println(e.getMessage()); + System.exit(-2); + } + } + /** + * Prints failures to the standard output + */ + public synchronized void print(TestResult result) { + printErrors(result); + printFailures(result); + printHeader(result); + } + /** + * Prints the errors to the standard output + */ + public void printErrors(TestResult result) { + if (result.errorCount() != 0) { + if (result.errorCount() == 1) + writer().println("There was "+result.errorCount()+" error:"); + else + writer().println("There were "+result.errorCount()+" errors:"); + + int i= 1; + for (Iterator e= result.errors(); e.hasNext(); i++) { + TestFailure failure= e.next(); + writer().println(i+") "+failure.failedTest()); + writer().print(getFilteredTrace(failure.thrownException())); + } + } + } + /** + * Prints failures to the standard output + */ + public void printFailures(TestResult result) { + if (result.failureCount() != 0) { + if (result.failureCount() == 1) + writer().println("There was " + result.failureCount() + " failure:"); + else + writer().println("There were " + result.failureCount() + " failures:"); + int i = 1; + for (Iterator e= result.failures(); e.hasNext(); i++) { + TestFailure failure= (TestFailure) e.next(); + writer().print(i + ") " + failure.failedTest()); + Throwable t= failure.thrownException(); + writer().print(getFilteredTrace(failure.thrownException())); + } + } + } + /** + * Prints the header of the report + */ + public void printHeader(TestResult result) { + if (result.wasSuccessful()) { + writer().println(); + writer().print("OK"); + writer().println (" (" + result.runCount() + " tests)"); + + } else { + writer().println(); + writer().println("FAILURES!!!"); + writer().println("Tests run: "+result.runCount()+ + ", Failures: "+result.failureCount()+ + ", Errors: "+result.errorCount()); + } + } + + + /** + * Starts a test run. Analyzes the command line arguments + * and runs the given test suite. + */ + protected TestResult start(String args[]) throws Exception { + if(args.length == 0){ + throw new Exception("Usage: TestRunner testCaseName"); + } + String testCase= args[0]; + + try { + Test suite= getTest(testCase); + return doRun(suite); + } + catch(Exception e) { + throw new Exception("Could not create and run test suite: "+e); + } + } + + protected void runFailed(String message) { + System.err.println(message); + System.exit(-1); + } + + /** + * Runs a suite extracted from a TestCase subclass. + */ + static public void run(Class testClass) { + run(new TestSuite(testClass)); + } + /** + * Runs a single test and collects its results. + * This method can be used to start a test run + * from your program. + *

+	 * public static void main (String[] args) {
+	 *     test.textui.TestRunner.run(suite());
+	 * }
+	 * 
+ */ + static public void run(Test suite) { + TestRunner aTestRunner= new TestRunner(); + aTestRunner.doRun(suite); + } + + protected PrintStream writer() { + return writer; + } + + +} \ No newline at end of file