1 16 package org.apache.commons.lang.time; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 29 public class StopWatchTest extends TestCase { 30 31 public static void main(String [] args) { 32 TestRunner.run(suite()); 33 } 34 35 public static Test suite() { 36 TestSuite suite = new TestSuite(StopWatchTest.class); 37 suite.setName("StopWatch Tests"); 38 return suite; 39 } 40 41 public StopWatchTest(String s) { 42 super(s); 43 } 44 45 public void testStopWatchSimple(){ 47 StopWatch watch = new StopWatch(); 48 watch.start(); 49 try {Thread.sleep(550);} catch (InterruptedException ex) {} 50 watch.stop(); 51 long time = watch.getTime(); 52 assertEquals(time, watch.getTime()); 53 54 assertTrue(time >= 500); 55 assertTrue(time < 700); 56 57 watch.reset(); 58 assertEquals(0, watch.getTime()); 59 } 60 61 public void testStopWatchSimpleGet(){ 62 StopWatch watch = new StopWatch(); 63 assertEquals(0, watch.getTime()); 64 assertEquals("0:00:00.000", watch.toString()); 65 66 watch.start(); 67 try {Thread.sleep(500);} catch (InterruptedException ex) {} 68 assertTrue(watch.getTime() < 2000); 69 } 70 71 public void testStopWatchSplit(){ 72 StopWatch watch = new StopWatch(); 73 watch.start(); 74 try {Thread.sleep(550);} catch (InterruptedException ex) {} 75 watch.split(); 76 long splitTime = watch.getSplitTime(); 77 try {Thread.sleep(550);} catch (InterruptedException ex) {} 78 watch.unsplit(); 79 try {Thread.sleep(550);} catch (InterruptedException ex) {} 80 watch.stop(); 81 long totalTime = watch.getTime(); 82 83 assertTrue(splitTime >= 500); 84 assertTrue(splitTime < 700); 85 assertTrue(totalTime >= 1500); 86 assertTrue(totalTime < 1900); 87 } 88 89 public void testStopWatchSuspend(){ 90 StopWatch watch = new StopWatch(); 91 watch.start(); 92 try {Thread.sleep(550);} catch (InterruptedException ex) {} 93 watch.suspend(); 94 long suspendTime = watch.getTime(); 95 try {Thread.sleep(550);} catch (InterruptedException ex) {} 96 watch.resume(); 97 try {Thread.sleep(550);} catch (InterruptedException ex) {} 98 watch.stop(); 99 long totalTime = watch.getTime(); 100 101 assertTrue(suspendTime >= 500); 102 assertTrue(suspendTime < 700); 103 assertTrue(totalTime >= 1000); 104 assertTrue(totalTime < 1300); 105 } 106 107 public void testBadStates() { 109 StopWatch watch = new StopWatch(); 110 try { 111 watch.stop(); 112 fail("Calling stop on an unstarted StopWatch should throw an exception. "); 113 } catch(IllegalStateException ise) { 114 } 116 117 try { 118 watch.stop(); 119 fail("Calling stop on an unstarted StopWatch should throw an exception. "); 120 } catch(IllegalStateException ise) { 121 } 123 124 try { 125 watch.suspend(); 126 fail("Calling suspend on an unstarted StopWatch should throw an exception. "); 127 } catch(IllegalStateException ise) { 128 } 130 131 try { 132 watch.split(); 133 fail("Calling split on a non-running StopWatch should throw an exception. "); 134 } catch(IllegalStateException ise) { 135 } 137 138 try { 139 watch.unsplit(); 140 fail("Calling unsplit on an unsplit StopWatch should throw an exception. "); 141 } catch(IllegalStateException ise) { 142 } 144 145 try { 146 watch.resume(); 147 fail("Calling resume on an unsuspended StopWatch should throw an exception. "); 148 } catch(IllegalStateException ise) { 149 } 151 152 watch.start(); 153 154 try { 155 watch.start(); 156 fail("Calling start on a started StopWatch should throw an exception. "); 157 } catch(IllegalStateException ise) { 158 } 160 161 try { 162 watch.unsplit(); 163 fail("Calling unsplit on an unsplit StopWatch should throw an exception. "); 164 } catch(IllegalStateException ise) { 165 } 167 168 try { 169 watch.getSplitTime(); 170 fail("Calling getSplitTime on an unsplit StopWatch should throw an exception. "); 171 } catch(IllegalStateException ise) { 172 } 174 175 try { 176 watch.resume(); 177 fail("Calling resume on an unsuspended StopWatch should throw an exception. "); 178 } catch(IllegalStateException ise) { 179 } 181 182 watch.stop(); 183 184 try { 185 watch.start(); 186 fail("Calling start on a stopped StopWatch should throw an exception as it needs to be reset. "); 187 } catch(IllegalStateException ise) { 188 } 190 191 } 192 193 } 194 | Popular Tags |