/core/src/test/java/org/jclouds/date/DateServiceTest.java

https://github.com/richardcloudsoft/legacy-jclouds · Java · 264 lines · 195 code · 38 blank · 31 comment · 8 complexity · 2507ffa3a1d53737152175c388cafd16 MD5 · raw file

  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.date;
  20. import static org.testng.Assert.assertEquals;
  21. import java.util.Date;
  22. import java.util.List;
  23. import org.jclouds.PerformanceTest;
  24. import org.testng.annotations.BeforeTest;
  25. import org.testng.annotations.Test;
  26. import com.google.common.collect.Lists;
  27. import com.google.inject.Guice;
  28. import com.google.inject.Injector;
  29. /*
  30. * TODO: Scrap any non-DateService references (eg Joda & Amazon) if/when
  31. * we confirm that the DateService is fast enough.
  32. */
  33. /**
  34. * Compares performance of date operations
  35. *
  36. * @author Adrian Cole
  37. * @author James Murty
  38. */
  39. // NOTE:without testName, this will fail w/NPE during surefire
  40. @Test(groups = "performance", singleThreaded = true, timeOut = 2 * 60 * 1000, testName = "DateServiceTest")
  41. public class DateServiceTest extends PerformanceTest {
  42. protected DateService dateService;
  43. @BeforeTest
  44. protected void createDateService() {
  45. Injector i = Guice.createInjector();
  46. dateService = i.getInstance(DateService.class);
  47. }
  48. protected TestData[] testData;
  49. protected class TestData {
  50. public final String iso8601DateString;
  51. public final String iso8601DateStringTz;
  52. public final String iso8601SecondsDateString;
  53. public final String rfc822DateString;
  54. public final String cDateString;
  55. public final Date date;
  56. TestData(String iso8601, String iso8601DateStringTz, String iso8601Seconds, String rfc822, String cDateString,
  57. Date dateTime) {
  58. this.iso8601DateString = iso8601;
  59. this.iso8601DateStringTz = iso8601DateStringTz;
  60. this.iso8601SecondsDateString = iso8601Seconds;
  61. this.rfc822DateString = rfc822;
  62. this.cDateString = cDateString;
  63. this.date = dateTime;
  64. }
  65. }
  66. public DateServiceTest() {
  67. // Constant time test values, each TestData item must contain matching
  68. // times!
  69. testData = new TestData[] {
  70. new TestData("2009-03-12T02:00:07.000Z", "2009-03-12T06:00:07+0400", "2009-03-12T02:00:07Z",
  71. "Thu, 12 Mar 2009 02:00:07 GMT", "Thu Mar 12 02:00:07 +0000 2009", new Date(1236823207000l)),
  72. new TestData("2009-03-12T02:00:07.000Z", "2009-03-12T06:00:07+0400", "2009-03-12T02:00:07Z",
  73. "Thu, 12 Mar 2009 02:00:07 GMT", "Thu Mar 12 02:00:07 +0000 2009", new Date(1236823207000l)),
  74. new TestData("2009-03-14T04:00:07.000Z", "2009-03-14T08:00:07+0400", "2009-03-14T04:00:07Z",
  75. "Sat, 14 Mar 2009 04:00:07 GMT", "Thu Mar 14 04:00:07 +0000 2009", new Date(1237003207000l)),
  76. new TestData("2009-03-16T06:00:07.000Z", "2009-03-16T10:00:07+0400", "2009-03-16T06:00:07Z",
  77. "Mon, 16 Mar 2009 06:00:07 GMT", "Thu Mar 16 06:00:07 +0000 2009", new Date(1237183207000l)),
  78. new TestData("2009-03-18T08:00:07.000Z", "2009-03-18T12:00:07+0400", "2009-03-18T08:00:07Z",
  79. "Wed, 18 Mar 2009 08:00:07 GMT", "Thu Mar 18 08:00:07 +0000 2009", new Date(1237363207000l)),
  80. new TestData("2009-03-20T10:00:07.000Z", "2009-03-20T14:00:07+0400", "2009-03-20T10:00:07Z",
  81. "Fri, 20 Mar 2009 10:00:07 GMT", "Thu Mar 20 10:00:07 +0000 2009", new Date(1237543207000l)) };
  82. }
  83. @Test
  84. public void testIso8601DateParse() {
  85. Date dsDate = dateService.iso8601DateParse(testData[0].iso8601DateString);
  86. assertEquals(dsDate, testData[0].date);
  87. }
  88. @Test(expectedExceptions = IllegalArgumentException.class)
  89. public void testIso8601DateParseIllegal() {
  90. dateService.iso8601DateParse("-1");
  91. }
  92. @Test
  93. public void testIso8601DateParseTz() {
  94. Date dsDate = dateService.iso8601SecondsDateParse(testData[0].iso8601DateStringTz);
  95. assertEquals(dsDate, testData[0].date);
  96. }
  97. @Test
  98. public void testIso8601SecondsDateParse() {
  99. Date dsDate = dateService.iso8601SecondsDateParse(testData[0].iso8601SecondsDateString);
  100. assertEquals(dsDate, testData[0].date);
  101. }
  102. @Test(expectedExceptions = IllegalArgumentException.class)
  103. public void testIso8601SecondsDateParseIllegal() {
  104. dateService.iso8601SecondsDateParse("-1");
  105. }
  106. @Test
  107. public void testCDateParse() {
  108. Date dsDate = dateService.cDateParse(testData[0].cDateString);
  109. assertEquals(dsDate, testData[0].date);
  110. }
  111. @Test(expectedExceptions = IllegalArgumentException.class)
  112. public void testCDateParseIllegal() {
  113. dateService.cDateParse("foo");
  114. }
  115. @Test
  116. public void testRfc822DateParse() {
  117. Date dsDate = dateService.rfc822DateParse(testData[0].rfc822DateString);
  118. assertEquals(dsDate, testData[0].date);
  119. }
  120. @Test(expectedExceptions = IllegalArgumentException.class)
  121. public void testRfc822DateParseIllegal() {
  122. dateService.rfc822DateParse("foo");
  123. }
  124. @Test
  125. public void testIso8601DateFormat() {
  126. String dsString = dateService.iso8601DateFormat(testData[0].date);
  127. assertEquals(dsString, testData[0].iso8601DateString);
  128. }
  129. @Test
  130. public void testIso8601SecondsDateFormat() {
  131. String dsString = dateService.iso8601SecondsDateFormat(testData[0].date);
  132. assertEquals(dsString, testData[0].iso8601SecondsDateString);
  133. }
  134. @Test
  135. public void testCDateFormat() {
  136. String dsString = dateService.cDateFormat(testData[0].date);
  137. assertEquals(dsString, testData[0].cDateString);
  138. }
  139. @Test
  140. public void testRfc822DateFormat() {
  141. String dsString = dateService.rfc822DateFormat(testData[0].date);
  142. assertEquals(dsString, testData[0].rfc822DateString);
  143. }
  144. @Test
  145. void testIso8601DateFormatResponseTime() {
  146. for (int i = 0; i < LOOP_COUNT; i++)
  147. dateService.iso8601DateFormat();
  148. }
  149. @Test
  150. void testUTCIsGMT() {
  151. assertEquals(dateService.iso8601SecondsDateParse("2012-11-26T17:32:31UTC+0000").getTime(), dateService.iso8601SecondsDateParse("2012-11-26T17:32:31UTC+0000").getTime());
  152. }
  153. @Test
  154. void testTz() {
  155. assertEquals(dateService.iso8601SecondsDateParse("2011-05-26T02:14:13-04:00").getTime(), 1306390453000l);
  156. }
  157. @Test
  158. void testTzNoT() {
  159. assertEquals(dateService.iso8601DateParse("2011-05-25 16:12:21.656+0000").getTime(), 1306339941656l);
  160. }
  161. @Test
  162. void testRfc822DateFormatResponseTime() {
  163. for (int i = 0; i < LOOP_COUNT; i++)
  164. dateService.rfc822DateFormat();
  165. }
  166. @Test
  167. void testCDateFormatResponseTime() {
  168. for (int i = 0; i < LOOP_COUNT; i++)
  169. dateService.cDateFormat();
  170. }
  171. @Test
  172. void testFormatIso8601DateCorrectnessInParallel() throws Throwable {
  173. List<Runnable> tasks = Lists.newArrayListWithCapacity(testData.length);
  174. for (final TestData myData : testData) {
  175. tasks.add(new Runnable() {
  176. public void run() {
  177. String dsString = dateService.iso8601DateFormat(myData.date);
  178. assertEquals(dsString, myData.iso8601DateString);
  179. }
  180. });
  181. }
  182. executeMultiThreadedCorrectnessTest(tasks);
  183. }
  184. @Test
  185. void testFormatIso8601DatePerformanceInParallel() throws Throwable {
  186. List<Runnable> tasks = Lists.newArrayListWithCapacity(testData.length);
  187. for (final TestData myData : testData) {
  188. tasks.add(new Runnable() {
  189. public void run() {
  190. dateService.iso8601DateFormat(myData.date);
  191. }
  192. });
  193. }
  194. executeMultiThreadedPerformanceTest("testFormatIso8601DatePerformanceInParallel", tasks);
  195. }
  196. @Test
  197. void testParseIso8601DateSerialResponseTime() {
  198. for (int i = 0; i < LOOP_COUNT; i++)
  199. dateService.iso8601DateParse(testData[0].iso8601DateString);
  200. }
  201. @Test
  202. void testParseIso8601DateCorrectnessInParallel() throws Throwable {
  203. List<Runnable> tasks = Lists.newArrayListWithCapacity(testData.length);
  204. for (final TestData myData : testData) {
  205. tasks.add(new Runnable() {
  206. public void run() {
  207. Date dsDate = dateService.iso8601DateParse(myData.iso8601DateString);
  208. assertEquals(dsDate, myData.date);
  209. }
  210. });
  211. }
  212. executeMultiThreadedCorrectnessTest(tasks);
  213. }
  214. @Test
  215. void testParseIso8601DatePerformanceInParallel() throws Throwable {
  216. List<Runnable> tasks = Lists.newArrayListWithCapacity(testData.length);
  217. for (final TestData myData : testData) {
  218. tasks.add(new Runnable() {
  219. public void run() {
  220. dateService.iso8601DateParse(myData.iso8601DateString);
  221. }
  222. });
  223. }
  224. executeMultiThreadedPerformanceTest("testParseIso8601DatePerformanceInParallel", tasks);
  225. }
  226. }