/* * Sonar C# Plugin :: StyleCop * Copyright (C) 2010 Jose Chillan, Alexandre Victoor and SonarSource * dev@sonar.codehaus.org * * 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 02 */ package org.sonar.plugins.csharp.stylecop; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Collection; import java.util.List; import org.apache.commons.configuration.BaseConfiguration; import org.apache.commons.configuration.Configuration; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.utils.SonarException; import org.sonar.dotnet.tools.commons.visualstudio.VisualStudioProject; import org.sonar.dotnet.tools.commons.visualstudio.VisualStudioSolution; import org.sonar.plugins.csharp.api.CSharpConfiguration; import org.sonar.plugins.csharp.api.MicrosoftWindowsEnvironment; import org.sonar.plugins.csharp.stylecop.profiles.StyleCopProfileExporter; import com.google.common.collect.Lists; public class StyleCopSensorTest { @Test public void testShouldExecuteOnProject() throws Exception { VisualStudioProject vsProject = mock(VisualStudioProject.class); when(vsProject.getName()).thenReturn("Project #1"); VisualStudioSolution solution = mock(VisualStudioSolution.class); when(solution.getProjects()).thenReturn(Lists.newArrayList(vsProject)); MicrosoftWindowsEnvironment microsoftWindowsEnvironment = new MicrosoftWindowsEnvironment(); microsoftWindowsEnvironment.setCurrentSolution(solution); Configuration conf = new BaseConfiguration(); StyleCopSensor sensor = new StyleCopSensor(null, null, null, null, new CSharpConfiguration(conf), microsoftWindowsEnvironment); Project project = mock(Project.class); when(project.getLanguageKey()).thenReturn("cs"); when(project.getName()).thenReturn("Project #1"); assertTrue(sensor.shouldExecuteOnProject(project)); conf.addProperty(StyleCopConstants.MODE, StyleCopConstants.MODE_SKIP); sensor = new StyleCopSensor(null, null, null, null, new CSharpConfiguration(conf), microsoftWindowsEnvironment); assertFalse(sensor.shouldExecuteOnProject(project)); } @Test public void testGenerateConfigurationFile() throws Exception { File sonarDir = new File("target/sonar"); sonarDir.mkdirs(); ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); when(fileSystem.getSonarWorkingDirectory()).thenReturn(sonarDir); StyleCopProfileExporter profileExporter = mock(StyleCopProfileExporter.class); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws IOException { FileWriter writer = (FileWriter) invocation.getArguments()[1]; writer.write("Hello"); return null; } }).when(profileExporter).exportProfile((RulesProfile) anyObject(), (FileWriter) anyObject()); StyleCopSensor sensor = new StyleCopSensor(fileSystem, null, profileExporter, null, new CSharpConfiguration(new BaseConfiguration()), null); sensor.generateConfigurationFile(); File report = new File(sonarDir, StyleCopConstants.STYLECOP_RULES_FILE); assertTrue(report.exists()); report.delete(); } @Test(expected = SonarException.class) public void testGenerateConfigurationFileWithUnexistingFolder() throws Exception { File sonarDir = new File("target/sonar/NON-EXISTING-FOLDER"); ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); when(fileSystem.getSonarWorkingDirectory()).thenReturn(sonarDir); StyleCopProfileExporter profileExporter = mock(StyleCopProfileExporter.class); doAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws IOException { FileWriter writer = (FileWriter) invocation.getArguments()[1]; writer.write("Hello"); return null; } }).when(profileExporter).exportProfile((RulesProfile) anyObject(), (FileWriter) anyObject()); StyleCopSensor sensor = new StyleCopSensor(fileSystem, null, profileExporter, null, new CSharpConfiguration(new BaseConfiguration()), null); sensor.generateConfigurationFile(); } }