aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-duplications/src/test/java/org/sonar/duplications/detector/CloneGroupMatcher.java
blob: 7671259291f39548db9e57c535105813e19ff8c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.duplications.detector;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.sonar.duplications.index.CloneGroup;
import org.sonar.duplications.index.ClonePart;

import static org.hamcrest.CoreMatchers.hasItem;

public class CloneGroupMatcher extends TypeSafeMatcher<CloneGroup> {

  public static Matcher<Iterable<? super CloneGroup>> hasCloneGroup(int expectedLen, ClonePart... expectedParts) {
    return hasItem(new CloneGroupMatcher(expectedLen, expectedParts));
  }

  private final int expectedLen;
  private final ClonePart[] expectedParts;

  private CloneGroupMatcher(int expectedLen, ClonePart... expectedParts) {
    this.expectedLen = expectedLen;
    this.expectedParts = expectedParts;
  }

  @Override
  public boolean matchesSafely(CloneGroup cloneGroup) {
    // Check length
    if (expectedLen != cloneGroup.getCloneUnitLength()) {
      return false;
    }
    // Check number of parts
    if (expectedParts.length != cloneGroup.getCloneParts().size()) {
      return false;
    }
    // Check origin
    if (!expectedParts[0].equals(cloneGroup.getOriginPart())) {
      return false;
    }
    // Check parts
    for (ClonePart expectedPart : expectedParts) {
      boolean matched = false;
      for (ClonePart part : cloneGroup.getCloneParts()) {
        if (part.equals(expectedPart)) {
          matched = true;
          break;
        }
      }
      if (!matched) {
        return false;
      }
    }
    return true;
  }

  public void describeTo(Description description) {
    StringBuilder builder = new StringBuilder();
    for (ClonePart part : expectedParts) {
      builder.append(part).append(" - ");
    }
    builder.append(expectedLen);
    description.appendText(builder.toString());
  }

}