KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > StopWatchTests


1  
2 /*
3  * Copyright 2002-2005 the original author or authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.springframework.util;
19
20 import junit.framework.TestCase;
21
22 /**
23  * @author Rod Johnson
24  */

25 public class StopWatchTests extends TestCase {
26
27     /**
28      * Are timings off in JUnit?
29      */

30     public void testValidUsage() throws Exception JavaDoc {
31         StopWatch sw = new StopWatch();
32         long int1 = 166L;
33         long int2 = 45L;
34         String JavaDoc name1 = "Task 1";
35         String JavaDoc 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         // TODO are timings off in JUnit? Why do these assertions sometimes fail
45
// under both Ant and Eclipse?
46

47         //assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1);
48
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor);
49
sw.start(name2);
50         Thread.sleep(int2);
51         sw.stop();
52         //assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1 + int2);
53
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + int2 + fudgeFactor);
54

55         assertTrue(sw.getTaskCount() == 2);
56         String JavaDoc 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 JavaDoc {
68         StopWatch sw = new StopWatch();
69         sw.setKeepTaskList(false);
70         long int1 = 166L;
71         long int2 = 45L;
72         String JavaDoc name1 = "Task 1";
73         String JavaDoc 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         // TODO are timings off in JUnit? Why do these assertions sometimes fail
83
// under both Ant and Eclipse?
84

85         //assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1);
86
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + fudgeFactor);
87
sw.start(name2);
88         Thread.sleep(int2);
89         sw.stop();
90         //assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() >= int1 + int2);
91
//assertTrue("Unexpected timing " + sw.getTotalTime(), sw.getTotalTime() <= int1 + int2 + fudgeFactor);
92

93         assertTrue(sw.getTaskCount() == 2);
94         String JavaDoc pp = sw.prettyPrint();
95         assertTrue(pp.indexOf("kept") != -1);
96         sw.toString();
97     
98         try {
99             sw.getTaskInfo();
100             fail();
101         }
102         catch (UnsupportedOperationException JavaDoc ex) {
103             // Ok
104
}
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 JavaDoc ex) {
114             // Ok
115
}
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 JavaDoc ex) {
125             // Ok
126
}
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 JavaDoc ex) {
140             // Ok
141
}
142     }
143
144 }
145
Popular Tags