public class DuplicationPersisterTest extends AbstractDaoTestCase {

  @org.junit.Rule public ExpectedException thrown = ExpectedException.none();

  public static final int FILE_SNAPSHOT_ID = 3003;

  DuplicationPersister duplicationPersister;
  RuleFinder ruleFinder = mock(RuleFinder.class);
  File aFile = File.create("org/foo/Bar.java");
  Snapshot fileSnapshot = snapshot(FILE_SNAPSHOT_ID);

  private DuplicationCache duplicationCache;

  @Before
  public void mockResourcePersister() {
    duplicationCache = mock(DuplicationCache.class);

    ResourceCache resourceCache = mock(ResourceCache.class);
    BatchResource batchResource = mock(BatchResource.class);
    when(batchResource.resource()).thenReturn(aFile);
    when(batchResource.snapshotId()).thenReturn(FILE_SNAPSHOT_ID);
    when(resourceCache.get("foo:org/foo/Bar.java")).thenReturn(batchResource);

    MetricFinder metricFinder = mock(MetricFinder.class);
    when(metricFinder.findByKey(CoreMetrics.DUPLICATIONS_DATA_KEY))
        .thenReturn(CoreMetrics.DUPLICATIONS_DATA.setId(2));

    duplicationPersister =
        new DuplicationPersister(
            getMyBatis(), ruleFinder, resourceCache, duplicationCache, metricFinder);
  }

  @Test
  public void should_insert_duplications() {
    setupData("empty");

    Duplication.Block originBlock = new Duplication.Block("foo:org/foo/Bar.java", 1, 4);

    DefaultDuplication group = new DefaultDuplication().setOriginBlock(originBlock);
    group.duplicates().add(new Duplication.Block("foo:org/foo/Foo.java", 5, 9));

    when(duplicationCache.componentKeys()).thenReturn(Arrays.asList("foo:org/foo/Bar.java"));

    when(duplicationCache.byComponent("foo:org/foo/Bar.java")).thenReturn(Arrays.asList(group));

    duplicationPersister.persist();

    checkTables("shouldInsertDuplication", "project_measures");
  }

  private static Snapshot snapshot(int id) {
    Snapshot snapshot = mock(Snapshot.class);
    when(snapshot.getId()).thenReturn(id);
    return snapshot;
  }
}
Exemplo n.º 2
0
 @Override
 public <P extends Perspective> P as(Class<P> perspectiveClass, InputPath inputPath) {
   Resource r;
   if (inputPath instanceof InputDir) {
     r = Directory.create(((InputDir) inputPath).relativePath());
   } else if (inputPath instanceof InputFile) {
     r = File.create(((InputFile) inputPath).relativePath());
   } else {
     throw new IllegalArgumentException("Unknow input path type: " + inputPath);
   }
   return as(perspectiveClass, r);
 }
Exemplo n.º 3
0
  @Test
  public void test_measure_coder() throws Exception {
    Resource file1 = File.create("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt");

    Measure measure = new Measure(CoreMetrics.NCLOC, 3.14);
    measure.setData("data");
    measure.setAlertStatus(Level.ERROR);
    measure.setAlertText("alert");
    measure.setDate(new Date());
    measure.setDescription("description");
    measure.setPersistenceMode(null);
    measure.setPersonId(3);
    measure.setUrl("http://foo");
    measure.setVariation1(11.0);
    measure.setVariation2(12.0);
    measure.setVariation3(13.0);
    measure.setVariation4(14.0);
    measure.setVariation5(15.0);
    measureCache.put(file1, measure);

    Measure savedMeasure = measureCache.byResource(file1).iterator().next();
    assertThat(EqualsBuilder.reflectionEquals(measure, savedMeasure)).isTrue();
  }