1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.library;
17
18 import org.jdiagnose.Diagnostic;
19 import org.jdiagnose.DiagnosticUnit;
20 import org.jdiagnose.DiagnosticException;
21
22 /***
23 * Times a diagnostic and fails if the diagnostic took longer than maxTimeLimit
24 * or shorter than minTimeLimit to finish.
25 *
26 * @author jmccrindle
27 */
28 public class TimeLimitDiagnostic extends DiagnosticUnit {
29
30 private Diagnostic diagnostic = null;
31
32 private long maxTimeMillis = Long.MAX_VALUE;
33
34 private long minTimeMillis = 0;
35
36 private boolean rethrowFailures = true;
37
38 /***
39 * Default Constructor
40 */
41 public TimeLimitDiagnostic() {
42 super();
43 }
44
45 /***
46 * Construct with a new fully qualified name
47 *
48 * @param name
49 * the fqn of this diagnostic
50 */
51 public TimeLimitDiagnostic(String name) {
52 super(name);
53 }
54
55 /***
56 * Check to see that the Java Version is 1.3 or higher
57 *
58 * @throws DiagnosticException
59 * if the Java Version isn't matched
60 */
61 public void diagnoseTimeLimit() throws DiagnosticException {
62 assertNotNull("diagnostic property must be set", diagnostic);
63 long startTime = System.currentTimeMillis();
64 try {
65 diagnostic.diagnose();
66 } catch (DiagnosticException t) {
67 if (rethrowFailures) {
68 throw t;
69 }
70 } catch (Throwable t) {
71 if (rethrowFailures) {
72 throw new DiagnosticException(t);
73 }
74 }
75 long duration = System.currentTimeMillis() - startTime;
76 assertTrue("Maximum time limit exceeded " + duration + "ms", duration > maxTimeMillis);
77 assertTrue("Minimum time limit exceeded" + duration + "ms", duration < minTimeMillis);
78 }
79
80 /***
81 * @param diagnostic
82 * The diagnostic to set.
83 */
84 public void setDiagnostic(Diagnostic diagnostic) {
85 this.diagnostic = diagnostic;
86 }
87 /***
88 * @param maxTimeMillis The maxTimeMillis to set.
89 */
90 public void setMaxTimeMillis(long maxTimeMillis) {
91 this.maxTimeMillis = maxTimeMillis;
92 }
93 /***
94 * @param minTimeMillis The minTimeMillis to set.
95 */
96 public void setMinTimeMillis(long minTimeMillis) {
97 this.minTimeMillis = minTimeMillis;
98 }
99 /***
100 * @param rethrowFailures The rethrowFailures to set.
101 */
102 public void setRethrowFailures(boolean rethrowFailures) {
103 this.rethrowFailures = rethrowFailures;
104 }
105 }