NetworkedCounter implements Counter { public void flush() { if (lastFlushCount == count) { return; } try { String fullUrl = String.format("%s/setcounter?counterName=%s&stageName=%s&create=%s&value=%d", url, URLEncoder.encode(counterName, "UTF-8"), URLEncoder.encode(stageName, "UTF-8"), URLEncoder.encode(instance, "UTF-8"), count); connectUrl(fullUrl); lastFlushCount = count; } catch (Exception e) { } } NetworkedCounter(String counterName, String stageName, String instance, String url); void increment(); void incrementBy(int value); void flush(); void connectUrl(String url);  }
@Test public void testFlush() { MockCounter counter = new MockCounter("b", "c", "d", "a"); counter.flush(); assertEquals("a/setcounter?counterName=b&stageName=c&create=d&value=0", counter.connectedUrl); counter.connectedUrl = null; counter.flush(); assertEquals(null, counter.connectedUrl); counter.increment(); counter.flush(); assertEquals("a/setcounter?counterName=b&stageName=c&create=d&value=1", counter.connectedUrl); counter.incrementBy(6); counter.flush(); assertEquals("a/setcounter?counterName=b&stageName=c&create=d&value=7", counter.connectedUrl); counter.connectedUrl = null; counter.flush(); assertEquals(null, counter.connectedUrl); }