PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/plugin/jql/function/TestEndOfWeekFunction.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 160 lines | 130 code | 21 blank | 9 comment | 6 complexity | eb6340b7f53995ac669eccbfa51f4cad MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.plugin.jql.function;
  2. import com.atlassian.core.util.Clock;
  3. import com.atlassian.jira.jql.operand.QueryLiteral;
  4. import com.atlassian.jira.util.ConstantClock;
  5. import com.atlassian.jira.util.I18nHelper;
  6. import com.atlassian.jira.web.bean.MockI18nBean;
  7. import com.atlassian.query.clause.TerminalClause;
  8. import com.atlassian.query.operand.FunctionOperand;
  9. import org.junit.Test;
  10. import java.util.Calendar;
  11. import java.util.Collections;
  12. import java.util.Date;
  13. import java.util.GregorianCalendar;
  14. import java.util.List;
  15. import java.util.Locale;
  16. import java.util.TimeZone;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.mockito.Mockito.when;
  20. /**
  21. * @since v4.3
  22. */
  23. public class TestEndOfWeekFunction extends AbstractDateFunctionTestCase {
  24. private TerminalClause terminalClause = null;
  25. @Test
  26. public void testGetValues() throws Exception {
  27. when(timeZoneManager.getLoggedInUserTimeZone()).thenReturn(TimeZone.getDefault());
  28. FunctionOperand function = new FunctionOperand(getFunctionName(), Collections.<String>emptyList());
  29. final List<QueryLiteral> value = getInstanceToTest().getValues(null, function, terminalClause);
  30. assertNotNull(value);
  31. assertEquals(1, value.size());
  32. GregorianCalendar now = new GregorianCalendar();
  33. if (Calendar.MONDAY == new GregorianCalendar().getFirstDayOfWeek()) {
  34. now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
  35. } else {
  36. now.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
  37. }
  38. GregorianCalendar cal = new GregorianCalendar();
  39. cal.setTimeInMillis(value.get(0).getLongValue().longValue());
  40. assertEquals(now.get(Calendar.YEAR), cal.get(Calendar.YEAR));
  41. assertEquals(now.get(Calendar.MONTH), cal.get(Calendar.MONTH));
  42. assertEquals(now.get(Calendar.WEEK_OF_MONTH), cal.get(Calendar.WEEK_OF_MONTH));
  43. if (Calendar.MONDAY == new GregorianCalendar().getFirstDayOfWeek()) {
  44. assertEquals(Calendar.SUNDAY, cal.get(Calendar.DAY_OF_WEEK));
  45. } else {
  46. assertEquals(Calendar.SATURDAY, cal.get(Calendar.DAY_OF_WEEK));
  47. }
  48. assertEquals(23, cal.get(Calendar.HOUR_OF_DAY));
  49. assertEquals(59, cal.get(Calendar.MINUTE));
  50. assertEquals(59, cal.get(Calendar.SECOND));
  51. assertEquals(function, value.get(0).getSourceOperand());
  52. }
  53. @Test
  54. public void testWithUserTimeZone() throws Exception {
  55. TimeZone userTimeZone = TimeZone.getTimeZone("Europe/Berlin");
  56. TimeZone systemTimeZone = TimeZone.getTimeZone("Australia/Sydney");
  57. GregorianCalendar expectedSystemTime = new GregorianCalendar(systemTimeZone);
  58. expectedSystemTime.set(Calendar.YEAR, 2011);
  59. expectedSystemTime.set(Calendar.MONTH, 3);
  60. expectedSystemTime.set(Calendar.DAY_OF_MONTH, 3);
  61. expectedSystemTime.set(Calendar.HOUR_OF_DAY, 7);
  62. expectedSystemTime.set(Calendar.MINUTE, 59);
  63. expectedSystemTime.set(Calendar.SECOND, 59);
  64. expectedSystemTime.getTime();
  65. when(timeZoneManager.getLoggedInUserTimeZone()).thenReturn(userTimeZone);
  66. GregorianCalendar systemTime = new GregorianCalendar(systemTimeZone);
  67. systemTime.set(Calendar.YEAR, 2011);
  68. systemTime.set(Calendar.MONTH, 3);
  69. systemTime.set(Calendar.DAY_OF_MONTH, 3);
  70. systemTime.set(Calendar.HOUR_OF_DAY, 4);
  71. systemTime.set(Calendar.MINUTE, 12);
  72. systemTime.set(Calendar.SECOND, 12);
  73. systemTime.getTime();
  74. doTest(expectedSystemTime, systemTime, systemTimeZone);
  75. }
  76. @Test
  77. public void testknowDates() throws Exception {
  78. when(timeZoneManager.getLoggedInUserTimeZone()).thenReturn(TimeZone.getDefault());
  79. Locale saveLocale = Locale.getDefault();
  80. try {
  81. // First day is Monday
  82. Locale.setDefault(new Locale("fr", "FR"));
  83. assertEquals(Calendar.MONDAY, new GregorianCalendar().getFirstDayOfWeek());
  84. // Test Known wednesday to tuesday
  85. doTest(new GregorianCalendar(2010, 4, 16, 23, 59, 59), new GregorianCalendar(2010, 4, 12, 12, 30, 20), TimeZone.getDefault());
  86. doTest(new GregorianCalendar(2010, 4, 16, 23, 59, 59), new GregorianCalendar(2010, 4, 13, 12, 30, 20), TimeZone.getDefault());
  87. doTest(new GregorianCalendar(2010, 4, 16, 23, 59, 59), new GregorianCalendar(2010, 4, 14, 12, 30, 20), TimeZone.getDefault());
  88. doTest(new GregorianCalendar(2010, 4, 16, 23, 59, 59), new GregorianCalendar(2010, 4, 15, 12, 30, 20), TimeZone.getDefault());
  89. doTest(new GregorianCalendar(2010, 4, 16, 23, 59, 59), new GregorianCalendar(2010, 4, 16, 12, 30, 20), TimeZone.getDefault());
  90. doTest(new GregorianCalendar(2010, 4, 23, 23, 59, 59), new GregorianCalendar(2010, 4, 17, 12, 30, 20), TimeZone.getDefault());
  91. doTest(new GregorianCalendar(2010, 4, 23, 23, 59, 59), new GregorianCalendar(2010, 4, 18, 12, 30, 20), TimeZone.getDefault());
  92. // Some other days
  93. doTest(new GregorianCalendar(2001, 6, 29, 23, 59, 59), new GregorianCalendar(2001, 6, 25, 12, 30, 20), TimeZone.getDefault());
  94. doTest(new GregorianCalendar(1900, 0, 7, 23, 59, 59), new GregorianCalendar(1900, 0, 1, 23, 59, 59), TimeZone.getDefault());
  95. doTest(new GregorianCalendar(2004, 1, 29, 23, 59, 59), new GregorianCalendar(2004, 1, 29, 12, 30, 20), TimeZone.getDefault());
  96. doTest(new GregorianCalendar(2004, 1, 29, 23, 59, 59), new GregorianCalendar(2004, 1, 28, 12, 30, 20), TimeZone.getDefault());
  97. doTest(new GregorianCalendar(2099, 6, 26, 23, 59, 59), new GregorianCalendar(2099, 6, 25, 12, 30, 20), TimeZone.getDefault());
  98. doTest(new GregorianCalendar(2011, 0, 2, 23, 59, 59), new GregorianCalendar(2010, 11, 31, 23, 59, 59), TimeZone.getDefault());
  99. doTest(new GregorianCalendar(2012, 2, 4, 23, 59, 59), new GregorianCalendar(2012, 2, 1, 23, 59, 59), TimeZone.getDefault());
  100. // First day is Sunday
  101. Locale.setDefault(new Locale("en", "US"));
  102. // Test Known wednesday to tuesday
  103. doTest(new GregorianCalendar(2010, 4, 15, 23, 59, 59), new GregorianCalendar(2010, 4, 12, 12, 30, 20), TimeZone.getDefault());
  104. doTest(new GregorianCalendar(2010, 4, 15, 23, 59, 59), new GregorianCalendar(2010, 4, 13, 12, 30, 20), TimeZone.getDefault());
  105. doTest(new GregorianCalendar(2010, 4, 15, 23, 59, 59), new GregorianCalendar(2010, 4, 14, 12, 30, 20), TimeZone.getDefault());
  106. doTest(new GregorianCalendar(2010, 4, 15, 23, 59, 59), new GregorianCalendar(2010, 4, 15, 12, 30, 20), TimeZone.getDefault());
  107. doTest(new GregorianCalendar(2010, 4, 22, 23, 59, 59), new GregorianCalendar(2010, 4, 16, 12, 30, 20), TimeZone.getDefault());
  108. doTest(new GregorianCalendar(2010, 4, 22, 23, 59, 59), new GregorianCalendar(2010, 4, 17, 12, 30, 20), TimeZone.getDefault());
  109. doTest(new GregorianCalendar(2010, 4, 22, 23, 59, 59), new GregorianCalendar(2010, 4, 18, 12, 30, 20), TimeZone.getDefault());
  110. // Some other days
  111. doTest(new GregorianCalendar(2001, 6, 28, 23, 59, 59), new GregorianCalendar(2001, 6, 25, 12, 30, 20), TimeZone.getDefault());
  112. doTest(new GregorianCalendar(1900, 0, 6, 23, 59, 59), new GregorianCalendar(1900, 0, 1, 23, 59, 59), TimeZone.getDefault());
  113. doTest(new GregorianCalendar(2004, 2, 6, 23, 59, 59), new GregorianCalendar(2004, 1, 29, 12, 30, 20), TimeZone.getDefault());
  114. doTest(new GregorianCalendar(2004, 1, 28, 23, 59, 59), new GregorianCalendar(2004, 1, 28, 12, 30, 20), TimeZone.getDefault());
  115. doTest(new GregorianCalendar(2099, 6, 25, 23, 59, 59), new GregorianCalendar(2099, 6, 25, 12, 30, 20), TimeZone.getDefault());
  116. doTest(new GregorianCalendar(2011, 0, 1, 23, 59, 59), new GregorianCalendar(2010, 11, 31, 23, 59, 59), TimeZone.getDefault());
  117. doTest(new GregorianCalendar(2012, 2, 3, 23, 59, 59), new GregorianCalendar(2012, 2, 1, 23, 59, 59), TimeZone.getDefault());
  118. doTestwithIncrement("+1M", new GregorianCalendar(2010, 10, 20, 23, 59, 59), new GregorianCalendar(2010, 9, 18, 12, 30, 20));
  119. } finally {
  120. Locale.setDefault(saveLocale);
  121. }
  122. }
  123. @Override
  124. String getFunctionName() {
  125. return EndOfWeekFunction.FUNCTION_END_OF_WEEK;
  126. }
  127. @Override
  128. AbstractDateFunction getInstanceToTest() {
  129. return getInstanceToTest(new ConstantClock(new Date()));
  130. }
  131. AbstractDateFunction getInstanceToTest(Clock clock) {
  132. return new EndOfWeekFunction(clock, timeZoneManager) {
  133. @Override
  134. protected I18nHelper getI18n() {
  135. return new MockI18nBean();
  136. }
  137. };
  138. }
  139. }