001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import junit.framework.*;
036:
037: public class TestMetricsComparator extends TestCase {
038: public void testSortOn() {
039: MetricsComparator c = new MetricsComparator("foo",
040: StatisticalMeasurement.DISPOSE_IGNORE);
041:
042: assertEquals("c.Name()", "foo", c.getName());
043: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
044: .getDirection());
045:
046: c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE);
047:
048: assertEquals("c.Name()", "foo", c.getName());
049: assertEquals("c.Direction()", MetricsComparator.DESCENDING, c
050: .getDirection());
051:
052: c.sortOn("foo", StatisticalMeasurement.DISPOSE_IGNORE);
053:
054: assertEquals("c.Name()", "foo", c.getName());
055: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
056: .getDirection());
057:
058: c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE);
059:
060: assertEquals("c.Name()", "bar", c.getName());
061: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
062: .getDirection());
063:
064: c.sortOn("bar", StatisticalMeasurement.DISPOSE_IGNORE);
065:
066: assertEquals("c.Name()", "bar", c.getName());
067: assertEquals("c.Direction()", MetricsComparator.DESCENDING, c
068: .getDirection());
069:
070: c.sortOn("baz", StatisticalMeasurement.DISPOSE_IGNORE);
071:
072: assertEquals("c.Name()", "baz", c.getName());
073: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
074: .getDirection());
075:
076: c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE);
077:
078: assertEquals("c.Name()", "foobar", c.getName());
079: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
080: .getDirection());
081:
082: c.sortOn("foobar", StatisticalMeasurement.DISPOSE_IGNORE);
083:
084: assertEquals("c.Name()", "foobar", c.getName());
085: assertEquals("c.Direction()", MetricsComparator.DESCENDING, c
086: .getDirection());
087:
088: c.sortOn("foobar", StatisticalMeasurement.DISPOSE_MINIMUM);
089:
090: assertEquals("c.Name()", "foobar", c.getName());
091: assertEquals("c.Direction()", MetricsComparator.ASCENDING, c
092: .getDirection());
093: }
094:
095: public void testCompareTo() {
096: Metrics m1 = new Metrics("m1");
097: Metrics m2 = new Metrics("m2");
098:
099: m1.track("foo", new CounterMeasurement(null, null, null));
100: m1.track("bar", new CounterMeasurement(null, null, null));
101: m1.track("baz", new CounterMeasurement(null, null, null));
102: m2.track("foo", new CounterMeasurement(null, null, null));
103: m2.track("bar", new CounterMeasurement(null, null, null));
104: m2.track("baz", new CounterMeasurement(null, null, null));
105:
106: m1.addToMeasurement("foo", 1);
107: m1.addToMeasurement("bar", 2);
108: m1.addToMeasurement("baz", 3);
109: m2.addToMeasurement("foo", 3);
110: m2.addToMeasurement("bar", 2);
111: m2.addToMeasurement("baz", 1);
112:
113: MetricsComparator c1 = new MetricsComparator("foo");
114: MetricsComparator c2 = new MetricsComparator("bar");
115: MetricsComparator c3 = new MetricsComparator("baz");
116:
117: assertTrue(c1.compare(m1, m2) < 0);
118: assertTrue(c2.compare(m1, m2) == 0);
119: assertTrue(c3.compare(m1, m2) > 0);
120:
121: c1.reverse();
122: c2.reverse();
123: c3.reverse();
124:
125: assertTrue(c1.compare(m1, m2) > 0);
126: assertTrue(c2.compare(m1, m2) == 0);
127: assertTrue(c3.compare(m1, m2) < 0);
128: }
129:
130: public void testCompareNaN() {
131: Metrics m1 = new Metrics("m1");
132: Metrics m2 = new Metrics("m2");
133:
134: m1.track("foo", new CounterMeasurement(null, null, null));
135: m2.track("foo", new CounterMeasurement(null, null, null));
136: m1.track("bar", new CounterMeasurement(null, null, null));
137: m2.track("bar", new CounterMeasurement(null, null, null));
138: m1.track("baz", new CounterMeasurement(null, null, null));
139: m2.track("baz", new CounterMeasurement(null, null, null));
140:
141: m1.addToMeasurement("foo", Double.NaN);
142: m2.addToMeasurement("foo", Double.NaN);
143: m1.addToMeasurement("bar", Double.NaN);
144: m2.addToMeasurement("bar", 1);
145: m1.addToMeasurement("baz", 1);
146: m2.addToMeasurement("baz", Double.NaN);
147:
148: MetricsComparator c1 = new MetricsComparator("foo");
149: MetricsComparator c2 = new MetricsComparator("bar");
150: MetricsComparator c3 = new MetricsComparator("baz");
151:
152: assertTrue(c1.compare(m1, m2) == 0);
153: assertTrue(c2.compare(m1, m2) > 0);
154: assertTrue(c3.compare(m1, m2) < 0);
155:
156: c1.reverse();
157: c2.reverse();
158: c3.reverse();
159:
160: assertTrue(c1.compare(m1, m2) == 0);
161: assertTrue(c2.compare(m1, m2) > 0);
162: assertTrue(c3.compare(m1, m2) < 0);
163: }
164: }
|