]> source.dussan.org Git - sonarqube.git/commitdiff
Fix Quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sat, 12 Nov 2016 14:14:55 +0000 (15:14 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sat, 12 Nov 2016 15:24:37 +0000 (16:24 +0100)
- missing tests for Protobuf
- remove incorrect imports of org.elasticsearch.common.Strings

server/sonar-server/src/main/java/org/sonar/server/authentication/BasicAuthenticator.java
server/sonar-server/src/main/java/org/sonar/server/authentication/JwtHttpHandler.java
server/sonar-server/src/main/java/org/sonar/server/authentication/RealmAuthenticator.java
server/sonar-server/src/main/java/org/sonar/server/authentication/ws/LoginAction.java
server/sonar-server/src/main/java/org/sonar/server/setting/ws/ListDefinitionsAction.java
server/sonar-server/src/main/java/org/sonar/server/setting/ws/ValuesAction.java
sonar-core/src/test/java/org/sonar/core/util/ProtobufTest.java

index 3c000d9b467c3fc82b1a8ed256a5b963adab1a37..9808d57b8f3d9b987b8b0c0a753d0363d8e5303d 100644 (file)
@@ -20,9 +20,6 @@
 
 package org.sonar.server.authentication;
 
-import static java.util.Locale.ENGLISH;
-import static org.elasticsearch.common.Strings.isEmpty;
-
 import com.google.common.base.Charsets;
 import java.util.Base64;
 import java.util.Optional;
@@ -33,6 +30,9 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.usertoken.UserTokenAuthenticator;
 
+import static java.util.Locale.ENGLISH;
+import static org.apache.commons.lang.StringUtils.isEmpty;
+
 public class BasicAuthenticator {
 
   private static final Base64.Decoder BASE64_DECODER = Base64.getDecoder();
index bcb9f5ba3497cc088fec7a7278e141210f0c4678..54d571abe9875443456b0c04d99ebad64ea2cec5 100644 (file)
@@ -38,8 +38,8 @@ import org.sonar.db.DbSession;
 import org.sonar.db.user.UserDto;
 
 import static java.util.Objects.requireNonNull;
+import static org.apache.commons.lang.StringUtils.isEmpty;
 import static org.apache.commons.lang.time.DateUtils.addSeconds;
-import static org.elasticsearch.common.Strings.isNullOrEmpty;
 import static org.sonar.server.authentication.CookieUtils.findCookie;
 
 @ServerSide
@@ -117,7 +117,7 @@ public class JwtHttpHandler {
     }
     Cookie cookie = jwtCookie.get();
     String token = cookie.getValue();
-    if (isNullOrEmpty(token)) {
+    if (isEmpty(token)) {
       return Optional.empty();
     }
     return Optional.of(token);
index d2ee0426bc493ac1028b1faf986017901f247735..76ccd170e91f14888752f7dfea11f6eafc234776 100644 (file)
 
 package org.sonar.server.authentication;
 
-import static java.util.Objects.requireNonNull;
-import static org.apache.commons.lang.StringUtils.trimToNull;
-import static org.elasticsearch.common.Strings.isNullOrEmpty;
-import static org.sonar.api.CoreProperties.CORE_AUTHENTICATOR_CREATE_USERS;
-import static org.sonar.server.user.UserUpdater.SQ_AUTHORITY;
-
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.Locale;
@@ -47,6 +41,12 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.user.SecurityRealmFactory;
 
+import static java.util.Objects.requireNonNull;
+import static org.apache.commons.lang.StringUtils.isEmpty;
+import static org.apache.commons.lang.StringUtils.trimToNull;
+import static org.sonar.api.CoreProperties.CORE_AUTHENTICATOR_CREATE_USERS;
+import static org.sonar.server.user.UserUpdater.SQ_AUTHORITY;
+
 public class RealmAuthenticator implements Startable {
 
   private static final Logger LOG = Loggers.get(RealmAuthenticator.class);
@@ -107,7 +107,7 @@ public class RealmAuthenticator implements Startable {
     String name = details.getName();
     UserIdentity.Builder userIdentityBuilder = UserIdentity.builder()
       .setLogin(userLogin)
-      .setName(isNullOrEmpty(name) ? userLogin : name)
+      .setName(isEmpty(name) ? userLogin : name)
       .setEmail(trimToNull(details.getEmail()))
       .setProviderLogin(userLogin);
     if (externalGroupsProvider != null) {
index eb69f2b569d80aabd0f0ee285b834c0480232d2c..3a959c4fd122414f68db982e03dbfb9e152e338c 100644 (file)
@@ -38,7 +38,7 @@ import org.sonar.server.user.ServerUserSession;
 import org.sonar.server.user.ThreadLocalUserSession;
 
 import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
-import static org.elasticsearch.common.Strings.isNullOrEmpty;
+import static org.apache.commons.lang.StringUtils.isEmpty;
 
 public class LoginAction extends ServletFilter {
 
@@ -85,7 +85,7 @@ public class LoginAction extends ServletFilter {
   private UserDto authenticate(HttpServletRequest request) {
     String login = request.getParameter("login");
     String password = request.getParameter("password");
-    if (isNullOrEmpty(login) || isNullOrEmpty(password)) {
+    if (isEmpty(login) || isEmpty(password)) {
       throw new UnauthorizedException();
     }
     return credentialsAuthenticator.authenticate(login, password, request);
index 6c4f0a639f8431a10e3e31b55eee36ff8db0d01f..1ada6c3c0e761a1c7b3ca9f83e490395ed66ce5a 100644 (file)
@@ -38,7 +38,8 @@ import org.sonarqube.ws.Settings;
 import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
 import org.sonarqube.ws.client.setting.ListDefinitionsRequest;
 
-import static org.elasticsearch.common.Strings.isNullOrEmpty;
+import static com.google.common.base.Strings.emptyToNull;
+import static org.sonar.core.util.Protobuf.setNullable;
 import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
 import static org.sonar.server.setting.ws.SettingsWsComponentParameters.addComponentParameters;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
@@ -134,30 +135,14 @@ public class ListDefinitionsAction implements SettingsWsAction {
       .setKey(key)
       .setType(Settings.Type.valueOf(definition.type().name()))
       .setMultiValues(definition.multiValues());
-    String deprecatedKey = definition.deprecatedKey();
-    if (!isNullOrEmpty(deprecatedKey)) {
-      builder.setDeprecatedKey(deprecatedKey);
-    }
-    String name = definition.name();
-    if (!isNullOrEmpty(name)) {
-      builder.setName(name);
-    }
-    String description = definition.description();
-    if (!isNullOrEmpty(description)) {
-      builder.setDescription(description);
-    }
+    setNullable(emptyToNull(definition.deprecatedKey()), builder::setDeprecatedKey);
+    setNullable(emptyToNull(definition.name()), builder::setName);
+    setNullable(emptyToNull(definition.description()), builder::setDescription);
     String category = propertyDefinitions.getCategory(key);
-    if (!isNullOrEmpty(category)) {
-      builder.setCategory(category);
-    }
+    setNullable(emptyToNull(category), builder::setCategory);
     String subCategory = propertyDefinitions.getSubCategory(key);
-    if (!isNullOrEmpty(subCategory)) {
-      builder.setSubCategory(subCategory);
-    }
-    String defaultValue = definition.defaultValue();
-    if (!isNullOrEmpty(defaultValue)) {
-      builder.setDefaultValue(defaultValue);
-    }
+    setNullable(emptyToNull(subCategory), builder::setSubCategory);
+    setNullable(emptyToNull(definition.defaultValue()), builder::setDefaultValue);
     List<String> options = definition.options();
     if (!options.isEmpty()) {
       builder.addAllOptions(options);
index e5e6567e6d6d0b4c038270c17230efd5af8b6bca..4388c04c97e28e944e17071a6843597ada2ebc1c 100644 (file)
@@ -47,7 +47,7 @@ import org.sonarqube.ws.Settings.ValuesWsResponse;
 import org.sonarqube.ws.client.setting.ValuesRequest;
 
 import static java.lang.String.format;
-import static org.elasticsearch.common.Strings.isNullOrEmpty;
+import static org.apache.commons.lang.StringUtils.isEmpty;
 import static org.sonar.api.PropertyType.PROPERTY_SET;
 import static org.sonar.server.component.ComponentFinder.ParamNames.ID_AND_KEY;
 import static org.sonar.server.setting.ws.SettingsWsComponentParameters.addComponentParameters;
@@ -156,7 +156,7 @@ public class ValuesAction implements SettingsWsAction {
   private List<Setting> loadDefaultSettings(Set<String> keys) {
     return propertyDefinitions.getAll().stream()
       .filter(definition -> keys.contains(definition.key()))
-      .filter(defaultProperty -> !isNullOrEmpty(defaultProperty.defaultValue()))
+      .filter(defaultProperty -> !isEmpty(defaultProperty.defaultValue()))
       .map(Setting::createForDefinition)
       .collect(Collectors.toList());
   }
index 95e2f6ee648f0983b89eff4bfd365618629cf5d1..dcbef1412cb6c179994d3532183a7e4a3b6261a3 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.core.util;
 
 import java.io.File;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -30,6 +31,7 @@ import org.sonar.test.TestUtils;
 import static java.util.Arrays.asList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.core.test.Test.Fake;
+import static org.sonar.core.util.Protobuf.setNullable;
 
 public class ProtobufTest {
 
@@ -51,13 +53,13 @@ public class ProtobufTest {
 
     File file = temp.newFile();
     FileUtils.forceDelete(file);
-    Protobuf.read(file, Fake.PARSER);
+    Protobuf.read(file, Fake.parser());
   }
 
   @Test
   public void read_file_returns_empty_message_if_file_is_empty() throws Exception {
     File file = temp.newFile();
-    Fake msg = Protobuf.read(file, Fake.PARSER);
+    Fake msg = Protobuf.read(file, Fake.parser());
     assertThat(msg).isNotNull();
     assertThat(msg.isInitialized()).isTrue();
   }
@@ -66,7 +68,7 @@ public class ProtobufTest {
   public void read_file_returns_message() throws Exception {
     File file = temp.newFile();
     Protobuf.write(Fake.getDefaultInstance(), file);
-    Fake message = Protobuf.read(file, Fake.PARSER);
+    Fake message = Protobuf.read(file, Fake.parser());
     assertThat(message).isNotNull();
     assertThat(message.isInitialized()).isTrue();
   }
@@ -88,7 +90,7 @@ public class ProtobufTest {
     Fake item2 = Fake.newBuilder().setLabel("two").build();
     Protobuf.writeStream(asList(item1, item2), file, false);
 
-    CloseableIterator<Fake> it = Protobuf.readStream(file, Fake.PARSER);
+    CloseableIterator<Fake> it = Protobuf.readStream(file, Fake.parser());
     Fake read = it.next();
     assertThat(read.getLabel()).isEqualTo("one");
     assertThat(read.getLine()).isEqualTo(1);
@@ -104,16 +106,43 @@ public class ProtobufTest {
     thrown.expectMessage("Unable to read messages");
 
     File dir = temp.newFolder();
-    Protobuf.readStream(dir, Fake.PARSER);
+    Protobuf.readStream(dir, Fake.parser());
   }
 
   @Test
   public void read_empty_stream() throws Exception {
     File file = temp.newFile();
-    CloseableIterator<Fake> it = Protobuf.readStream(file, Fake.PARSER);
+    CloseableIterator<Fake> it = Protobuf.readStream(file, Fake.parser());
     assertThat(it).isNotNull();
     assertThat(it.hasNext()).isFalse();
   }
 
-  // TODO test in-moemry file
+  @Test
+  public void setNullable_sets_field_if_value_is_not_null() {
+    Fake.Builder builder = Fake.newBuilder();
+
+    setNullable("foo", builder::setLabel);
+    assertThat(builder.getLabel()).isEqualTo("foo");
+
+    builder.clear();
+    setNullable(null, builder::setLabel);
+    assertThat(builder.hasLabel()).isFalse();
+  }
+
+  @Test
+  public void setNullable_converts_value_and_sets_field_if_value_is_not_null() {
+    Fake.Builder builder = Fake.newBuilder();
+
+    setNullable("foo", builder::setLabel, StringUtils::upperCase);
+    assertThat(builder.getLabel()).isEqualTo("FOO");
+
+    builder.clear();
+    setNullable((String)null, builder::setLabel, StringUtils::upperCase);
+    assertThat(builder.hasLabel()).isFalse();
+
+    // do not set field if value is present but result of conversion is null
+    builder.clear();
+    setNullable("    ", builder::setLabel, StringUtils::trimToNull);
+    assertThat(builder.hasLabel()).isFalse();
+  }
 }