]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6990 add one missing UT on DuplicationsPublisher
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 6 Nov 2015 17:38:02 +0000 (18:38 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 9 Nov 2015 15:34:20 +0000 (16:34 +0100)
sonar-batch/src/test/java/org/sonar/batch/report/DuplicationsPublisherTest.java

index b57c18df8e2c74e48586a6225b71d10434aea840..6482786f7dd7e5f546434f85497983094abedb93 100644 (file)
@@ -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<Throwable> {
+
+    private final Class<? extends Throwable> type;
+    private final String expectedMessage;
+
+    public CauseMatcher(Class<? extends Throwable> 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 {