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
}