Example #1
0
  /** Test method for {@link it.reexon.lib.date.DateUtils#average(it.reexon.lib.date.DateRange)}. */
  @Test
  public final void testAverageDateRange() {
    try {
      DateRange dateRange = new DateRange(date01, date05);
      Date averageDate = DateUtils.average(dateRange);
      Assert.assertEquals(date03.getTime(), averageDate.getTime());

      try {
        dateRange.setDateFrom(null);
        DateUtils.average(Collections.emptyList());
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        dateRange.setDateFrom(null);
        dateRange.setDateTo(null);
        DateUtils.average(Collections.emptyList());
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #2
0
  /** Test method for {@link it.reexon.lib.date.DateUtils#getDateEndMonth(java.util.Date)}. */
  @Test
  public final void testGetDateEndMonth() {
    try {
      Date endDate = DateUtils.getDateEndMonth(date10);
      Date expectedDate =
          new Calendar.Builder()
              .setDate(2016, 2, 31)
              .set(Calendar.HOUR_OF_DAY, 23)
              .set(Calendar.MINUTE, 59)
              .set(Calendar.SECOND, 59)
              .set(Calendar.MILLISECOND, 999)
              .build()
              .getTime();
      Assert.assertEquals(expectedDate.getTime(), endDate.getTime());

      try {
        DateUtils.getDateEndMonth(null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #3
0
  /**
   * Test method for {@link it.reexon.lib.date.DateUtils#between(java.util.Date, java.util.Date,
   * java.util.Date)}.
   */
  @Test
  public final void testBetween() {
    try {
      Boolean between = DateUtils.between(date05, date01, date10);
      Assert.assertTrue(between);

      between = DateUtils.between(date01, date05, date10);
      Assert.assertFalse(between);

      try {
        DateUtils.between(date05, null, date10);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        DateUtils.between(null, null, null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #4
0
 /** Test method for {@link it.reexon.lib.date.DateUtils#addOneDay(java.util.Date)}. */
 @Test
 public final void testAddOneDay() {
   try {
     Date dateReturned = DateUtils.addOneDay(date01);
     Assert.assertEquals(date02.getTime(), dateReturned.getTime());
     try {
       dateReturned = DateUtils.addOneDay(null);
       Assert.fail("Should have thrown an exception");
     } catch (Exception e) {
       Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
     }
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
     throw new RuntimeException(e);
   }
 }
Example #5
0
  /**
   * Test method for {@link it.reexon.lib.date.DateUtils#getDatesBetween(java.util.Date,
   * java.util.Date)}.
   */
  @Test
  public final void testGetDatesBetween() {
    try {
      List<Date> listOfDate = DateUtils.getDatesBetween(date01, date10);
      Assert.assertNotNull(listOfDate);
      Assert.assertEquals(10, listOfDate.size());
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 1)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 2)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 3)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 4)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 5)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 6)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 7)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 8)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 9)));
      Assert.assertTrue(listOfDate.contains(getDate(2016, 02, 10)));

      listOfDate = DateUtils.getDatesBetween(date01, date01);
      Assert.assertNotNull(listOfDate);
      Assert.assertEquals(1, listOfDate.size());
      Assert.assertEquals(getDate(2016, 02, 01).getTime(), listOfDate.get(0).getTime());

      try {
        DateUtils.getDatesBetween(null, date10);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        DateUtils.getDatesBetween(null, null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }

      try {
        DateUtils.getDatesBetween(date10, date01);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }

    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #6
0
  private static Date getDate(int year, int month, int day) {
    if (year < 0 || month < 0 || day < 0)
      throw new IllegalArgumentException("Values must be greater of 0");

    final Calendar c = Calendar.getInstance();
    c.set(year, month, day);

    return DateUtils.setTimeToBeginningOfDay(c).getTime();
  }
Example #7
0
  /** Test method for {@link it.reexon.lib.date.DateUtils#average(java.util.List)}. */
  @Test
  public final void testAverageListOfDate() {
    try {
      List<Date> dates =
          new LinkedList<Date>(ListUtils.createList(date01, date02, date03, date04, date05));
      Date averageDate = DateUtils.average(dates);
      Assert.assertEquals(date03.getTime(), averageDate.getTime());

      try {
        DateUtils.average(Collections.emptyList());
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #8
0
  /** Test method for {@link it.reexon.lib.date.DateUtils#getDateBeginningMonth(java.util.Date)}. */
  @Test
  public final void testGetDateBeginningMonth() {
    try {
      Date endDate = DateUtils.getDateBeginningMonth(date10);
      Calendar expectedCal = new Calendar.Builder().setInstant(date10).build();
      expectedCal.set(Calendar.DAY_OF_MONTH, 1);
      Assert.assertEquals(expectedCal.getTime().getTime(), endDate.getTime());

      try {
        DateUtils.getDateBeginningMonth(null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }
Example #9
0
 /** Test method for {@link it.reexon.lib.date.DateUtils#setTimeToEndofDay(java.util.Date)}. */
 @Test
 public final void testSetTimeToEndofDayDate() {
   try {
     Date timeToEndOfDay = DateUtils.setTimeToEndofDay(date01);
     Calendar cal = new Calendar.Builder().setInstant(timeToEndOfDay).build();
     cal.set(Calendar.HOUR_OF_DAY, 23);
     cal.set(Calendar.SECOND, 59);
     cal.set(Calendar.MILLISECOND, 999);
     Assert.assertEquals(cal.getTimeInMillis(), timeToEndOfDay.getTime());
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
     throw new RuntimeException(e);
   }
 }
Example #10
0
  /**
   * Test method for {@link it.reexon.lib.date.DateUtils#untilDate(java.util.List, java.util.Date)}.
   */
  @Test
  public final void testUntilDate() {
    try {
      List<Date> dates =
          new LinkedList<>(ListUtils.createList(date01, date02, date03, date04, date04, date05));
      Date dateReturned = DateUtils.untilDate(dates, date04);
      Assert.assertEquals(date04.getTime(), dateReturned.getTime());

      dates =
          new LinkedList<>(ListUtils.createList(date01, date02, date03, date04, date04, date05));
      dateReturned = DateUtils.untilDate(dates, date10);
      Assert.assertEquals(date05.getTime(), dateReturned.getTime());

      dates = new LinkedList<>(ListUtils.createList(date01, date02, date03, date05));
      dateReturned = DateUtils.untilDate(dates, date04);
      Assert.assertEquals(date03.getTime(), dateReturned.getTime());

      try {
        DateUtils.untilDate(null, date04);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        DateUtils.untilDate(null, null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        DateUtils.untilDate(Collections.emptyList(), null);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }
      try {
        DateUtils.untilDate(Collections.emptyList(), date04);
        Assert.fail("Should have thrown an exception");
      } catch (Exception e) {
        Assert.assertEquals(e.getClass(), IllegalArgumentException.class);
      }

    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      throw new RuntimeException(e);
    }
  }