KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > TestMetricsComparator


1 /*
2  * Copyright (c) 2001-2005, 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 java.util.*;
36
37 import junit.framework.*;
38
39 public class TestMetricsComparator extends TestCase {
40     public void testSortOn() {
41         MetricsComparator c = new MetricsComparator("foo", StatisticalMeasurement.DISPOSE_IGNORE);
42
43         assertEquals("c.Name()", "foo", c.getName());
44         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
45
46         c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE);
47         
48         assertEquals("c.Name()", "foo", c.getName());
49         assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection());
50         
51         c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE);
52         
53         assertEquals("c.Name()", "foo", c.getName());
54         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
55
56         c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE);
57
58         assertEquals("c.Name()", "bar", c.getName());
59         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
60
61         c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE);
62
63         assertEquals("c.Name()", "bar", c.getName());
64         assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection());
65
66         c.sortOn("baz", StatisticalMeasurement.DISPOSE_IGNORE);
67
68         assertEquals("c.Name()", "baz", c.getName());
69         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
70
71         c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE);
72
73         assertEquals("c.Name()", "foobar", c.getName());
74         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
75
76         c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE);
77
78         assertEquals("c.Name()", "foobar", c.getName());
79         assertEquals("c.Direction()", MetricsComparator.DESCENDING, c.getDirection());
80
81         c.sortOn("foobar", StatisticalMeasurement.DISPOSE_MINIMUM);
82
83         assertEquals("c.Name()", "foobar", c.getName());
84         assertEquals("c.Direction()", MetricsComparator.ASCENDING, c.getDirection());
85     }
86     
87     public void testCompareTo() {
88         Metrics m1 = new Metrics("m1");
89         Metrics m2 = new Metrics("m2");
90
91         m1.track("foo", new CounterMeasurement(null, null, null));
92         m1.track("bar", new CounterMeasurement(null, null, null));
93         m1.track("baz", new CounterMeasurement(null, null, null));
94         m2.track("foo", new CounterMeasurement(null, null, null));
95         m2.track("bar", new CounterMeasurement(null, null, null));
96         m2.track("baz", new CounterMeasurement(null, null, null));
97         
98         m1.addToMeasurement("foo", 1);
99         m1.addToMeasurement("bar", 2);
100         m1.addToMeasurement("baz", 3);
101         m2.addToMeasurement("foo", 3);
102         m2.addToMeasurement("bar", 2);
103         m2.addToMeasurement("baz", 1);
104         
105         MetricsComparator c1 = new MetricsComparator("foo");
106         MetricsComparator c2 = new MetricsComparator("bar");
107         MetricsComparator c3 = new MetricsComparator("baz");
108
109         assertTrue(c1.compare(m1, m2) < 0);
110         assertTrue(c2.compare(m1, m2) == 0);
111         assertTrue(c3.compare(m1, m2) > 0);
112
113         c1.reverse();
114         c2.reverse();
115         c3.reverse();
116
117         assertTrue(c1.compare(m1, m2) > 0);
118         assertTrue(c2.compare(m1, m2) == 0);
119         assertTrue(c3.compare(m1, m2) < 0);
120     }
121     
122     public void testCompareNaN() {
123         Metrics m1 = new Metrics("m1");
124         Metrics m2 = new Metrics("m2");
125
126         m1.track("foo", new CounterMeasurement(null, null, null));
127         m2.track("foo", new CounterMeasurement(null, null, null));
128         m1.track("bar", new CounterMeasurement(null, null, null));
129         m2.track("bar", new CounterMeasurement(null, null, null));
130         m1.track("baz", new CounterMeasurement(null, null, null));
131         m2.track("baz", new CounterMeasurement(null, null, null));
132         
133         m1.addToMeasurement("foo", Double.NaN);
134         m2.addToMeasurement("foo", Double.NaN);
135         m1.addToMeasurement("bar", Double.NaN);
136         m2.addToMeasurement("bar", 1);
137         m1.addToMeasurement("baz", 1);
138         m2.addToMeasurement("baz", Double.NaN);
139         
140         MetricsComparator c1 = new MetricsComparator("foo");
141         MetricsComparator c2 = new MetricsComparator("bar");
142         MetricsComparator c3 = new MetricsComparator("baz");
143
144         assertTrue(c1.compare(m1, m2) == 0);
145         assertTrue(c2.compare(m1, m2) > 0);
146         assertTrue(c3.compare(m1, m2) < 0);
147
148         c1.reverse();
149         c2.reverse();
150         c3.reverse();
151
152         assertTrue(c1.compare(m1, m2) == 0);
153         assertTrue(c2.compare(m1, m2) > 0);
154         assertTrue(c3.compare(m1, m2) < 0);
155     }
156 }
157
Popular Tags