1 | /* |
2 | * Copyright (c) 2001-2009, Jean Tessier |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * |
12 | * * Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * |
16 | * * Neither the name of Jean Tessier nor the names of his contributors |
17 | * may be used to endorse or promote products derived from this software |
18 | * without specific prior written permission. |
19 | * |
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR |
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | */ |
32 | |
33 | package com.jeantessier.metrics; |
34 | |
35 | import junit.framework.*; |
36 | |
37 | public class TestMetricsComparator extends TestCase { |
38 | public void testSortOn() { |
39 | MetricsComparator c = new MetricsComparator("foo", StatisticalMeasurement.DISPOSE_IGNORE); |
40 | |
41 | assertEquals("c.Name()", "foo", c.getName()); |
42 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
43 | |
44 | c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE); |
45 | |
46 | assertEquals("c.Name()", "foo", c.getName()); |
47 | assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection()); |
48 | |
49 | c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE); |
50 | |
51 | assertEquals("c.Name()", "foo", c.getName()); |
52 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
53 | |
54 | c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE); |
55 | |
56 | assertEquals("c.Name()", "bar", c.getName()); |
57 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
58 | |
59 | c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE); |
60 | |
61 | assertEquals("c.Name()", "bar", c.getName()); |
62 | assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection()); |
63 | |
64 | c.sortOn("baz", StatisticalMeasurement.DISPOSE_IGNORE); |
65 | |
66 | assertEquals("c.Name()", "baz", c.getName()); |
67 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
68 | |
69 | c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE); |
70 | |
71 | assertEquals("c.Name()", "foobar", c.getName()); |
72 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
73 | |
74 | c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE); |
75 | |
76 | assertEquals("c.Name()", "foobar", c.getName()); |
77 | assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection()); |
78 | |
79 | c.sortOn("foobar", StatisticalMeasurement.DISPOSE_MINIMUM); |
80 | |
81 | assertEquals("c.Name()", "foobar", c.getName()); |
82 | assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection()); |
83 | } |
84 | |
85 | public void testCompareTo() { |
86 | Metrics m1 = new Metrics("m1"); |
87 | Metrics m2 = new Metrics("m2"); |
88 | |
89 | m1.track("foo", new CounterMeasurement(null, null, null)); |
90 | m1.track("bar", new CounterMeasurement(null, null, null)); |
91 | m1.track("baz", new CounterMeasurement(null, null, null)); |
92 | m2.track("foo", new CounterMeasurement(null, null, null)); |
93 | m2.track("bar", new CounterMeasurement(null, null, null)); |
94 | m2.track("baz", new CounterMeasurement(null, null, null)); |
95 | |
96 | m1.addToMeasurement("foo", 1); |
97 | m1.addToMeasurement("bar", 2); |
98 | m1.addToMeasurement("baz", 3); |
99 | m2.addToMeasurement("foo", 3); |
100 | m2.addToMeasurement("bar", 2); |
101 | m2.addToMeasurement("baz", 1); |
102 | |
103 | MetricsComparator c1 = new MetricsComparator("foo"); |
104 | MetricsComparator c2 = new MetricsComparator("bar"); |
105 | MetricsComparator c3 = new MetricsComparator("baz"); |
106 | |
107 | assertTrue(c1.compare(m1, m2) < 0); |
108 | assertTrue(c2.compare(m1, m2) == 0); |
109 | assertTrue(c3.compare(m1, m2) > 0); |
110 | |
111 | c1.reverse(); |
112 | c2.reverse(); |
113 | c3.reverse(); |
114 | |
115 | assertTrue(c1.compare(m1, m2) > 0); |
116 | assertTrue(c2.compare(m1, m2) == 0); |
117 | assertTrue(c3.compare(m1, m2) < 0); |
118 | } |
119 | |
120 | public void testCompareNaN() { |
121 | Metrics m1 = new Metrics("m1"); |
122 | Metrics m2 = new Metrics("m2"); |
123 | |
124 | m1.track("foo", new CounterMeasurement(null, null, null)); |
125 | m2.track("foo", new CounterMeasurement(null, null, null)); |
126 | m1.track("bar", new CounterMeasurement(null, null, null)); |
127 | m2.track("bar", new CounterMeasurement(null, null, null)); |
128 | m1.track("baz", new CounterMeasurement(null, null, null)); |
129 | m2.track("baz", new CounterMeasurement(null, null, null)); |
130 | |
131 | m1.addToMeasurement("foo", Double.NaN); |
132 | m2.addToMeasurement("foo", Double.NaN); |
133 | m1.addToMeasurement("bar", Double.NaN); |
134 | m2.addToMeasurement("bar", 1); |
135 | m1.addToMeasurement("baz", 1); |
136 | m2.addToMeasurement("baz", Double.NaN); |
137 | |
138 | MetricsComparator c1 = new MetricsComparator("foo"); |
139 | MetricsComparator c2 = new MetricsComparator("bar"); |
140 | MetricsComparator c3 = new MetricsComparator("baz"); |
141 | |
142 | assertTrue(c1.compare(m1, m2) == 0); |
143 | assertTrue(c2.compare(m1, m2) > 0); |
144 | assertTrue(c3.compare(m1, m2) < 0); |
145 | |
146 | c1.reverse(); |
147 | c2.reverse(); |
148 | c3.reverse(); |
149 | |
150 | assertTrue(c1.compare(m1, m2) == 0); |
151 | assertTrue(c2.compare(m1, m2) > 0); |
152 | assertTrue(c3.compare(m1, m2) < 0); |
153 | } |
154 | } |