Commits

Mathias Kluba committed 143f38b

oops miss some files
---
.../dotnet/commons/project/ProjectReference.java | 100 ++++++++++
.../dotnet/coverage/CoverageResultParserTest.java | 207 ++++++++++++++++++++
.../dotnet/core/ProjectDependenciesSensor.java | 135 +++++++++++++
3 files changed, 442 insertions(+), 0 deletions(-)
create mode 100644 maven/dotnet-commons/src/main/java/org/apache/maven/dotnet/commons/project/ProjectReference.java
create mode 100644 sonar/sonar-plugin-coverage/src/test/java/org/sonar/plugin/dotnet/coverage/CoverageResultParserTest.java
create mode 100644 sonar/sonar-plugin-dotnet-core/src/main/java/org/sonar/plugin/dotnet/core/ProjectDependenciesSensor.java

Comments (0)

Files changed (3)

maven/dotnet-commons/src/main/java/org/apache/maven/dotnet/commons/project/ProjectReference.java

+/*
+ * Maven and Sonar plugin for .Net
+ * Copyright (C) 2010 Jose Chillan and Alexandre Victoor
+ * mailto: jose.chillan@codehaus.org or alexvictoor@codehaus.org
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.apache.maven.dotnet.commons.project;
+
+import java.util.UUID;
+
+public class ProjectReference {
+
+  private String path; 
+  private UUID guid; 
+  private String name;
+  
+
+  public String getPath() {
+	return path;
+}
+
+public void setPath(String path) {
+	this.path = path;
+}
+
+public UUID getGuid() {
+	return guid;
+}
+
+public void setGuid(UUID guid) {
+	this.guid = guid;
+}
+
+public String getName() {
+	return name;
+}
+
+public void setName(String name) {
+	this.name = name;
+}
+
+@Override
+  public String toString() {
+    return path+':'+guid;
+  }
+
+  @Override
+  public int hashCode() {
+    final int prime = 31;
+    int result = 1;
+    result = prime * result
+        + ((path == null) ? 0 : path.hashCode());
+    result = prime * result + ((name == null) ? 0 : name.hashCode());
+    result = prime * result + ((guid == null) ? 0 : guid.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    ProjectReference other = (ProjectReference) obj;
+    if (path == null) {
+      if (other.path != null)
+        return false;
+    } else if (!path.equals(other.path))
+      return false;
+    if (name == null) {
+      if (other.name != null)
+        return false;
+    } else if (!name.equals(other.name))
+      return false;
+    if (guid == null) {
+      if (other.guid != null)
+        return false;
+    } else if (!guid.equals(other.guid))
+      return false;
+    return true;
+  }
+  
+  
+}

sonar/sonar-plugin-coverage/src/test/java/org/sonar/plugin/dotnet/coverage/CoverageResultParserTest.java

+/*
+ * Maven and Sonar plugin for .Net
+ * Copyright (C) 2010 Jose Chillan and Alexandre Victoor
+ * mailto: jose.chillan@codehaus.org or alexvictoor@codehaus.org
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+package org.sonar.plugin.dotnet.coverage;
+
+import static org.junit.Assert.*;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.sonar.plugin.dotnet.coverage.CoverageResultParser;
+import org.sonar.plugin.dotnet.coverage.model.FileCoverage;
+import org.sonar.plugin.dotnet.coverage.model.ProjectCoverage;
+
+public class CoverageResultParserTest {
+
+	private CoverageResultParser parser;
+
+	@Before
+	public void setUp() {
+		parser = new CoverageResultParser();
+	}
+	
+	private File findFile(String fileName) {
+    return new File("target/test-classes", fileName);
+  }
+	
+	@Test
+	public void testParsePartCover22() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "coverage-report-2.2.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 4;
+		params.fileName = "Money.cs";
+		params.coveredLines = 46;
+		params.lines = 48;
+		
+		checkParsing(params);
+	}
+	
+	@Test
+  public void testParsePartCover22Empty() {
+    ParsingParameters params = new ParsingParameters();
+    params.report = "coverage-report-2.2-empty.xml";
+    params.assemblyName = "Example.Core";
+    params.fileNumber = 1;
+    params.fileName = "Money.cs";
+    params.coveredLines = 0;
+    params.lines = 4;
+    
+    checkParsing(params);
+  }
+	
+	@Test
+  public void testParsePartCover22Full() {
+    ParsingParameters params = new ParsingParameters();
+    params.report = "coverage-report-2.2-full.xml";
+    params.assemblyName = "Example.Core";
+    params.fileNumber = 1;
+    params.fileName = "Money.cs";
+    params.coveredLines = 4;
+    params.lines = 4;
+    
+    checkParsing(params);
+  }
+
+	@Test
+	public void testParsePartCover23() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "coverage-report-2.3.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 3;
+		params.fileName = "MoneyBag.cs";
+		params.coveredLines = 114;
+		params.lines = 125;
+		
+		checkParsing(params);
+	}
+	
+	@Test
+	public void testParsePartCover23NoVersionNumber() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "coverage-report-2.3.no.version.number.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 3;
+		params.fileName = "MoneyBag.cs";
+		params.coveredLines = 114;
+		params.lines = 125;
+		
+		checkParsing(params);
+	}
+	
+	
+	
+	@Test
+	public void testParsePartCover40NoVersionNumber() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "coverage-report-4.0.no.version.number.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 2;
+		params.fileName = "Money.cs";
+		params.coveredLines = 25;
+		params.lines = 27;
+		
+		checkParsing(params);
+	}
+	
+	@Test
+	public void testParsePartCover40() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "coverage-report-4.0.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 3;
+		params.fileName = "Money.cs";
+		params.coveredLines = 45;
+		params.lines = 215;
+		
+		checkParsing(params);
+		ProjectCoverage projectCoverage = parser.getProjects().get(0);
+		assertEquals(0.42, projectCoverage.getCoverage(),0.0001);
+	}
+	
+	@Test
+	public void testParseNCover3() {
+		ParsingParameters params = new ParsingParameters();
+		params.report = "Coverage.NCover3.xml";
+		params.assemblyName = "Example.Core";
+		params.fileNumber = 3;
+		params.fileName = "Money.cs";
+		params.coveredLines = 35;
+		params.lines = 37;
+		
+		checkParsing(params);
+	}
+	
+  @Test
+  public void testCanUseExclusionFilters() {
+    parser.setExclusionPatterns("**/*.xy", "**/*Generated*", "*.flat");
+    Object[][] tests = {
+            {false, "test.xy"},
+            {false, "a/test.xy"},
+            {false, "a\\test.xy"},
+            {false, "a/b/test.xy"},
+            {false, "a/b/Generated.Source.cs"},
+            {false, "a/b/designer.Generated.Source.cs"},
+            {true, "a/b/RegularClass.cs"},
+            {true, "a/b/RegularFile.flat"},
+            {false, "RegularFile.flat"},
+    };
+    for (Object[] test : tests) {
+      assertEquals(test[1].toString(), test[0], parser.isSourceFileIncluded(test[1].toString()));
+    }
+  }
+	
+	private void checkParsing(ParsingParameters parameters) {
+	  File file = findFile(parameters.report);
+		parser.parse(file);
+		List<FileCoverage> files = parser.getFiles();
+		List<ProjectCoverage> projects = parser.getProjects();
+		
+		assertEquals(1, projects.size());
+		assertEquals(parameters.fileNumber, files.size());
+		
+		
+		assertEquals(parameters.assemblyName, projects.get(0).getAssemblyName());
+		
+		FileCoverage firstFileCoverage = files.get(0);
+		
+		assertTrue(StringUtils.contains(firstFileCoverage.getFile().getName(), parameters.fileName));
+		
+		assertEquals(parameters.coveredLines, firstFileCoverage.getCoveredLines());
+		
+		assertEquals(parameters.lines, firstFileCoverage.getCountLines());
+	
+  }
+	
+	public static class ParsingParameters {
+	  public String report;
+	  public String assemblyName;
+	  public int fileNumber;
+	  public String fileName;
+	  public int coveredLines;
+	  public int lines;
+  }
+
+}

sonar/sonar-plugin-dotnet-core/src/main/java/org/sonar/plugin/dotnet/core/ProjectDependenciesSensor.java

+/**
+ * Maven and Sonar plugin for .Net
+ * Copyright (C) 2010 Jose Chillan and Alexandre Victoor
+ * mailto: jose.chillan@codehaus.org or alexvictoor@codehaus.org
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+/*
+ * Created on Sep 1, 2009
+ */
+package org.sonar.plugin.dotnet.core;
+
+import java.util.List;
+
+import org.apache.maven.dotnet.commons.project.BinaryReference;
+import org.apache.maven.dotnet.commons.project.DotNetProjectException;
+import org.apache.maven.dotnet.commons.project.ProjectReference;
+import org.apache.maven.dotnet.commons.project.VisualStudioProject;
+import org.apache.maven.dotnet.commons.project.VisualStudioSolution;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.batch.Sensor;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.batch.SonarIndex;
+import org.sonar.api.design.Dependency;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.PersistenceMode;
+import org.sonar.api.resources.Library;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.plugin.dotnet.core.project.VisualUtils;
+import org.sonar.plugin.dotnet.core.resource.CLRAssembly;
+
+public class ProjectDependenciesSensor implements Sensor {
+
+	private final static Logger log = LoggerFactory
+			.getLogger(BinaryDependenciesSensor.class);
+
+	private final SonarIndex index;
+
+	public ProjectDependenciesSensor(SonarIndex index) {
+		this.index = index;
+	}
+
+	@Override
+	public void analyse(Project project, SensorContext context) {
+
+		try {
+			VisualStudioSolution solution = VisualUtils.getSolution(project);
+			List<VisualStudioProject> projects = solution.getProjects();
+
+			// resolve dependencies for each projects of the solution
+			for (VisualStudioProject vsProject : projects) {
+
+				// find the referenced project in the Maven modules
+				Resource<?> subProject = CLRAssembly.fromName(project,
+						vsProject.getAssemblyName());
+				
+				Resource<?> savedSubPrj = context
+						.getResource(subProject);
+				if (savedSubPrj == null) {
+					context.saveResource(subProject);
+					savedSubPrj = context.getResource(subProject);
+				}
+				subProject = savedSubPrj;
+
+				// resolve project references
+				List<ProjectReference> projectReferences = vsProject
+						.getProjectReferences();
+				for (ProjectReference projectReference : projectReferences) {
+
+					// find the referenced project in the solution
+					VisualStudioProject vsReferencedProject = solution
+							.getProject(projectReference.getGuid());
+
+					Resource<?> referencedProject = CLRAssembly.fromName(
+							project, vsReferencedProject.getAssemblyName());
+
+					Resource<?> savedReferencedPrj = context
+							.getResource(referencedProject);
+					if (savedReferencedPrj == null) {
+						log.info("Saving resource"+savedReferencedPrj.toString());
+						context.saveResource(referencedProject);
+						savedReferencedPrj = context.getResource(referencedProject);
+					}
+					referencedProject = savedReferencedPrj;
+					
+					Dependency dependency = new Dependency(subProject, referencedProject);
+					dependency.setUsage("compile");
+					dependency.setWeight(1);
+					context.saveDependency(dependency);
+					log.info("Saving dependency"+dependency.toString());
+					
+/*
+					String json = "[{\"i\":"+referencedProject.getId()+
+						",\"n\":\""+referencedProject.getName()+
+						"\",\"q\":\""+referencedProject.getQualifier()+"\",\"v\":[{}]";
+					
+					Measure measure = new Measure(CoreMetrics.DEPENDENCY_MATRIX, json);
+				    measure.setPersistenceMode(PersistenceMode.DATABASE);
+				    context.saveMeasure(subProject, measure);
+				    log.info("Saving measure"+measure.toString());*/
+				    
+				    // TODO: do it also for PRJ > Folder/Package > File dependencies
+				}
+
+			}
+
+		} catch (DotNetProjectException e) {
+			log.error("Error during binary dependency analysis", e);
+		}
+	}
+
+	@Override
+	public boolean shouldExecuteOnProject(Project project) {
+		String packaging = project.getPackaging();
+		// We only accept the "sln" packaging
+		return "sln".equals(packaging);
+	}
+
+}