### Question:
Pinger { public void ping() throws IOException { pingCount++; StringBuffer uri = new StringBuffer("http: for (String key : usageDataMap.keySet()) { uri.append(key); uri.append("="); uri.append(usageDataMap.get(key)); uri.append("+"); } if ( pingCount == 0 ) { uri.append( "p=-1" ); pingCount++; } else { uri.append( "p=" ).append( pingCount ); } URL url = new URL(uri.toString()); URLConnection con = url.openConnection(); con.setDoInput(true); con.setDoOutput(false); con.setUseCaches(false); con.connect(); con.getInputStream(); } Pinger( String address, Map<String, String> usageDataMap, boolean crashPing ); void ping(); Integer getPingCount();  }### Answer:
@Test public void shouldPingServer() { final String hostURL = hostname + ":" + server.getServicePort(); final Map<String,String> udcFields = new HashMap<String, String>(); udcFields.put("id", EXPECTED_STORE_ID); udcFields.put("v", EXPTECTED_KERNEL_VERSION); Pinger p = new Pinger( hostURL, udcFields, false ); Exception thrownException = null; try { p.ping(); } catch (IOException e) { thrownException = e; e.printStackTrace(); } assertThat(thrownException, nullValue()); Map<String, String> actualQueryMap = handler.getQueryMap(); assertThat(actualQueryMap, notNullValue()); assertThat(actualQueryMap.get("id"), is(EXPECTED_STORE_ID)); }
@Test public void normalPingSequenceShouldBeOneThenTwoThenThreeEtc() throws Exception { int[] expectedSequence = { 1, 2, 3, 4 }; final String hostURL = hostname + ":" + server.getServicePort(); final Map<String, String> udcFields = new HashMap<String, String>(); Pinger p = new Pinger( hostURL, udcFields, false ); for ( int i = 0; i < expectedSequence.length; i++ ) { p.ping(); int count = Integer.parseInt( handler.getQueryMap().get( "p" ) ); assertEquals( expectedSequence[i], count ); } }
@Test public void crashPingSequenceShouldBeMinusOneThenTwoThenThreeEtc() throws Exception { int[] expectedSequence = { -1, 2, 3, 4 }; final String hostURL = hostname + ":" + server.getServicePort(); final Map<String, String> udcFields = new HashMap<String, String>(); Pinger p = new Pinger( hostURL, udcFields, true ); for ( int i = 0; i < expectedSequence.length; i++ ) { p.ping(); int count = Integer.parseInt( handler.getQueryMap().get( "p" ) ); assertEquals( expectedSequence[i], count ); } }