aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-09-11 16:18:34 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-09-15 08:51:08 +0200
commitf76f82c6ef66e60e4986521d141514763e5c405a (patch)
tree87f9aae33bcf56de22a0f1f2306d2a209682d935 /server
parent3078c44cf8dd58238ac05ee39b77dff660a20560 (diff)
downloadsonarqube-f76f82c6ef66e60e4986521d141514763e5c405a.tar.gz
sonarqube-f76f82c6ef66e60e4986521d141514763e5c405a.zip
SONAR-9718 Fail with a clear message when sonar.projectDate is empty
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryFactory.java20
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java42
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java3
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java3
6 files changed, 52 insertions, 24 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryFactory.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryFactory.java
index 678de1ce81d..579a53bd147 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryFactory.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQueryFactory.java
@@ -25,6 +25,9 @@ import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
+import java.time.Clock;
+import java.time.OffsetDateTime;
+import java.time.Period;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -40,12 +43,9 @@ import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.BooleanUtils;
-import org.joda.time.DateTime;
-import org.joda.time.format.ISOPeriodFormat;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
-import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
@@ -90,12 +90,12 @@ public class IssueQueryFactory {
private static final ComponentDto UNKNOWN_COMPONENT = new ComponentDto().setUuid(UNKNOWN).setProjectUuid(UNKNOWN);
private final DbClient dbClient;
- private final System2 system;
+ private final Clock clock;
private final UserSession userSession;
- public IssueQueryFactory(DbClient dbClient, System2 system, UserSession userSession) {
+ public IssueQueryFactory(DbClient dbClient, Clock clock, UserSession userSession) {
this.dbClient = dbClient;
- this.system = system;
+ this.clock = clock;
this.userSession = userSession;
}
@@ -138,8 +138,10 @@ public class IssueQueryFactory {
Date actualCreatedAfter = createdAfter;
if (createdInLast != null) {
- actualCreatedAfter = new DateTime(system.now()).minus(
- ISOPeriodFormat.standard().parsePeriod("P" + createdInLast.toUpperCase(Locale.ENGLISH))).toDate();
+ actualCreatedAfter = Date.from(
+ OffsetDateTime.now(clock)
+ .minus(Period.parse("P" + createdInLast.toUpperCase(Locale.ENGLISH)))
+ .toInstant());
}
return actualCreatedAfter;
}
@@ -392,7 +394,7 @@ public class IssueQueryFactory {
return mainBranchProjectUuid == null ? componentDto.projectUuid() : mainBranchProjectUuid;
}
- private static void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch){
+ private static void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch) {
builder.branchUuid(branch == null ? null : component.projectUuid());
builder.mainBranch(branch == null || !branch.equals(component.getBranch()));
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
index 7b7fc68f854..b8dda7b6467 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel1.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.platform.platformlevel;
+import java.time.Clock;
import java.util.Properties;
import javax.annotation.Nullable;
import org.sonar.NetworkUtils;
@@ -101,6 +102,7 @@ public class PlatformLevel1 extends PlatformLevel {
TempFolderCleaner.class,
new TempFolderProvider(),
System2.INSTANCE,
+ Clock.systemDefaultZone(),
// user session
ThreadLocalUserSession.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
index 2ac3e6455c4..e1f4cd234c3 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueQueryFactoryTest.java
@@ -19,6 +19,8 @@
*/
package org.sonar.server.issue;
+import java.time.Clock;
+import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import org.junit.Rule;
@@ -27,7 +29,6 @@ import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
@@ -59,8 +60,8 @@ public class IssueQueryFactoryTest {
@Rule
public DbTester db = DbTester.create();
- private System2 system = mock(System2.class);
- private IssueQueryFactory underTest = new IssueQueryFactory(db.getDbClient(), system, userSession);
+ private Clock clock = mock(Clock.class);
+ private IssueQueryFactory underTest = new IssueQueryFactory(db.getDbClient(), clock, userSession);
@Test
public void create_from_parameters() {
@@ -127,6 +128,26 @@ public class IssueQueryFactoryTest {
}
@Test
+ public void creation_date_support_localdate() {
+ SearchWsRequest request = new SearchWsRequest()
+ .setCreatedAt("2013-04-16");
+
+ IssueQuery query = underTest.create(request);
+
+ assertThat(query.createdAt()).isEqualTo(DateUtils.parseDate("2013-04-16"));
+ }
+
+ @Test
+ public void creation_date_support_zoneddatetime() {
+ SearchWsRequest request = new SearchWsRequest()
+ .setCreatedAt("2013-04-16T09:08:24+0200");
+
+ IssueQuery query = underTest.create(request);
+
+ assertThat(query.createdAt()).isEqualTo(DateUtils.parseDateTime("2013-04-16T09:08:24+0200"));
+ }
+
+ @Test
public void add_unknown_when_no_component_found() {
SearchWsRequest request = new SearchWsRequest()
.setComponentKeys(asList("does_not_exist"));
@@ -403,8 +424,8 @@ public class IssueQueryFactoryTest {
.setComponentKeys(singletonList(file.getKey()))
.setBranch(branch.getBranch())
.setOnComponentOnly(true)))
- .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch)
- .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
+ .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.componentUuids()), IssueQuery::isMainBranch)
+ .containsOnly(branch.uuid(), singletonList(file.uuid()), false);
}
@Test
@@ -415,13 +436,13 @@ public class IssueQueryFactoryTest {
assertThat(underTest.create(new SearchWsRequest()
.setProjectKeys(singletonList(project.getKey()))
.setBranch("master")))
- .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
- .containsOnly(project.uuid(), singletonList(project.uuid()), true);
+ .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
+ .containsOnly(project.uuid(), singletonList(project.uuid()), true);
assertThat(underTest.create(new SearchWsRequest()
.setComponentKeys(singletonList(project.getKey()))
.setBranch("master")))
- .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
- .containsOnly(project.uuid(), singletonList(project.uuid()), true);
+ .extracting(IssueQuery::branchUuid, query -> new ArrayList<>(query.projectUuids()), IssueQuery::isMainBranch)
+ .containsOnly(project.uuid(), singletonList(project.uuid()), true);
}
@Test
@@ -441,7 +462,8 @@ public class IssueQueryFactoryTest {
@Test
public void set_created_after_from_created_since() {
Date now = DateUtils.parseDateTime("2013-07-25T07:35:00+0100");
- when(system.now()).thenReturn(now.getTime());
+ when(clock.instant()).thenReturn(now.toInstant());
+ when(clock.getZone()).thenReturn(ZoneOffset.UTC);
SearchWsRequest request = new SearchWsRequest()
.setCreatedInLast("1y2m3w4d");
assertThat(underTest.create(request).createdAfter()).isEqualTo(DateUtils.parseDateTime("2012-04-30T07:35:00+0100"));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java
index 8ad91f54f46..25d10313b58 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java
@@ -20,6 +20,7 @@
package org.sonar.server.issue.ws;
import java.io.IOException;
+import java.time.Clock;
import java.util.Arrays;
import java.util.Date;
import org.junit.Rule;
@@ -93,7 +94,7 @@ public class SearchActionComponentsTest {
private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new AuthorizationTypeSupport(userSession));
private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
private ViewIndexer viewIndexer = new ViewIndexer(dbClient, es.client());
- private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, System2.INSTANCE, userSession);
+ private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, Clock.systemUTC(), userSession);
private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter();
private IssueWorkflow issueWorkflow = new IssueWorkflow(new FunctionExecutor(issueFieldsSetter), issueFieldsSetter);
private SearchResponseLoader searchResponseLoader = new SearchResponseLoader(userSession, dbClient, new ActionFinder(userSession),
diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java
index 850c3099fb3..6699b0ab8b4 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.issue.ws;
+import java.time.Clock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -95,10 +96,11 @@ public class SearchActionTest {
private DbSession session = db.getSession();
private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSessionRule, new AuthorizationTypeSupport(userSessionRule));
private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient));
- private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, System2.INSTANCE, userSessionRule);
+ private IssueQueryFactory issueQueryFactory = new IssueQueryFactory(dbClient, Clock.systemUTC(), userSessionRule);
private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter();
private IssueWorkflow issueWorkflow = new IssueWorkflow(new FunctionExecutor(issueFieldsSetter), issueFieldsSetter);
- private SearchResponseLoader searchResponseLoader = new SearchResponseLoader(userSessionRule, dbClient, new ActionFinder(userSessionRule), new TransitionService(userSessionRule, issueWorkflow));
+ private SearchResponseLoader searchResponseLoader = new SearchResponseLoader(userSessionRule, dbClient, new ActionFinder(userSessionRule),
+ new TransitionService(userSessionRule, issueWorkflow));
private Languages languages = new Languages();
private SearchResponseFormat searchResponseFormat = new SearchResponseFormat(new Durations(), new WsResponseCommonFormat(languages), languages, new AvatarResolverImpl());
private WsActionTester ws = new WsActionTester(new SearchAction(userSessionRule, issueIndex, issueQueryFactory, searchResponseLoader, searchResponseFormat));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java
index 210403f9810..21448a106f1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/util/RubyUtilsTest.java
@@ -24,7 +24,6 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.utils.DateUtils;
-import org.sonar.api.utils.SonarException;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
@@ -132,7 +131,7 @@ public class RubyUtilsTest {
@Test
public void toDate_bad_format() {
- throwable.expect(SonarException.class);
+ throwable.expect(RuntimeException.class);
RubyUtils.toDate("01/02/2013");
}