From ae4728b46e5b88b084bd0ae939b3c6bf5f81ed01 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Tue, 1 Sep 2015 14:13:13 +0200 Subject: Improve batch unit tests --- .../java/org/sonar/batch/rule/DefaultRulesLoader.java | 2 +- .../repository/user/UserRepositoryLoaderTest.java | 8 +++++++- .../org/sonar/batch/rule/DefaultRulesLoaderTest.java | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) (limited to 'sonar-batch') diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java index 9d055b064c1..b427cfed871 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java @@ -57,7 +57,7 @@ public class DefaultRulesLoader implements RulesLoader { try (InputStream is = input.openStream()) { return ListResponse.parseFrom(is); } catch (IOException e) { - throw new IllegalStateException("Unable to get previous issues", e); + throw new IllegalStateException("Unable to get rules", e); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryLoaderTest.java index c63bfda8d4c..5410f18a7c1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/user/UserRepositoryLoaderTest.java @@ -19,8 +19,9 @@ */ package org.sonar.batch.repository.user; -import org.sonar.batch.cache.WSLoaderResult; +import org.assertj.core.util.Lists; +import org.sonar.batch.cache.WSLoaderResult; import org.sonar.batch.cache.WSLoader; import org.junit.Before; import com.google.common.collect.ImmutableList; @@ -59,6 +60,11 @@ public class UserRepositoryLoaderTest { userRepo = new UserRepositoryLoader(wsLoader); } + @Test + public void testLoadEmptyList() { + assertThat(userRepo.load(Lists.emptyList())).isEmpty(); + } + @Test public void testLoad() throws IOException { Map userMap = ImmutableMap.of("fmallet", "Freddy Mallet", "sbrandhof", "Simon"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java index 8995d3457b7..1c593a51361 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java @@ -22,8 +22,9 @@ package org.sonar.batch.rule; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import org.sonar.batch.cache.WSLoaderResult; +import org.junit.rules.ExpectedException; +import org.sonar.batch.cache.WSLoaderResult; import org.sonar.batch.cache.WSLoader; import org.apache.commons.lang.mutable.MutableBoolean; import org.sonarqube.ws.Rules.ListResponse.Rule; @@ -38,6 +39,9 @@ import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class DefaultRulesLoaderTest { + @org.junit.Rule + public ExpectedException exception = ExpectedException.none(); + @Test public void testParseServerResponse() throws IOException { WSLoader wsLoader = mock(WSLoader.class); @@ -60,4 +64,17 @@ public class DefaultRulesLoaderTest { assertThat(fromCache.booleanValue()).isTrue(); } + @Test + public void testError() { + WSLoader wsLoader = mock(WSLoader.class); + ByteSource source = ByteSource.wrap(new String("trash").getBytes()); + when(wsLoader.loadSource(anyString())).thenReturn(new WSLoaderResult<>(source, true)); + DefaultRulesLoader loader = new DefaultRulesLoader(wsLoader); + + exception.expect(IllegalStateException.class); + exception.expectMessage("Unable to get rules"); + + loader.load(null); + } + } -- cgit v1.2.3