From 54efd097b74af0fccad20f19555652ce693d6020 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 6 Nov 2015 18:38:02 +0100 Subject: [PATCH] SONAR-6990 add one missing UT on DuplicationsPublisher --- .../report/DuplicationsPublisherTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/DuplicationsPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/DuplicationsPublisherTest.java index b57c18df8e2..6482786f7dd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/DuplicationsPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/DuplicationsPublisherTest.java @@ -22,9 +22,12 @@ package org.sonar.batch.report; import java.io.File; import java.util.Arrays; import java.util.Collections; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; @@ -37,6 +40,7 @@ import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReportReader; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.core.util.CloseableIterator; +import org.sonar.core.util.ContextException; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.anyString; @@ -47,6 +51,8 @@ public class DuplicationsPublisherTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); private DuplicationCache duplicationCache; private DuplicationsPublisher publisher; @@ -65,6 +71,48 @@ public class DuplicationsPublisherTest { publisher = new DuplicationsPublisher(resourceCache, duplicationCache); } + @Test + public void publishDuplications_throws_IAE_if_resource_of_duplicate_does_not_exist() throws Exception { + + DefaultDuplication dup1 = new DefaultDuplication() + .setOriginBlock(new Duplication.Block("foo:src/Foo.php", 11, 10)) + .isDuplicatedBy("another", 20, 50); + when(duplicationCache.byComponent("foo:src/Foo.php")).thenReturn(Arrays.asList(dup1)); + + expectedException.expect(ContextException.class); + expectedException.expectCause(new CauseMatcher(IllegalStateException.class, "No cross project duplication supported on batch side: another")); + + File outputDir = temp.newFolder(); + BatchReportWriter writer = new BatchReportWriter(outputDir); + + publisher.publish(writer); + } + + private static class CauseMatcher extends TypeSafeMatcher { + + private final Class type; + private final String expectedMessage; + + public CauseMatcher(Class type, String expectedMessage) { + this.type = type; + this.expectedMessage = expectedMessage; + } + + @Override + protected boolean matchesSafely(Throwable item) { + return item.getClass().isAssignableFrom(type) + && item.getMessage().contains(expectedMessage); + } + + @Override + public void describeTo(Description description) { + description.appendText("expects type ") + .appendValue(type) + .appendText(" and a message ") + .appendValue(expectedMessage); + } + } + @Test public void publishDuplications() throws Exception { -- 2.39.5