1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.util; |
---|
16 | |
---|
17 | import static org.junit.Assert.*; |
---|
18 | |
---|
19 | import org.junit.Test; |
---|
20 | |
---|
21 | /** |
---|
22 | * |
---|
23 | */ |
---|
24 | public class StopWatchTest { |
---|
25 | |
---|
26 | /** |
---|
27 | * |
---|
28 | */ |
---|
29 | @Test |
---|
30 | public void test_01() throws Exception { |
---|
31 | StopWatch stopWatch = new StopWatch(); |
---|
32 | |
---|
33 | String id = "id1"; |
---|
34 | stopWatch.start(id); |
---|
35 | Thread.sleep(100); |
---|
36 | stopWatch.stop(id); |
---|
37 | |
---|
38 | assertTrue((90 < stopWatch.getDuration(id)) && (stopWatch.getDuration(id) < 110)); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * |
---|
43 | */ |
---|
44 | @Test |
---|
45 | public void test_02() throws Exception { |
---|
46 | StopWatch stopWatch = new StopWatch(); |
---|
47 | |
---|
48 | String id; |
---|
49 | for (int i = 0; i < 10; i++) { |
---|
50 | id = "id" + i; |
---|
51 | stopWatch.start(id); |
---|
52 | } |
---|
53 | |
---|
54 | for (int i = 0; i < 10; i++) { |
---|
55 | id = "id" + i; |
---|
56 | stopWatch.stop(id); |
---|
57 | } |
---|
58 | |
---|
59 | for (int i = 0; i < 10; i++) { |
---|
60 | id = "id" + i; |
---|
61 | assertTrue(stopWatch.getDuration(id) >= 0); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * |
---|
67 | */ |
---|
68 | @Test(expected=IllegalArgumentException.class) |
---|
69 | public void test_03() throws Exception { |
---|
70 | StopWatch stopWatch = new StopWatch(); |
---|
71 | |
---|
72 | stopWatch.stop("id1"); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * |
---|
77 | */ |
---|
78 | @Test(expected=IllegalArgumentException.class) |
---|
79 | public void test_04() throws Exception { |
---|
80 | StopWatch stopWatch = new StopWatch(); |
---|
81 | |
---|
82 | stopWatch.getDuration("id1"); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * |
---|
87 | */ |
---|
88 | @Test(expected=IllegalStateException.class) |
---|
89 | public void test_05() throws Exception { |
---|
90 | StopWatch stopWatch = new StopWatch(); |
---|
91 | |
---|
92 | String id = "id1"; |
---|
93 | stopWatch.start(id); |
---|
94 | stopWatch.stop(id); |
---|
95 | |
---|
96 | stopWatch.stop(id); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * |
---|
101 | */ |
---|
102 | @Test(expected=IllegalStateException.class) |
---|
103 | public void test_06() throws Exception { |
---|
104 | StopWatch stopWatch = new StopWatch(); |
---|
105 | |
---|
106 | String id = "id1"; |
---|
107 | stopWatch.start(id); |
---|
108 | stopWatch.getDuration(id); |
---|
109 | |
---|
110 | stopWatch.stop(id); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * |
---|
115 | */ |
---|
116 | @Test(expected=IllegalStateException.class) |
---|
117 | public void test_07() throws Exception { |
---|
118 | StopWatch stopWatch = new StopWatch(); |
---|
119 | |
---|
120 | String id = "id1"; |
---|
121 | stopWatch.start(id); |
---|
122 | |
---|
123 | stopWatch.start(id); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * |
---|
129 | */ |
---|
130 | @Test |
---|
131 | public void test_08() throws Exception { |
---|
132 | StopWatch stopWatch = new StopWatch(); |
---|
133 | |
---|
134 | String id; |
---|
135 | // start 0 - 4 |
---|
136 | for (int i = 0; i < 5; i++) { |
---|
137 | id = "id" + i; |
---|
138 | stopWatch.start(id); |
---|
139 | } |
---|
140 | |
---|
141 | Thread.sleep(10); |
---|
142 | |
---|
143 | // stop 0 - 2 |
---|
144 | for (int i = 0; i < 3; i++) { |
---|
145 | id = "id" + i; |
---|
146 | stopWatch.stop(id); |
---|
147 | } |
---|
148 | |
---|
149 | // start 5 - 7 |
---|
150 | for (int i = 5; i < 8; i++) { |
---|
151 | id = "id" + i; |
---|
152 | stopWatch.start(id); |
---|
153 | } |
---|
154 | |
---|
155 | Thread.sleep(10); |
---|
156 | |
---|
157 | // stop 3 - 6 |
---|
158 | for (int i = 3; i < 7; i++) { |
---|
159 | id = "id" + i; |
---|
160 | stopWatch.stop(id); |
---|
161 | } |
---|
162 | |
---|
163 | // start 8 - 9 |
---|
164 | for (int i = 8; i < 10; i++) { |
---|
165 | id = "id" + i; |
---|
166 | stopWatch.start(id); |
---|
167 | } |
---|
168 | |
---|
169 | Thread.sleep(10); |
---|
170 | |
---|
171 | // start 0 - 6 |
---|
172 | for (int i = 0; i < 7; i++) { |
---|
173 | id = "id" + i; |
---|
174 | stopWatch.start(id); |
---|
175 | } |
---|
176 | |
---|
177 | Thread.sleep(10); |
---|
178 | |
---|
179 | // stop all |
---|
180 | for (int i = 2; i < 7; i++) { |
---|
181 | id = "id" + i; |
---|
182 | stopWatch.stop(id); |
---|
183 | } |
---|
184 | |
---|
185 | // start all |
---|
186 | for (int i = 2; i < 7; i++) { |
---|
187 | id = "id" + i; |
---|
188 | stopWatch.start(id); |
---|
189 | } |
---|
190 | |
---|
191 | Thread.sleep(10); |
---|
192 | |
---|
193 | // stop all and ensure, that durations are returned |
---|
194 | for (int i = 2; i < 7; i++) { |
---|
195 | id = "id" + i; |
---|
196 | assertTrue(stopWatch.getDuration(id) >= 10); |
---|
197 | } |
---|
198 | } |
---|
199 | } |
---|