敏捷开发学习之一

/**
 * 
 
*/

package  test.student;


import   static  org.junit.Assert.assertEquals;

import  org.junit.After;
import  org.junit.Before;
import  org.junit.Test;

import  student.Student;

/**
 * 
@author 
 *
 
*/

public   class  StudentTest  {

    
/**
     * 
@throws java.lang.Exception
     
*/

    @Before
    
public void setUp() throws Exception {
    }


    
/**
     * 
@throws java.lang.Exception
     
*/

    @After
    
public void tearDown() throws Exception {
    }

    @Test
    
public void create()throws Exception {
        Student student
=new Student("new name");
        String name
=student.getName();
        assertEquals(
"new name",name);
        
        Student student1
=new Student("new1 name");
        String name1
=student1.getName();
        assertEquals(
"new1 name",name1);
    }

}

/**
 * 
 
*/

package  test.student;

import   static  org.junit.Assert.assertEquals;

import  java.util.ArrayList;
import  java.util.Date;

import  org.junit.Before;
import  org.junit.Test;

import  student.CourseSession;
import  student.DateUtil;
import  student.Student;


/**
 * 
@author 
 *
 
*/

public   class  CourseSessionTest  {
    
final String name1="ENG";
    
final String number1="101";
    CourseSession courseSession;
    @Before
    
public void setUp(){
        
int year=2008;
        
int month=3;
        
int day=11;
        Date startDate
=DateUtil.createDate(year,month,day);
         courseSession
=new CourseSession(name1,number1,startDate);
    }

    @Test
    
public void create(){
        assertEquals(name1,courseSession.getDepartment());
        assertEquals(number1,courseSession.getNumber());
        assertEquals(
0,courseSession.getNumberOfStudent());
    }

    @Test
    
public void enrollStudent(){
//        CourseSession courseSession=new CourseSession(name1,number1);
        Student student1=new Student("student1");
        courseSession.enroll(student1);
        courseSession.enroll(student1);
        assertEquals(
2,courseSession.getNumberOfStudent());
        
        ArrayList 
<Student> list=courseSession.getAllStudents();
        assertEquals(
2,courseSession.getNumberOfStudent());
        assertEquals(student1,list.get(
0));
        
    }

    @Test
    
public void courseDate(){

         
int year=2008;
         
int month=6;
         
int day=29;
         Date sixteenWeeksOut
=DateUtil.createDate(year,month,day);
         assertEquals(sixteenWeeksOut,courseSession.getEndDate());
    }

    
    
    
    
    
    
}

package  test.student;

import   static  org.junit.Assert.assertEquals;

import  java.util.Calendar;
import  java.util.Date;
import  java.util.GregorianCalendar;

import  org.junit.Before;
import  org.junit.Test;

import  student.CourseSession;
import  student.DateUtil;
import  student.RosterReporter;
import  student.Student;


public   class  RosterReporterTest  {
    
final String name1="ENG";
    
final String number1="101";
    CourseSession courseSession;
    @Before
    
public void setUp(){
        
int year=2008;
        
int month=3;
        
int day=11;
        Date startDate
=DateUtil.createDate(year,month,day);
         courseSession
=new CourseSession(name1,number1,startDate);
    }

    @Test 
    
public void rosterReport(){
        courseSession.enroll(
new Student("a"));
        RosterReporter report
=new RosterReporter(courseSession);
        assertEquals(
"info:"+"a"+CourseSession.NEWLINE,report.getRosterReport());
    }

    
}

package  test.student;

import  org.junit.runner.RunWith;
import  org.junit.runners.Suite;
import  org.junit.runners.Suite.SuiteClasses;

import  junit.framework.Test;
import  junit.framework.TestSuite;
@RunWith(Suite.
class )
@SuiteClasses(
{
    StudentTest.
class,
    CourseSessionTest.
class,
    RosterReporterTest.
class
}
)
public   class  AllTests  {

//
// the class remains completely empty, 
    // being used only as a holder for the above annotations


}

package  student;

public   class  Student  {
    
private String name;
    
public Student(String name) {
        
this.name=name;
    }


    
public String getName() {
        
// TODO Auto-generated method stub
        return this.name;
    }


}


package  student;

import  java.util.ArrayList;
import  java.util.Calendar;
import  java.util.Date;
import  java.util.GregorianCalendar;
import  java.util.List;

public   class  CourseSession  {
    
private String department;
    
private String number;
    
private Date startDate;
    
public static final String NEWLINE =System.getProperty("line.separator");

    
private List <Student> students=new ArrayList<Student> ();
    
public CourseSession(String department, String number) {
        
this.department=department;
        
this.number=number;
    }


    
public CourseSession(String department, String number, Date startDate) {
        
this.department=department;
        
this.number=number;
        
this.startDate=startDate;
    }


    
public String getDepartment() {
        
return department;
    }


    
public String getNumber() {
        
return number;
    }


    
public int getNumberOfStudent() {
        
return students.size();
    }


    
public void enroll(Student student1) {
        students.add(student1);
    }


    
public ArrayList<Student> getAllStudents() {
        
return (ArrayList) students;
    }

    
    
public Student get( int index){
        
return this.students.get(index);
    }


    
public Date getEndDate() {
        Calendar calendar
=new GregorianCalendar();
        calendar.setTime(
this.startDate);
        
int days=16*7-3;
        calendar.add(Calendar.DAY_OF_YEAR,days );
        
return calendar.getTime();
    }


    

}



/**
 * 
 
*/

package  student;

/**
 * 
@author
 *
 
*/

public   class  RosterReporter  {
    
private CourseSession courseSession;

    
public RosterReporter(CourseSession courseSession) {
        
this.courseSession = courseSession;
    }

    
public String getRosterReport() {
        
// TODO Auto-generated method stub
        StringBuilder str=new StringBuilder();
        str.append(
"info:");
        
for(Student student: courseSession.getAllStudents()){
            str.append(student.getName());
            str.append(CourseSession.NEWLINE);
        }

        
return str.toString();
    }

}



package  student;

import  java.util.Calendar;
import  java.util.Date;
import  java.util.GregorianCalendar;

public   class  DateUtil  {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub

    }

    
public static Date createDate(int year,int month,int day){
        Calendar calendar 
=new GregorianCalendar();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        
return calendar.getTime();
    }

}

 

你可能感兴趣的:(敏捷开发学习之一)