PageRenderTime 55ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/org/ocpsoft/prettytime/PrettyTimeI18n_AR_Test.java

http://github.com/ocpsoft/prettytime
Java | 295 lines | 234 code | 38 blank | 23 comment | 0 complexity | 2c9dac4a01ed22470ef29b8aa8a77b34 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.ocpsoft.prettytime;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertTrue;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Locale;
  23. import org.junit.After;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.ocpsoft.prettytime.Duration;
  27. import org.ocpsoft.prettytime.PrettyTime;
  28. import org.ocpsoft.prettytime.TimeUnit;
  29. import org.ocpsoft.prettytime.format.SimpleTimeFormat;
  30. public class PrettyTimeI18n_AR_Test
  31. {
  32. SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
  33. // Stores current locale so that it can be restored
  34. private Locale locale;
  35. // Method setUp() is called automatically before every test method
  36. @Before
  37. public void setUp() throws Exception
  38. {
  39. locale = new Locale("ar");
  40. Locale.setDefault(locale);
  41. }
  42. @Test
  43. public void testCeilingInterval() throws Exception
  44. {
  45. Date then = format.parse("5/20/2009");
  46. Date ref = format.parse("6/17/2009");
  47. PrettyTime t = new PrettyTime(ref);
  48. assertEquals("1 ??? ???", t.format(then));
  49. }
  50. @Test
  51. public void testNullDate() throws Exception
  52. {
  53. PrettyTime t = new PrettyTime();
  54. Date date = null;
  55. assertEquals("??? ?????", t.format(date));
  56. }
  57. @Test
  58. public void testRightNow() throws Exception
  59. {
  60. PrettyTime t = new PrettyTime();
  61. assertEquals("??? ?????", t.format(new Date()));
  62. }
  63. @Test
  64. public void testRightNowVariance() throws Exception
  65. {
  66. PrettyTime t = new PrettyTime(new Date(0));
  67. assertEquals("??? ?????", t.format(new Date(600)));
  68. }
  69. @Test
  70. public void testMinutesFromNow() throws Exception
  71. {
  72. PrettyTime t = new PrettyTime(new Date(0));
  73. assertEquals("12 ????? ?? ????", t.format(new Date(1000 * 60 * 12)));
  74. }
  75. @Test
  76. public void testHoursFromNow() throws Exception
  77. {
  78. PrettyTime t = new PrettyTime(new Date(0));
  79. assertEquals("3 ????? ?? ????", t.format(new Date(1000 * 60 * 60 * 3)));
  80. }
  81. @Test
  82. public void testDaysFromNow() throws Exception
  83. {
  84. PrettyTime t = new PrettyTime(new Date(0));
  85. assertEquals("3 ???? ?? ????", t.format(new Date(1000 * 60 * 60 * 24 * 3)));
  86. }
  87. @Test
  88. public void testWeeksFromNow() throws Exception
  89. {
  90. PrettyTime t = new PrettyTime(new Date(0));
  91. assertEquals("3 ?????? ?? ????", t.format(new Date(1000 * 60 * 60 * 24 * 7 * 3)));
  92. }
  93. @Test
  94. public void testMonthsFromNow() throws Exception
  95. {
  96. PrettyTime t = new PrettyTime(new Date(0));
  97. assertEquals("3 ???? ?? ????", t.format(new Date(2629743830L * 3L)));
  98. }
  99. @Test
  100. public void testYearsFromNow() throws Exception
  101. {
  102. PrettyTime t = new PrettyTime(new Date(0));
  103. assertEquals("3 ????? ?? ????", t.format(new Date(2629743830L * 12L * 3L)));
  104. }
  105. @Test
  106. public void testDecadesFromNow() throws Exception
  107. {
  108. PrettyTime t = new PrettyTime(new Date(0));
  109. assertEquals("3 ???? ?? ????", t.format(new Date(315569259747L * 3L)));
  110. }
  111. @Test
  112. public void testCenturiesFromNow() throws Exception
  113. {
  114. PrettyTime t = new PrettyTime(new Date(0));
  115. assertEquals("3 ???? ?? ????", t.format(new Date(3155692597470L * 3L)));
  116. }
  117. /*
  118. * Past
  119. */
  120. @Test
  121. public void testMomentsAgo() throws Exception
  122. {
  123. PrettyTime t = new PrettyTime(new Date(6000));
  124. assertEquals("??? ?????", t.format(new Date(0)));
  125. }
  126. @Test
  127. public void testMinutesAgo() throws Exception
  128. {
  129. PrettyTime t = new PrettyTime(new Date(1000 * 60 * 12));
  130. assertEquals("12 ????? ???", t.format(new Date(0)));
  131. }
  132. @Test
  133. public void testHoursAgo() throws Exception
  134. {
  135. PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 3));
  136. assertEquals("3 ????? ???", t.format(new Date(0)));
  137. }
  138. @Test
  139. public void testDaysAgo() throws Exception
  140. {
  141. PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 3));
  142. assertEquals("3 ???? ???", t.format(new Date(0)));
  143. }
  144. @Test
  145. public void testWeeksAgo() throws Exception
  146. {
  147. PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 7 * 3));
  148. assertEquals("3 ?????? ???", t.format(new Date(0)));
  149. }
  150. @Test
  151. public void testMonthsAgo() throws Exception
  152. {
  153. PrettyTime t = new PrettyTime(new Date(2629743830L * 3L));
  154. assertEquals("3 ???? ???", t.format(new Date(0)));
  155. }
  156. @Test
  157. public void testCustomFormat() throws Exception
  158. {
  159. PrettyTime t = new PrettyTime(new Date(0));
  160. TimeUnit unit = new TimeUnit()
  161. {
  162. @Override
  163. public long getMaxQuantity()
  164. {
  165. return 0;
  166. }
  167. @Override
  168. public long getMillisPerUnit()
  169. {
  170. return 5000;
  171. }
  172. };
  173. t.clearUnits();
  174. t.registerUnit(unit, new SimpleTimeFormat()
  175. .setName("tick").setPluralName("ticks")
  176. .setPattern("%n %u").setRoundingTolerance(20)
  177. .setFutureSuffix("... RUN!")
  178. .setFuturePrefix("self destruct in: ").setPastPrefix("self destruct was: ").setPastSuffix(
  179. " ago..."));
  180. assertEquals("self destruct in: 5 ticks ... RUN!", t.format(new Date(25000)));
  181. t.setReference(new Date(25000));
  182. assertEquals("self destruct was: 5 ticks ago...", t.format(new Date(0)));
  183. }
  184. @Test
  185. public void testYearsAgo() throws Exception
  186. {
  187. PrettyTime t = new PrettyTime(new Date(2629743830L * 12L * 3L));
  188. assertEquals("3 ????? ???", t.format(new Date(0)));
  189. }
  190. @Test
  191. public void testDecadesAgo() throws Exception
  192. {
  193. PrettyTime t = new PrettyTime(new Date(315569259747L * 3L));
  194. assertEquals("3 ???? ???", t.format(new Date(0)));
  195. }
  196. @Test
  197. public void testCenturiesAgo() throws Exception
  198. {
  199. PrettyTime t = new PrettyTime(new Date(3155692597470L * 3L));
  200. assertEquals("3 ???? ???", t.format(new Date(0)));
  201. }
  202. @Test
  203. public void testWithinTwoHoursRounding() throws Exception
  204. {
  205. PrettyTime t = new PrettyTime();
  206. assertEquals("2 ????? ???", t.format(new Date(new Date().getTime() - 6543990)));
  207. }
  208. @Test
  209. public void testPreciseInTheFuture() throws Exception
  210. {
  211. PrettyTime t = new PrettyTime();
  212. List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() + 1000
  213. * (10 * 60 + 5 * 60 * 60)));
  214. assertTrue(durations.size() >= 2); // might be more because of milliseconds between date capturing and result
  215. // calculation
  216. assertEquals(5, durations.get(0).getQuantity());
  217. assertEquals(10, durations.get(1).getQuantity());
  218. }
  219. @Test
  220. public void testPreciseInThePast() throws Exception
  221. {
  222. PrettyTime t = new PrettyTime();
  223. List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() - 1000
  224. * (10 * 60 + 5 * 60 * 60)));
  225. assertTrue(durations.size() >= 2); // might be more because of milliseconds between date capturing and result
  226. // calculation
  227. assertEquals(-5, durations.get(0).getQuantity());
  228. assertEquals(-10, durations.get(1).getQuantity());
  229. }
  230. @Test
  231. public void testFormattingDurationListInThePast() throws Exception
  232. {
  233. PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 3 + 1000 * 60 * 60 * 15 + 1000 * 60 * 38));
  234. List<Duration> durations = t.calculatePreciseDuration(new Date(0));
  235. assertEquals("3 ???? 15 ????? 38 ????? ???", t.format(durations));
  236. }
  237. @Test
  238. public void testFormattingDurationListInTheFuture() throws Exception
  239. {
  240. PrettyTime t = new PrettyTime(new Date(0));
  241. List<Duration> durations = t.calculatePreciseDuration(new Date(1000 * 60 * 60 * 24 * 3 + 1000 * 60 * 60 * 15
  242. + 1000 * 60 * 38));
  243. assertEquals("3 ???? 15 ????? 38 ????? ?? ????", t.format(durations));
  244. }
  245. @Test
  246. public void testSetLocale() throws Exception
  247. {
  248. PrettyTime t = new PrettyTime(new Date(315569259747L * 3L));
  249. assertEquals("3 ???? ???", t.format(new Date(0)));
  250. }
  251. // Method tearDown() is called automatically after every test method
  252. @After
  253. public void tearDown() throws Exception
  254. {
  255. Locale.setDefault(locale);
  256. }
  257. }