Skip to content

Commit

Permalink
Add test to demonstrate that widget works well with a timezone provider
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham1g5 committed Oct 5, 2024
1 parent 4196b83 commit d0374b2
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions src/test/java/org/javarosa/xform/util/test/CalendarTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.javarosa.xform.util.test;

import org.javarosa.core.model.utils.DateUtils;
import org.javarosa.core.model.utils.TimezoneProvider;
import org.javarosa.core.services.locale.Localization;
import org.javarosa.core.services.locale.TableLocaleSource;
import org.javarosa.xform.util.CalendarUtils;
import org.javarosa.xform.util.UniversalDate;
import org.joda.time.DateTimeZone;
Expand All @@ -25,8 +29,10 @@ public void testTimesFallOnSameDate() {
Calendar nepaliBeginningOfDayDate = Calendar.getInstance(nepaliTimeZone.toTimeZone());
nepaliBeginningOfDayDate.set(2007, Calendar.JULY, 7, 0, 0);

UniversalDate middleOfDay = CalendarUtils.fromMillis(nepaliMiddleOfDayDate.getTimeInMillis(), nepaliTimeZone);
UniversalDate beginningOfDay = CalendarUtils.fromMillis(nepaliBeginningOfDayDate.getTimeInMillis(), nepaliTimeZone);
UniversalDate middleOfDay = CalendarUtils.fromMillis(nepaliMiddleOfDayDate.getTimeInMillis(),
nepaliTimeZone);
UniversalDate beginningOfDay = CalendarUtils.fromMillis(nepaliBeginningOfDayDate.getTimeInMillis(),
nepaliTimeZone);
assertSameDate(middleOfDay, beginningOfDay);

Calendar nepaliEndOfDayDate = Calendar.getInstance(nepaliTimeZone.toTimeZone());
Expand All @@ -35,6 +41,45 @@ public void testTimesFallOnSameDate() {
assertSameDate(endOfDay, beginningOfDay);
}

// millis <=> date is different in every timezone
@Test
public void testConvertToNepaliString() {
configureLocaleForCalendar();
MockTimeZoneProvider mockTimeZoneProvider = new MockTimeZoneProvider(TimeZone.getTimeZone("Europe/Madrid"));
DateUtils.setTimezoneProvider(mockTimeZoneProvider);
DateTimeZone timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset());
// this is what Nepali widget uses to calculate the millis from date fields
long millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone);
String nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null);
assertEquals( "16 Ashwin 2081", nepaliDateStr);


mockTimeZoneProvider.setTimezone(TimeZone.getTimeZone("Asia/Kathmandu"));
timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset());
millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone);
nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null);
assertEquals( "16 Ashwin 2081", nepaliDateStr);


mockTimeZoneProvider.setTimezone(TimeZone.getTimeZone("America/Chicago"));
timeZone = DateTimeZone.forOffsetMillis(mockTimeZoneProvider.getTimezone().getRawOffset());
millis = CalendarUtils.toMillisFromJavaEpoch(2081, 6, 16, timeZone);
nepaliDateStr = CalendarUtils.convertToNepaliString(new Date(millis), null);
assertEquals( "16 Ashwin 2081", nepaliDateStr);
DateUtils.setTimezoneProvider(null);
}

private void configureLocaleForCalendar() {
Localization.getGlobalLocalizerAdvanced().addAvailableLocale("default");
Localization.setLocale("default");
TableLocaleSource localeData = new TableLocaleSource();
localeData.setLocaleMapping("ethiopian_months",
"Mäskäräm,T’ïk’ïmt,Hïdar,Tahsas,T’ïr,Yäkatit,Mägabit,Miyaziya,Gïnbot,Säne,Hämle,Nähäse,P’agume");
localeData.setLocaleMapping("nepali_months",
"Baishakh,Jestha,Ashadh,Shrawan,Bhadra,Ashwin,Kartik,Mangsir,Poush,Magh,Falgun,Chaitra");
Localization.getGlobalLocalizerAdvanced().registerLocaleResource("default", localeData);
}

private static void assertSameDate(UniversalDate a, UniversalDate b) {
assertEquals(a.day, b.day);
assertEquals(a.month, b.month);
Expand Down Expand Up @@ -63,7 +108,8 @@ public void testUnpackingDateInDifferentTimezone() {
mexicoCal.set(2007, Calendar.JULY, 7, 18, 46);

UniversalDate mexicanDate = CalendarUtils.fromMillis(mexicoCal.getTimeInMillis(), mexicanTimeZone);
long time = CalendarUtils.toMillisFromJavaEpoch(mexicanDate.year, mexicanDate.month, mexicanDate.day, mexicanTimeZone);
long time = CalendarUtils.toMillisFromJavaEpoch(mexicanDate.year, mexicanDate.month, mexicanDate.day,
mexicanTimeZone);
UniversalDate rebuiltDateInUsingDifferentTimezone = CalendarUtils.fromMillis(time, nepaliTimeZone);
assertSameDate(rebuiltDateInUsingDifferentTimezone, mexicanDate);
}
Expand Down Expand Up @@ -103,4 +149,22 @@ public void serializeUniversalDateViaMillisTest() {
deserializedNepaliDate = CalendarUtils.fromMillis(date.getTime(), bostonTimeZone);
assertSameDate(nepaliDate, deserializedNepaliDate);
}

private class MockTimeZoneProvider extends TimezoneProvider {

private TimeZone timeZone;

public MockTimeZoneProvider(TimeZone timeZone) {
this.timeZone = timeZone;
}

public void setTimezone(TimeZone timeZone){
this.timeZone = timeZone;
}

@Override
public TimeZone getTimezone() {
return timeZone;
}
}
}

0 comments on commit d0374b2

Please sign in to comment.