On 2019-05-10 10:29, mike wrote:
> So how can I update test to simulate my error?
That's a bit trickier, but the code below should give you an idea.
> Yes I am also thinking of using read(char []). I think it is more appropriate. The only thing that will be a bit tricky is to find my separator char.I have to check if we have ] then another ] so there will be many ifs.
Save yourself trouble and use a java.util.Scanner. See below.
Code:
````
import org.junit.Test;
import java.io.*;
import java.util.Objects;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class NoEol {
static final String DATA =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ " <hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
+ " <capabilities>\n"
+ " <capability>\n"
+ " urn:ietf:params:netconf:base:1.1\n"
+ " </capability>\n"
+ " <capability>\n"
+ " urn:ietf:params:ns:netconf:capability:startup:1.0\n"
+ " </capability>\n"
+ " </capabilities>\n"
+ " <session-id>4</session-id>\n"
+ " </hello>\n"
+ " ]]>]]>"; //NOTE no newline at the end
@Test
public void testBufferedReaderWithNewLine(){
try(Supplier s = Supplier.newInstance(DATA + "\n")){
globWithBufferedReader( s.input(), "]]>]]>");
}
catch ( IOException e ) {
e.printStackTrace();
}
}
@Test
public void testBufferedReaderWithoutNewLine(){
try(Supplier s = Supplier.newInstance(DATA)){
globWithBufferedReader( s.input(), "]]>]]>");
}
catch ( IOException e ) {
e.printStackTrace();
}
}
@Test
public void testScannerWithNewLine(){
try(Supplier s = Supplier.newInstance(DATA + "\n")){
globWithScanner( s.input(), "]]>]]>");
}
}
@Test
public void testScannerWithoutNewLine(){
try(Supplier s = Supplier.newInstance(DATA)){
globWithScanner( s.input(), "]]>]]>");
}
}
static void globWithBufferedReader( Reader r, String sep) throws
IOException {
BufferedReader br = new BufferedReader(r);
StringBuilder sb = new StringBuilder();
for(String line; null != (line = br.readLine()); ){
sb.append(line);
if( line.contains( sep ) ){
break;
}
}
System.out.println("Read finished: " + sb);
}
static void globWithScanner(Reader r, String sep){
Scanner sc = new Scanner(r);
sc.useDelimiter( sep );
System.out.println("Read finished: " + sc.next());
}
private static final class Supplier implements Runnable, AutoCloseable {
static final ExecutorService executor =
Executors.newCachedThreadPool();
private final String data;
private final PipedReader pipeIn;
private final PipedWriter pipeOut;
private volatile Thread thread;
Supplier( String data )
{
this.data = Objects.requireNonNull( data, "data" );
pipeOut = new PipedWriter();
try {
pipeIn = new PipedReader( pipeOut );
}
catch ( IOException x ) {
throw new AssertionError( "Could not cerate pipe", x );
}
}
public Reader input() {
return pipeIn;
}
@Override
public void close() {
if ( thread != null ) {
thread.interrupt();
thread = null;
}
}
@Override
public void run() {
thread = Thread.currentThread();
try {
pipeOut.write( data );
synchronized ( this ) {
wait();
}
}
catch ( InterruptedException e ) {
System.err.println( "Interrupted." );
Thread.currentThread().interrupt();
}
catch ( IOException e ) {
e.printStackTrace();
}
System.err.println( "Supplier exiting." );
}
public static Supplier newInstance( String data ) {
Supplier ret = new Supplier(data);
executor.submit( ret );
return ret;
}
}
}
````
--
DF.