From c48a643629430e988f8d91740d9f84da8f731935 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 16 Aug 2013 23:11:08 +0200 Subject: [PATCH] Fix tests when timezone is not GMT+2 --- .../org/sonar/batch/scan/JsonReportTest.java | 10 +++++ .../batch/scan/JsonReportTest/report.json | 4 +- .../core/issue/IssueNotificationsTest.java | 4 +- .../core/permission/PermissionDaoTest.java | 12 +++--- ...reateNonAsciiPermissionTemplate-result.xml | 2 +- .../createPermissionTemplate-result.xml | 2 +- .../internal/HttpRequestFactoryTest.java | 39 +++++++++++-------- 7 files changed, 46 insertions(+), 27 deletions(-) diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/JsonReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/JsonReportTest.java index 51ad93918b4..71cf7d91048 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/JsonReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/JsonReportTest.java @@ -21,6 +21,7 @@ package org.sonar.batch.scan; import com.google.common.collect.Lists; import org.json.JSONException; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -45,6 +46,7 @@ import java.io.IOException; import java.io.StringWriter; import java.util.Collections; import java.util.Locale; +import java.util.TimeZone; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -62,9 +64,12 @@ public class JsonReportTest { RuleI18nManager ruleI18nManager = mock(RuleI18nManager.class); Settings settings; IssueCache issueCache = mock(IssueCache.class); + TimeZone initialTimeZone; @Before public void setUp() { + initialTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); when(resource.getEffectiveKey()).thenReturn("Action.java"); when(server.getVersion()).thenReturn("3.6"); @@ -73,6 +78,11 @@ public class JsonReportTest { jsonReport = new JsonReport(settings, fileSystem, server, ruleI18nManager, issueCache, mock(EventBus.class)); } + @After + public void tearDown() throws Exception { + TimeZone.setDefault(initialTimeZone); + } + @Test public void should_write_json() throws JSONException { DefaultIssue issue = new DefaultIssue() diff --git a/sonar-batch/src/test/resources/org/sonar/batch/scan/JsonReportTest/report.json b/sonar-batch/src/test/resources/org/sonar/batch/scan/JsonReportTest/report.json index 6b6ead4619e..a6a5165f1b1 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/scan/JsonReportTest/report.json +++ b/sonar-batch/src/test/resources/org/sonar/batch/scan/JsonReportTest/report.json @@ -11,8 +11,8 @@ "reporter": "julien", "assignee": "simon", "effortToFix": 3.14, - "creationDate": "2013-04-24T00:00:00+0200", - "updateDate": "2013-04-25T00:00:00+0200" + "creationDate": "2013-04-24T00:00:00+0000", + "updateDate": "2013-04-25T00:00:00+0000" } ], "components": [ { diff --git a/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java index f0b464fb987..825890ac7d2 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/IssueNotificationsTest.java @@ -58,12 +58,12 @@ public class IssueNotificationsTest { @Test public void should_send_new_issues() throws Exception { - Date date = DateUtils.parseDateTime("2013-05-18T00:00:03+0200"); + Date date = DateUtils.parseDateTime("2013-05-18T13:00:03+0200"); Project project = new Project("struts").setAnalysisDate(date); Notification notification = issueNotifications.sendNewIssues(project, 42); assertThat(notification.getFieldValue("count")).isEqualTo("42"); - assertThat(notification.getFieldValue("projectDate")).isEqualTo("2013-05-18T00:00:03+0200"); + assertThat(DateUtils.parseDateTime(notification.getFieldValue("projectDate"))).isEqualTo(date); Mockito.verify(manager).scheduleForSending(notification); } diff --git a/sonar-core/src/test/java/org/sonar/core/permission/PermissionDaoTest.java b/sonar-core/src/test/java/org/sonar/core/permission/PermissionDaoTest.java index 424943b66ad..45b8bd1ca77 100644 --- a/sonar-core/src/test/java/org/sonar/core/permission/PermissionDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/permission/PermissionDaoTest.java @@ -28,6 +28,8 @@ import org.sonar.core.date.DateProvider; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.persistence.MyBatis; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -36,13 +38,13 @@ import static org.mockito.Mockito.*; public class PermissionDaoTest extends AbstractDaoTestCase { - private final Date now = DateUtils.parseDateTime("2013-01-02T03:04:05+0100"); - - private PermissionDao permissionDao; - private DateProvider dateProvider; + Date now; + PermissionDao permissionDao; + DateProvider dateProvider; @Before - public void setUpDao() { + public void setUpDao() throws ParseException { + now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-01-02 01:04:05"); dateProvider = mock(DateProvider.class); stub(dateProvider.now()).toReturn(now); permissionDao = new PermissionDao(getMyBatis(), dateProvider); diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createNonAsciiPermissionTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createNonAsciiPermissionTemplate-result.xml index 962c1d62f61..d80de102071 100644 --- a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createNonAsciiPermissionTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createNonAsciiPermissionTemplate-result.xml @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createPermissionTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createPermissionTemplate-result.xml index 0fbf6524539..0c2bd559342 100644 --- a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createPermissionTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionDaoTest/createPermissionTemplate-result.xml @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java index 79f399cb7ea..ff870ec5938 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/internal/HttpRequestFactoryTest.java @@ -31,6 +31,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; +import java.util.TimeZone; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -44,7 +45,7 @@ public class HttpRequestFactoryTest { httpServer.stubStatusCode(200).stubResponseBody("{'issues': []}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - String json = factory.get("/api/issues", Collections. emptyMap()); + String json = factory.get("/api/issues", Collections.emptyMap()); assertThat(json).isEqualTo("{'issues': []}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); @@ -54,7 +55,7 @@ public class HttpRequestFactoryTest { public void should_throw_illegal_state_exc_if_connect_exception() { HttpRequestFactory factory = new HttpRequestFactory("http://localhost:1"); try { - factory.get("/api/issues", Collections. emptyMap()); + factory.get("/api/issues", Collections.emptyMap()); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); @@ -68,7 +69,7 @@ public class HttpRequestFactoryTest { httpServer.stubStatusCode(200).stubResponseBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()); - String json = factory.post("/api/issues/change", Collections. emptyMap()); + String json = factory.post("/api/issues/change", Collections.emptyMap()); assertThat(json).isEqualTo("{}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/change"); @@ -79,7 +80,7 @@ public class HttpRequestFactoryTest { httpServer.stubStatusCode(200).stubResponseBody("{}"); HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()).setLogin("karadoc").setPassword("legrascestlavie"); - String json = factory.get("/api/issues", Collections. emptyMap()); + String json = factory.get("/api/issues", Collections.emptyMap()); assertThat(json).isEqualTo("{}"); assertThat(httpServer.requestedPath()).isEqualTo("/api/issues"); @@ -89,10 +90,10 @@ public class HttpRequestFactoryTest { @Test public void test_proxy() throws Exception { HttpRequestFactory factory = new HttpRequestFactory(httpServer.url()) - .setProxyHost("localhost").setProxyPort(1) - .setProxyLogin("john").setProxyPassword("smith"); + .setProxyHost("localhost").setProxyPort(1) + .setProxyLogin("john").setProxyPassword("smith"); try { - factory.get("/api/issues", Collections. emptyMap()); + factory.get("/api/issues", Collections.emptyMap()); fail(); } catch (IllegalStateException e) { // it's not possible to check that the proxy is correctly configured @@ -102,18 +103,24 @@ public class HttpRequestFactoryTest { @Test public void should_encode_characters() { - HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); - httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); + TimeZone initialTimeZone = TimeZone.getDefault(); + TimeZone.setDefault(TimeZone.getTimeZone("GMT")); + try { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.stubResponseBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); - IssueClient client = new DefaultIssueClient(requestFactory); - client.find(IssueQuery.create().issues("ABC DE")); - assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC%20DE"); + IssueClient client = new DefaultIssueClient(requestFactory); + client.find(IssueQuery.create().issues("ABC DE")); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC%20DE"); - client.find(IssueQuery.create().issues("ABC+BDE")); - assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC%2BBDE"); + client.find(IssueQuery.create().issues("ABC+BDE")); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?issues=ABC%2BBDE"); - client.find(IssueQuery.create().createdAfter(toDate("2013-01-01"))); - assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?createdAfter=2013-01-01T00:00:00%2B0100"); + client.find(IssueQuery.create().createdAfter(toDate("2013-01-01"))); + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/search?createdAfter=2013-01-01T00:00:00%2B0000"); + } finally { + TimeZone.setDefault(initialTimeZone); + } } protected static Date toDate(String sDate) { -- 2.39.5