Skip to content

Commit

Permalink
BWCE-4934 ClassNotFoundException Error while running BWUnit tests wit…
Browse files Browse the repository at this point in the history
…h Java Projects
  • Loading branch information
richardyam-tibco committed Oct 18, 2022
1 parent 4ba38a4 commit 5f1d21b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.w3c.dom.Element;

import com.tibco.bw.maven.plugin.osgi.helpers.ManifestParser;
import com.tibco.bw.maven.plugin.test.helpers.BWTestConfig;
import com.tibco.bw.maven.plugin.utils.Constants;

public class ConfigFileGenerator
{
Expand All @@ -44,13 +53,16 @@ public void generateConfig()
addPluginsFromDir(target, builder);
}

List<MavenProject> cxfProjects = new ArrayList<MavenProject>();
List<MavenProject> requiredDevPropertiesProjects = new ArrayList<MavenProject>();

List<MavenProject> projects = BWTestConfig.INSTANCE.getSession().getProjects();

for( MavenProject project : projects ) {
if (project.getPackaging().equals("bwmodule") || project.getPackaging().equals("bwear")) {

if (isJavaProject(project)) {
requiredDevPropertiesProjects.add(project);
}
Set<Artifact> artifacts = project.getDependencyArtifacts();
if(artifacts != null)
{
Expand All @@ -65,7 +77,7 @@ public void generateConfig()
}
}
if(isCXF){
cxfProjects.add(project);
requiredDevPropertiesProjects.add(project);
}
}
}
Expand Down Expand Up @@ -94,7 +106,7 @@ public void generateConfig()
stream.flush();
stream.close();

generateDevPropertiesFile(cxfProjects);
generateDevPropertiesFile(requiredDevPropertiesProjects);
}

catch(Exception e )
Expand All @@ -104,18 +116,52 @@ public void generateConfig()

}

private void generateDevPropertiesFile(List<MavenProject> cxfProjects) throws IOException, DependencyResolutionRequiredException{
private boolean isJavaProject(MavenProject project) {
File projectFile = new File(project.getBasedir(), Constants.DOT_PROJECT_FILE);
NodeList nList = getNatureList(projectFile);
if (nList != null) {
for(int i = 0; i < nList.getLength(); i++) {
if (nList.item(i) != null) {
Element node = (Element)nList.item(i);
String nature = node.getTextContent();
if (nature != null && nature.equals(Constants.NATURE_JAVE_PROJECT_PROPERTY))
return true;
}
}
}
return false;
}

private NodeList getNatureList(File dotProject) {
NodeList nList = null;
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(dotProject);
nList = doc.getElementsByTagName(Constants.NATURE_PROPERTY);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return nList;
}

private void generateDevPropertiesFile(List<MavenProject> requiredDevPropertiesProjects) throws IOException, DependencyResolutionRequiredException{
File devProps = new File( BWTestConfig.INSTANCE.getConfigDir() , "dev.properties");
devProps.createNewFile();

Properties properties = new Properties();
properties.put("@ignoredot@","true");
for(MavenProject cxfProject : cxfProjects)
for(MavenProject reqProject : requiredDevPropertiesProjects)
{
//find classpath
Manifest projectManifest = ManifestParser.parseManifest( cxfProject.getBasedir() );
Manifest projectManifest = ManifestParser.parseManifest( reqProject.getBasedir() );
String bundleClassPath = projectManifest.getMainAttributes().getValue("Bundle-ClassPath");
BWTestConfig.INSTANCE.getLogger().debug("Bundle-Classpath for project "+ cxfProject.getName() +" -> "+bundleClassPath);
BWTestConfig.INSTANCE.getLogger().debug("Bundle-Classpath for project "+ reqProject.getName() +" -> "+bundleClassPath);
String pathString = "";
if(bundleClassPath != null)
{
Expand All @@ -126,8 +172,8 @@ private void generateDevPropertiesFile(List<MavenProject> cxfProjects) throws IO
}
}
pathString = "bin,target/classes" + pathString;
properties.put(cxfProject.getName(), pathString);
BWTestConfig.INSTANCE.getLogger().debug("Adding CXF project entry to dev.properties -> "+ cxfProject.getName()+ "="+ pathString);
properties.put(reqProject.getName(), pathString);
BWTestConfig.INSTANCE.getLogger().debug("Adding project entry to dev.properties -> "+ reqProject.getName()+ "="+ pathString);
}

FileOutputStream stream = new FileOutputStream(devProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface Constants {
public static final String TEMP_SUBSTVAR = "external.substvar";
public static final String TIMESTAMP = "timestamp";
public static final String COMPONENT_PROCESS = "ComponentProcess";
public static final String DOT_PROJECT_FILE = ".project";
public static final String NATURE_PROPERTY = "nature";
public static final String NATURE_JAVE_PROJECT_PROPERTY = "org.eclipse.jdt.core.javanature";

// TCI
public static final String TCI_SERVER_ENDPOINT_ENV = "TCI_PLATFORM_API_ENDPOINT";
Expand Down

0 comments on commit 5f1d21b

Please sign in to comment.