Skip to content

Commit

Permalink
SAK-29214 Tests showing failure when time changes.
Browse files Browse the repository at this point in the history
Some times during the year there is no 01:00:00 time as the clocks are going forward. These tests show the bug happening in the Europe/London timezone in 2015.
  • Loading branch information
buckett committed Mar 30, 2015
1 parent 6df0f5a commit 2e933a5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
12 changes: 12 additions & 0 deletions calendar/calendar-util/util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
<groupId>org.sakaiproject.kernel</groupId>
<artifactId>sakai-kernel-api</artifactId>
</dependency>
<dependency>
<groupId>org.sakaiproject.kernel</groupId>
<artifactId>sakai-component-manager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.sakaiproject.kernel</groupId>
<artifactId>sakai-kernel-util</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,24 +548,32 @@ public Date getPrevTime(int days){

/**
* Get the String representing AM in the users Locale
* @return
* @return A String representing the morning for the current user.
*/
public static String getLocalAMString() {

return getLocalAMString(new DateTime());
}

// Used for tests
static String getLocalAMString(DateTime now) {
//we need an AM date
DateTime dt = new DateTime().withTime(1, 0, 0, 0);
DateTime dt = now.withTime(1, 0, 0, 0);
DateTimeFormatter df = new DateTimeFormatterBuilder().appendHalfdayOfDayText().toFormatter().withLocale(new ResourceLoader("calendar").getLocale());
return df.print(dt);
}

/**
* Get the string representing PM in the users Locale
* @return
* @return A String representing the afternoon for the current user.
*/
public static String getLocalPMString() {
return getLocalPMString(new DateTime());
}

// Used for tests
static String getLocalPMString(DateTime now) {
//we need an PM date
DateTime dt = new DateTime().withTime(14, 0, 0, 0);
DateTime dt = now.withTime(14, 0, 0, 0);
DateTimeFormatter df = new DateTimeFormatterBuilder().appendHalfdayOfDayText().toFormatter().withLocale(new ResourceLoader("calendar").getLocale());
return df.print(dt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.sakaiproject.util;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;

import java.util.Locale;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;

public class CalendarUtilTest {


@Before
public void setUp() {
// Need this so that developers in locales that don't use AM/PM have passing tests
// If you want to test this setting is working set it to Locale.JAPAN which should
// result in failing tests.
Locale.setDefault(Locale.ENGLISH);
}

@Test
public void testLocalAMString() {
// This is to test that the schedule tool works through GMT -> BST transitions
DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"));
// This a day that the clocks go forward in London
DateTime dateTime = new DateTime(2015, 3, 29, 3,0, zone);
assertEquals("AM", CalendarUtil.getLocalAMString(dateTime));
}

@Test
public void testLocalPMString() {
// This is to test that the schedule tool works through GMT -> BST transitions
DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"));
// This a day that the clocks go forward in London
DateTime dateTime = new DateTime(2015, 3, 29, 16,0, zone);
assertEquals("PM", CalendarUtil.getLocalPMString(dateTime));
}

@Test
public void testLocalPMStringStartOfDay() {
// This is to test that the schedule tool works through GMT -> BST transitions
DateTimeZone zone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Europe/London"));
// This a day that the clocks go forward in London
DateTime dateTime = new DateTime(2015, 3, 29, 0, 0, zone);
assertEquals("PM", CalendarUtil.getLocalPMString(dateTime));
}


}

0 comments on commit 2e933a5

Please sign in to comment.