Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Blazemeter/jmeter-bzm-plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
undera committed Nov 7, 2017
2 parents d8c3592 + 57716fc commit b288ed4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ public int getPos() {

@Override
public int read() throws IOException {
pos++;
return super.read();
int res = super.read();
if (res <= Byte.MAX_VALUE) {
pos++;
} else {
byte[] buf = new String(new char[]{(char) res}).getBytes();
pos += buf.length;
}
return res;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.blazemeter.csv;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.*;

public class RandomBufferedReader extends BufferedReader {

Expand All @@ -21,7 +18,48 @@ public void seek(long pos) throws IOException {

@Override
public int read() throws IOException {
return raf.read();
return readUtf8Char(raf);
}

public static int readUtf8Char(final DataInput dataInput) throws IOException {
int char1, char2, char3;

try {
char1 = dataInput.readByte() & 0xff;
switch (char1 >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
/* 0xxxxxxx*/
return (char) char1;
case 12:
case 13:
/* 110x xxxx 10xx xxxx*/
char2 = dataInput.readByte() & 0xff;
if ((char2 & 0xC0) != 0x80) {
throw new UTFDataFormatException("malformed input");
}
return (char) (((char1 & 0x1F) << 6) | (char2 & 0x3F));
case 14:
/* 1110 xxxx 10xx xxxx 10xx xxxx */
char2 = dataInput.readByte() & 0xff;
char3 = dataInput.readByte() & 0xff;
if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) {
throw new UTFDataFormatException("malformed input");
}
return (char) (((char1 & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
default:
/* 10xx xxxx, 1111 xxxx */
throw new UTFDataFormatException("malformed input");
}
} catch (EOFException ex) {
return -1;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blazemeter.csv;

import com.mchange.util.AssertException;
import kg.apc.emulators.TestJMeterUtils;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -67,6 +68,53 @@ public void testReadWithHeaderAndRepeat() throws Exception {
assertEquals("1393227741258", reader.getNextRecord()[0]);
}

@Test
public void testMultiBytes() throws Exception {
String path = this.getClass().getResource("/text.csv").getPath();

RandomCSVReader reader = new RandomCSVReader(path, "UTF-8", ";", true, false, false, false);

assertEquals("[firstname, lastname, street, city]",
Arrays.toString(reader.getHeader()));

String[] record;

for (int i = 0; i < 4; i++) {
assertTrue(reader.hasNextRecord());
record = reader.getNextRecord();
assertRecord(record);
}

assertFalse(reader.hasNextRecord());
}

private void assertRecord(String[] record) {
switch (record[0]) {
case "Hänsel" :
assertEquals("Mustermann", record[1]);
assertEquals("Einbahnstraße", record[2]);
assertEquals("Hamburg", record[3]);
break;
case "André" :
assertEquals("Lecompte", record[1]);
assertEquals("Rue du marché", record[2]);
assertEquals("Moÿ-de-l'Aisne", record[3]);
break;
case "Ἀλέξανδρος" :
assertEquals("Павлов", record[1]);
assertEquals("Большая Пироговская улица", record[2]);
assertEquals("Москва́", record[3]);
break;
case "בנימין" : // idea shows incorrect this line. firstname is real Benjamin -> 'בנימין'
assertEquals("يعقوب", record[1]);
assertEquals("Street", record[2]);
assertEquals("Megapolis", record[3]);
break;
default:
throw new AssertException("No such firstname in csv file " + record[0]);
}
}

@Test
public void testRecordsCount() throws Exception {
String path = this.getClass().getResource("/JMeterCsvResults.csv").getPath();
Expand Down
5 changes: 5 additions & 0 deletions random-csv-data-set/src/test/resources/text.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
firstname;lastname;street;city
Hänsel;Mustermann;Einbahnstraße;Hamburg
André;Lecompte;Rue du marché;Moÿ-de-l'Aisne
Ἀλέξανδρος;Павлов;Большая Пироговская улица;Москва́
בנימין;يعقوب;Street;Megapolis

0 comments on commit b288ed4

Please sign in to comment.