Skip to content

Commit

Permalink
NullPointer Exception while generating code coverage report for Subpr…
Browse files Browse the repository at this point in the history
…ocess having dependency on ESM TIBCOSoftware#597
  • Loading branch information
SujataDarekar committed May 26, 2021
1 parent a2811c2 commit 8c1e301
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,74 @@
package com.tibco.bw.maven.plugin.test.coverage;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.project.DefaultDependencyResolutionRequest;
import org.apache.maven.project.DependencyResolutionException;
import org.apache.maven.project.DependencyResolutionResult;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectDependenciesResolver;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.tibco.bw.maven.plugin.osgi.helpers.ManifestParser;
import com.tibco.bw.maven.plugin.test.dto.CompleteReportDTO;
import com.tibco.bw.maven.plugin.test.dto.ProcessCoverageDTO;
import com.tibco.bw.maven.plugin.test.dto.TestSuiteResultDTO;
import com.tibco.bw.maven.plugin.test.helpers.BWTestConfig;
import com.tibco.bw.maven.plugin.utils.BWFileUtils;
import com.tibco.bw.maven.plugin.utils.Constants;

import org.eclipse.aether.graph.Dependency;

public class ProcessCoverageParser
{

Map<String,ProcessCoverage> processMap = new HashMap<>();

@Component
ProjectDependenciesResolver resolver;

HashMap<File,String> artifactFiles = new HashMap<File,String>();

@SuppressWarnings({ "rawtypes", "unchecked" })
public Map<String,ProcessCoverage> loadCoverage( CompleteReportDTO complete)
{
List<MavenProject> projects = BWTestConfig.INSTANCE.getSession().getProjects();
resolver = BWTestConfig.INSTANCE.getResolver();


for( MavenProject project : projects )
{
if( project.getPackaging().equals("bwmodule") )
{
loadProcesses( project);
loadProcessesFromESM(project);


}
}



for( int count = 0 ; count < complete.getModuleResult().size() ; count++ )
{
TestSuiteResultDTO result = (TestSuiteResultDTO) complete.getModuleResult().get( count );
Expand All @@ -45,17 +77,71 @@ public Map<String,ProcessCoverage> loadCoverage( CompleteReportDTO complete)
for( int i = 0; i < coverage.size() ; i++ )
{
ProcessCoverageDTO dto = (ProcessCoverageDTO) coverage.get( i );
ProcessCoverage pc = processMap.get( dto.getProcessName());
pc.setProcessExecuted(true);
pc.getActivitiesExec().addAll( dto.getActivityCoverage() );
pc.getTransitionExec().addAll( dto.getTransitionCoverage() );
if(processMap.get( dto.getProcessName())!=null){
ProcessCoverage pc = processMap.get( dto.getProcessName());
pc.setProcessExecuted(true);
pc.getActivitiesExec().addAll( dto.getActivityCoverage() );
pc.getTransitionExec().addAll( dto.getTransitionCoverage() );
}
}
}

return processMap;
}


private void loadProcessesFromESM(MavenProject project) {
DependencyResolutionResult resolutionResult = getDependencies(project,BWTestConfig.INSTANCE.getSession());
if (resolutionResult != null) {
for(Dependency dependency : resolutionResult.getDependencies()) {
if(!dependency.getArtifact().getVersion().equals("0.0.0")) {
artifactFiles.put(dependency.getArtifact().getFile(),dependency.getArtifact().getArtifactId());
}
}
}
for(File file : artifactFiles.keySet()) {
if( file.getName().indexOf("com.tibco.bw.palette.shared") != -1 || file.getName().indexOf("com.tibco.xml.cxf.common") != -1 || file.getName().indexOf("tempbw") != -1){
continue;
}
boolean isSharedModule = false;
Manifest mf = ManifestParser.parseManifestFromJAR( file);
if(mf == null){
try {
throw new Exception("Failed to get Manifest for - "+ file.getName() +". Please verify if jar file is valid, the MANIFEST.MF should be first or second entry in the jar file. Use Command - jar tf <Jar_File_Path> to verify.");
} catch (Exception e) {
e.printStackTrace();
}
}
for( Object str : mf.getMainAttributes().keySet())
{
if( Constants.TIBCO_SHARED_MODULE.equals(str.toString() ))
{
isSharedModule = true;
break;
}
}
if(isSharedModule){
try {
parseESM(file, artifactFiles.get(file));
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

private DependencyResolutionResult getDependencies(MavenProject project, MavenSession session) {
DependencyResolutionResult resolutionResult = null;

try {
DefaultDependencyResolutionRequest resolution = new DefaultDependencyResolutionRequest(project, session.getRepositorySession());
resolutionResult = resolver.resolve(resolution);
} catch (DependencyResolutionException e) {
e.printStackTrace();
resolutionResult = e.getResult();
}
return resolutionResult;
}
private void loadProcesses( MavenProject project )
{
List<File> files = getProcessFiles(project);
Expand Down Expand Up @@ -92,10 +178,44 @@ private void parse( File processFile , String module ) throws Exception
coverage.setModuleName(module);
processMap.put( coverage.getProcessName(), coverage);
}
}

private void parseESM( File processFile , String module ) throws Exception
{
String xml=null;
String zipFileName = processFile.getAbsolutePath();





try (FileInputStream fis = new FileInputStream(zipFileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ZipInputStream stream = new ZipInputStream(bis)) {

ZipEntry entry;
ZipFile zf = new ZipFile(zipFileName);
while ((entry = stream.getNextEntry()) != null) {
String name = entry.getName();
if (name.endsWith(".bwp")) {
InputStream in = zf.getInputStream(entry);
xml = IOUtils.toString(in, StandardCharsets.UTF_8.name());
if( null != xml ){
ProcessParser parser = new ProcessParser();
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(parser );
reader.parse(new InputSource(new StringReader( xml )));
ProcessCoverage coverage = parser.getCoverage();
if( coverage.isSubProcess())
{
coverage.setModuleName(module);
processMap.put( coverage.getProcessName(), coverage);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private List<File> getProcessFiles( MavenProject project )
Expand All @@ -105,8 +225,6 @@ private List<File> getProcessFiles( MavenProject project )

return files;



}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.apache.commons.io.FileUtils;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectDependenciesResolver;

public class BWTestConfig
{
Expand All @@ -36,6 +38,8 @@ public class BWTestConfig

private MavenProject project;

ProjectDependenciesResolver resolver;

private Log logger;

public HashMap<String,String> testCaseWithProcessNameMap = new HashMap<>();
Expand Down Expand Up @@ -205,5 +209,13 @@ public Map<String, Boolean> getUserTestSuiteNames() {
public void setUserTestSuiteNames(Map<String, Boolean> userTestSuiteNames) {
this.userTestSuiteNames = userTestSuiteNames;
}

public ProjectDependenciesResolver getResolver() {
return resolver;
}

public void setResolver(ProjectDependenciesResolver resolver) {
this.resolver = resolver;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.ProjectDependenciesResolver;
import org.apache.maven.reporting.AbstractMavenReport;
import org.apache.maven.reporting.MavenReportException;

Expand All @@ -33,11 +34,14 @@ public class BWTestsReport extends AbstractMavenReport
@Parameter(defaultValue="${session}", readonly=true)
private MavenSession session;

@Parameter( property = "showFailureDetails" , defaultValue = "false" )
@Parameter( property = "showFailureDetails" , defaultValue = "true" )
private boolean showFailureDetails;

@Parameter( property = "testSuiteName" , defaultValue = "" )
private String testSuiteName;

@Component
ProjectDependenciesResolver resolver;


@Override
Expand Down Expand Up @@ -114,6 +118,7 @@ protected void executeReport(Locale arg0) throws MavenReportException
BWTestConfig.INSTANCE.init( tibcoHome , bwHome , session, getProject() , logger );
TestFileParser.INSTANCE.setshowFailureDetails(showFailureDetails);
BWTestConfig.INSTANCE.setTestSuiteName(testSuiteName);
BWTestConfig.INSTANCE.setResolver(resolver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand Down

0 comments on commit 8c1e301

Please sign in to comment.