diff options
23 files changed, 170 insertions, 99 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingResult.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingResult.java index 08e8c39b51c..fda029a616e 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingResult.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/IssueTrackingResult.java @@ -28,11 +28,11 @@ import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.db.IssueDto; import java.util.Collection; -import java.util.HashSet; import java.util.IdentityHashMap; +import java.util.Set; class IssueTrackingResult { - private final HashSet<IssueDto> unmatched = Sets.newHashSet(); + private final Set<IssueDto> unmatched = Sets.newHashSet(); private final Multimap<RuleKey, IssueDto> unmatchedByRule = LinkedHashMultimap.create(); private final IdentityHashMap<DefaultIssue, IssueDto> matched = Maps.newIdentityHashMap(); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java index 20186ee91ff..2d8e5a39120 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/security/ApplyProjectRolesDecorator.java @@ -34,8 +34,8 @@ import java.util.Set; @DryRunIncompatible public class ApplyProjectRolesDecorator implements Decorator { + private static final Set<String> QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.SUBVIEW); private final ResourcePermissions resourcePermissions; - private final Set<String> QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.SUBVIEW); public ApplyProjectRolesDecorator(ResourcePermissions resourcePermissions) { this.resourcePermissions = resourcePermissions; diff --git a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java index 5009626fd13..afafdb93a2a 100644 --- a/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java +++ b/plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/DefaultPurgeTask.java @@ -22,9 +22,6 @@ package org.sonar.plugins.dbcleaner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; -import org.sonar.api.Properties; -import org.sonar.api.Property; -import org.sonar.api.PropertyType; import org.sonar.api.config.Settings; import org.sonar.api.resources.Scopes; import org.sonar.api.utils.TimeUtils; @@ -91,9 +88,9 @@ public class DefaultPurgeTask implements PurgeTask { } private PurgeConfiguration newConf(long resourceId) { - String[] scopes = new String[] {Scopes.FILE}; + String[] scopes = new String[]{Scopes.FILE}; if (settings.getBoolean(DbCleanerConstants.PROPERTY_CLEAN_DIRECTORY)) { - scopes = new String[] {Scopes.DIRECTORY, Scopes.FILE}; + scopes = new String[]{Scopes.DIRECTORY, Scopes.FILE}; } return new PurgeConfiguration(resourceId, scopes, settings.getInt(DbCleanerConstants.DAYS_BEFORE_DELETING_CLOSED_ISSUES)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java index 606ec6a8a14..dfc5ecf1b82 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java @@ -84,10 +84,10 @@ public class PastSnapshotFinder implements BatchExtension { break; case 4: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_4; - break; // NOSONAR false-positive: constant 4 is the same than 5 (empty string) + break; case 5: defaultValue = CoreProperties.TIMEMACHINE_DEFAULT_PERIOD_5; - break; // NOSONAR false-positive: constant 5 is the same than 4 (empty string) + break; } return conf.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index, defaultValue); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java index a341f7337f0..02c0264c946 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java @@ -31,7 +31,6 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import javax.persistence.Query; - import java.util.Date; import java.util.List; @@ -48,16 +47,16 @@ public class TimeMachineConfiguration implements BatchExtension { private DatabaseSession session; public TimeMachineConfiguration(DatabaseSession session, Project project, Settings settings, - PastSnapshotFinder pastSnapshotFinder) { + PastSnapshotFinder pastSnapshotFinder) { this.session = session; this.project = project; this.settings = settings; initPastSnapshots(pastSnapshotFinder, getRootProject(project).getQualifier()); } - private Project getRootProject(Project project){ + private Project getRootProject(Project project) { if (!project.isRoot()) { - return getRootProject(project.getRoot()); + return getRootProject(project.getRoot()); } return project; } @@ -79,7 +78,7 @@ public class TimeMachineConfiguration implements BatchExtension { private Snapshot buildProjectSnapshot() { Query query = session - .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled"); + .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled"); query.setParameter("resourceKey", project.getKey()); query.setParameter("lib", Qualifiers.LIBRARY); query.setParameter("enabled", Boolean.TRUE); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java index f7b2b0f776a..c4cda7dc06e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataCache.java @@ -43,8 +43,8 @@ public class ComponentDataCache implements BatchComponent { public String getStringData(String componentKey, String dataType) { Data data = (Data) cache.get(componentKey, dataType); - return data==null ? null : ((StringData)data).data(); - } + return data == null ? null : ((StringData) data).data(); + } public <D extends Data> Iterable<Cache.Entry<D>> entries(String componentKey) { return cache.entries(componentKey); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java index d0ef14aebeb..7d7532b2503 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ComponentDataPersister.java @@ -20,6 +20,8 @@ package org.sonar.batch.index; import org.sonar.api.database.model.Snapshot; +import org.sonar.core.persistence.BatchSession; +import org.sonar.core.persistence.MyBatis; import org.sonar.core.source.jdbc.SnapshotDataDao; import org.sonar.core.source.jdbc.SnapshotDataDto; @@ -29,15 +31,19 @@ public class ComponentDataPersister implements ScanPersister { private final ComponentDataCache data; private final SnapshotCache snapshots; private final SnapshotDataDao dao; + private final MyBatis mybatis; - public ComponentDataPersister(ComponentDataCache data, SnapshotCache snapshots, SnapshotDataDao dao) { + public ComponentDataPersister(ComponentDataCache data, SnapshotCache snapshots, + SnapshotDataDao dao, MyBatis mybatis) { this.data = data; this.snapshots = snapshots; this.dao = dao; + this.mybatis = mybatis; } @Override public void persist() { + BatchSession session = mybatis.openBatchSession(); for (Map.Entry<String, Snapshot> componentEntry : snapshots.snapshots()) { String componentKey = componentEntry.getKey(); Snapshot snapshot = componentEntry.getValue(); @@ -48,11 +54,10 @@ public class ComponentDataPersister implements ScanPersister { dto.setResourceId(snapshot.getResourceId()); dto.setDataType(dataEntry.key()); dto.setData(dataEntry.value().writeString()); - - // TODO bulk insert - dao.insert(dto); + dao.insert(session, dto); } } } + session.commit(); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/SnapshotCache.java b/sonar-batch/src/main/java/org/sonar/batch/index/SnapshotCache.java index 0fd37e1e1ed..e758b0420bb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/SnapshotCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/SnapshotCache.java @@ -24,7 +24,6 @@ import com.google.common.collect.Maps; import org.sonar.api.BatchComponent; import org.sonar.api.database.model.Snapshot; -import java.util.Collection; import java.util.Map; import java.util.Set; diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java index 27fb9db5e34..3910b0ca85b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/ComponentDataPersisterTest.java @@ -56,7 +56,7 @@ public class ComponentDataPersisterTest extends AbstractDaoTestCase { data.setStringData("org/struts/Other.java", "SYMBOL", "unregistered component, should not be persisted"); SnapshotDataDao dataDao = new SnapshotDataDao(getMyBatis()); - ComponentDataPersister persister = new ComponentDataPersister(data, snapshots, dataDao); + ComponentDataPersister persister = new ComponentDataPersister(data, snapshots, dataDao, getMyBatis()); persister.persist(); checkTables("should_persist_component_data", new String[]{"id", "created_at", "updated_at"}, "snapshot_data"); diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanMapper.java index 8ca67acbaaa..4ea956c9a9a 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/ActionPlanMapper.java @@ -22,7 +22,6 @@ package org.sonar.core.issue.db; import org.apache.ibatis.annotations.Param; -import java.util.Collection; import java.util.List; /** @@ -36,11 +35,11 @@ public interface ActionPlanMapper { void delete(@Param("key") String key); - List<ActionPlanDto> findByKeys(@Param("keys") List <List<String>> keys); + List<ActionPlanDto> findByKeys(@Param("keys") List<List<String>> keys); ActionPlanDto findByKey(@Param("key") String key); List<ActionPlanDto> findOpenByProjectId(@Param("projectId") Long projectId); - List<ActionPlanDto> findByNameAndProjectId(@Param("name")String name, @Param("projectId") Long projectId); + List<ActionPlanDto> findByNameAndProjectId(@Param("name") String name, @Param("projectId") Long projectId); } diff --git a/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java b/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java index 6f00448d4d9..8a9b997f7f7 100644 --- a/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java +++ b/sonar-core/src/main/java/org/sonar/core/purge/PurgeConfiguration.java @@ -27,8 +27,6 @@ import java.util.Date; public class PurgeConfiguration { - private static final int ONE_DAY_IN_MS = 24 * 60 * 60 * 1000; - private final long rootProjectId; private final String[] scopesWithoutHistoricalData; private final int maxAgeInDaysOfClosedIssues; diff --git a/sonar-core/src/main/java/org/sonar/core/source/jdbc/SnapshotDataDao.java b/sonar-core/src/main/java/org/sonar/core/source/jdbc/SnapshotDataDao.java index 370f6401938..7b99f9bbcdf 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/jdbc/SnapshotDataDao.java +++ b/sonar-core/src/main/java/org/sonar/core/source/jdbc/SnapshotDataDao.java @@ -40,9 +40,7 @@ public class SnapshotDataDao implements BatchComponent, ServerComponent { } public Collection<SnapshotDataDto> selectSnapshotData(long snapshotId, List<String> dataTypes) { - SqlSession session = mybatis.openSession(); - try { SnapshotDataMapper mapper = session.getMapper(SnapshotDataMapper.class); return mapper.selectSnapshotData(snapshotId, dataTypes); @@ -52,16 +50,18 @@ public class SnapshotDataDao implements BatchComponent, ServerComponent { } } - public void insert(SnapshotDataDto snapshotData) { - - SqlSession session = mybatis.openBatchSession(); - + void insert(SnapshotDataDto snapshotData) { + SqlSession session = mybatis.openSession(); try { - SnapshotDataMapper mapper = session.getMapper(SnapshotDataMapper.class); - mapper.insert(snapshotData); + insert(session, snapshotData); session.commit(); } finally { MyBatis.closeQuietly(session); } } + + public void insert(SqlSession session, SnapshotDataDto snapshotData) { + SnapshotDataMapper mapper = session.getMapper(SnapshotDataMapper.class); + mapper.insert(snapshotData); + } } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java index 3801d70d743..4fb4cabc624 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java @@ -93,4 +93,23 @@ public class DefaultIssueTest { issue.setMessage(null); assertThat(issue.message()).isNull(); } + + @Test + public void test_nullable_fields() throws Exception { + issue.setEffortToFix(null).setSeverity(null).setLine(null); + assertThat(issue.effortToFix()).isNull(); + assertThat(issue.severity()).isNull(); + assertThat(issue.line()).isNull(); + } + + @Test + public void test_equals_and_hashCode() throws Exception { + DefaultIssue a1 = new DefaultIssue().setKey("AAA"); + DefaultIssue a2 = new DefaultIssue().setKey("AAA"); + DefaultIssue b = new DefaultIssue().setKey("BBB"); + assertThat(a1).isEqualTo(a1); + assertThat(a1).isEqualTo(a2); + assertThat(a1).isNotEqualTo(b); + assertThat(a1.hashCode()).isEqualTo(a1.hashCode()); + } } diff --git a/sonar-duplications/src/main/java/net/sourceforge/pmd/cpd/SourceCode.java b/sonar-duplications/src/main/java/net/sourceforge/pmd/cpd/SourceCode.java index ff05e226da8..d44eef24e5a 100644 --- a/sonar-duplications/src/main/java/net/sourceforge/pmd/cpd/SourceCode.java +++ b/sonar-duplications/src/main/java/net/sourceforge/pmd/cpd/SourceCode.java @@ -99,7 +99,7 @@ public class SourceCode { public static class StringCodeLoader extends CodeLoader { public static final String DEFAULT_NAME = "CODE_LOADED_FROM_STRING"; - private String source_code; + private String sourceCode; private String name; @@ -108,13 +108,13 @@ public class SourceCode { } public StringCodeLoader(String code, String name) { - this.source_code = code; + this.sourceCode = code; this.name = name; } @Override public Reader getReader() { - return new StringReader(source_code); + return new StringReader(sourceCode); } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/component/Component.java b/sonar-plugin-api/src/main/java/org/sonar/api/component/Component.java index 5b21a35f399..b658be87d19 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/component/Component.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/component/Component.java @@ -19,9 +19,12 @@ */ package org.sonar.api.component; -public interface Component<CHILD extends Component> { +public interface Component<C extends Component> { String key(); + String name(); + String longName(); + String qualifier(); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java index 0260bffcd7d..85241153f7d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/IssueFinder.java @@ -17,19 +17,9 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - package org.sonar.api.issue; import org.sonar.api.ServerComponent; -import org.sonar.api.component.Component; -import org.sonar.api.rules.Rule; -import org.sonar.api.utils.Paging; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.Collection; -import java.util.List; /** * Search for issues. This component can be used only by server-side extensions. Batch extensions should diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java index 56b99f0cb56..3a4e856e3e2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java @@ -26,7 +26,6 @@ import org.apache.commons.lang.builder.ToStringStyle; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.io.Serializable; -import java.util.HashMap; import java.util.Map; /** @@ -34,13 +33,13 @@ import java.util.Map; * This class represents a notification that will be delivered to users. This is a general concept and it has no * knowledge of the possible ways to be delivered (see {@link NotificationChannel}) or of the users who should * receive it (see {@link NotificationDispatcher}). - * </p> + * </p> * <p> - * When creating a new notification, it is strongly advised to give a default message that can be used by channels - * that don't want to specifically format messages for different notification types. You can use + * When creating a new notification, it is strongly advised to give a default message that can be used by channels + * that don't want to specifically format messages for different notification types. You can use * {@link Notification#setDefaultMessage(String)} for that purpose. * </p> - * + * * @since 2.10 */ public class Notification implements Serializable { @@ -55,7 +54,7 @@ public class Notification implements Serializable { * Create a new {@link Notification} of the given type. * </p> * Example: type = "new-violations" - * + * * @param type the type of notification */ public Notification(String type) { @@ -64,7 +63,7 @@ public class Notification implements Serializable { /** * Returns the type of the notification - * + * * @return the type */ public String getType() { @@ -73,14 +72,14 @@ public class Notification implements Serializable { /** * <p> - * When creating a new notification, it is strongly advised to give a default message that can be + * When creating a new notification, it is strongly advised to give a default message that can be * used by channels that don't want to specifically format messages for different notification types. * </p> * <p> - * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with + * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with * {@link #setFieldValue(String, String)}. - * </p> - * + * </p> + * * @since 3.5 */ public Notification setDefaultMessage(String value) { @@ -101,7 +100,7 @@ public class Notification implements Serializable { /** * Adds a field (kind of property) to the notification - * + * * @param field the name of the field (= the key) * @param value the value of the field * @return the notification itself @@ -113,7 +112,7 @@ public class Notification implements Serializable { /** * Returns the value of a field. - * + * * @param field the field * @return the value of the field */ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeUtils.java index f425d3662e9..952c0587796 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/TimeUtils.java @@ -27,26 +27,33 @@ public final class TimeUtils { private TimeUtils() { } + /** + * Label for a duration, expressed in numbers of ms, seconds or minutes. + * <p/> + * Examples: + * <ul> + * <li>10 -> "10ms"</li> + * <li>100 -> "100ms"</li> + * <li>10000 -> "10s"</li> + * <li>100000 -> "1min 40s"</li> + * </ul> + */ public static String formatDuration(long durationInMs) { if (durationInMs < 1000) { return String.format("%sms", durationInMs); - } - else { + } else { long sec = durationInMs / 1000; if (sec < 60) { return String.format("%ss", sec); - } - else { + } else { long min = sec / 60; long remainingSec = sec - (min * 60); if (remainingSec > 0) { return String.format("%smin %ss", min, remainingSec); - } - else { + } else { return String.format("%smin", min); } } } } - } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeUtilsTest.java new file mode 100644 index 00000000000..308e9266044 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/TimeUtilsTest.java @@ -0,0 +1,38 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class TimeUtilsTest { + @Test + public void formatDuration() throws Exception { + assertThat(TimeUtils.formatDuration(0)).isEqualTo("0ms"); + assertThat(TimeUtils.formatDuration(100)).isEqualTo("100ms"); + assertThat(TimeUtils.formatDuration(1000)).isEqualTo("1s"); + assertThat(TimeUtils.formatDuration(10000)).isEqualTo("10s"); + assertThat(TimeUtils.formatDuration(100000)).isEqualTo("1min 40s"); + assertThat(TimeUtils.formatDuration(600000)).isEqualTo("10min"); + assertThat(TimeUtils.formatDuration(1000000)).isEqualTo("16min 40s"); + assertThat(TimeUtils.formatDuration(10000000)).isEqualTo("166min 40s"); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java index ceef7fe98fc..727a2e4c409 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/DefaultIssueClient.java @@ -35,14 +35,14 @@ import java.util.Map; public class DefaultIssueClient implements IssueClient { private final HttpRequestFactory requestFactory; - private final IssueParser parser; + private final IssueJsonParser parser; /** * For internal use. Use {@link org.sonar.wsclient.SonarClient} to get an instance. */ public DefaultIssueClient(HttpRequestFactory requestFactory) { this.requestFactory = requestFactory; - this.parser = new IssueParser(); + this.parser = new IssueJsonParser(); } public Issues find(IssueQuery query) { diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java index 855ad83698f..fc1b1fb6ccc 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueParser.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueJsonParser.java @@ -32,7 +32,7 @@ import java.util.Map; /** * @since 3.6 */ -class IssueParser { +class IssueJsonParser { Issues parseIssues(String json) { Issues result = new Issues(); @@ -43,39 +43,54 @@ class IssueParser { result.add(new Issue(jsonIssue)); } } + parseRules(result, jsonRoot); + parseUsers(result, jsonRoot); + parseComponents(result, jsonRoot); + parseProjects(result, jsonRoot); + parsePaging(result, jsonRoot); + return result; + } - List<Map> jsonRules = (List) jsonRoot.get("rules"); - if (jsonRules != null) { - for (Map jsonRule : jsonRules) { - result.add(new Rule(jsonRule)); - } - } + private void parsePaging(Issues result, Map jsonRoot) { + Map paging = (Map) jsonRoot.get("paging"); + result.setPaging(new Paging(paging)); + result.setMaxResultsReached(JsonUtils.getBoolean(jsonRoot, "maxResultsReached")); + } - List<Map> jsonUsers = (List) jsonRoot.get("users"); - if (jsonUsers != null) { - for (Map jsonUser : jsonUsers) { - result.add(new User(jsonUser)); + private void parseProjects(Issues result, Map jsonRoot) { + List<Map> jsonProjects = (List) jsonRoot.get("projects"); + if (jsonProjects != null) { + for (Map jsonProject : jsonProjects) { + result.addProject(new Component(jsonProject)); } } + } + private void parseComponents(Issues result, Map jsonRoot) { List<Map> jsonComponents = (List) jsonRoot.get("components"); if (jsonComponents != null) { for (Map jsonComponent : jsonComponents) { result.addComponent(new Component(jsonComponent)); } } + } - List<Map> jsonProjects = (List) jsonRoot.get("projects"); - if (jsonProjects != null) { - for (Map jsonProject : jsonProjects) { - result.addProject(new Component(jsonProject)); + private void parseUsers(Issues result, Map jsonRoot) { + List<Map> jsonUsers = (List) jsonRoot.get("users"); + if (jsonUsers != null) { + for (Map jsonUser : jsonUsers) { + result.add(new User(jsonUser)); } } + } - Map paging = (Map) jsonRoot.get("paging"); - result.setPaging(new Paging(paging)); - result.setMaxResultsReached(JsonUtils.getBoolean(jsonRoot, "maxResultsReached")); - return result; + private void parseRules(Issues result, Map jsonRoot) { + List<Map> jsonRules = (List) jsonRoot.get("rules"); + if (jsonRules != null) { + for (Map jsonRule : jsonRules) { + result.add(new Rule(jsonRule)); + } + } } List<String> parseTransitions(String json) { diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueJsonParserTest.java index 7f0a76ecb4d..82c71f7446d 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueParserTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/IssueJsonParserTest.java @@ -28,11 +28,11 @@ import java.util.List; import static org.fest.assertions.Assertions.assertThat; -public class IssueParserTest { +public class IssueJsonParserTest { @Test public void test_GET_search() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/search.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues).isNotNull(); List<Issue> list = issues.list(); assertThat(list).hasSize(2); @@ -84,7 +84,7 @@ public class IssueParserTest { @Test public void test_GET_empty_search() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/empty.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues).isNotNull(); assertThat(issues.list()).isEmpty(); assertThat(issues.rules()).isEmpty(); @@ -94,7 +94,7 @@ public class IssueParserTest { @Test public void test_GET_transitions() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/getTransitions.json")); - List<String> transitions = new IssueParser().parseTransitions(json); + List<String> transitions = new IssueJsonParser().parseTransitions(json); assertThat(transitions).isNotNull(); assertThat(transitions).hasSize(2); @@ -104,7 +104,7 @@ public class IssueParserTest { @Test public void should_parse_comments() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-comments.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.size()).isEqualTo(1); Issue issue = issues.list().get(0); @@ -126,7 +126,7 @@ public class IssueParserTest { @Test public void should_parse_users() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-users.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.users()).hasSize(2); @@ -146,7 +146,7 @@ public class IssueParserTest { @Test public void should_parse_components() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-components.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.components()).hasSize(1); @@ -160,7 +160,7 @@ public class IssueParserTest { @Test public void should_parse_projects() throws Exception { String json = IOUtils.toString(getClass().getResourceAsStream("/org/sonar/wsclient/issue/IssueParserTest/issue-with-projects.json")); - Issues issues = new IssueParser().parseIssues(json); + Issues issues = new IssueJsonParser().parseIssues(json); assertThat(issues.projects()).hasSize(1); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java index 475e95c4f70..d42a0256f08 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/user/UserQueryTest.java @@ -53,5 +53,8 @@ public class UserQueryTest { public void should_search_by_text() throws Exception { UserQuery query = UserQuery.create().searchText("sim"); assertThat(query.urlParams().get("s")).isEqualTo("sim"); + + query = UserQuery.create().searchText("sim").searchText(null); + assertThat(query.urlParams().get("s")).isNull(); } } |