From 40689b7e6e2803153ceacfca02aae5b3c9ff9f27 Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Fri, 28 Oct 2011 13:13:05 +0100 Subject: [PATCH] Avoid NoSuchElementException if the connection stalls. --- .../plugins/bluetooth/BluetoothTest.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java b/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java index ba8452cc3c..8b24fb1a01 100644 --- a/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java +++ b/test/net/sf/briar/plugins/bluetooth/BluetoothTest.java @@ -19,12 +19,16 @@ abstract class BluetoothTest { out.println(CHALLENGE); System.out.println("Sent challenge: " + CHALLENGE); Scanner in = new Scanner(s.getInputStream()); - String response = in.nextLine(); - System.out.println("Received response: " + response); - if(BluetoothClientTest.RESPONSE.equals(response)) { - System.out.println("Correct response"); + if(in.hasNextLine()) { + String response = in.nextLine(); + System.out.println("Received response: " + response); + if(BluetoothClientTest.RESPONSE.equals(response)) { + System.out.println("Correct response"); + } else { + System.out.println("Incorrect response"); + } } else { - System.out.println("Incorrect response"); + System.out.println("No response"); } s.dispose(true); } catch(IOException e) { @@ -36,14 +40,18 @@ abstract class BluetoothTest { void receiveChallengeAndSendResponse(StreamTransportConnection s) { try { Scanner in = new Scanner(s.getInputStream()); - String challenge = in.nextLine(); - System.out.println("Received challenge: " + challenge); - if(BluetoothServerTest.CHALLENGE.equals(challenge)) { - PrintStream out = new PrintStream(s.getOutputStream()); - out.println(RESPONSE); - System.out.println("Sent response: " + RESPONSE); + if(in.hasNextLine()) { + String challenge = in.nextLine(); + System.out.println("Received challenge: " + challenge); + if(BluetoothServerTest.CHALLENGE.equals(challenge)) { + PrintStream out = new PrintStream(s.getOutputStream()); + out.println(RESPONSE); + System.out.println("Sent response: " + RESPONSE); + } else { + System.out.println("Incorrect challenge"); + } } else { - System.out.println("Incorrect challenge"); + System.out.println("No challenge"); } s.dispose(true); } catch(IOException e) { -- GitLab