diff --git a/src/java/org/apache/commons/lang/time/StopWatch.java b/src/java/org/apache/commons/lang/time/StopWatch.java index 607e9b6da..0f0786a36 100644 --- a/src/java/org/apache/commons/lang/time/StopWatch.java +++ b/src/java/org/apache/commons/lang/time/StopWatch.java @@ -115,7 +115,9 @@ public class StopWatch { if(this.runningState != STATE_RUNNING && this.runningState != STATE_SUSPENDED) { throw new IllegalStateException("Stopwatch is not running. "); } - stopTime = System.currentTimeMillis(); + if(this.runningState == STATE_RUNNING) { + stopTime = System.currentTimeMillis(); + } this.runningState = STATE_STOPPED; } diff --git a/src/test/org/apache/commons/lang/time/StopWatchTest.java b/src/test/org/apache/commons/lang/time/StopWatchTest.java index 46f063aa3..bb7b41244 100644 --- a/src/test/org/apache/commons/lang/time/StopWatchTest.java +++ b/src/test/org/apache/commons/lang/time/StopWatchTest.java @@ -108,6 +108,18 @@ public class StopWatchTest extends TestCase { assertTrue(totalTime < 1300); } + public void testLang315() { + StopWatch watch = new StopWatch(); + watch.start(); + try {Thread.sleep(200);} catch (InterruptedException ex) {} + watch.suspend(); + long suspendTime = watch.getTime(); + try {Thread.sleep(200);} catch (InterruptedException ex) {} + watch.stop(); + long totalTime = watch.getTime(); + assertTrue( suspendTime == totalTime ); + } + // test bad states public void testBadStates() { StopWatch watch = new StopWatch();