simplify special character handling intregration test with Stream.readAll()

This commit is contained in:
Kris Heid 2021-03-04 13:28:52 +01:00
parent f4f41efbc3
commit 597102ab83
1 changed files with 2 additions and 36 deletions

View File

@ -7,6 +7,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import se.vidstige.jadb.JadbConnection;
import se.vidstige.jadb.JadbDevice;
import se.vidstige.jadb.Stream;
import java.io.BufferedReader;
import java.io.IOException;
@ -60,41 +61,6 @@ public class ExecuteCmdTests {
@Test
public void testExecuteWithSpecialChars() throws Exception {
InputStream response = jadbDevice.execute("echo", input);
ResponseReader responseReader = new ResponseReader(response);
responseReader.start();
responseReader.join(1000L);
String ret = responseReader.output;
//remove newline
ret = ret.replaceAll("\n$", "");
Assert.assertEquals(input, ret);
Assert.assertEquals(input, Stream.readAll(response, StandardCharsets.UTF_8).replaceAll("\n$", ""));
}
private class ResponseReader extends Thread {
public String output;
private InputStream response;
ResponseReader(InputStream response) {
super();
this.response = response;
}
@Override
public void run() {
try {
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader
(response, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}
output = textBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}