0

I have a java class file like below(class file1). It contains multiple test methods. here two test methods are present. I have to return the code part of each test methods in between the STARTING TEST CASE and ENDING TEST CASE. In a list I have to return like below. Using regex or any other way I can do this?

  LOGGER.info("STARTING TEST CASE : " + testCaseId);
// coding 
        
     List<String> testMethods = new ArrayList<>();
        String source = new String(Files.readAllBytes(Paths.get(classFile)));
        Matcher matcher = TEST_METHOD_PATTERN.matcher(source);
        while (matcher.find()) {
            testMethods.add(matcher.group(2));
        }   
        LOGGER.info("ENDING TEST CASE: " + testCaseId);

and

 LOGGER.info("STARTING TEST CASE: TEXTME");
       List<String> methods = listMethods("public static void main(String[] args) {}\n" +
                "public void method1() {}");

        for (String method : methods) {
            System.out.println(method);
        }
        LOGGER.info("ENDING TEST CASE: TEXTME");

Class file (1)

public class hellome {

    /**
     * Test script to validate FRAMCE24
    
     */


    public void testme(Settop settop) {

        // Variable for test case ID

        try {
            LOGGER.info("#######################################################################################");
            LOGGER.info("STARTING TEST CASE : " + testCaseId);
            // coding 

            List < String > testMethods = new ArrayList < > ();
            String source = new String(Files.readAllBytes(Paths.get(classFile)));
            Matcher matcher = TEST_METHOD_PATTERN.matcher(source);
            while (matcher.find()) {
                testMethods.add(matcher.group(2));
            }
            LOGGER.info("ENDING TEST CASE: " + testCaseId);
        }
    }

    /**
     * Test script to validate selgoet
    
     */

    public void testfrance24(GetData data) {
        // Variable declaration starts
        boolean status = false;
        long startTime = 0 L;
        // Variable declaration ends
        try {
            LOGGER.info("#######################################################################################");
            LOGGER.info("STARTING TEST CASE: TEXTME");
            List < String > methods = listMethods("public static void main(String[] args) {}\n" +
                "public void method1() {}");

            for (String method: methods) {
                System.out.println(method);
            }
            LOGGER.info("ENDING TEST CASE: TEXTME");
        }

    }

Tried this way but not working

 public static void main(String[] args) throws IOException {
    String str= "\r\n"
        + "\r\n"
        + "public class hellome  {\r\n"
        + "\r\n"
        + "      /**\r\n"
        + "     * Test script to validate FRAMCE24\r\n"
        + "    \r\n"
        + "     */\r\n"
        + "\r\n"
        + " \r\n"
        + "    public void testme(Settop settop) {\r\n"
        + "\r\n"
        + " // Variable for test case ID\r\n"
        + "\r\n"
        + " try {\r\n"
        + "     LOGGER.info(\"#######################################################################################\");\r\n"
        + "     LOGGER.info(\"STARTING TEST CASE : \" + testCaseId);\r\n"
        + "// coding \r\n"
        + "     \r\n"
        + "  List<String> testMethods = new ArrayList<>();\r\n"
        + "        String source = new String(Files.readAllBytes(Paths.get(classFile)));\r\n"
        + "        Matcher matcher = TEST_METHOD_PATTERN.matcher(source);\r\n"
        + "        while (matcher.find()) {\r\n"
        + "            testMethods.add(matcher.group(2));\r\n"
        + "        }   \r\n"
        + "     LOGGER.info(\"ENDING TEST CASE: \" + testCaseId);\r\n"
        + " }\r\n"
        + "    }\r\n"
        + "\r\n"
        + "    /**\r\n"
        + "     * Test script to validate selgoet\r\n"
        + "    \r\n"
        + "     */\r\n"
        + "\r\n"
        + "    public void testfrance24(GetData data) {\r\n"
        + " // Variable declaration starts\r\n"
        + " boolean status = false;\r\n"
        + " long startTime = 0L;\r\n"
        + " // Variable declaration ends\r\n"
        + " try {\r\n"
        + "     LOGGER.info(\"#######################################################################################\");\r\n"
        + "     LOGGER.info(\"STARTING TEST CASE: TEXTME\");\r\n"
        + "    List<String> methods = listMethods(\"public static void main(String[] args) {}\\n\" +\r\n"
        + "                \"public void method1() {}\");\r\n"
        + "\r\n"
        + "        for (String method : methods) {\r\n"
        + "            System.out.println(method);\r\n"
        + "        }\r\n"
        + "     LOGGER.info(\"ENDING TEST CASE: TEXTME\");\r\n"
        + " }\r\n"
        + "\r\n"
        + "    }\r\n"
        + "\r\n"
        + "   ";
    
    Matcher m = Pattern.compile(
                                Pattern.quote("STARTING TEST CASE")
                                + "(.*?)"
                                + Pattern.quote("ENDING TEST CASE")
                       ).matcher(str);
    while(m.find()){
        System.out.println("inside");
        String match = m.group(1);
        System.out.println(">"+match+"<");
        //here you insert 'match' into the list
    } 
Psl
  • 3,830
  • 16
  • 46
  • 84
  • In "class file 1" create one method for each test case. Call those methods from class "hellome". – Thomas Behr Aug 18 '23 at 12:16
  • Does this answer your question? [Match multiline text using regular expression](https://stackoverflow.com/questions/3651725/match-multiline-text-using-regular-expression) – tgdavies Aug 18 '23 at 12:30
  • tried with that but not working – Psl Aug 18 '23 at 12:37

1 Answers1

0

If you want to create a regex pattern to capture the contents between STARTING TEST CASE and ENDING TEST CASE logs:

  1. Use (?s) at the start of the regex pattern to enable "dot matches newline" mode. This makes sure that the . in the regex pattern matches newlines as well.
  2. Use a non-greedy quantifier .*? to capture the content in between two patterns. This ensures that the regex captures the shortest possible match.

This should print out the contents between STARTING TEST CASE and ENDING TEST CASE for each method in your class.

Remember, regex might not handle all edge cases due to its limitations in parsing nested structures. If your class file structure is consistent as provided, this solution should work. Otherwise, you might want to consider a more robust parser for Java source code.

import java.util.regex.*;
import java.util.*;
public class Test {
        
            public static void main(String[] args) {
            String classFile= "\r\n"
                    + "\r\n"
                    + "public class hellome  {\r\n"
                    + "\r\n"
                    + "      /**\r\n"
                    + "     * Test script to validate FRAMCE24\r\n"
                    + "    \r\n"
                    + "     */\r\n"
                    + "\r\n"
                    + " \r\n"
                    + "    public void testme(Settop settop) {\r\n"
                    + "\r\n"
                    + " // Variable for test case ID\r\n"
                    + "\r\n"
                

+ " try {\r\n"
                + "     LOGGER.info(\"#######################################################################################\");\r\n"
                + "     LOGGER.info(\"STARTING TEST CASE : \" + testCaseId);\r\n"
                + "// coding \r\n"
                + "     \r\n"
                + "  List<String> testMethods = new ArrayList<>();\r\n"
                + "        String source = new String(Files.readAllBytes(Paths.get(classFile)));\r\n"
                + "        Matcher matcher = TEST_METHOD_PATTERN.matcher(source);\r\n"
                + "        while (matcher.find()) {\r\n"
                + "            testMethods.add(matcher.group(2));\r\n"
                + "        }   \r\n"
                + "     LOGGER.info(\"ENDING TEST CASE: \" + testCaseId);\r\n"
                + " }\r\n"
                + "    }\r\n"
                + "\r\n"
                + "    /**\r\n"
                + "     * Test script to validate selgoet\r\n"
                + "    \r\n"
                + "     */\r\n"
                + "\r\n"
                + "    public void testfrance24(GetData data) {\r\n"
                + " // Variable declaration starts\r\n"
                + " boolean status = false;\r\n"
                + " long startTime = 0L;\r\n"
                + " // Variable declaration ends\r\n"
                + " try {\r\n"
                + "     LOGGER.info(\"#######################################################################################\");\r\n"
                + "     LOGGER.info(\"STARTING TEST CASE: TEXTME\");\r\n"
                + "    List<String> methods = listMethods(\"public static void main(String[] args) {}\\n\" +\r\n"
                + "                \"public void method1() {}\");\r\n"
                + "\r\n"
                + "        for (String method : methods) {\r\n"
                + "            System.out.println(method);\r\n"
                + "        }\r\n"
                + "     LOGGER.info(\"ENDING TEST CASE: TEXTME\");\r\n"
                + " }\r\n"
                + "\r\n"
                + "    }\r\n"
                + "\r\n"
                + "   ";
        // Regex pattern to match the START and END log statements and capture the content in between
        Pattern pattern = Pattern.compile("(?s)LOGGER\\.info\\(\"STARTING TEST CASE.*?LOGGER\\.info\\(\"ENDING TEST CASE");

        Matcher matcher = pattern.matcher(classFile);

        List<String> testMethodsCode = new ArrayList<>();

        while (matcher.find()) {
            testMethodsCode.add(matcher.group());
        }

        // Print the captured contents
        int methodCount = 1;
        for (String methodCode : testMethodsCode) {
            System.out.println("Method " + methodCount + ":");
            System.out.println(methodCode);
            System.out.println("----------------------------");
            methodCount++;
        }
    }
    }

The output is: enter image description here

Shila Mosammami
  • 999
  • 6
  • 20