-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support analysis property
sonar.ws.timeout
- Loading branch information
1 parent
3f35372
commit 9bf1cbe
Showing
4 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
...rg/jenkinsci/plugins/sonargerrit/sonar/SonarInstallationAdditionalAnalysisProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.jenkinsci.plugins.sonargerrit.sonar; | ||
|
||
import hudson.plugins.sonar.SonarInstallation; | ||
import java.time.Duration; | ||
import java.time.temporal.TemporalUnit; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
|
||
/** @author Réda Housni Alaoui */ | ||
@Restricted(NoExternalUse.class) | ||
public class SonarInstallationAdditionalAnalysisProperties { | ||
|
||
private final Map<String, String> properties; | ||
|
||
private SonarInstallationAdditionalAnalysisProperties(SonarInstallation sonarInstallation) { | ||
String additionalAnalysisProperties = sonarInstallation.getAdditionalAnalysisProperties(); | ||
if (additionalAnalysisProperties == null) { | ||
properties = Collections.emptyMap(); | ||
return; | ||
} | ||
|
||
properties = | ||
Stream.of(StringUtils.split(additionalAnalysisProperties)) | ||
.map(property -> StringUtils.split(property, "=")) | ||
.filter(keyValue -> keyValue.length == 2) | ||
.collect( | ||
Collectors.toMap( | ||
keyValue -> keyValue[0], | ||
keyValue -> keyValue[1], | ||
(firstValue, secondValue) -> secondValue)); | ||
} | ||
|
||
public static SonarInstallationAdditionalAnalysisProperties parse( | ||
SonarInstallation sonarInstallation) { | ||
return new SonarInstallationAdditionalAnalysisProperties(sonarInstallation); | ||
} | ||
|
||
public Optional<Duration> getDuration(String key, TemporalUnit temporalUnit) { | ||
return Optional.ofNullable(properties.get(key)) | ||
.map( | ||
stringValue -> { | ||
try { | ||
return Long.parseLong(stringValue); | ||
} catch (NumberFormatException e) { | ||
return null; | ||
} | ||
}) | ||
.map(longValue -> Duration.of(longValue, temporalUnit)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...enkinsci/plugins/sonargerrit/sonar/SonarInstallationAdditionalAnalysisPropertiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.jenkinsci.plugins.sonargerrit.sonar; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import hudson.plugins.sonar.SonarInstallation; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** @author Réda Housni Alaoui */ | ||
class SonarInstallationAdditionalAnalysisPropertiesTest { | ||
|
||
@Test | ||
@DisplayName("Get duration") | ||
void test1() { | ||
assertThat( | ||
SonarInstallationAdditionalAnalysisProperties.parse( | ||
createSonarInstallation( | ||
"foo=bar sonar.ws.timeout=6 bar=foo sonar.ws.timeout=600")) | ||
.getDuration("sonar.ws.timeout", ChronoUnit.SECONDS)) | ||
.contains(Duration.of(600, ChronoUnit.SECONDS)); | ||
} | ||
|
||
@Test | ||
@DisplayName("Get empty duration") | ||
void test2() { | ||
assertThat( | ||
SonarInstallationAdditionalAnalysisProperties.parse( | ||
createSonarInstallation("sonar.ws.timeout= =foo")) | ||
.getDuration("sonar.ws.timeout", ChronoUnit.SECONDS)) | ||
.isEmpty(); | ||
} | ||
|
||
private SonarInstallation createSonarInstallation(String additionalAnalysisProperties) { | ||
return new SonarInstallation( | ||
UUID.randomUUID().toString(), | ||
UUID.randomUUID().toString(), | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
additionalAnalysisProperties, | ||
null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters