PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/regularfry/jclouds
Java | 244 lines | 180 code | 33 blank | 31 comment | 8 complexity | 98a2bba19151595ee32d1c41a577979d 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.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.concurrent.ExecutionException;
  25. import org.jclouds.PerformanceTest;
  26. import org.jclouds.date.DateService;
  27. import org.testng.annotations.BeforeTest;
  28. import org.testng.annotations.Test;
  29. import com.google.inject.Guice;
  30. import com.google.inject.Injector;
  31. /*
  32. * TODO: Scrap any non-DateService references (eg Joda & Amazon) if/when
  33. * we confirm that the DateService is fast enough.
  34. */
  35. /**
  36. * Compares performance of date operations
  37. *
  38. * @author Adrian Cole
  39. * @author James Murty
  40. */
  41. // NOTE:without testName, this will fail w/NPE during surefire
  42. @Test(groups = "performance", singleThreaded = true, timeOut = 2 * 60 * 1000, testName = "DateServiceTest")
  43. public class DateServiceTest extends PerformanceTest {
  44. protected DateService dateService;
  45. @BeforeTest
  46. protected void createDateService() {
  47. Injector i = Guice.createInjector();
  48. dateService = i.getInstance(DateService.class);
  49. }
  50. protected TestData[] testData;
  51. protected class TestData {
  52. public final String iso8601DateString;
  53. public final String iso8601DateStringTz;
  54. public final String iso8601SecondsDateString;
  55. public final String rfc822DateString;
  56. public final String cDateString;
  57. public final Date date;
  58. TestData(String iso8601, String iso8601DateStringTz, String iso8601Seconds, String rfc822, String cDateString,
  59. Date dateTime) {
  60. this.iso8601DateString = iso8601;
  61. this.iso8601DateStringTz = iso8601DateStringTz;
  62. this.iso8601SecondsDateString = iso8601Seconds;
  63. this.rfc822DateString = rfc822;
  64. this.cDateString = cDateString;
  65. this.date = dateTime;
  66. }
  67. }
  68. public DateServiceTest() {
  69. // Constant time test values, each TestData item must contain matching
  70. // times!
  71. testData = new TestData[] {
  72. new TestData("2009-03-12T02:00:07.000Z", "2009-03-12T02:00:07-04:00", "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-12T02:00:07.000Z", "2009-03-12T02:00:07+04:00", "2009-03-12T02:00:07Z",
  75. "Thu, 12 Mar 2009 02:00:07 GMT", "Thu Mar 12 02:00:07 +0000 2009", new Date(1236823207000l)),
  76. new TestData("2009-03-14T04:00:07.000Z", "2009-03-14T04:00:07Z+04:00", "2009-03-14T04:00:07Z",
  77. "Sat, 14 Mar 2009 04:00:07 GMT", "Thu Mar 14 04:00:07 +0000 2009", new Date(1237003207000l)),
  78. new TestData("2009-03-16T06:00:07.000Z", "2009-03-16T06:00:07Z+04:00", "2009-03-16T06:00:07Z",
  79. "Mon, 16 Mar 2009 06:00:07 GMT", "Thu Mar 16 06:00:07 +0000 2009", new Date(1237183207000l)),
  80. new TestData("2009-03-18T08:00:07.000Z", "2009-03-18T08:00:07Z+04:00", "2009-03-18T08:00:07Z",
  81. "Wed, 18 Mar 2009 08:00:07 GMT", "Thu Mar 18 08:00:07 +0000 2009", new Date(1237363207000l)),
  82. new TestData("2009-03-20T10:00:07.000Z", "2009-03-20T10:00:07Z+04:00", "2009-03-20T10:00:07Z",
  83. "Fri, 20 Mar 2009 10:00:07 GMT", "Thu Mar 20 10:00:07 +0000 2009", new Date(1237543207000l)) };
  84. }
  85. @Test
  86. public void testIso8601DateParse() throws ExecutionException, InterruptedException {
  87. Date dsDate = dateService.iso8601DateParse(testData[0].iso8601DateString);
  88. assertEquals(dsDate, testData[0].date);
  89. }
  90. @Test
  91. public void testIso8601DateParseTz() throws ExecutionException, InterruptedException {
  92. Date dsDate = dateService.iso8601SecondsDateParse(testData[0].iso8601DateStringTz);
  93. assertEquals(dsDate, testData[0].date);
  94. }
  95. @Test
  96. public void testIso8601SecondsDateParse() throws ExecutionException, InterruptedException {
  97. Date dsDate = dateService.iso8601SecondsDateParse(testData[0].iso8601SecondsDateString);
  98. assertEquals(dsDate, testData[0].date);
  99. }
  100. @Test
  101. public void testCDateParse() throws ExecutionException, InterruptedException {
  102. Date dsDate = dateService.cDateParse(testData[0].cDateString);
  103. assertEquals(dsDate, testData[0].date);
  104. }
  105. @Test
  106. public void testRfc822DateParse() throws ExecutionException, InterruptedException {
  107. Date dsDate = dateService.rfc822DateParse(testData[0].rfc822DateString);
  108. assertEquals(dsDate, testData[0].date);
  109. }
  110. @Test
  111. public void testIso8601DateFormat() throws ExecutionException, InterruptedException {
  112. String dsString = dateService.iso8601DateFormat(testData[0].date);
  113. assertEquals(dsString, testData[0].iso8601DateString);
  114. }
  115. @Test
  116. public void testIso8601SecondsDateFormat() throws ExecutionException, InterruptedException {
  117. String dsString = dateService.iso8601SecondsDateFormat(testData[0].date);
  118. assertEquals(dsString, testData[0].iso8601SecondsDateString);
  119. }
  120. @Test
  121. public void testCDateFormat() throws ExecutionException, InterruptedException {
  122. String dsString = dateService.cDateFormat(testData[0].date);
  123. assertEquals(dsString, testData[0].cDateString);
  124. }
  125. @Test
  126. public void testRfc822DateFormat() throws ExecutionException, InterruptedException {
  127. String dsString = dateService.rfc822DateFormat(testData[0].date);
  128. assertEquals(dsString, testData[0].rfc822DateString);
  129. }
  130. @Test
  131. void testIso8601DateFormatResponseTime() throws ExecutionException, InterruptedException {
  132. for (int i = 0; i < LOOP_COUNT; i++)
  133. dateService.iso8601DateFormat();
  134. }
  135. @Test
  136. void testFromSeconds() throws ExecutionException, InterruptedException {
  137. long seconds = 1254008225;
  138. Date date = dateService.fromSeconds(seconds);
  139. assertEquals(dateService.rfc822DateFormat(date), "Sat, 26 Sep 2009 23:37:05 GMT");
  140. }
  141. @Test
  142. void testTzWithExtraZ() throws ExecutionException, InterruptedException {
  143. assertEquals(dateService.iso8601SecondsDateParse("2011-05-26T06:14:13-04:00").getTime(), 1306390453000l);
  144. assertEquals(dateService.iso8601SecondsDateParse("2011-05-26T06:14:13-04:00Z").getTime(), 1306390453000l);
  145. }
  146. @Test
  147. void testRfc822DateFormatResponseTime() throws ExecutionException, InterruptedException {
  148. for (int i = 0; i < LOOP_COUNT; i++)
  149. dateService.rfc822DateFormat();
  150. }
  151. @Test
  152. void testCDateFormatResponseTime() throws ExecutionException, InterruptedException {
  153. for (int i = 0; i < LOOP_COUNT; i++)
  154. dateService.cDateFormat();
  155. }
  156. @Test
  157. void testFormatIso8601DateCorrectnessInParallel() throws Throwable {
  158. List<Runnable> tasks = new ArrayList<Runnable>(testData.length);
  159. for (final TestData myData : testData) {
  160. tasks.add(new Runnable() {
  161. public void run() {
  162. String dsString = dateService.iso8601DateFormat(myData.date);
  163. assertEquals(dsString, myData.iso8601DateString);
  164. }
  165. });
  166. }
  167. executeMultiThreadedCorrectnessTest(tasks);
  168. }
  169. @Test
  170. void testFormatIso8601DatePerformanceInParallel() throws Throwable {
  171. List<Runnable> tasks = new ArrayList<Runnable>(testData.length);
  172. for (final TestData myData : testData) {
  173. tasks.add(new Runnable() {
  174. public void run() {
  175. dateService.iso8601DateFormat(myData.date);
  176. }
  177. });
  178. }
  179. executeMultiThreadedPerformanceTest("testFormatIso8601DatePerformanceInParallel", tasks);
  180. }
  181. @Test
  182. void testParseIso8601DateSerialResponseTime() throws ExecutionException, InterruptedException {
  183. for (int i = 0; i < LOOP_COUNT; i++)
  184. dateService.iso8601DateParse(testData[0].iso8601DateString);
  185. }
  186. @Test
  187. void testParseIso8601DateCorrectnessInParallel() throws Throwable {
  188. List<Runnable> tasks = new ArrayList<Runnable>(testData.length);
  189. for (final TestData myData : testData) {
  190. tasks.add(new Runnable() {
  191. public void run() {
  192. Date dsDate = dateService.iso8601DateParse(myData.iso8601DateString);
  193. assertEquals(dsDate, myData.date);
  194. }
  195. });
  196. }
  197. executeMultiThreadedCorrectnessTest(tasks);
  198. }
  199. @Test
  200. void testParseIso8601DatePerformanceInParallel() throws Throwable {
  201. List<Runnable> tasks = new ArrayList<Runnable>(testData.length);
  202. for (final TestData myData : testData) {
  203. tasks.add(new Runnable() {
  204. public void run() {
  205. dateService.iso8601DateParse(myData.iso8601DateString);
  206. }
  207. });
  208. }
  209. executeMultiThreadedPerformanceTest("testParseIso8601DatePerformanceInParallel", tasks);
  210. }
  211. }