A Problem element from an Android Studio file.
        /// <summary>Generates a user-facing description for a problem, using the description supplied at
        /// construction time if it is present; otherwise, generates a description from the problem type.</summary>
        /// <param name="problem">The problem.</param>
        /// <returns>A user usable description message.</returns>
        public static string GetShortDescriptionForProblem(AndroidStudioProblem problem)
        {
            string desc = problem.Description;
            if (desc == null)
            {
                return String.Format(CultureInfo.InvariantCulture, ConverterResources.AndroidStudioDescriptionUnknown, problem.ProblemClass);
            }

            return desc;
        }
        public Result ConvertProblemToSarifResult(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.Message = new Message {
                    Text = description
                };
            }
            else
            {
                result.Message = new Message {
                    Text = GenerateFullMessage(description, problem.Hints)
                };
            }

            SetSarifResultPropertiesForProblem(result, problem);
            var location = new Location();

            location.FullyQualifiedLogicalName = CreateFullyQualifiedLogicalName(problem);

            Uri    uri;
            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.PhysicalLocation = new PhysicalLocation
                {
                    FileLocation = new FileLocation(),
                    Region       = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                };

                RemoveBadRoot(file, out uri);
                location.PhysicalLocation.FileLocation.Uri = uri;
            }

            result.Locations = new List <Location> {
                location
            };

            return(result);
        }
        public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescription()
        {
            var builder = new AndroidStudioProblem.Builder
            {
                ProblemClass = "Unused",
                File         = "Unused",
                Description  = "hungry EVIL zombies",
                Hints        = ImmutableArray.Create("comment", "delete")
            };

            var    uut    = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);

            Assert.Equal(@"hungry EVIL zombies
Possible resolution: comment
Possible resolution: delete", result.Message.Text);
        }
        /// <summary>Processes an Android Studio log and writes issues therein to an instance of
        /// <see cref="IResultLogWriter"/>.</summary>
        /// <param name="xmlReader">The XML reader from which AndroidStudio format shall be read.</param>
        /// <param name="output">The <see cref="IResultLogWriter"/> to write the output to.</param>
        private void ProcessAndroidStudioLog(XmlReader xmlReader, IResultLogWriter output)
        {
            int problemsDepth = xmlReader.Depth;

            xmlReader.ReadStartElement(_strings.Problems);

            while (xmlReader.Depth > problemsDepth)
            {
                var problem = AndroidStudioProblem.Parse(xmlReader, _strings);
                if (problem != null)
                {
                    output.WriteResult(AndroidStudioConverter.ConvertProblemToSarifIssue(problem));
                }
            }

            xmlReader.ReadEndElement(); // </problems>
        }
        private string CreateLogicalLocation(AndroidStudioProblem problem)
        {
            string parentLogicalLocationKey = null;

            parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.Module, LogicalLocationKind.Module);
            parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.Package, LogicalLocationKind.Package);

            if (problem.EntryPointName != null)
            {
                if ("class".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
                {
                    parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.EntryPointName, LogicalLocationKind.Type);
                }
                else if ("method".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
                {
                    parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.EntryPointName, LogicalLocationKind.Member);
                }
            }

            return(parentLogicalLocationKey);
        }
        private static Dictionary <string, string> GetSarifIssuePropertiesForProblem(AndroidStudioProblem problem)
        {
            var props = new Dictionary <string, string>();

            if (problem.Severity != null)
            {
                props.Add("severity", problem.Severity);
            }

            if (problem.AttributeKey != null)
            {
                props.Add("attributeKey", problem.AttributeKey);
            }

            if (props.Count == 0)
            {
                return(null);
            }

            return(props);
        }
        /// <summary>Processes an Android Studio log and writes issues therein to an instance of
        /// <see cref="IResultLogWriter"/>.</summary>
        /// <param name="xmlReader">The XML reader from which AndroidStudio format shall be read.</param>
        /// <returns>
        /// A list of the <see cref="Result"/> objects translated from the AndroidStudio format.
        /// </returns>
        private ISet <Result> ProcessAndroidStudioLog(XmlReader xmlReader)
        {
            var results = new HashSet <Result>(Result.ValueComparer);

            int problemsDepth = xmlReader.Depth;

            xmlReader.ReadStartElement(_strings.Problems);

            while (xmlReader.Depth > problemsDepth)
            {
                var problem = AndroidStudioProblem.Parse(xmlReader, _strings);
                if (problem != null)
                {
                    results.Add(ConvertProblemToSarifResult(problem));
                }
            }

            xmlReader.ReadEndElement(); // </problems>

            return(results);
        }
        private static string CreateSignature(AndroidStudioProblem problem)
        {
            string entryPointName = problem.EntryPointName;

            if ("file".Equals(problem.EntryPointType))
            {
                entryPointName = null;
            }

            string[] parts   = new string[] { problem.Module, problem.Package, entryPointName };
            var      updated = parts
                               .Where(part => !String.IsNullOrEmpty(part));

            string joinedParts = String.Join(@"\", updated);

            if (String.IsNullOrEmpty(joinedParts))
            {
                return(problem.Module);
            }
            else
            {
                return(joinedParts);
            }
        }
        private string CreateLogicalLocation(AndroidStudioProblem problem)
        {
            string parentLogicalLocationKey = null;

            parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.Module, LogicalLocationKind.Module);
            parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.Package, LogicalLocationKind.Package);

            if (problem.EntryPointName != null)
            {
                if ("class".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
                {
                    parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.EntryPointName, LogicalLocationKind.Type);
                }
                else if ("method".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
                {
                    parentLogicalLocationKey = TryAddLogicalLocation(parentLogicalLocationKey, problem.EntryPointName, LogicalLocationKind.Member);
                }
            }

            return parentLogicalLocationKey;
        }
        public Result ConvertProblemToSarifResult(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.Message = description;
            }
            else
            {
                result.Message = GenerateFullMessage(description, problem.Hints);
            }

            SetSarifResultPropertiesForProblem(result, problem);
            var location = new Location();

            location.FullyQualifiedLogicalName = CreateSignature(problem);

            string logicalLocationKey = CreateLogicalLocation(problem);

            if (logicalLocationKey != location.FullyQualifiedLogicalName)
            {
                location.LogicalLocationKey = logicalLocationKey;
            }

            Uri    uri;
            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.ResultFile = new PhysicalLocation
                {
                    Region = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                };

                if (RemoveBadRoot(file, out uri))
                {
                    location.ResultFile.UriBaseId = PROJECT_DIR;
                }
                location.ResultFile.Uri = uri;
            }

            if ("file".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                if (location.AnalysisTarget != null)
                {
                    location.ResultFile = location.AnalysisTarget;
                }

                location.AnalysisTarget = new PhysicalLocation();

                if (RemoveBadRoot(problem.EntryPointName, out uri))
                {
                    location.AnalysisTarget.UriBaseId = PROJECT_DIR;
                }
                location.AnalysisTarget.Uri = uri;
            }

            result.Locations = new List <Location> {
                location
            };

            return(result);
        }
        public static Result ConvertProblemToSarifIssue(AndroidStudioProblem problem)
        {
            var result = new Result();

            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);

            if (problem.Hints.IsEmpty)
            {
                result.FullMessage = description;
            }
            else
            {
                result.ShortMessage = description;
                result.FullMessage  = GenerateFullMessage(description, problem.Hints);
            }

            result.Properties = GetSarifIssuePropertiesForProblem(problem);
            var location = new Location();

            result.Locations = new[] { location };
            var logicalLocation = new List <LogicalLocationComponent>();

            if (!String.IsNullOrWhiteSpace(problem.Module))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.Module,
                    Kind = LogicalLocationKind.AndroidModule
                });
            }

            if (!String.IsNullOrWhiteSpace(problem.Package))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.Package,
                    Kind = LogicalLocationKind.JvmPackage
                });
            }

            if ("class".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.EntryPointName,
                    Kind = LogicalLocationKind.JvmType
                });
            }

            if ("method".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                logicalLocation.Add(new LogicalLocationComponent
                {
                    Name = problem.EntryPointName,
                    Kind = LogicalLocationKind.JvmFunction
                });
            }

            if (logicalLocation.Count != 0)
            {
                location.LogicalLocation           = logicalLocation;
                location.FullyQualifiedLogicalName = String.Join("\\", location.LogicalLocation.Select(x => x.Name));
            }

            string file = problem.File;

            if (!String.IsNullOrEmpty(file))
            {
                location.ResultFile = new[]
                {
                    new PhysicalLocationComponent
                    {
                        Uri      = RemoveBadRoot(file),
                        MimeType = MimeType.Java,
                        Region   = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                    }
                };
            }

            if ("file".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                if (location.AnalysisTarget != null)
                {
                    location.ResultFile = location.AnalysisTarget;
                }

                location.AnalysisTarget = new[]
                {
                    new PhysicalLocationComponent
                    {
                        Uri      = RemoveBadRoot(problem.EntryPointName),
                        MimeType = MimeType.Java
                    }
                };
            }

            return(result);
        }
        public void AndroidStudioConverter_ConvertToSarifResult_HintsAreStapledToDescription()
        {
            var builder = new AndroidStudioProblem.Builder
            {
                ProblemClass = "Unused",
                File = "Unused",
                Description = "hungry EVIL zombies",
                Hints = ImmutableArray.Create("comment", "delete")
            };

            var uut = new AndroidStudioProblem(builder);
            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
            Assert.AreEqual(@"hungry EVIL zombies
            Possible resolution: comment
            Possible resolution: delete", result.Message);
        }
 public void AndroidStudioConverter_ConvertToSarifResult_MultiplePropertiesArePersisted()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.AttributeKey = "key";
     builder.Severity = "warning";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(2);
     result.GetProperty("severity").Should().Be("warning");
     result.GetProperty("attributeKey").Should().Be("key");
 }
 public void AndroidStudioConverter_ConvertToSarifResult_SeverityIsPersistedInProperties()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.Severity = "warning";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("severity").Should().Be("warning");
 }
示例#15
0
 private static AndroidStudioProblem Parse(XmlReader reader)
 {
     return(AndroidStudioProblem.Parse(reader, new AndroidStudioStrings(reader.NameTable)));
 }
        private static void SetSarifResultPropertiesForProblem(Result result, AndroidStudioProblem problem)
        {
            if (problem.Severity != null)
            {
                result.SetProperty("severity", problem.Severity);
            }

            if (problem.AttributeKey != null)
            {
                result.SetProperty("attributeKey", problem.AttributeKey);
            }
        }
        private static string CreateSignature(AndroidStudioProblem problem)
        {
            string entryPointName = problem.EntryPointName;
            if ("file".Equals(problem.EntryPointType))
            {
                entryPointName = null;
            }

            string[] parts = new string[] { problem.Module, problem.Package, entryPointName };
            var updated = parts
                    .Where(part => !String.IsNullOrEmpty(part));

            string joinedParts = String.Join(@"\", updated);

            if (String.IsNullOrEmpty(joinedParts))
            {
                return problem.Module;
            }
            else
            {
                return joinedParts;
            }
        }
        public Result ConvertProblemToSarifResult(AndroidStudioProblem problem)
        {
            var result = new Result();
            result.RuleId = problem.ProblemClass;
            string description = AndroidStudioConverter.GetShortDescriptionForProblem(problem);
            if (problem.Hints.IsEmpty)
            {
                result.Message = description;
            }
            else
            {
                result.Message = GenerateFullMessage(description, problem.Hints);
            }

            SetSarifResultPropertiesForProblem(result, problem);
            var location = new Location();
            location.FullyQualifiedLogicalName = CreateSignature(problem);

            string logicalLocationKey = CreateLogicalLocation(problem);

            if (logicalLocationKey != location.FullyQualifiedLogicalName)
            {
                location.LogicalLocationKey = logicalLocationKey;
            }

            Uri uri;
            string file = problem.File;
            if (!String.IsNullOrEmpty(file))
            {
                location.ResultFile = new PhysicalLocation
                {
                    Region = problem.Line <= 0 ? null : Extensions.CreateRegion(problem.Line)
                };

                if (RemoveBadRoot(file, out uri))
                {
                    location.ResultFile.UriBaseId = PROJECT_DIR;
                }
                location.ResultFile.Uri = uri;
            }

            if ("file".Equals(problem.EntryPointType, StringComparison.OrdinalIgnoreCase))
            {
                if (location.AnalysisTarget != null)
                {
                    location.ResultFile = location.AnalysisTarget;
                }

                location.AnalysisTarget = new PhysicalLocation();

                if (RemoveBadRoot(problem.EntryPointName, out uri))
                {
                    location.AnalysisTarget.UriBaseId = PROJECT_DIR;
                }
                location.AnalysisTarget.Uri = uri;
            }

            result.Locations = new List<Location> { location };

            return result;
        }
        private static LocationInfo GetLocationInfoForBuilder(AndroidStudioProblem.Builder builder)
        {
            var converter = new AndroidStudioConverter();
            Result result = converter.ConvertProblemToSarifResult(new AndroidStudioProblem(builder));

            Location location = result.Locations.First();

            string logicalLocationKey = converter.LogicalLocationsDictionary.Keys.SingleOrDefault();
            LogicalLocation logicalLocation = logicalLocationKey != null
                ? converter.LogicalLocationsDictionary[logicalLocationKey]
                : null;

            return new LocationInfo
            {
                Location = location,
                LogicalLocation = logicalLocation
            };
        }
 public void AndroidStudioConverter_GetShortDescription_UsesDescriptionIfPresent()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.Description = "Cute fluffy kittens";
     var uut = new AndroidStudioProblem(builder);
     string result = AndroidStudioConverter.GetShortDescriptionForProblem(uut);
     Assert.AreEqual("Cute fluffy kittens", result);
 }
 private static void AssertThatProblemHasDefaultProblemProperties(AndroidStudioProblem uut)
 {
     Assert.AreEqual("file://$PROJECT_DIR$/file.java", uut.File);
     Assert.AreEqual(42, uut.Line);
     Assert.AreEqual("mod", uut.Module);
     Assert.AreEqual("pack", uut.Package);
     Assert.AreEqual("file", uut.EntryPointType);
     Assert.AreEqual("fqname", uut.EntryPointName);
     Assert.AreEqual("WARNING", uut.Severity);
     Assert.AreEqual("WARNING_ATTRIBUTES", uut.AttributeKey);
     Assert.AreEqual("Assertions", uut.ProblemClass);
     uut.Hints.Should().Equal(new[] { "some hint content" });
     Assert.AreEqual("Method is never used.", uut.Description);
 }
 public void AndroidStudioConverter_ConvertToSarifResult_AttributeKeyIsPersistedInProperties()
 {
     var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
     builder.AttributeKey = "key";
     var uut = new AndroidStudioProblem(builder);
     Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("attributeKey").Should().Be("key");
 }
示例#23
0
 private static ImmutableArray <string> ReadHints(XmlReader reader)
 {
     return(AndroidStudioProblem.ReadHints(reader, new AndroidStudioStrings(reader.NameTable)));
 }
        public void AndroidStudioConverter_ConvertToSarifResult_EmptyHintsDoNotAffectDescription()
        {
            var builder = AndroidStudioProblemTests.GetDefaultProblemBuilder();
            builder.Description = "hungry EVIL zombies";
            var uut = new AndroidStudioProblem(builder);

            Result result = new AndroidStudioConverter().ConvertProblemToSarifResult(uut);
            Assert.AreEqual("hungry EVIL zombies", result.Message);
        }