PageRenderTime 148ms CodeModel.GetById 46ms app.highlight 78ms RepoModel.GetById 13ms app.codeStats 1ms

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