1 2 17 18 package org.springframework.util; 19 20 import junit.framework.TestCase; 21 22 25 public class StopWatchTests extends TestCase { 26 27 30 public void testValidUsage() throws Exception { 31 StopWatch sw = new StopWatch(); 32 long int1 = 166L; 33 long int2 = 45L; 34 String name1 = "Task 1"; 35 String name2 = "Task 2"; 36 37 long fudgeFactor = 5L; 38 assertFalse(sw.isRunning()); 39 sw.start(name1); 40 Thread.sleep(int1); 41 assertTrue(sw.isRunning()); 42 sw.stop(); 43 44 47 sw.start(name2); 50 Thread.sleep(int2); 51 sw.stop(); 52 55 assertTrue(sw.getTaskCount() == 2); 56 String pp = sw.prettyPrint(); 57 assertTrue(pp.indexOf(name1) != -1); 58 assertTrue(pp.indexOf(name2) != -1); 59 60 StopWatch.TaskInfo[] tasks = sw.getTaskInfo(); 61 assertTrue(tasks.length == 2); 62 assertTrue(tasks[0].getTaskName().equals(name1)); 63 assertTrue(tasks[1].getTaskName().equals(name2)); 64 sw.toString(); 65 } 66 67 public void testValidUsageNotKeepingTaskList() throws Exception { 68 StopWatch sw = new StopWatch(); 69 sw.setKeepTaskList(false); 70 long int1 = 166L; 71 long int2 = 45L; 72 String name1 = "Task 1"; 73 String name2 = "Task 2"; 74 75 long fudgeFactor = 5L; 76 assertFalse(sw.isRunning()); 77 sw.start(name1); 78 Thread.sleep(int1); 79 assertTrue(sw.isRunning()); 80 sw.stop(); 81 82 85 sw.start(name2); 88 Thread.sleep(int2); 89 sw.stop(); 90 93 assertTrue(sw.getTaskCount() == 2); 94 String pp = sw.prettyPrint(); 95 assertTrue(pp.indexOf("kept") != -1); 96 sw.toString(); 97 98 try { 99 sw.getTaskInfo(); 100 fail(); 101 } 102 catch (UnsupportedOperationException ex) { 103 } 105 } 106 107 public void testFailureToStartBeforeGettingTimings() { 108 StopWatch sw = new StopWatch(); 109 try { 110 sw.getLastTaskTimeMillis(); 111 fail("Can't get last interval if no tests run"); 112 } 113 catch (IllegalStateException ex) { 114 } 116 } 117 118 public void testFailureToStartBeforeStop() { 119 StopWatch sw = new StopWatch(); 120 try { 121 sw.stop(); 122 fail("Can't stop without starting"); 123 } 124 catch (IllegalStateException ex) { 125 } 127 } 128 129 public void testRejectsStartTwice() { 130 StopWatch sw = new StopWatch(); 131 try { 132 sw.start(""); 133 sw.stop(); 134 sw.start(""); 135 assertTrue(sw.isRunning()); 136 sw.start(""); 137 fail("Can't start twice"); 138 } 139 catch (IllegalStateException ex) { 140 } 142 } 143 144 } 145 | Popular Tags |