Skip to content

Commit

Permalink
Add initial source code
Browse files Browse the repository at this point in the history
adnanabh committed Dec 15, 2015
1 parent 0b1fdab commit 7bbb995
Showing 12 changed files with 820 additions and 0 deletions.
15 changes: 15 additions & 0 deletions JtlParser.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.atlantbh.jmeter.plugins.jtlparser</groupId>
<artifactId>atlantbh-components</artifactId>
<version>1.0-SNAPSHOT</version>


</project>
61 changes: 61 additions & 0 deletions src/main/java/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import com.atlantbh.jmeter.plugins.jtlparser.JtlParser;
import com.atlantbh.jmeter.plugins.jtlparser.builder.JunitModelBuilder;
import com.atlantbh.jmeter.plugins.jtlparser.builder.JunitXmlBuilder;
import com.atlantbh.jmeter.plugins.jtlparser.model.jtl.ThreadGroup;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestCase;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestStep;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestSuite;
import com.sun.javadoc.Doc;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;

/**
* Created by adnan on 12/12/15.
*/
public class Example {
public static void main(String[] args) {
// TODO Auto-generated method stub
JtlParser parser = new JtlParser();
String fileLocation = "/Users/adnan/example.jtl";
try {
NodeList list = parser.getStartNode(fileLocation);
parser.parseJtl(list, null);

ArrayList<ThreadGroup> threads = parser.getThreadGroups();
JunitModelBuilder builder = JunitModelBuilder.newInstance();
TestSuite testSuite = builder.generateTestSuite(threads);
ArrayList<TestSuite> testSuites = new ArrayList<TestSuite>();
testSuites.add(testSuite);

JunitXmlBuilder xmlBuilder = JunitXmlBuilder.newInstance();
Document doc = xmlBuilder.generateXmlDoc(testSuites);
xmlBuilder.writeXmlDoc(doc, "/Users/adnan/parsedjunit.xml");


/*
System.out.println("Test Suite name: " + testSuite.getName());
System.out.println("Number of tests in Test Suite: " + testSuite.getTests());
System.out.println("Test Suite Execution time: " + testSuite.getTime() + "ms");
System.out.println("FAILURES: " + testSuite.getFailures());
System.out.println("///////////////////////////////////\n");
for (TestCase testCase : testSuite.getTestCases()) {
System.out.println("Test Case name: " + testCase.getClassName());
System.out.println("");
for (TestStep testStep : testCase.getTestSteps()) {
System.out.println("Test Step name: " + testStep.getName());
System.out.println("Test Step Execution time: " + testStep.getTime());
if (!testStep.getFailureMessage().equals(""))
System.out.println("Failure: " + testStep.getFailureMessage());
System.out.println("");
}
System.out.print("/////////////////////////////\n");
}*/
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
161 changes: 161 additions & 0 deletions src/main/java/com/atlantbh/jmeter/plugins/jtlparser/JtlParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.atlantbh.jmeter.plugins.jtlparser;

import com.atlantbh.jmeter.plugins.jtlparser.model.jtl.*;
import com.atlantbh.jmeter.plugins.jtlparser.model.jtl.ThreadGroup;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
* Created by adnan on 12/12/15.
*/
public class JtlParser {
///////////////////////////
//PRIVATE HELPER METHODS//
/////////////////////////
private HashMap<String,ThreadGroup> threadGroups;

private String getThreadGroupName(String name){
String threadGroupName = name.substring(0, name.lastIndexOf(" "));
return threadGroupName;
}

private String getThreadIteration(String name){
String threadIteration = name.substring(name.lastIndexOf(" ") + 1);
return threadIteration;
}

private Sampler createSampler(Node samplerNode){
Element element = (Element) samplerNode;
Sampler sampler = new Sampler();
String samplerIteration = getThreadIteration(element.getAttribute("tn"));

sampler.setSamplerName(samplerNode.getNodeName());
sampler.setName(element.getAttribute("lb") + " " + samplerIteration );
// sampler.setResponseCode(element.getAttribute("rc"));
// sampler.setResponseMessage(element.getAttribute("rm"));
sampler.setTime(element.getAttribute("t"));
return sampler;
}

private ThreadGroup createThreadGroup(String threadName){
ThreadGroup newThreadGroup = new ThreadGroup();
newThreadGroup.setThreadName(threadName);
threadGroups.put(threadName, newThreadGroup);
return newThreadGroup;
}

private AssertionResult createAssertionResult(Node assertNode){
AssertionResult assertionResult = new AssertionResult();
NodeList assertChildNodes = assertNode.getChildNodes();

//Get AssertionResult details
for(int i = 0; i < assertChildNodes.getLength(); i++) {
Node node = assertChildNodes.item(i);
if (node.getNodeName() == "name")
assertionResult.setName(node.getTextContent());
if (node.getNodeName() == "failure")
assertionResult.setFailure(node.getTextContent());
if (node.getNodeName() == "error")
assertionResult.setError(node.getTextContent());
if (node.getNodeName() == "failureMessage")
assertionResult.setFailureMessage(node.getTextContent());
}
return assertionResult;
}

///////////////////
//PUBLIC METHODS//
/////////////////

public JtlParser(){
super();
threadGroups = new HashMap<String, ThreadGroup>();
}

// Get ThreadGroup from a HashMap with key which equals ThreadGroup name.
public ThreadGroup getThreadGroup(String threadGroupName) {
Object item = threadGroups.get(threadGroupName);
if(item instanceof ThreadGroup){
return (ThreadGroup) item;
}
return null;
}

public HashMap<String, ThreadGroup> getThreadGroupsMap(){
return threadGroups;
}

public ArrayList<ThreadGroup> getThreadGroups(){
return new ArrayList<ThreadGroup>(threadGroups.values());
}

public NodeList getStartNode(String fileLocation) throws Exception{
File jtlFile = new File(fileLocation);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
NodeList nodeList = null;
try{
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document jtl = dBuilder.parse(jtlFile);
jtl.getDocumentElement().normalize();
nodeList = jtl.getElementsByTagName("testResults");
} catch (ParserConfigurationException e) {
e.getMessage();
e.printStackTrace();
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
}

if (nodeList == null) {
throw new Exception("Node list has not been populated!");
} else {
return nodeList;
}
}

public void parseJtl(NodeList nodeList, Sampler parentSampler) throws IOException, ParserConfigurationException {
for (int i =0; i < nodeList.getLength(); i++){
Node node = nodeList.item(i);
Sampler sampler = null;
boolean foundAssertion = false;

if (node.getNodeType() == Node.ELEMENT_NODE) {
String samplePattern = "^.*[sS]ample";
Element element = (Element) node;

if (node.getNodeName().matches(samplePattern)){

String threadName = getThreadGroupName(element.getAttribute("tn"));
sampler = createSampler(node);

if(getThreadGroup(threadName) != null){
ThreadGroup threadGroup = getThreadGroup(threadName);
threadGroup.addSampler(sampler);
}else{
ThreadGroup threadGroup = createThreadGroup(threadName);
threadGroup.addSampler(sampler);
}
}
if(node.getNodeName() == "assertionResult") {
parentSampler.addAssertionResult(createAssertionResult(node));
foundAssertion = true;
}
}

if(node.hasChildNodes() && !foundAssertion){
parseJtl(node.getChildNodes(), sampler);
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.atlantbh.jmeter.plugins.jtlparser.builder;

import com.atlantbh.jmeter.plugins.jtlparser.model.jtl.*;
import com.atlantbh.jmeter.plugins.jtlparser.model.jtl.ThreadGroup;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestCase;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestStep;
import com.atlantbh.jmeter.plugins.jtlparser.model.junit.TestSuite;

import java.util.ArrayList;
import java.util.StringTokenizer;

/**
* Created by adnan on 12/12/15.
*/
public class JunitModelBuilder {

private static JunitModelBuilder builder = null;
private int totalFailedTestSteps = 0;
private int totalTestSteps = 0;
private double totalTestStepsExecutionTime = 0;

private JunitModelBuilder() {
super();
}


public static JunitModelBuilder newInstance(){
if(builder == null) {
builder = new JunitModelBuilder();
}
return builder;
}

private TestStep createTestStep(Sampler sampler) {
TestStep testStep = new TestStep();
testStep.setName(sampler.getSamplerName() + ": " +sampler.getName());
for (AssertionResult assertion: sampler.getAssertionResults()) {
if (assertion.isFailure()) {
testStep.setAssertionFailures(assertion.getName(),assertion.getFailureMessage());
}
}
if (sampler.hasFailedAssertions())
totalFailedTestSteps++;
testStep.setTime(sampler.getTime());
totalTestStepsExecutionTime += Double.parseDouble(sampler.getTime());

return testStep;
}

private TestCase createTestCase(ThreadGroup threadGroup){
TestCase testCase = new TestCase();
testCase.setClassName(threadGroup.getThreadName());

for(Sampler sampler: threadGroup.getSamplers()) {
TestStep testStep = createTestStep(sampler);
testCase.addTestStep(testStep);
totalTestSteps++;
}
return testCase;
}

public TestSuite generateTestSuite(ArrayList<ThreadGroup> threadGroups){
TestSuite testSuite = new TestSuite();
testSuite.setName("Test Plan");
totalFailedTestSteps = 0;
totalTestSteps = 0;
totalTestStepsExecutionTime = 0;


for (ThreadGroup threadGroup: threadGroups) {
TestCase testCase = createTestCase(threadGroup);
testSuite.addTestCase(testCase);
}

testSuite.setTime(String.valueOf(totalTestStepsExecutionTime));
testSuite.setTests(String.valueOf(totalTestSteps));
testSuite.setFailures(String.valueOf(totalFailedTestSteps));
return testSuite;
}
}
Loading
Oops, something went wrong.

0 comments on commit 7bbb995

Please sign in to comment.