Skip to content

Commit

Permalink
KNL-1565 Added tests for TimeRange (sakaiproject#4952)
Browse files Browse the repository at this point in the history
Simple tests of the behaviour of TimeRange.
  • Loading branch information
buckett authored and jonespm committed Nov 6, 2017
1 parent c151ca1 commit 21fb5ee
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Objects;
Expand Down Expand Up @@ -89,6 +90,17 @@ public void setUserLocaleService(UserLocaleServiceImpl userLocaleService) {
this.userLocaleService = userLocaleService;
}

// Can be injected for testing
private Clock clock = Clock.systemDefaultZone();

public void setClock(Clock clock) {
this.clock = clock;
}

public Clock getClock() {
return clock;
}

/**
* Final initialization, once all dependencies are set.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public MyTime(BasicTimeService timeService, String str)
public MyTime(BasicTimeService timeService)
{
this.timeService = timeService;
m_millisecondsSince = System.currentTimeMillis();
m_millisecondsSince = timeService.getClock().millis();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package org.sakaiproject.time.impl.test;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.sakaiproject.time.api.TimeRange;
import org.sakaiproject.time.api.TimeService;
import org.sakaiproject.time.api.UserTimeService;
import org.sakaiproject.time.impl.BasicTimeService;
import org.sakaiproject.time.impl.UserLocaleServiceImpl;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;

import static org.junit.Assert.assertEquals;

public class TimeRangeTest {

private TimeService timeService;
private Clock fixed;

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Mock
private UserLocaleServiceImpl userLocaleService;
@Mock
private UserTimeService userTimeService;

@Before
public void setUp() {
fixed = Clock.fixed(Instant.EPOCH, ZoneOffset.UTC);
BasicTimeService timeService = new BasicTimeService();
timeService.setUserLocaleService(userLocaleService);
timeService.setUserTimeService(userTimeService);
timeService.setClock(fixed);
timeService.init();
this.timeService = timeService;
}

@Test
public void testTimeRangeToStringStart() {
TimeRange timeRange ;
timeRange= timeService.newTimeRange(0, 0);
assertEquals("19700101000000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(0), true, true);
assertEquals("19700101000000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(0), false, true);
assertEquals("19700101000000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(0), true, false);
assertEquals("19700101000000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(0), false, false);
assertEquals("19700101000000000", timeRange.toString());
}

@Test
public void testTimeRangeToStringStartEnd() {
TimeRange timeRange = timeService.newTimeRange(0, 3600*1000);
assertEquals("19700101000000000-19700101010000000", timeRange.toString());
}

@Test
public void testTimeRangeToStringInclusions() {
TimeRange timeRange;
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), true, true);
assertEquals("19700101000000000-19700101010000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), false, true);
assertEquals("19700101000000000[19700101010000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), false, false);
assertEquals("19700101000000000~19700101010000000", timeRange.toString());
timeRange = timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), true, false);
assertEquals("19700101000000000]19700101010000000", timeRange.toString());
}

@Test
public void testTimeRangeParseRelative() {
TimeRange timeRange;
timeRange = timeService.newTimeRange("19700101000000000-=3600000");
assertEquals(timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), true, true), timeRange);
timeRange = timeService.newTimeRange("=3600000-19700101010000000");
assertEquals(timeService.newTimeRange(timeService.newTime(0), timeService.newTime(3600*1000), true, true), timeRange);
}

@Test
public void testTimeRangeParseInvalid() {
// invalid parses return now(our fixed clock)
TimeRange timeRange;
// 2 relative dates isn't going to work.
timeRange = timeService.newTimeRange("=1000-=1000");
assertEquals(fixed.millis(), timeRange.firstTime().getTime());
// Relative start time
timeRange = timeService.newTimeRange("=1000");
assertEquals(fixed.millis(), timeRange.firstTime().getTime());
// Junk
timeRange = timeService.newTimeRange("junk");
assertEquals(fixed.millis(), timeRange.firstTime().getTime());
}

@Test
public void testTimeRangeParseReversed() {
// TimeRange will swap times if end is before start.
TimeRange swapped = timeService.newTimeRange("19700101010000000-19700101000000000");
TimeRange correct = timeService.newTimeRange("19700101000000000-19700101010000000");
assertEquals(correct, swapped);
}

}

0 comments on commit 21fb5ee

Please sign in to comment.