From 5f1d21bdf86784883ae37f2c40aefab84a46bce9 Mon Sep 17 00:00:00 2001 From: richardyam-tibco Date: Tue, 18 Oct 2022 15:24:27 -0700 Subject: [PATCH] BWCE-4934 ClassNotFoundException Error while running BWUnit tests with Java Projects --- .../test/setuplocal/ConfigFileGenerator.java | 64 ++++++++++++++++--- .../bw/maven/plugin/utils/Constants.java | 3 + 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/test/setuplocal/ConfigFileGenerator.java b/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/test/setuplocal/ConfigFileGenerator.java index 104d9404..1a011852 100644 --- a/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/test/setuplocal/ConfigFileGenerator.java +++ b/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/test/setuplocal/ConfigFileGenerator.java @@ -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 { @@ -44,13 +53,16 @@ public void generateConfig() addPluginsFromDir(target, builder); } - List cxfProjects = new ArrayList(); + List requiredDevPropertiesProjects = new ArrayList(); List 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 artifacts = project.getDependencyArtifacts(); if(artifacts != null) { @@ -65,7 +77,7 @@ public void generateConfig() } } if(isCXF){ - cxfProjects.add(project); + requiredDevPropertiesProjects.add(project); } } } @@ -94,7 +106,7 @@ public void generateConfig() stream.flush(); stream.close(); - generateDevPropertiesFile(cxfProjects); + generateDevPropertiesFile(requiredDevPropertiesProjects); } catch(Exception e ) @@ -104,18 +116,52 @@ public void generateConfig() } - private void generateDevPropertiesFile(List 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 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) { @@ -126,8 +172,8 @@ private void generateDevPropertiesFile(List 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); diff --git a/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/utils/Constants.java b/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/utils/Constants.java index 91fa2bdd..e80bf5e7 100644 --- a/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/utils/Constants.java +++ b/Source/bw6-maven-plugin/src/main/java/com/tibco/bw/maven/plugin/utils/Constants.java @@ -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";