|
| 1 | +/* |
| 2 | + * Python :: ITs :: Plugin |
| 3 | + * Copyright (C) 2012 SonarSource and Waleri Enns |
| 4 | + |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public |
| 17 | + * License along with this program; if not, write to the Free Software |
| 18 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 |
| 19 | + */ |
| 20 | +package com.sonar.python.it.plugin; |
| 21 | + |
| 22 | +import com.sonar.orchestrator.Orchestrator; |
| 23 | +import com.sonar.orchestrator.build.SonarRunner; |
| 24 | +import org.junit.BeforeClass; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.Test; |
| 27 | +import org.sonar.wsclient.Sonar; |
| 28 | +import org.sonar.wsclient.services.Measure; |
| 29 | +import org.sonar.wsclient.services.Resource; |
| 30 | +import org.sonar.wsclient.services.ResourceQuery; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | + |
| 34 | +import static org.fest.assertions.Assertions.assertThat; |
| 35 | + |
| 36 | +public class MetricsTest { |
| 37 | + |
| 38 | + private static final String PROJECT_KEY = "metrics"; |
| 39 | + |
| 40 | + @ClassRule |
| 41 | + public static Orchestrator ORCHESTRATOR = Tests.ORCHESTRATOR; |
| 42 | + |
| 43 | + private static Sonar wsClient; |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void startServer() { |
| 47 | + SonarRunner build = SonarRunner.create() |
| 48 | + .setProjectDir(new File("projects/metrics")) |
| 49 | + .setProjectKey(PROJECT_KEY) |
| 50 | + .setProjectName(PROJECT_KEY) |
| 51 | + .setProjectVersion("1.0-SNAPSHOT") |
| 52 | + .setProfile("no_rule") |
| 53 | + .setSourceDirs("src"); |
| 54 | + ORCHESTRATOR.executeBuild(build); |
| 55 | + |
| 56 | + wsClient = ORCHESTRATOR.getServer().getWsClient(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void project_level() { |
| 61 | + // Size |
| 62 | + assertThat(getProjectMeasure("ncloc").getIntValue()).isEqualTo(1); |
| 63 | + assertThat(getProjectMeasure("lines").getIntValue()).isEqualTo(6); |
| 64 | + assertThat(getProjectMeasure("files").getIntValue()).isEqualTo(1); |
| 65 | + assertThat(getProjectMeasure("statements").getIntValue()).isEqualTo(1); |
| 66 | + assertThat(getProjectMeasure("directories").getIntValue()).isEqualTo(1); |
| 67 | + assertThat(getProjectMeasure("functions").getIntValue()).isEqualTo(0); |
| 68 | + assertThat(getProjectMeasure("classes").getIntValue()).isEqualTo(0); |
| 69 | + // Documentation |
| 70 | + assertThat(getProjectMeasure("comment_lines").getIntValue()).isEqualTo(1); |
| 71 | + assertThat(getProjectMeasure("comment_lines_density").getValue()).isEqualTo(50.0); |
| 72 | + // Complexity |
| 73 | + assertThat(getProjectMeasure("complexity").getValue()).isEqualTo(0.0); |
| 74 | + assertThat(getProjectMeasure("function_complexity")).isNull(); |
| 75 | + assertThat(getProjectMeasure("function_complexity_distribution").getData()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0;20=0;30=0"); |
| 76 | + assertThat(getProjectMeasure("file_complexity").getValue()).isEqualTo(0.0); |
| 77 | + assertThat(getProjectMeasure("file_complexity_distribution").getData()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0"); |
| 78 | + // Duplication |
| 79 | + assertThat(getProjectMeasure("duplicated_lines").getValue()).isEqualTo(0.0); |
| 80 | + assertThat(getProjectMeasure("duplicated_blocks").getValue()).isEqualTo(0.0); |
| 81 | + assertThat(getProjectMeasure("duplicated_files").getValue()).isEqualTo(0.0); |
| 82 | + assertThat(getProjectMeasure("duplicated_lines_density").getValue()).isEqualTo(0.0); |
| 83 | + // Rules |
| 84 | + assertThat(getProjectMeasure("violations").getValue()).isEqualTo(0.0); |
| 85 | + |
| 86 | + assertThat(getProjectMeasure("tests")).isNull(); |
| 87 | + assertThat(getProjectMeasure("coverage")).isNull(); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void directory_level() { |
| 92 | + // Size |
| 93 | + assertThat(getDirectoryMeasure("ncloc").getIntValue()).isEqualTo(1); |
| 94 | + assertThat(getDirectoryMeasure("lines").getIntValue()).isEqualTo(6); |
| 95 | + assertThat(getDirectoryMeasure("files").getIntValue()).isEqualTo(1); |
| 96 | + assertThat(getDirectoryMeasure("directories").getIntValue()).isEqualTo(1); |
| 97 | + assertThat(getDirectoryMeasure("statements").getIntValue()).isEqualTo(1); |
| 98 | + assertThat(getDirectoryMeasure("functions").getIntValue()).isEqualTo(0); |
| 99 | + assertThat(getDirectoryMeasure("classes").getIntValue()).isEqualTo(0); |
| 100 | + // Documentation |
| 101 | + assertThat(getDirectoryMeasure("comment_lines").getIntValue()).isEqualTo(1); |
| 102 | + assertThat(getDirectoryMeasure("comment_lines_density").getValue()).isEqualTo(50.0); |
| 103 | + // Complexity |
| 104 | + assertThat(getDirectoryMeasure("complexity").getValue()).isEqualTo(0.0); |
| 105 | + assertThat(getDirectoryMeasure("function_complexity")).isNull(); |
| 106 | + assertThat(getProjectMeasure("function_complexity_distribution").getData()).isEqualTo("1=0;2=0;4=0;6=0;8=0;10=0;12=0;20=0;30=0"); |
| 107 | + assertThat(getDirectoryMeasure("file_complexity").getValue()).isEqualTo(0.0); |
| 108 | + assertThat(getDirectoryMeasure("file_complexity_distribution").getData()).isEqualTo("0=1;5=0;10=0;20=0;30=0;60=0;90=0"); |
| 109 | + // Duplication |
| 110 | + assertThat(getDirectoryMeasure("duplicated_lines").getValue()).isEqualTo(0.0); |
| 111 | + assertThat(getDirectoryMeasure("duplicated_blocks").getValue()).isEqualTo(0.0); |
| 112 | + assertThat(getDirectoryMeasure("duplicated_files").getValue()).isEqualTo(0.0); |
| 113 | + assertThat(getDirectoryMeasure("duplicated_lines_density").getValue()).isEqualTo(0.0); |
| 114 | + // Rules |
| 115 | + assertThat(getDirectoryMeasure("violations").getValue()).isEqualTo(0.0); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void file_level() { |
| 120 | + // Size |
| 121 | + assertThat(getFileMeasure("ncloc").getIntValue()).isEqualTo(1); |
| 122 | + assertThat(getFileMeasure("lines").getIntValue()).isEqualTo(6); |
| 123 | + assertThat(getFileMeasure("files").getIntValue()).isEqualTo(1); |
| 124 | + assertThat(getFileMeasure("statements").getIntValue()).isEqualTo(1); |
| 125 | + assertThat(getFileMeasure("functions").getIntValue()).isEqualTo(0); |
| 126 | + assertThat(getFileMeasure("classes").getIntValue()).isEqualTo(0); |
| 127 | + // Documentation |
| 128 | + assertThat(getFileMeasure("comment_lines").getIntValue()).isEqualTo(1); |
| 129 | + assertThat(getFileMeasure("comment_lines_density").getValue()).isEqualTo(50.0); |
| 130 | + // Complexity |
| 131 | + assertThat(getFileMeasure("complexity").getValue()).isEqualTo(0.0); |
| 132 | + assertThat(getFileMeasure("function_complexity")).isNull(); |
| 133 | + assertThat(getFileMeasure("function_complexity_distribution")).isNull(); |
| 134 | + assertThat(getFileMeasure("file_complexity").getValue()).isEqualTo(0.0); |
| 135 | + assertThat(getFileMeasure("file_complexity_distribution")).isNull(); |
| 136 | + // Duplication |
| 137 | + assertThat(getFileMeasure("duplicated_lines")).isNull(); |
| 138 | + assertThat(getFileMeasure("duplicated_blocks")).isNull(); |
| 139 | + assertThat(getFileMeasure("duplicated_files")).isNull(); |
| 140 | + assertThat(getFileMeasure("duplicated_lines_density")).isNull(); |
| 141 | + // Rules |
| 142 | + assertThat(getFileMeasure("violations")).isNull(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * SONARPLUGINS-2184 |
| 147 | + */ |
| 148 | + @Test |
| 149 | + public void should_be_compatible_with_DevCockpit() { |
| 150 | + // TODO probably bug in Sonar: order might depend on JVM |
| 151 | + assertThat(getFileMeasure("ncloc_data").getData()) |
| 152 | + .contains("1=0") |
| 153 | + .contains("5=1"); |
| 154 | + assertThat(getFileMeasure("comment_lines_data").getData()) |
| 155 | + .contains("1=1") |
| 156 | + .contains("4=0"); |
| 157 | + } |
| 158 | + |
| 159 | + /* Helper methods */ |
| 160 | + |
| 161 | + private Measure getProjectMeasure(String metricKey) { |
| 162 | + Resource resource = wsClient.find(ResourceQuery.createForMetrics(PROJECT_KEY, metricKey)); |
| 163 | + return resource == null ? null : resource.getMeasure(metricKey); |
| 164 | + } |
| 165 | + |
| 166 | + private Measure getDirectoryMeasure(String metricKey) { |
| 167 | + Resource resource = wsClient.find(ResourceQuery.createForMetrics(keyFor("dir"), metricKey)); |
| 168 | + return resource == null ? null : resource.getMeasure(metricKey); |
| 169 | + } |
| 170 | + |
| 171 | + private Measure getFileMeasure(String metricKey) { |
| 172 | + Resource resource = wsClient.find(ResourceQuery.createForMetrics(keyFor("dir/HelloWorld.py"), metricKey)); |
| 173 | + return resource == null ? null : resource.getMeasure(metricKey); |
| 174 | + } |
| 175 | + |
| 176 | + private static String keyFor(String s) { |
| 177 | + return PROJECT_KEY + ":src/" + s; |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments