Lab 2

Lab 2 SVV

JUnit 2

Let us consider the class below

   1 class VVSDate {
   2     public VVSDate(int day, int month, int year);
   3     public int getDays(VVSDate other);
   4 }

Implement a class VVSDate having a method meant to return the distance (pozitive or negative) in days between the date represented by the current object and another date, passed as parameter.

Start by writing a test class VVSDateTest and define there at least 2 tests before starting the actual implementation of VVSDate.

Keep adding tests and functionality in parallel. Do so until you end up with a test suite you consider to properly cover all representative/relevant cases.

To find the actual number of days, you can perform a google search for "days between dates", but you should keep in mind that Google considers the differences between the Gregorian and Julian calendars, and the fact that the Gregorian calendar started being used since 1582, while VVSDate simply considers the Gregorian calendar as having been always in use.

If during the implementation of VVSDate you will also define other auxiliary methods, unit tests will always be written before their implementation (for example the method that identifies an year as bissextile or not).

   1 public class VVSDate {
   2         private int day,month,year;
   3 
   4         public VVSDate(int day, int month, int year) {
   5                 this.day = day;
   6                 this.month = month;
   7                 this.year = year;
   8         }
   9 
  10         public int getDays(VVSDate other) {
  11                 return daysSinceZero() - other.daysSinceZero();
  12         }
  13 
  14         private int daysSinceZero() {
  15                 int[] daySums = new int[]{0,31,59,90,120,151,181,212,243,273,304,334,365};
  16                 int d = day + daySums[month-1] + (year*365) + year/4 - year/100 + year/400;
  17                 if ((year%400==0 || (year%4==0 && year%100!=0)) && month<=2)
  18                         d--;
  19                 return d;
  20         }
  21 
  22 }

Alternate Problem

Let us consider the class below

   1 class Promotion {
   2    private Calendar startDate = null;
   3    private Calendar endDate = null;
   4    private String weekdays = "1,2,3...7"; //1=Monday
   5    private String startHour = "10:30";
   6    private String endHour = "12:00";
   7 
   8    public boolean isActive(){
   9        Calendar now = Calendar.getInstance();
  10        return isActiveHelper(now);
  11    }
  12 
  13    boolean isActiveHelper(Calendar c){
  14        ...
  15    }
  16 }

Class Promotion has a method isActive(), which returns true if the promotion object is currently active, and false otherwise. While tests written for this method might pass in the beginning but end up failling later on, as the promotion gets deactivated etc., method isActiveHelper() has been added, which can be tested via its argument of Calendar type.

Do implement the method boolean isActiveHelper(Calendar c) taking into account that each class attribute can be null. See the example below.

   1 Promotion(null, null, "1,2", "08:00", "10:00"); //active on any Monday and Tuesday from 8 to 12 AM.
   2 Calendar d1 = Calendar.getInstance();
   3 d1.set(2011,d1.NOVEMBER,1);
   4 Promotion(null, d1, "6,7", null, "12:00"); //active until the 1st of November 2011 in weekend until 12 AM.

Follow the below principles:

  • write tests and code in parallel, beginning with the tests
  • give your tests meaningful names, that would descrive the tested case
  • write a separate test for each thing you wish to test

Go on until you have reached a test suite you consider to properly cover all the relevant/representative cases.

Some useful links for implementation:
https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.Builder.html