aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/mediumtest/TaskResult.java
blob: 6d2eeabcc735008ea47ed241100f0e096e997b0d (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2014 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube 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.
 *
 * SonarQube 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.batch.mediumtest;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.dependency.internal.DefaultDependency;
import org.sonar.api.batch.sensor.duplication.Duplication;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.SyntaxHighlightingRule;
import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure;
import org.sonar.api.issue.Issue;
import org.sonar.api.issue.internal.DefaultIssue;
import org.sonar.api.measures.Measure;
import org.sonar.api.source.Symbol;
import org.sonar.batch.dependency.DependencyCache;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.highlighting.SyntaxHighlightingData;
import org.sonar.batch.index.Cache.Entry;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.batch.issue.IssueCache;
import org.sonar.batch.scan.ProjectScanContainer;
import org.sonar.batch.scan.filesystem.InputPathCache;
import org.sonar.batch.scan.measure.MeasureCache;
import org.sonar.batch.symbol.SymbolData;
import org.sonar.core.source.SnapshotDataTypes;

import javax.annotation.CheckForNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TaskResult implements org.sonar.batch.mediumtest.ScanTaskObserver {

  private static final Logger LOG = LoggerFactory.getLogger(TaskResult.class);

  private List<Issue> issues = new ArrayList<>();
  private List<org.sonar.api.batch.sensor.measure.Measure> measures = new ArrayList<>();
  private Map<String, List<Duplication>> duplications = new HashMap<>();
  private Map<String, InputFile> inputFiles = new HashMap<>();
  private Map<String, InputDir> inputDirs = new HashMap<>();
  private Map<InputFile, SyntaxHighlightingData> highlightingPerFile = new HashMap<>();
  private Map<InputFile, SymbolData> symbolTablePerFile = new HashMap<>();
  private Map<String, Map<String, Integer>> dependencies = new HashMap<>();

  @Override
  public void scanTaskCompleted(ProjectScanContainer container) {
    LOG.info("Store analysis results in memory for later assertions in medium test");
    for (DefaultIssue issue : container.getComponentByType(IssueCache.class).all()) {
      issues.add(issue);
    }

    storeFs(container);

    storeMeasures(container);

    storeComponentData(container);
    storeDuplication(container);
    // storeTestCases(container);
    // storeCoveragePerTest(container);
    storeDependencies(container);

  }

  private void storeMeasures(ProjectScanContainer container) {
    InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class);
    for (Entry<Measure> measureEntry : container.getComponentByType(MeasureCache.class).entries()) {
      String componentKey = measureEntry.key()[0].toString();
      InputFile file = inputFileCache.getFile(StringUtils.substringBeforeLast(componentKey, ":"), StringUtils.substringAfterLast(componentKey, ":"));
      Measure oldMeasure = measureEntry.value();
      DefaultMeasure<Serializable> newMeasure = new DefaultMeasure<>()
        .forMetric(oldMeasure.getMetric());
      if (file != null) {
        newMeasure.onFile(file);
      } else {
        newMeasure.onProject();
      }
      newMeasure.withValue(oldMeasure.value());
      measures.add(newMeasure);
    }
  }

  private void storeDuplication(ProjectScanContainer container) {
    DuplicationCache duplicationCache = container.getComponentByType(DuplicationCache.class);
    for (String effectiveKey : duplicationCache.componentKeys()) {
      duplications.put(effectiveKey, Lists.<Duplication>newArrayList(duplicationCache.byComponent(effectiveKey)));
    }
  }

  private void storeComponentData(ProjectScanContainer container) {
    ComponentDataCache componentDataCache = container.getComponentByType(ComponentDataCache.class);
    for (InputFile file : inputFiles.values()) {
      SyntaxHighlightingData highlighting = componentDataCache.getData(((DefaultInputFile) file).key(), SnapshotDataTypes.SYNTAX_HIGHLIGHTING);
      if (highlighting != null) {
        highlightingPerFile.put(file, highlighting);
      }
      SymbolData symbolTable = componentDataCache.getData(((DefaultInputFile) file).key(), SnapshotDataTypes.SYMBOL_HIGHLIGHTING);
      if (symbolTable != null) {
        symbolTablePerFile.put(file, symbolTable);
      }
    }
  }

  private void storeFs(ProjectScanContainer container) {
    InputPathCache inputFileCache = container.getComponentByType(InputPathCache.class);
    for (InputFile inputPath : inputFileCache.allFiles()) {
      inputFiles.put(inputPath.relativePath(), inputPath);
    }
    for (InputDir inputPath : inputFileCache.allDirs()) {
      inputDirs.put(inputPath.relativePath(), inputPath);
    }
  }

  private void storeDependencies(ProjectScanContainer container) {
    DependencyCache dependencyCache = container.getComponentByType(DependencyCache.class);
    for (Entry<DefaultDependency> entry : dependencyCache.entries()) {
      String fromKey = entry.key()[1].toString();
      String toKey = entry.key()[2].toString();
      if (!dependencies.containsKey(fromKey)) {
        dependencies.put(fromKey, new HashMap<String, Integer>());
      }
      dependencies.get(fromKey).put(toKey, entry.value().weight());
    }
  }

  public List<Issue> issues() {
    return issues;
  }

  public List<org.sonar.api.batch.sensor.measure.Measure> measures() {
    return measures;
  }

  public Collection<InputFile> inputFiles() {
    return inputFiles.values();
  }

  @CheckForNull
  public InputFile inputFile(String relativePath) {
    return inputFiles.get(relativePath);
  }

  public Collection<InputDir> inputDirs() {
    return inputDirs.values();
  }

  @CheckForNull
  public InputDir inputDir(String relativePath) {
    return inputDirs.get(relativePath);
  }

  public List<Duplication> duplicationsFor(InputFile inputFile) {
    return duplications.get(((DefaultInputFile) inputFile).key());
  }

  /**
   * Get highlighting types at a given position in an inputfile
   * @param charIndex 0-based offset in file
   */
  public List<TypeOfText> highlightingTypeFor(InputFile file, int charIndex) {
    SyntaxHighlightingData syntaxHighlightingData = highlightingPerFile.get(file);
    if (syntaxHighlightingData == null) {
      return Collections.emptyList();
    }
    List<TypeOfText> result = new ArrayList<TypeOfText>();
    for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.syntaxHighlightingRuleSet()) {
      if (sortedRule.getStartPosition() <= charIndex && sortedRule.getEndPosition() > charIndex) {
        result.add(sortedRule.getTextType());
      }
    }
    return result;
  }

  /**
   * Get list of all positions of a symbol in an inputfile
   * @param symbolStartOffset 0-based start offset for the symbol in file
   * @param symbolEndOffset 0-based end offset for the symbol in file
   */
  @CheckForNull
  public Set<Integer> symbolReferencesFor(InputFile file, int symbolStartOffset, int symbolEndOffset) {
    SymbolData data = symbolTablePerFile.get(file);
    if (data == null) {
      return null;
    }
    for (Symbol symbol : data.referencesBySymbol().keySet()) {
      if (symbol.getDeclarationStartOffset() == symbolStartOffset && symbol.getDeclarationEndOffset() == symbolEndOffset) {
        return data.referencesBySymbol().get(symbol);
      }
    }
    return null;
  }

  /**
   * @return null if no dependency else return dependency weight.
   */
  @CheckForNull
  public Integer dependencyWeight(InputFile from, InputFile to) {
    String fromKey = ((DefaultInputFile) from).key();
    String toKey = ((DefaultInputFile) to).key();
    return dependencies.containsKey(fromKey) ? dependencies.get(fromKey).get(toKey) : null;
  }
}