SonarQube Arguments value












0















Currently I'm crating rule which should check if method contains @Test and @TestInfo annotation. If it does @TestInfo should have not empty argument testCaseId.



There are several different possible ways to fill testCaseId. Examples:



@Test
@TestInfo(testCaseId = { "ABC-123", "DEF-154" }, component = "Component")
public int foo() {
return 0;
}


@Test
@TestInfo(testCaseId = ("ABC-123"), component = "Component")
public int foo() {
return 0;
}


@Test
@TestInfo(testCaseId = "ABC-123", component = "Component")
public int foo() {
return 0;
}


I already have covered cases when @TestInfo does not exist, there is no testCaseId argument or testCaseId is equal to "".



But I find it difficult to find how to get whole value of testCaseId.



This is how I find arguments:



Arguments arguments = annotationTree.arguments();
if (!arguments.isEmpty()) {
for (int i = 0; i < arguments.size(); i++) {
checkArgument(arguments.get(i));
}
}
if (!annotationsContainsTestCaseId) {
reportIssue(arguments, "Method annotated with @TestInfo should have not empty testCaseId parameter");
}

private void checkArgument(ExpressionTree argument) {

String parameter = argument.firstToken().text();
String parameterValue = argument.lastToken().text();

if (isParameterTestCaseId(parameter)) {
annotationsContainsTestCaseId = true;
if (isTestCaseIdEmpty(parameterValue)) {
reportIssue(argument, "Method annotated with @TestInfo should have not empty testCaseId parameter");
}
}
}

private boolean isParameterTestCaseId(String parameter) {
return parameter.matches("testCaseId");
}

private boolean isTestCaseIdEmpty(String parameterValue) {
// if "" is empty, length is 2
return parameterValue.length() == 2;
}


But when testCaseId is in () of {}, lastToken.text() only return ) or }.



Is there a way to get value of argument within () or {}?










share|improve this question



























    0















    Currently I'm crating rule which should check if method contains @Test and @TestInfo annotation. If it does @TestInfo should have not empty argument testCaseId.



    There are several different possible ways to fill testCaseId. Examples:



    @Test
    @TestInfo(testCaseId = { "ABC-123", "DEF-154" }, component = "Component")
    public int foo() {
    return 0;
    }


    @Test
    @TestInfo(testCaseId = ("ABC-123"), component = "Component")
    public int foo() {
    return 0;
    }


    @Test
    @TestInfo(testCaseId = "ABC-123", component = "Component")
    public int foo() {
    return 0;
    }


    I already have covered cases when @TestInfo does not exist, there is no testCaseId argument or testCaseId is equal to "".



    But I find it difficult to find how to get whole value of testCaseId.



    This is how I find arguments:



    Arguments arguments = annotationTree.arguments();
    if (!arguments.isEmpty()) {
    for (int i = 0; i < arguments.size(); i++) {
    checkArgument(arguments.get(i));
    }
    }
    if (!annotationsContainsTestCaseId) {
    reportIssue(arguments, "Method annotated with @TestInfo should have not empty testCaseId parameter");
    }

    private void checkArgument(ExpressionTree argument) {

    String parameter = argument.firstToken().text();
    String parameterValue = argument.lastToken().text();

    if (isParameterTestCaseId(parameter)) {
    annotationsContainsTestCaseId = true;
    if (isTestCaseIdEmpty(parameterValue)) {
    reportIssue(argument, "Method annotated with @TestInfo should have not empty testCaseId parameter");
    }
    }
    }

    private boolean isParameterTestCaseId(String parameter) {
    return parameter.matches("testCaseId");
    }

    private boolean isTestCaseIdEmpty(String parameterValue) {
    // if "" is empty, length is 2
    return parameterValue.length() == 2;
    }


    But when testCaseId is in () of {}, lastToken.text() only return ) or }.



    Is there a way to get value of argument within () or {}?










    share|improve this question

























      0












      0








      0








      Currently I'm crating rule which should check if method contains @Test and @TestInfo annotation. If it does @TestInfo should have not empty argument testCaseId.



      There are several different possible ways to fill testCaseId. Examples:



      @Test
      @TestInfo(testCaseId = { "ABC-123", "DEF-154" }, component = "Component")
      public int foo() {
      return 0;
      }


      @Test
      @TestInfo(testCaseId = ("ABC-123"), component = "Component")
      public int foo() {
      return 0;
      }


      @Test
      @TestInfo(testCaseId = "ABC-123", component = "Component")
      public int foo() {
      return 0;
      }


      I already have covered cases when @TestInfo does not exist, there is no testCaseId argument or testCaseId is equal to "".



      But I find it difficult to find how to get whole value of testCaseId.



      This is how I find arguments:



      Arguments arguments = annotationTree.arguments();
      if (!arguments.isEmpty()) {
      for (int i = 0; i < arguments.size(); i++) {
      checkArgument(arguments.get(i));
      }
      }
      if (!annotationsContainsTestCaseId) {
      reportIssue(arguments, "Method annotated with @TestInfo should have not empty testCaseId parameter");
      }

      private void checkArgument(ExpressionTree argument) {

      String parameter = argument.firstToken().text();
      String parameterValue = argument.lastToken().text();

      if (isParameterTestCaseId(parameter)) {
      annotationsContainsTestCaseId = true;
      if (isTestCaseIdEmpty(parameterValue)) {
      reportIssue(argument, "Method annotated with @TestInfo should have not empty testCaseId parameter");
      }
      }
      }

      private boolean isParameterTestCaseId(String parameter) {
      return parameter.matches("testCaseId");
      }

      private boolean isTestCaseIdEmpty(String parameterValue) {
      // if "" is empty, length is 2
      return parameterValue.length() == 2;
      }


      But when testCaseId is in () of {}, lastToken.text() only return ) or }.



      Is there a way to get value of argument within () or {}?










      share|improve this question














      Currently I'm crating rule which should check if method contains @Test and @TestInfo annotation. If it does @TestInfo should have not empty argument testCaseId.



      There are several different possible ways to fill testCaseId. Examples:



      @Test
      @TestInfo(testCaseId = { "ABC-123", "DEF-154" }, component = "Component")
      public int foo() {
      return 0;
      }


      @Test
      @TestInfo(testCaseId = ("ABC-123"), component = "Component")
      public int foo() {
      return 0;
      }


      @Test
      @TestInfo(testCaseId = "ABC-123", component = "Component")
      public int foo() {
      return 0;
      }


      I already have covered cases when @TestInfo does not exist, there is no testCaseId argument or testCaseId is equal to "".



      But I find it difficult to find how to get whole value of testCaseId.



      This is how I find arguments:



      Arguments arguments = annotationTree.arguments();
      if (!arguments.isEmpty()) {
      for (int i = 0; i < arguments.size(); i++) {
      checkArgument(arguments.get(i));
      }
      }
      if (!annotationsContainsTestCaseId) {
      reportIssue(arguments, "Method annotated with @TestInfo should have not empty testCaseId parameter");
      }

      private void checkArgument(ExpressionTree argument) {

      String parameter = argument.firstToken().text();
      String parameterValue = argument.lastToken().text();

      if (isParameterTestCaseId(parameter)) {
      annotationsContainsTestCaseId = true;
      if (isTestCaseIdEmpty(parameterValue)) {
      reportIssue(argument, "Method annotated with @TestInfo should have not empty testCaseId parameter");
      }
      }
      }

      private boolean isParameterTestCaseId(String parameter) {
      return parameter.matches("testCaseId");
      }

      private boolean isTestCaseIdEmpty(String parameterValue) {
      // if "" is empty, length is 2
      return parameterValue.length() == 2;
      }


      But when testCaseId is in () of {}, lastToken.text() only return ) or }.



      Is there a way to get value of argument within () or {}?







      java annotations sonarqube arguments






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 21 hours ago









      Tomas GriušysTomas Griušys

      4010




      4010
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I figured it out. I switched to using FileScannerContext.
          First i check if method is annotated with @Test and @TestInfo and then get in which line @TestInfo is located. Later it is just finding what I need in String line.



          This is my final working solution:



          private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
          private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
          private static final String REPORT_EMPTY_TEST_CASE_ID = "Method annotated with @TestInfo should have not empty testCaseId parameter";
          private static final String REPORT_MISSING_TEST_INFO = "Method annotated with @Test should also be annotated with @TestInfo";
          private static final String TEST_CASE_ID_SPLIT_PLACE = "testCaseId=";

          private int methodStartLine = 0;
          private int methodEndLine = 0;
          private int annotationLine;
          private String annotationLineText;

          @Override
          public List<Tree.Kind> nodesToVisit() {
          return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.ANNOTATION);
          }

          @Override
          public void visitNode(Tree tree) {
          if (tree.is(Tree.Kind.METHOD)) {

          MethodTree methodTree = (MethodTree) tree;
          scanMethodTree(methodTree);

          } else if (tree.is(Tree.Kind.ANNOTATION)) {

          AnnotationTree annotationTree = (AnnotationTree) tree;
          scanAnnotationTree(annotationTree);

          }

          }

          private void scanMethodTree(MethodTree methodTree) {
          if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
          if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {

          methodStartLine = methodTree.firstToken().line();
          methodEndLine = methodTree.lastToken().line();

          } else {
          reportIssue(methodTree.simpleName(), REPORT_MISSING_TEST_INFO);
          }
          }
          }

          private void scanAnnotationTree(AnnotationTree annotationTree) {
          if (annotationTree.symbolType().fullyQualifiedName().equals(TEST_INFO_ANNOTATION_PATH)) {

          annotationLine = annotationTree.firstToken().line();
          if (annotationLine >= methodStartLine && annotationLine < methodEndLine) {

          annotationLineText = context.getFileLines().get(annotationLine - 1).replaceAll("\s", "");

          if (annotationLineText.contains("testCaseId")) {
          // {"ABC-123","DEF-456"} OR {("ABC-123"), ("DEF-456")}
          if ((annotationLineText.contains("{("") || annotationLineText.contains("{""))
          && (annotationLineText.contains("")}") || annotationLineText.contains(""}"))) {
          reportCase1();
          }
          // ("ABC-123")
          else if (annotationLineText.contains("("") && annotationLineText.contains("")")) {
          reportCase2();
          }
          // "ABC-123"
          else if (annotationLineText.contains(""") && annotationLineText.contains("",")) {
          reportCase3();
          }

          } else {
          addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
          }
          }
          }
          }

          /**
          * Method report an issue if testCaseId = {"ABC-123","DEF-456",...,"AEF-159"} or
          * testCaseId = {("ABC-123"),("DEF-456"),...,("AEF-159")} has no values between
          * quotes.
          */
          private void reportCase1() {
          String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("}")[0];
          testCaseId = testCaseId.replaceAll(""", "").replaceAll("\{", "").replaceAll(",", "").replaceAll("\(", "")
          .replaceAll("\)", "");

          if (testCaseId.length() == 0) {
          addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
          }
          }

          /**
          * Method report an issue if testCaseId = ("ABC-123") has no value between
          * quotes.
          */
          private void reportCase2() {
          String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split(""\)")[0];
          testCaseId = testCaseId.replaceAll(""", "").replaceAll("\(", "").replaceAll(",", "");

          if (testCaseId.length() == 0) {
          addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
          }
          }

          /**
          * Method report an issue if testCaseId = "ABC-123" has no value between quotes.
          */
          private void reportCase3() {
          String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("",")[0];
          testCaseId = testCaseId.replaceAll(""", "").replaceAll(",", "");

          if (testCaseId.length() == 0) {
          addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
          }
          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250794%2fsonarqube-arguments-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I figured it out. I switched to using FileScannerContext.
            First i check if method is annotated with @Test and @TestInfo and then get in which line @TestInfo is located. Later it is just finding what I need in String line.



            This is my final working solution:



            private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
            private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
            private static final String REPORT_EMPTY_TEST_CASE_ID = "Method annotated with @TestInfo should have not empty testCaseId parameter";
            private static final String REPORT_MISSING_TEST_INFO = "Method annotated with @Test should also be annotated with @TestInfo";
            private static final String TEST_CASE_ID_SPLIT_PLACE = "testCaseId=";

            private int methodStartLine = 0;
            private int methodEndLine = 0;
            private int annotationLine;
            private String annotationLineText;

            @Override
            public List<Tree.Kind> nodesToVisit() {
            return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.ANNOTATION);
            }

            @Override
            public void visitNode(Tree tree) {
            if (tree.is(Tree.Kind.METHOD)) {

            MethodTree methodTree = (MethodTree) tree;
            scanMethodTree(methodTree);

            } else if (tree.is(Tree.Kind.ANNOTATION)) {

            AnnotationTree annotationTree = (AnnotationTree) tree;
            scanAnnotationTree(annotationTree);

            }

            }

            private void scanMethodTree(MethodTree methodTree) {
            if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
            if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {

            methodStartLine = methodTree.firstToken().line();
            methodEndLine = methodTree.lastToken().line();

            } else {
            reportIssue(methodTree.simpleName(), REPORT_MISSING_TEST_INFO);
            }
            }
            }

            private void scanAnnotationTree(AnnotationTree annotationTree) {
            if (annotationTree.symbolType().fullyQualifiedName().equals(TEST_INFO_ANNOTATION_PATH)) {

            annotationLine = annotationTree.firstToken().line();
            if (annotationLine >= methodStartLine && annotationLine < methodEndLine) {

            annotationLineText = context.getFileLines().get(annotationLine - 1).replaceAll("\s", "");

            if (annotationLineText.contains("testCaseId")) {
            // {"ABC-123","DEF-456"} OR {("ABC-123"), ("DEF-456")}
            if ((annotationLineText.contains("{("") || annotationLineText.contains("{""))
            && (annotationLineText.contains("")}") || annotationLineText.contains(""}"))) {
            reportCase1();
            }
            // ("ABC-123")
            else if (annotationLineText.contains("("") && annotationLineText.contains("")")) {
            reportCase2();
            }
            // "ABC-123"
            else if (annotationLineText.contains(""") && annotationLineText.contains("",")) {
            reportCase3();
            }

            } else {
            addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
            }
            }
            }
            }

            /**
            * Method report an issue if testCaseId = {"ABC-123","DEF-456",...,"AEF-159"} or
            * testCaseId = {("ABC-123"),("DEF-456"),...,("AEF-159")} has no values between
            * quotes.
            */
            private void reportCase1() {
            String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("}")[0];
            testCaseId = testCaseId.replaceAll(""", "").replaceAll("\{", "").replaceAll(",", "").replaceAll("\(", "")
            .replaceAll("\)", "");

            if (testCaseId.length() == 0) {
            addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
            }
            }

            /**
            * Method report an issue if testCaseId = ("ABC-123") has no value between
            * quotes.
            */
            private void reportCase2() {
            String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split(""\)")[0];
            testCaseId = testCaseId.replaceAll(""", "").replaceAll("\(", "").replaceAll(",", "");

            if (testCaseId.length() == 0) {
            addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
            }
            }

            /**
            * Method report an issue if testCaseId = "ABC-123" has no value between quotes.
            */
            private void reportCase3() {
            String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("",")[0];
            testCaseId = testCaseId.replaceAll(""", "").replaceAll(",", "");

            if (testCaseId.length() == 0) {
            addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
            }
            }





            share|improve this answer




























              0














              I figured it out. I switched to using FileScannerContext.
              First i check if method is annotated with @Test and @TestInfo and then get in which line @TestInfo is located. Later it is just finding what I need in String line.



              This is my final working solution:



              private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
              private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
              private static final String REPORT_EMPTY_TEST_CASE_ID = "Method annotated with @TestInfo should have not empty testCaseId parameter";
              private static final String REPORT_MISSING_TEST_INFO = "Method annotated with @Test should also be annotated with @TestInfo";
              private static final String TEST_CASE_ID_SPLIT_PLACE = "testCaseId=";

              private int methodStartLine = 0;
              private int methodEndLine = 0;
              private int annotationLine;
              private String annotationLineText;

              @Override
              public List<Tree.Kind> nodesToVisit() {
              return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.ANNOTATION);
              }

              @Override
              public void visitNode(Tree tree) {
              if (tree.is(Tree.Kind.METHOD)) {

              MethodTree methodTree = (MethodTree) tree;
              scanMethodTree(methodTree);

              } else if (tree.is(Tree.Kind.ANNOTATION)) {

              AnnotationTree annotationTree = (AnnotationTree) tree;
              scanAnnotationTree(annotationTree);

              }

              }

              private void scanMethodTree(MethodTree methodTree) {
              if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
              if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {

              methodStartLine = methodTree.firstToken().line();
              methodEndLine = methodTree.lastToken().line();

              } else {
              reportIssue(methodTree.simpleName(), REPORT_MISSING_TEST_INFO);
              }
              }
              }

              private void scanAnnotationTree(AnnotationTree annotationTree) {
              if (annotationTree.symbolType().fullyQualifiedName().equals(TEST_INFO_ANNOTATION_PATH)) {

              annotationLine = annotationTree.firstToken().line();
              if (annotationLine >= methodStartLine && annotationLine < methodEndLine) {

              annotationLineText = context.getFileLines().get(annotationLine - 1).replaceAll("\s", "");

              if (annotationLineText.contains("testCaseId")) {
              // {"ABC-123","DEF-456"} OR {("ABC-123"), ("DEF-456")}
              if ((annotationLineText.contains("{("") || annotationLineText.contains("{""))
              && (annotationLineText.contains("")}") || annotationLineText.contains(""}"))) {
              reportCase1();
              }
              // ("ABC-123")
              else if (annotationLineText.contains("("") && annotationLineText.contains("")")) {
              reportCase2();
              }
              // "ABC-123"
              else if (annotationLineText.contains(""") && annotationLineText.contains("",")) {
              reportCase3();
              }

              } else {
              addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
              }
              }
              }
              }

              /**
              * Method report an issue if testCaseId = {"ABC-123","DEF-456",...,"AEF-159"} or
              * testCaseId = {("ABC-123"),("DEF-456"),...,("AEF-159")} has no values between
              * quotes.
              */
              private void reportCase1() {
              String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("}")[0];
              testCaseId = testCaseId.replaceAll(""", "").replaceAll("\{", "").replaceAll(",", "").replaceAll("\(", "")
              .replaceAll("\)", "");

              if (testCaseId.length() == 0) {
              addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
              }
              }

              /**
              * Method report an issue if testCaseId = ("ABC-123") has no value between
              * quotes.
              */
              private void reportCase2() {
              String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split(""\)")[0];
              testCaseId = testCaseId.replaceAll(""", "").replaceAll("\(", "").replaceAll(",", "");

              if (testCaseId.length() == 0) {
              addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
              }
              }

              /**
              * Method report an issue if testCaseId = "ABC-123" has no value between quotes.
              */
              private void reportCase3() {
              String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("",")[0];
              testCaseId = testCaseId.replaceAll(""", "").replaceAll(",", "");

              if (testCaseId.length() == 0) {
              addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
              }
              }





              share|improve this answer


























                0












                0








                0







                I figured it out. I switched to using FileScannerContext.
                First i check if method is annotated with @Test and @TestInfo and then get in which line @TestInfo is located. Later it is just finding what I need in String line.



                This is my final working solution:



                private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
                private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
                private static final String REPORT_EMPTY_TEST_CASE_ID = "Method annotated with @TestInfo should have not empty testCaseId parameter";
                private static final String REPORT_MISSING_TEST_INFO = "Method annotated with @Test should also be annotated with @TestInfo";
                private static final String TEST_CASE_ID_SPLIT_PLACE = "testCaseId=";

                private int methodStartLine = 0;
                private int methodEndLine = 0;
                private int annotationLine;
                private String annotationLineText;

                @Override
                public List<Tree.Kind> nodesToVisit() {
                return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.ANNOTATION);
                }

                @Override
                public void visitNode(Tree tree) {
                if (tree.is(Tree.Kind.METHOD)) {

                MethodTree methodTree = (MethodTree) tree;
                scanMethodTree(methodTree);

                } else if (tree.is(Tree.Kind.ANNOTATION)) {

                AnnotationTree annotationTree = (AnnotationTree) tree;
                scanAnnotationTree(annotationTree);

                }

                }

                private void scanMethodTree(MethodTree methodTree) {
                if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
                if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {

                methodStartLine = methodTree.firstToken().line();
                methodEndLine = methodTree.lastToken().line();

                } else {
                reportIssue(methodTree.simpleName(), REPORT_MISSING_TEST_INFO);
                }
                }
                }

                private void scanAnnotationTree(AnnotationTree annotationTree) {
                if (annotationTree.symbolType().fullyQualifiedName().equals(TEST_INFO_ANNOTATION_PATH)) {

                annotationLine = annotationTree.firstToken().line();
                if (annotationLine >= methodStartLine && annotationLine < methodEndLine) {

                annotationLineText = context.getFileLines().get(annotationLine - 1).replaceAll("\s", "");

                if (annotationLineText.contains("testCaseId")) {
                // {"ABC-123","DEF-456"} OR {("ABC-123"), ("DEF-456")}
                if ((annotationLineText.contains("{("") || annotationLineText.contains("{""))
                && (annotationLineText.contains("")}") || annotationLineText.contains(""}"))) {
                reportCase1();
                }
                // ("ABC-123")
                else if (annotationLineText.contains("("") && annotationLineText.contains("")")) {
                reportCase2();
                }
                // "ABC-123"
                else if (annotationLineText.contains(""") && annotationLineText.contains("",")) {
                reportCase3();
                }

                } else {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }
                }
                }

                /**
                * Method report an issue if testCaseId = {"ABC-123","DEF-456",...,"AEF-159"} or
                * testCaseId = {("ABC-123"),("DEF-456"),...,("AEF-159")} has no values between
                * quotes.
                */
                private void reportCase1() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("}")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll("\{", "").replaceAll(",", "").replaceAll("\(", "")
                .replaceAll("\)", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }

                /**
                * Method report an issue if testCaseId = ("ABC-123") has no value between
                * quotes.
                */
                private void reportCase2() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split(""\)")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll("\(", "").replaceAll(",", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }

                /**
                * Method report an issue if testCaseId = "ABC-123" has no value between quotes.
                */
                private void reportCase3() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("",")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll(",", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }





                share|improve this answer













                I figured it out. I switched to using FileScannerContext.
                First i check if method is annotated with @Test and @TestInfo and then get in which line @TestInfo is located. Later it is just finding what I need in String line.



                This is my final working solution:



                private static final String TEST_ANNOTATION_PATH = "org.testng.annotations.Test";
                private static final String TEST_INFO_ANNOTATION_PATH = "toolkit.utils.TestInfo";
                private static final String REPORT_EMPTY_TEST_CASE_ID = "Method annotated with @TestInfo should have not empty testCaseId parameter";
                private static final String REPORT_MISSING_TEST_INFO = "Method annotated with @Test should also be annotated with @TestInfo";
                private static final String TEST_CASE_ID_SPLIT_PLACE = "testCaseId=";

                private int methodStartLine = 0;
                private int methodEndLine = 0;
                private int annotationLine;
                private String annotationLineText;

                @Override
                public List<Tree.Kind> nodesToVisit() {
                return ImmutableList.of(Tree.Kind.METHOD, Tree.Kind.ANNOTATION);
                }

                @Override
                public void visitNode(Tree tree) {
                if (tree.is(Tree.Kind.METHOD)) {

                MethodTree methodTree = (MethodTree) tree;
                scanMethodTree(methodTree);

                } else if (tree.is(Tree.Kind.ANNOTATION)) {

                AnnotationTree annotationTree = (AnnotationTree) tree;
                scanAnnotationTree(annotationTree);

                }

                }

                private void scanMethodTree(MethodTree methodTree) {
                if (methodTree.symbol().metadata().isAnnotatedWith(TEST_ANNOTATION_PATH)) {
                if (methodTree.symbol().metadata().isAnnotatedWith(TEST_INFO_ANNOTATION_PATH)) {

                methodStartLine = methodTree.firstToken().line();
                methodEndLine = methodTree.lastToken().line();

                } else {
                reportIssue(methodTree.simpleName(), REPORT_MISSING_TEST_INFO);
                }
                }
                }

                private void scanAnnotationTree(AnnotationTree annotationTree) {
                if (annotationTree.symbolType().fullyQualifiedName().equals(TEST_INFO_ANNOTATION_PATH)) {

                annotationLine = annotationTree.firstToken().line();
                if (annotationLine >= methodStartLine && annotationLine < methodEndLine) {

                annotationLineText = context.getFileLines().get(annotationLine - 1).replaceAll("\s", "");

                if (annotationLineText.contains("testCaseId")) {
                // {"ABC-123","DEF-456"} OR {("ABC-123"), ("DEF-456")}
                if ((annotationLineText.contains("{("") || annotationLineText.contains("{""))
                && (annotationLineText.contains("")}") || annotationLineText.contains(""}"))) {
                reportCase1();
                }
                // ("ABC-123")
                else if (annotationLineText.contains("("") && annotationLineText.contains("")")) {
                reportCase2();
                }
                // "ABC-123"
                else if (annotationLineText.contains(""") && annotationLineText.contains("",")) {
                reportCase3();
                }

                } else {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }
                }
                }

                /**
                * Method report an issue if testCaseId = {"ABC-123","DEF-456",...,"AEF-159"} or
                * testCaseId = {("ABC-123"),("DEF-456"),...,("AEF-159")} has no values between
                * quotes.
                */
                private void reportCase1() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("}")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll("\{", "").replaceAll(",", "").replaceAll("\(", "")
                .replaceAll("\)", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }

                /**
                * Method report an issue if testCaseId = ("ABC-123") has no value between
                * quotes.
                */
                private void reportCase2() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split(""\)")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll("\(", "").replaceAll(",", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }

                /**
                * Method report an issue if testCaseId = "ABC-123" has no value between quotes.
                */
                private void reportCase3() {
                String testCaseId = annotationLineText.split(TEST_CASE_ID_SPLIT_PLACE)[1].split("",")[0];
                testCaseId = testCaseId.replaceAll(""", "").replaceAll(",", "");

                if (testCaseId.length() == 0) {
                addIssue(annotationLine, REPORT_EMPTY_TEST_CASE_ID);
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 13 hours ago









                Tomas GriušysTomas Griušys

                4010




                4010






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54250794%2fsonarqube-arguments-value%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Homophylophilia

                    Updating UILabel text programmatically using a function

                    Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage