From b9395c6abef43d82f2936a17af85ce02cbebb834 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Tue, 16 Aug 2011 22:41:04 +0200 Subject: [PATCH] SONAR-2677 JUnit tests containing several level of nested inner classes can't be analysed by Sonar --- .../surefire/api/AbstractSurefireParser.java | 2 +- .../surefire/data/SurefireStaxHandler.java | 2 +- .../org/sonar/plugins/surefire/FooTest.java | 46 +++++++++++++ .../api/AbstractSurefireParserTest.java | 13 ++++ ...plugins.surefire.NestedTest$Inner1$Run.xml | 69 +++++++++++++++++++ ...plugins.surefire.NestedTest$Inner2$Run.xml | 67 ++++++++++++++++++ 6 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/FooTest.java create mode 100644 plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner1$Run.xml create mode 100644 plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner2$Run.xml diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java index 85e7a2682f8..f2c22cb15b9 100644 --- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java @@ -91,7 +91,7 @@ public abstract class AbstractSurefireParser { for (String classname : index.getClassnames()) { if (StringUtils.contains(classname, "$")) { // Surefire reports classes whereas sonar supports files - String parentClassName = StringUtils.substringBeforeLast(classname, "$"); + String parentClassName = StringUtils.substringBefore(classname, "$"); index.merge(classname, parentClassName); } } diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/data/SurefireStaxHandler.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/data/SurefireStaxHandler.java index b9a0b3b9e41..38ebc7b3571 100644 --- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/data/SurefireStaxHandler.java +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/data/SurefireStaxHandler.java @@ -125,7 +125,7 @@ public class SurefireStaxHandler implements XmlStreamHandler { String classname = testCaseCursor.getAttrValue("classname"); String name = testCaseCursor.getAttrValue("name"); if (StringUtils.contains(classname, "$")) { - return StringUtils.substringAfterLast(classname, "$") + "/" + name; + return StringUtils.substringAfter(classname, "$") + "/" + name; } return name; } diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/FooTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/FooTest.java new file mode 100644 index 00000000000..aae1f119a48 --- /dev/null +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/FooTest.java @@ -0,0 +1,46 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * 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.plugins.surefire; + +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; + +@RunWith(Enclosed.class) +public class FooTest { + + @RunWith(Enclosed.class) + public static class Inner1 { + + public static class Run { + @Test + public void test1() {} + } + } + + @RunWith(Enclosed.class) + public static class Inner2 { + + public static class Run { + @Test + public void test2() {} + } + } +} \ No newline at end of file diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java index e4d195ebe42..51c666a6843 100644 --- a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/AbstractSurefireParserTest.java @@ -111,6 +111,19 @@ public class AbstractSurefireParserTest { verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")), any(Metric.class), anyDouble()); } + @Test + public void shouldMergeNestedInnerClasses() throws URISyntaxException { + AbstractSurefireParser parser = newParser(); + + SensorContext context = mockContext(); + parser.collect(new Project("foo"), context, getDir("nestedInnerClasses")); + + verify(context).saveMeasure( + argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "org.sonar.plugins.surefire.NestedInnerTest")), + eq(CoreMetrics.TESTS), + eq(3.0)); + } + private AbstractSurefireParser newParser() { return new AbstractSurefireParser() { diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner1$Run.xml b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner1$Run.xml new file mode 100644 index 00000000000..b3357bd1f87 --- /dev/null +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner1$Run.xml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner2$Run.xml b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner2$Run.xml new file mode 100644 index 00000000000..677686a7aa3 --- /dev/null +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/AbstractSurefireParserTest/nestedInnerClasses/TEST-org.sonar.plugins.surefire.NestedTest$Inner2$Run.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.39.5