diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-08-02 10:21:36 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-08-02 10:21:48 +0200 |
commit | 00f24d0af28bc7b2d9912b6e8f62b1787a2a6544 (patch) | |
tree | b7ef1ee68b259e79b15da13605750135490964c0 | |
parent | e9f71cf1c606731aeccb6120467699281254f2b4 (diff) | |
download | sonarqube-00f24d0af28bc7b2d9912b6e8f62b1787a2a6544.tar.gz sonarqube-00f24d0af28bc7b2d9912b6e8f62b1787a2a6544.zip |
Improve ServerIdGeneratorTest
* use assertJ
* support offline build
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java | 18 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java | 26 |
2 files changed, 15 insertions, 29 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java index 099e142f8ff..7c9b1299a66 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerIdGenerator.java @@ -19,12 +19,13 @@ */ package org.sonar.server.platform; -import com.google.common.collect.Lists; -import java.io.UnsupportedEncodingException; +import com.google.common.annotations.VisibleForTesting; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.regex.Pattern; @@ -32,9 +33,6 @@ import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.log.Loggers; -/** - * @since 2.11 - */ public class ServerIdGenerator { private static final Pattern ORGANIZATION_PATTERN = Pattern.compile("[a-zA-Z0-9]+[a-zA-Z0-9 ]*"); @@ -52,6 +50,7 @@ public class ServerIdGenerator { this(false); } + @VisibleForTesting ServerIdGenerator(boolean acceptPrivateAddress) { this.acceptPrivateAddress = acceptPrivateAddress; } @@ -82,12 +81,7 @@ public class ServerIdGenerator { String toId(String organisation, InetAddress address) { String id = new StringBuilder().append(organisation).append("-").append(address.getHostAddress()).toString(); - try { - return VERSION + DigestUtils.sha1Hex(id.getBytes("UTF-8")).substring(0, CHECKSUM_SIZE); - - } catch (UnsupportedEncodingException e) { - throw new IllegalArgumentException("Organisation is not UTF-8 encoded: " + organisation, e); - } + return VERSION + DigestUtils.sha1Hex(id.getBytes(StandardCharsets.UTF_8)).substring(0, CHECKSUM_SIZE); } private InetAddress toValidAddress(String ipAddress) { @@ -106,7 +100,7 @@ public class ServerIdGenerator { } public List<InetAddress> getAvailableAddresses() { - List<InetAddress> result = Lists.newArrayList(); + List<InetAddress> result = new ArrayList<>(); try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java index 70532ba30a5..72052685400 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ServerIdGeneratorTest.java @@ -19,17 +19,14 @@ */ package org.sonar.server.platform; +import java.net.InetAddress; +import java.net.UnknownHostException; import org.apache.commons.lang.StringUtils; -import org.hamcrest.core.Is; import org.junit.BeforeClass; import org.junit.Test; -import java.net.InetAddress; -import java.net.UnknownHostException; - +import static org.apache.commons.lang.StringUtils.isBlank; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertThat; public class ServerIdGeneratorTest { @@ -69,24 +66,19 @@ public class ServerIdGeneratorTest { @Test public void idShouldHaveTenCharacters() { String id = new ServerIdGenerator().toId("SonarSource", localhost); - assertThat(id.length(), Is.is(15)); // first character is version + 14 characters for checksum - assertThat(StringUtils.isBlank(id), Is.is(false)); + assertThat(id).hasSize(15); // first character is version + 14 characters for checksum + assertThat(isBlank(id)).isFalse(); } @Test public void idShouldStartWithVersion() { String id = new ServerIdGenerator().toId("SonarSource", localhost); - assertThat(id, startsWith(ServerIdGenerator.VERSION)); + assertThat(id).startsWith(ServerIdGenerator.VERSION); } @Test public void loopbackAddressesShouldNotBeAccepted() throws UnknownHostException { - assertThat(new ServerIdGenerator().isFixed(InetAddress.getByName("127.0.0.1")), Is.is(false)); - } - - @Test - public void publicAddressesNotBeAccepted() throws UnknownHostException { - assertThat(new ServerIdGenerator().isFixed(InetAddress.getByName("sonarsource.com")), Is.is(true)); + assertThat(new ServerIdGenerator().isFixed(InetAddress.getLoopbackAddress())).isFalse(); } @Test @@ -95,7 +87,7 @@ public class ServerIdGeneratorTest { String k1 = generator.generate("Corp One", "127.0.0.1"); String k2 = generator.generate("Corp Two", "127.0.0.1"); - assertThat(StringUtils.equals(k1, k2), Is.is(false)); + assertThat(StringUtils.equals(k1, k2)).isFalse(); } @Test @@ -103,7 +95,7 @@ public class ServerIdGeneratorTest { ServerIdGenerator generator = new ServerIdGenerator(true); String i1 = generator.generate("SonarSource", "127.0.0.1"); String i2 = generator.generate("SonarSource", "127.0.0.1"); - assertThat(StringUtils.equals(i1, i2), Is.is(true)); + assertThat(StringUtils.equals(i1, i2)).isTrue(); } } |