Skip to content

Commit

Permalink
AMBW-47001
Browse files Browse the repository at this point in the history
[Maven] Cant create unit test/suite file for newly created folder under 'Tests' folder
  • Loading branch information
mmilinds-tibco committed Mar 8, 2023
1 parent e0a9c21 commit fd594c2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public void collectSkipInitActivityName() throws Exception,FileNotFoundException
{
String[] testSuiteNames;

if(BWTestConfig.INSTANCE.getTestSuiteName().contains("/")){
testSuiteNames = StringUtils.splitByWholeSeparator(BWTestConfig.INSTANCE.getTestSuiteName(), "/");
if(BWTestConfig.INSTANCE.getTestSuiteName().contains(";")){
testSuiteNames = StringUtils.splitByWholeSeparator(BWTestConfig.INSTANCE.getTestSuiteName(), ";");
}
else{
testSuiteNames = new String []{BWTestConfig.INSTANCE.getTestSuiteName()};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class BWTSFileReaderWrapper {


public List<File> readBWTSFile(List<String> testSuiteList , String TestFolderPath, MavenProject project) throws IOException{
public List<File> readBWTSFile(List<String> testSuiteList , String TestFolderPath, MavenProject project, String testFoldername) throws IOException{
List<File> returnList = new ArrayList<>();
List<File> tempReturnList ;
HashMap<String,List<File>> testSuiteMap = new HashMap<String,List<File>>();
Expand All @@ -26,15 +26,19 @@ public List<File> readBWTSFile(List<String> testSuiteList , String TestFolderPat
BWTSModel bwtsModel = YMLBWTSFileReader.getModelFrom(path);
Object testCaseList = bwtsModel.getOthers().get("testCases");
tempReturnList = new ArrayList<>();
//to get the path till the the project module
String[] locationArray = TestFolderPath.split(testFoldername);

if(null != testCaseList){
for (Object obj : (ArrayList<?>)testCaseList) {
if(obj instanceof String){
returnList.add(new File(TestFolderPath.concat("//"+(String)obj)));
tempReturnList.add(new File(TestFolderPath.concat("//"+(String)obj)));
String location = locationArray[0]+testFoldername+File.separator+(String)obj;
returnList.add(new File(location));
tempReturnList.add(new File(location));
}
}
}
String testSuite = StringUtils.substringAfter(testSuiteName, TestFolderPath.concat("//"));
String testSuite = StringUtils.substringAfter(testSuiteName, "//");
testSuiteMap.put(testSuite, tempReturnList);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,64 @@
package com.tibco.bw.maven.plugin.testsuite;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.tibco.bw.maven.plugin.test.helpers.BWTestConfig;
import com.tibco.bw.maven.plugin.utils.BWFileUtils;

public class BWTestSuiteLoader {

private String testFolderName = null;

public List<File> collectTestCasesList(String baseDir, MavenProject project) throws IOException{
List<File> testSuitefile = new ArrayList<File>();
List<String> testSuiteNamePathList = new ArrayList<String>();
List<String> testSuiteNameList = new ArrayList<String>();

String testFolderPath = "";

File[] fileList = getFileList(baseDir);
String contents = FileUtils.readFileToString(fileList[0]);

if(contents != null) {
testFolderName = readProperties(contents,fileList[0]);
}

for(String testSuiteName : BWTestConfig.INSTANCE.getUserTestSuiteNames().keySet())
{
String folderPath = BWFileUtils.getTestFolderName(baseDir.toString(),testSuiteName);
String folderPath = BWFileUtils.getTestFolderName(baseDir.toString(),testSuiteName, testFolderName);
if(null != folderPath){
testFolderPath = folderPath;
testSuiteNamePathList.add(folderPath.concat("//"+testSuiteName));
testSuiteNameList.add(testSuiteName);
String[] finalTestSuiteName = null;
if(testSuiteName.contains(File.separator)) {
finalTestSuiteName = testSuiteName.split(Pattern.quote(File.separator));
}
if(finalTestSuiteName != null) {
testSuiteNamePathList.add(StringUtils.substringBefore(folderPath,File.separator+finalTestSuiteName[0]).concat("//"+testSuiteName));
testSuiteNameList.add(testSuiteName);
}else {
testSuiteNamePathList.add(folderPath.concat("//"+testSuiteName));
testSuiteNameList.add(testSuiteName);
}
BWTestConfig.INSTANCE.getUserTestSuiteNames().replace(testSuiteName, true);
}
else{
Expand All @@ -39,14 +69,77 @@ public List<File> collectTestCasesList(String baseDir, MavenProject project) thr

BWTestConfig.INSTANCE.setTestSuiteNameList(project, testSuiteNameList);



BWTSFileReaderWrapper fileReader = new BWTSFileReaderWrapper();
testSuitefile = fileReader.readBWTSFile(testSuiteNamePathList,testFolderPath, project);
testSuitefile = fileReader.readBWTSFile(testSuiteNamePathList,testFolderPath, project,testFolderName);
return testSuitefile;



}

private File[] getFileList(String baseDir) {
File dir = new File(baseDir);

File[] fileList = dir.listFiles(new FilenameFilter() {

public boolean accept(File dir, String name) {
return name.endsWith(".config");
}

});
return fileList;
}

protected static String readProperties(String contents,File propsFile) {
InputStream is = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
is = new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8));
Document document = builder.parse(is);
NodeList nodeList = document.getDocumentElement().getChildNodes();

for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
if (node instanceof Element)
{
Element el = (Element) node;
if ("config:specialFolders".equals(el.getNodeName())) {

NodeList childNodes = el.getChildNodes();

for (int j = 0; j < childNodes.getLength(); j++) {

Node cNode = childNodes.item(j);

if (cNode instanceof Element) {
Element cEl = (Element) cNode;

if ("config:folder".equals(cEl.getNodeName()))
{

if(cNode.getAttributes().getNamedItem("kind").getNodeValue().equals("bwtf")) {
return cNode.getAttributes().getNamedItem("location").getNodeValue();
}

}
}

}
}
}
}

} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return null;
}


public List<File> collectTestCasesListFromESM(String baseDir) throws IOException{
List<File> testSuitefile = new ArrayList<File>();
List<String> testSuiteNamePathList = new ArrayList<String>();
Expand All @@ -55,7 +148,16 @@ public List<File> collectTestCasesListFromESM(String baseDir) throws IOException
String testFolderPath = "";
for(String testSuiteName : BWTestConfig.INSTANCE.getUserESMTestSuiteNames().keySet())
{
String folderPath = BWFileUtils.getTestFolderName(baseDir,testSuiteName);
if(testFolderName == null) {
File[] fileList = getFileList(baseDir);
String contents = FileUtils.readFileToString(fileList[0]);

if(contents != null) {
testFolderName = readProperties(contents,fileList[0]);
}
}

String folderPath = BWFileUtils.getTestFolderName(baseDir,testSuiteName,testFolderName);
if(null != folderPath){
testFolderPath = folderPath;
testSuiteNamePathList.add(folderPath.concat("//"+testSuiteName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

public class BWFileUtils {

Expand Down Expand Up @@ -190,7 +191,7 @@ public static int indexOfLastSeparator(final String filename)
return Math.max(lastUnixPos, lastWindowsPos);
}

public static String getTestFolderName( final String location, final String testSuiteName){
public static String getTestFolderName( final String location, final String testSuiteName, final String utTestFolderName){
try
{
testFolderName = null;
Expand All @@ -205,11 +206,28 @@ public FileVisitResult preVisitDirectory(Path dir , BasicFileAttributes attrs) t
@Override
public FileVisitResult visitFile(Path file , BasicFileAttributes attrs) throws IOException
{
if( testSuiteName.equalsIgnoreCase(file.toFile().getName()) )
String fileName = file.toFile().getName();
String parent = null;
if(testSuiteName.contains(File.separator) && file.toString().contains(File.separator+utTestFolderName+File.separator)) {
fileName = StringUtils.substringAfter(file.toString(),utTestFolderName+File.separator);
}
if(!testSuiteName.contains(File.separator)) {
parent = new File(file.toFile().getParent()).getName();
}

if(parent == null && testSuiteName.equalsIgnoreCase(fileName))
{
testFolderName = file.toFile().getParent();
return FileVisitResult.TERMINATE;
}

else if (parent != null && parent.equals(utTestFolderName)) {
if(testSuiteName.equalsIgnoreCase(fileName)) {
testFolderName = file.toFile().getParent();
return FileVisitResult.TERMINATE;
}
}

return FileVisitResult.CONTINUE;
}

Expand Down

0 comments on commit fd594c2

Please sign in to comment.