private static SonarWebService GetSonarWebService(AnalysisConfig config)
        {
            FilePropertiesProvider sonarRunnerProperties = new FilePropertiesProvider(config.SonarRunnerPropertiesPath);

            string server   = sonarRunnerProperties.GetProperty(SonarProperties.HostUrl, DefaultSonarServerUrl);
            string username = sonarRunnerProperties.GetProperty(SonarProperties.SonarUserName, null);
            string password = sonarRunnerProperties.GetProperty(SonarProperties.SonarPassword, null);

            return(new SonarWebService(new WebClientDownloader(new WebClient(), username, password), server, "cs", "fxcop"));
        }
Beispiel #2
0
        private static void AssertExpectedValueReturned(FilePropertiesProvider provider, string propertyName, string expectedValue)
        {
            // Both "GetProperty" methods should return the same expected value
            string actualValue = provider.GetProperty(propertyName);

            Assert.AreEqual(expectedValue, actualValue, "Provider did not return the expected value for property '{0}'", propertyName);

            actualValue = provider.GetProperty(propertyName, Guid.NewGuid().ToString() /* supply a unique default - not expecting it to be returned*/);
            Assert.AreEqual(expectedValue, actualValue, "Provider did not return the expected value for property '{0}'", propertyName);
        }
Beispiel #3
0
        public void FilePropertiesProvider_EmptyFile()
        {
            // Arrange
            // File exists but has no contents
            string fullName = CreatePropertiesFile("EmptyFile.properties", string.Empty);

            // Act
            FilePropertiesProvider provider = new FilePropertiesProvider(fullName);

            // Assert
            AssertDefaultValueReturned(provider, "property1", "default1");
            AssertDefaultValueReturned(provider, "property1", null);
            AssertDefaultValueReturned(provider, "property2", "foo");
            AssertDefaultValueReturned(provider, "aaa", "123");
        }
        /// <summary>
        /// Gets the URL from the sonar-runner.properties file. Throws if the
        /// properties file cannot be located.
        /// </summary>
        private static string GetUrlFromPropertiesFile()
        {
            var sonarRunnerProperties = FileLocator.FindDefaultSonarRunnerProperties();

            if (sonarRunnerProperties == null)
            {
                throw new ArgumentException(Resources.ERROR_CouldNotFindSonarRunnerProperties);
            }

            var propertiesProvider = new FilePropertiesProvider(sonarRunnerProperties);
            var server             = propertiesProvider.GetProperty(SonarProperties.HostUrl);

            Debug.Assert(!string.IsNullOrWhiteSpace(server), "Not expecting the host url property in the sonar-runner.properties file to be null/empty");

            return(server);
        }
Beispiel #5
0
        public void FilePropertiesProvider_SonarRunnerProperties()
        {
            // Arrange
            string contents = @"
#Configure here general information about the environment, such as SonarQube DB details for example
#No information about specific project should appear here

#----- Default SonarQube server
sonar.host.url=http://tfsforsonarint.cloudapp.net:9000/

#----- PostgreSQL
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

#----- MySQL
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&amp;characterEncoding=utf8

#----- Oracle
#sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE

#----- Microsoft SQLServer
sonar.jdbc.url=jdbc:jtds:sqlserver://SonarForTfsInt:49590/sonar;instance=SQLEXPRESS;SelectMethod=Cursor

#----- Global database settings
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

#----- Default source code encoding
sonar.sourceEncoding=UTF-8

#----- Security (when 'sonar.forceAuthentication' is set to 'true')
sonar.login=admin
sonar.password=adminpwd";

            string fullName = CreatePropertiesFile("SonarRunner.properties", contents);

            // Act
            FilePropertiesProvider provider = new FilePropertiesProvider(fullName);

            // Assert
            AssertExpectedValueReturned(provider, SonarProperties.HostUrl, "http://tfsforsonarint.cloudapp.net:9000/");
            AssertExpectedValueReturned(provider, SonarProperties.DbConnectionString, "jdbc:jtds:sqlserver://SonarForTfsInt:49590/sonar;instance=SQLEXPRESS;SelectMethod=Cursor");
            AssertExpectedValueReturned(provider, SonarProperties.DbUserName, "sonar");
            AssertExpectedValueReturned(provider, SonarProperties.DbPassword, "sonar");
            AssertExpectedValueReturned(provider, SonarProperties.SourceEncoding, "UTF-8");
            AssertExpectedValueReturned(provider, SonarProperties.SonarUserName, "admin");
            AssertExpectedValueReturned(provider, SonarProperties.SonarPassword, "adminpwd");
        }
Beispiel #6
0
        public void FilePropertiesProvider_GetProperty_Throws()
        {
            // Arrange
            string contents = @"
a.b.c.=exists
";
            string fullName = CreatePropertiesFile("GetProperty_Throws.properties", contents);

            // Act
            FilePropertiesProvider provider = new FilePropertiesProvider(fullName);

            // Assert
            AssertExpectedValueReturned(provider, "a.b.c.", "exists");
            Exception ex = AssertException.Expects <ArgumentException>(() => provider.GetProperty("missing.property"));

            Assert.IsTrue(ex.Message.Contains(fullName), "Expecting the error message to contain the file name");
            Assert.IsTrue(ex.Message.Contains("missing.property"), "Expecting the error message to contain the name of the requested property");
        }
        private static void UpdateTeamBuildSummary(AnalysisConfig config, ProjectInfoAnalysisResult result, ILogger logger)
        {
            logger.LogMessage(Resources.Report_UpdatingTeamBuildSummary);

            int skippedProjectCount = GetProjectsByStatus(result, ProjectInfoValidity.NoFilesToAnalyze).Count();
            int invalidProjectCount = GetProjectsByStatus(result, ProjectInfoValidity.InvalidGuid).Count();

            invalidProjectCount += GetProjectsByStatus(result, ProjectInfoValidity.DuplicateGuid).Count();

            int excludedProjectCount = GetProjectsByStatus(result, ProjectInfoValidity.ExcludeFlagSet).Count();

            IEnumerable <ProjectInfo> validProjects = GetProjectsByStatus(result, ProjectInfoValidity.Valid);
            int productProjectCount = validProjects.Count(p => p.ProjectType == ProjectType.Product);
            int testProjectCount    = validProjects.Count(p => p.ProjectType == ProjectType.Test);

            using (BuildSummaryLogger summaryLogger = new BuildSummaryLogger(config.GetTfsUri(), config.GetBuildUri()))
            {
                string projectDescription = string.Format(System.Globalization.CultureInfo.CurrentCulture,
                                                          Resources.Report_SonarQubeProjectDescription, config.SonarProjectName, config.SonarProjectKey, config.SonarProjectVersion);

                // Add a link to SonarQube dashboard if analysis succeeded
                Debug.Assert(config.SonarRunnerPropertiesPath != null, "Not expecting the sonar-runner properties path to be null");
                if (config.SonarRunnerPropertiesPath != null && result.RanToCompletion)
                {
                    ISonarPropertyProvider propertyProvider = new FilePropertiesProvider(config.SonarRunnerPropertiesPath);
                    string hostUrl = propertyProvider.GetProperty(SonarProperties.HostUrl).TrimEnd('/');

                    string sonarUrl = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                    "{0}/dashboard/index/{1}", hostUrl, config.SonarProjectKey);

                    summaryLogger.WriteMessage(Resources.Report_AnalysisSucceeded, projectDescription, sonarUrl);
                }

                if (!result.RanToCompletion)
                {
                    summaryLogger.WriteMessage(Resources.Report_AnalysisFailed, projectDescription);
                }

                summaryLogger.WriteMessage(Resources.Report_ProductAndTestMessage, productProjectCount, testProjectCount);
                summaryLogger.WriteMessage(Resources.Report_InvalidSkippedAndExcludedMessage, invalidProjectCount, skippedProjectCount, excludedProjectCount);
            }
        }
Beispiel #8
0
        public void FilePropertiesProvider_SonarProjectProperties()
        {
            // Arrange
            string contents = @"
# Note: It is not recommended to use the colon ':' character in the projectKey 
sonar.projectKey=org.example.csharpplayground
sonar.projectName=C# playground
sonar.projectVersion=1.0
 
 
sonar.sourceEncoding=UTF-8
 
 
# Disable the Visual Studio bootstrapper 
sonar.visualstudio.enable=false
sonar.sources=
sonar.exclusions=obj/**
sonar.modules=CalcAddTest,CalcMultiplyTest,CalcDivideTest,CalcSubtractTest,MyLibrary
 
# Code Coverage 
sonar.cs.ncover3.reportsPaths=coverage.nccov
sonar.cs.opencover.reportsPaths=results.xml
sonar.cs.dotcover.reportsPaths=dotCover.html
sonar.cs.vscoveragexml.reportsPaths=VisualStudio.coveragexml
 
# Unit Test Results 
sonar.cs.vstest.reportsPaths=TestResults/*.trx
  
# Required only when using SonarQube < 4.2
sonar.language=cs
";
            string fullName = CreatePropertiesFile("WellKnownProperties.properties", contents);

            // Act
            FilePropertiesProvider provider = new FilePropertiesProvider(fullName);

            // Assert
            AssertExpectedValueReturned(provider, SonarProperties.ProjectKey, "org.example.csharpplayground");
            AssertExpectedValueReturned(provider, SonarProperties.ProjectName, "C# playground");
            AssertExpectedValueReturned(provider, SonarProperties.ProjectVersion, "1.0");
            AssertExpectedValueReturned(provider, SonarProperties.SourceEncoding, "UTF-8");
        }
Beispiel #9
0
        public void FilePropertiesProvider_ValidProperties()
        {
            // Arrange
            string contents = @"
# commented lines should be ignored
property1=abc
#property1=should not be returned

PROPERTY1=abcUpperCase

my.property.key1=key1 Value
my.property.key2=key2 Value
my.property.key2=key2 Value Duplicate

123.456=abc.def

empty.property=

invalid name=123
invalidname2 =abc
";
            string fullName = CreatePropertiesFile("ValidProperties.properties", contents);

            // Act
            FilePropertiesProvider provider = new FilePropertiesProvider(fullName);

            // Assert
            AssertExpectedValueReturned(provider, "property1", null, "abc");                         // commented-out value should be ignored
            AssertExpectedValueReturned(provider, "PROPERTY1", "123", "abcUpperCase");               // check case-sensitivity
            AssertExpectedValueReturned(provider, "my.property.key1", "xxx", "key1 Value");          // name can contain ".", value can contain spaces
            AssertExpectedValueReturned(provider, "my.property.key2", null, "key2 Value Duplicate"); // the last-defined value for a property name is used
            AssertExpectedValueReturned(provider, "123.456", "default", "abc.def");                  // property name is numeric
            AssertExpectedValueReturned(provider, "empty.property", "YYY", string.Empty);            // the property value is emtpy

            AssertDefaultValueReturned(provider, "invalid name", "default1");                        // invalid names should not be parsed (name contains a space)
            AssertDefaultValueReturned(provider, "invalidname2 ", "default2");                       // invalid names should not be parsed (space before the =)
        }
Beispiel #10
0
        private static void AssertExpectedValueReturned(FilePropertiesProvider provider, string propertyName, string suppliedDefault, string expectedValue)
        {
            string actualValue = provider.GetProperty(propertyName, suppliedDefault);

            Assert.AreEqual(expectedValue, actualValue, "Provider did not return the expected value for property '{0}'", propertyName);
        }