Skip to content

Commit 53d8d81

Browse files
added tests for sensor and parser
1 parent cfcaa87 commit 53d8d81

10 files changed

Lines changed: 34 additions & 1257 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515

16-
<sonar.buildVersion>3.6</sonar.buildVersion>
16+
<sonar.buildVersion>3.5.1</sonar.buildVersion>
1717
<jdk.min.version>1.5</jdk.min.version>
1818
</properties>
1919

src/main/java/com/godaddy/sonar/ruby/core/RubyFile.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.apache.commons.lang.StringUtils;
44
import org.apache.commons.lang.builder.ToStringBuilder;
5-
import org.slf4j.Logger;
6-
import org.slf4j.LoggerFactory;
75
import org.sonar.api.resources.Language;
86
import org.sonar.api.resources.Qualifiers;
97
import org.sonar.api.resources.Resource;

src/main/java/com/godaddy/sonar/ruby/simplecovrcov/SimpleCovRcovJsonParserImpl.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,16 @@
1212

1313
import com.google.common.collect.Maps;
1414

15-
public class SimpleCovRcovJsonParserImpl implements SimpleCovRcovJsonParser {
16-
15+
public class SimpleCovRcovJsonParserImpl implements SimpleCovRcovJsonParser
16+
{
1717
public Map<String, CoverageMeasuresBuilder> parse(File file) throws IOException
18-
{
19-
18+
{
2019
Map<String, CoverageMeasuresBuilder> coveredFiles = Maps.newHashMap();
2120

2221
File fileToFindCoverage = file;
23-
//read content of file as one string, assumes the content of the file is in json format
22+
2423
String fileString = FileUtils.readFileToString(fileToFindCoverage, "UTF-8");
2524

26-
//Parse the json string into a JSONObject, the coverageJsonObject will contain the map for files and their coverage
2725
JSONObject resultJsonObject = (JSONObject) JSONValue.parse(fileString);
2826
JSONObject coverageJsonObj = (JSONObject) ((JSONObject) resultJsonObject.get("RSpec")).get("coverage");
2927

@@ -38,15 +36,9 @@ public Map<String, CoverageMeasuresBuilder> parse(File file) throws IOException
3836
//for each line in the coverage array
3937
for(int i = 0; i < coverageArray.size(); i++)
4038
{
41-
//get the current line
4239
Long line = (Long) coverageArray.toArray()[i];
43-
//going to store that line as int, so it can be used with the setHits sonar api call
44-
Integer intLine = 0;
45-
46-
//the current line number will be the index of the array + 1, since there is not a line 0
47-
int lineNumber = i+1;
48-
49-
//if line is null, then that line was not covered, so set the value to 0
40+
Integer intLine = 0;
41+
int lineNumber = i+1;
5042
if(line!=null)
5143
{
5244
intLine = line.intValue();
@@ -56,7 +48,5 @@ public Map<String, CoverageMeasuresBuilder> parse(File file) throws IOException
5648
coveredFiles.put(filePath, fileCoverage);
5749
}
5850
return coveredFiles;
59-
}
60-
61-
51+
}
6252
}

src/main/java/com/godaddy/sonar/ruby/simplecovrcov/SimpleCovRcovSensor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ private void calculateMetrics(List<File> sourceDirs, File jsonFile, final Sensor
6161
for (Entry<String, CoverageMeasuresBuilder> entry : jsonResults.entrySet())
6262
{
6363
try
64-
{
65-
66-
String fileName = entry.getKey();
67-
68-
sourceFile = new File(fileName);
69-
64+
{
65+
String fileName = entry.getKey();
66+
sourceFile = new File(fileName);
7067
RubyFile rubyFile = new RubyFile(sourceFile, sourceDirs);
7168

7269
CoverageMeasuresBuilder fileCoverage = entry.getValue();

src/test/java/com/godaddy/sonar/ruby/simplecovrcov/SimpleCovRcovJsonParserTest.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@
22

33
import java.io.File;
44
import java.io.IOException;
5-
import java.util.Arrays;
6-
import java.util.Collections;
7-
import java.util.List;
85
import java.util.Map;
9-
import java.util.SortedMap;
10-
116
import org.junit.After;
127
import org.junit.Before;
138
import org.junit.Test;
149
import org.sonar.api.measures.CoverageMeasuresBuilder;
15-
1610
import junit.framework.TestCase;
1711

18-
import org.junit.Assert;
19-
2012
public class SimpleCovRcovJsonParserTest extends TestCase
2113
{
2214
private final static String JSON_FILE_NAME = "src/test/resources/test-data/results.json";
23-
private final static String JSON_INVALID_JSON= "src/test/resources/test-data/results-invalid-json.json";
2415

2516
private SimpleCovRcovJsonParserImpl parser = null;
17+
2618
@Before
2719
public void setUp() throws Exception
2820
{
@@ -41,18 +33,17 @@ public void testParserWithValidJson() throws IOException
4133
File reportFile = new File(JSON_FILE_NAME);
4234
Map<String, CoverageMeasuresBuilder> coveredFiles = parser.parse(reportFile);
4335

44-
String coveredFile1 = "/home/mxsmith/Projects/Godaddy-Hosting/cPanel-common/lib/orion_api/orion_api.rb";
45-
String coveredFile2 = "/home/mxsmith/Projects/Godaddy-Hosting/cPanel-common/lib/orion_api/orion_account_query.rb";
46-
String coveredFile3 = "/home/mxsmith/Projects/Godaddy-Hosting/cPanel-common/lib/orion_api/soap_client.rb";
36+
String coveredFile1 = "/project/source/subdir/file.rb";
37+
String coveredFile2 = "/project/source/subdir/file1.rb";
38+
String coveredFile3 = "/project/source/subdir/file1.rb";
4739

4840
assertEquals(coveredFiles.size(), 12);
4941
assertEquals(coveredFiles.containsKey(coveredFile1), true);
5042
assertEquals(coveredFiles.containsKey(coveredFile2), true);
5143
assertEquals(coveredFiles.containsKey(coveredFile3), true);
5244

53-
CoverageMeasuresBuilder builder1 = coveredFiles.get(coveredFile1);
54-
System.out.println(builder1);
55-
assertEquals(builder1.getCoveredLines(), 13);
45+
CoverageMeasuresBuilder builder = coveredFiles.get(coveredFile1);
46+
assertEquals(builder.getCoveredLines(), 13);
5647
}
5748

5849
}

src/test/java/com/godaddy/sonar/ruby/simplecovrcov/SimpleCovRcovSensorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class SimpleCovRcovSensorTest
2727
{
2828
private static String RESULT_JSON_FILE_MUTLI_SRC_DIR = "src/test/resources/test-data/results.json";
2929
private static String RESULT_JSON_FILE_ONE_SRC_DIR = "src/test/resources/test-data/results-one-src-dir.json";
30-
private static String RESULT_NO_TESTS_JSON_FILE = "src/test/resources/test-data/results-zero-tests.json";
3130

3231
private IMocksControl mocksControl;
3332
private ModuleFileSystem moduleFileSystem;
@@ -82,7 +81,7 @@ public void testShouldNotExecuteOnJavascriptProject()
8281
}
8382

8483
@Test
85-
public void testAnaluseWithOneSrcDir() throws IOException
84+
public void testAnalyseWithOneSrcDir() throws IOException
8685
{
8786
Map<String, CoverageMeasuresBuilder> jsonResults = new SimpleCovRcovJsonParserImpl().parse(new File(RESULT_JSON_FILE_ONE_SRC_DIR));
8887

0 commit comments

Comments
 (0)