diff options
3 files changed, 41 insertions, 50 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java index 58d676cedb0..63aefc3f2d6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/UserUpdater.java @@ -43,7 +43,6 @@ import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.user.db.UserGroupDao; import org.sonar.server.util.Validation; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.io.StringWriter; @@ -151,9 +150,12 @@ public class UserUpdater implements ServerComponent { validatePasswords(password, passwordConfirmation, messages); setEncryptedPassWord(password, userDto); - List<String> scmAccounts = sanitizeScmAccounts(newUser.scmAccounts()); - validateScmAccounts(dbSession, scmAccounts, login, email, null, messages); - userDto.setScmAccounts(convertScmAccountsToCsv(scmAccounts)); + List<String> scmAccounts = newUser.scmAccounts(); + if (scmAccounts != null && !scmAccounts.isEmpty()) { + scmAccounts = sanitizeScmAccounts(scmAccounts); + validateScmAccounts(dbSession, scmAccounts, login, email, null, messages); + userDto.setScmAccounts(convertScmAccountsToCsv(scmAccounts)); + } if (!messages.isEmpty()) { throw new BadRequestException(messages); @@ -184,9 +186,14 @@ public class UserUpdater implements ServerComponent { } if (updateUser.isScmAccountsChanged()) { - List<String> scmAccounts = sanitizeScmAccounts(updateUser.scmAccounts()); - validateScmAccounts(dbSession, scmAccounts, userDto.getLogin(), email != null ? email : userDto.getEmail(), userDto, messages); - userDto.setScmAccounts(convertScmAccountsToCsv(scmAccounts)); + List<String> scmAccounts = updateUser.scmAccounts(); + if (scmAccounts != null && !scmAccounts.isEmpty()) { + scmAccounts = sanitizeScmAccounts(updateUser.scmAccounts()); + validateScmAccounts(dbSession, scmAccounts, userDto.getLogin(), email != null ? email : userDto.getEmail(), userDto, messages); + userDto.setScmAccounts(convertScmAccountsToCsv(scmAccounts)); + } else { + userDto.setScmAccounts(null); + } } if (!messages.isEmpty()) { @@ -234,31 +241,23 @@ public class UserUpdater implements ServerComponent { } } - private void validateScmAccounts(DbSession dbSession, @Nullable List<String> scmAccounts, @Nullable String login, @Nullable String email, @Nullable UserDto existingUser, + private void validateScmAccounts(DbSession dbSession, List<String> scmAccounts, @Nullable String login, @Nullable String email, @Nullable UserDto existingUser, List<Message> messages) { - if (scmAccounts != null) { - for (String scmAccount : scmAccounts) { - if (scmAccount.equals(login) || scmAccount.equals(email)) { - messages.add(Message.of("user.login_or_email_used_as_scm_account")); - } else { - UserDto matchingUser = dbClient.userDao().selectNullableByScmAccountOrLoginOrName(dbSession, scmAccount); - if (matchingUser != null) { - if (existingUser == null || !matchingUser.getId().equals(existingUser.getId())) { - messages.add(Message.of("user.scm_account_already_used", scmAccount, matchingUser.getName(), matchingUser.getLogin())); - } - } + for (String scmAccount : scmAccounts) { + if (scmAccount.equals(login) || scmAccount.equals(email)) { + messages.add(Message.of("user.login_or_email_used_as_scm_account")); + } else { + UserDto matchingUser = dbClient.userDao().selectNullableByScmAccountOrLoginOrName(dbSession, scmAccount); + if (matchingUser != null && (existingUser == null || !matchingUser.getId().equals(existingUser.getId()))) { + messages.add(Message.of("user.scm_account_already_used", scmAccount, matchingUser.getName(), matchingUser.getLogin())); } } } } - @CheckForNull - private static List<String> sanitizeScmAccounts(@Nullable List<String> scmAccounts) { - if (scmAccounts != null) { - scmAccounts.removeAll(Arrays.asList(null, "")); - return scmAccounts; - } - return null; + private static List<String> sanitizeScmAccounts(List<String> scmAccounts) { + scmAccounts.removeAll(Arrays.asList(null, "")); + return scmAccounts; } private void saveUser(DbSession dbSession, UserDto userDto) { @@ -284,22 +283,18 @@ public class UserUpdater implements ServerComponent { userDto.setCryptedPassword(DigestUtils.sha1Hex("--" + saltHex + "--" + password + "--")); } - @CheckForNull - private static String convertScmAccountsToCsv(@Nullable List<String> scmAccounts) { - if (scmAccounts != null) { - // Add one empty character at the begin and at the end of the list to be able to generate a coma at the begin and at the end of the - // text - scmAccounts.add(0, ""); - scmAccounts.add(""); - int size = scmAccounts.size(); - StringWriter writer = new StringWriter(size); - CsvWriter csv = CsvWriter.of(writer); - csv.values(scmAccounts.toArray(new String[size])); - csv.close(); - // Remove useless line break added by CsvWriter at this end of the text - return CharMatcher.BREAKING_WHITESPACE.removeFrom(writer.toString()); - } - return null; + private static String convertScmAccountsToCsv(List<String> scmAccounts) { + // Add one empty character at the begin and at the end of the list to be able to generate a coma at the begin and at the end of the + // text + scmAccounts.add(0, ""); + scmAccounts.add(""); + int size = scmAccounts.size(); + StringWriter writer = new StringWriter(size); + CsvWriter csv = CsvWriter.of(writer); + csv.values(scmAccounts.toArray(new String[size])); + csv.close(); + // Remove useless line break added by CsvWriter at this end of the text + return CharMatcher.BREAKING_WHITESPACE.removeFrom(writer.toString()); } private void notifyNewUser(String login, String name, String email) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java index a5334ace9d6..78c12892025 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/report/JsonReport.java @@ -20,6 +20,7 @@ package org.sonar.batch.scan.report; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Charsets; import com.google.common.io.Closeables; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; @@ -48,11 +49,7 @@ import org.sonar.batch.events.EventBus; import org.sonar.batch.issue.IssueCache; import org.sonar.batch.scan.filesystem.InputPathCache; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -105,7 +102,7 @@ public class JsonReport implements BatchComponent { LOG.info("Export results to " + exportFile.getAbsolutePath()); Writer output = null; try { - output = new BufferedWriter(new FileWriter(exportFile)); + output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportFile), Charsets.UTF_8)); writeJson(output); } catch (IOException e) { diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java index 207ea46546c..ad7194360e4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JsonReportTest.java @@ -76,7 +76,6 @@ public class JsonReportTest { RuleFinder ruleFinder = mock(RuleFinder.class); Settings settings = new Settings(); IssueCache issueCache = mock(IssueCache.class); - private AnalysisMode mode; private UserFinder userFinder; @Before @@ -84,7 +83,7 @@ public class JsonReportTest { SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00")); when(resource.getEffectiveKey()).thenReturn("Action.java"); when(server.getVersion()).thenReturn("3.6"); - mode = mock(AnalysisMode.class); + AnalysisMode mode = mock(AnalysisMode.class); when(mode.isPreview()).thenReturn(true); userFinder = mock(UserFinder.class); DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts"); |