Skip to content

Commit

Permalink
[FIXES JENKINS-28787]: Fixes number format exception (#71)
Browse files Browse the repository at this point in the history
When JMeter parser is used and a csv file contains header line, a number format exception is thrown.
Fix is to check if the first line is a header and skip the line if it is a header.
Note: Noticed that if there was a test for the constructor with glob only, this issue
would have been caught in testing.

Test added and refactored for cleanup.
  • Loading branch information
hitesh22 authored and undera committed Oct 14, 2016
1 parent 453cc84 commit 86a14eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
22 changes: 19 additions & 3 deletions src/main/java/hudson/plugins/performance/JMeterCsvParser.java
Expand Up @@ -17,6 +17,7 @@ public class JMeterCsvParser extends AbstractParser {

public static final String DEFAULT_DELIMITER = ",";
public static final String DEFAULT_CSV_FORMAT = "timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,bytes,Latency";
public static final String COMMAS_NOT_INSIDE_QUOTES = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
private static final boolean DONT_SKIP_FIRST_LINE = false;
public final boolean skipFirstLine;
public final String delimiter;
Expand Down Expand Up @@ -124,7 +125,8 @@ PerformanceReport parse(File reportFile) throws Exception {
final BufferedReader reader = new BufferedReader(new FileReader(reportFile));
try {
String line = reader.readLine();
if (line != null && skipFirstLine) {
if (line != null && (skipFirstLine || isFirstLineHeaderLine(line))) {
// skip the header line
line = reader.readLine();
}
while (line != null) {
Expand All @@ -147,6 +149,21 @@ PerformanceReport parse(File reportFile) throws Exception {
}
}

/**
* If the first CSV value is a date then it definitely is not a header line.
* @param line First line of a CSV file.
* @return false if the first csv value is a date, else true.
*/
private boolean isFirstLineHeaderLine(String line) {
try {
final String[] values = line.split(COMMAS_NOT_INSIDE_QUOTES);
new Date(Long.valueOf(values[0]));
}catch (Exception e){
return true;
}
return false;
}

/**
* Parses a single HttpSample instance from a single CSV line.
*
Expand All @@ -155,8 +172,7 @@ PerformanceReport parse(File reportFile) throws Exception {
*/
private HttpSample getSample(String line) {
final HttpSample sample = new HttpSample();
final String commasNotInsideQuotes = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
final String[] values = line.split(commasNotInsideQuotes);
final String[] values = line.split(COMMAS_NOT_INSIDE_QUOTES);
sample.setDate(new Date(Long.valueOf(values[timestampIdx])));
sample.setDuration(Long.valueOf(values[elapsedIdx]));
sample.setHttpCode(values[responseCodeIdx]);
Expand Down
23 changes: 18 additions & 5 deletions src/test/java/hudson/plugins/performance/JMeterCsvParserTest.java
@@ -1,5 +1,6 @@
package hudson.plugins.performance;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
Expand All @@ -11,17 +12,29 @@ public class JMeterCsvParserTest {
private static final Boolean SKIP_FIRST_LINE = true;
private static final String TEST_FILE_PATTERN = "timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,bytes";
private static final String NO_GLOB = null;
private File reportFile;

@Before
public void beforeMethod() throws Exception {
reportFile = new File(getClass().getResource("/JMeterCsvResults.csv").toURI());
}

@Test
public void canParseCsvFile() throws Exception {
final File reportFile = new File(getClass().getResource("/JMeterCsvResults.csv").toURI());

final JMeterCsvParser parser = new JMeterCsvParser(NO_GLOB, TEST_FILE_PATTERN, DEFAULT_DELIMITER, SKIP_FIRST_LINE);
final PerformanceReport result = parser.parse(reportFile);
parseAndVerifyResult(parser);
}

@Test
public void canParseCsvFileWhenSkipFirstLineIsNotSpecifiedAndFirstLineHasHeader() throws Exception {
final JMeterCsvParser parser = new JMeterCsvParser(NO_GLOB);
parseAndVerifyResult(parser);
}

private void parseAndVerifyResult(JMeterCsvParser parser) throws Exception {
final PerformanceReport result = parser.parse(reportFile);
// Verify results.
assertNotNull(result);
assertEquals("The source file contains three samples. These should all have been added to the performance report.",
3, result.size());
assertEquals("The source file contains three samples. These should all have been added to the performance report.", 3, result.size());
}
}

0 comments on commit 86a14eb

Please sign in to comment.