1 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 |