forked from I2P_Developers/i2p.i2p
Change the Rate.equals(..) method to work for Rates w/o a parent RateStat
Change the RateStat.equals(..) method to work with deserialized RateStats Update and fix the JUnit tests for both
This commit is contained in:
@ -533,10 +533,13 @@ public class Rate {
|
|||||||
if ((obj == null) || !(obj instanceof Rate)) return false;
|
if ((obj == null) || !(obj instanceof Rate)) return false;
|
||||||
if (obj == this) return true;
|
if (obj == this) return true;
|
||||||
Rate r = (Rate) obj;
|
Rate r = (Rate) obj;
|
||||||
return _period == r.getPeriod() && _creationDate == r.getCreationDate() &&
|
if (_period != r.getPeriod() || _creationDate != r.getCreationDate())
|
||||||
// do this the easy way to avoid NPEs.
|
return false;
|
||||||
// Alternative: compare name and group name (very carefully to avoid NPEs)
|
if (_stat == null && r._stat == null)
|
||||||
_stat == r._stat;
|
return true;
|
||||||
|
if (_stat != null && r._stat != null)
|
||||||
|
return _stat.nameGroupDescEquals(r._stat);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,13 +166,16 @@ public class RateStat {
|
|||||||
if (obj == this)
|
if (obj == this)
|
||||||
return true;
|
return true;
|
||||||
RateStat rs = (RateStat) obj;
|
RateStat rs = (RateStat) obj;
|
||||||
if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
|
if (nameGroupDescEquals(rs))
|
||||||
&& DataHelper.eq(getName(), rs.getName())) {
|
|
||||||
return deepEquals(this._rates, rs._rates);
|
return deepEquals(this._rates, rs._rates);
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean nameGroupDescEquals(RateStat rs) {
|
||||||
|
return DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
|
||||||
|
&& DataHelper.eq(getName(), rs.getName());
|
||||||
|
}
|
||||||
|
|
||||||
public void store(OutputStream out, String prefix) throws IOException {
|
public void store(OutputStream out, String prefix) throws IOException {
|
||||||
StringBuilder buf = new StringBuilder(1024);
|
StringBuilder buf = new StringBuilder(1024);
|
||||||
|
@ -2,34 +2,55 @@ package net.i2p.stat;
|
|||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
|
||||||
public class RateStatTest extends TestCase {
|
public class RateStatTest extends TestCase {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoRates() throws Exception {
|
||||||
|
final long emptyArray[] = new long[0];
|
||||||
|
try {
|
||||||
|
new RateStat("test", "test RateStat getters etc", "tests", emptyArray);
|
||||||
|
fail("created a rate stat with no periods");
|
||||||
|
} catch (IllegalArgumentException expected){}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testGettersEtc() throws Exception{
|
public void testGettersEtc() throws Exception{
|
||||||
long emptyArray[] = new long[0];
|
final long periods[] = new long[]{10};
|
||||||
RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", emptyArray);
|
RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", periods);
|
||||||
|
|
||||||
// Test basic getters
|
// Test basic getters
|
||||||
assertEquals("test", rs.getName());
|
assertEquals("test", rs.getName());
|
||||||
assertEquals("tests", rs.getGroupName());
|
assertEquals("tests", rs.getGroupName());
|
||||||
assertEquals("test RateStat getters etc", rs.getDescription());
|
assertEquals("test RateStat getters etc", rs.getDescription());
|
||||||
|
|
||||||
// There should be no periods, so other getters should return defaults
|
// There should be no data, so other getters should return defaults
|
||||||
// TODO: Fix this so it checks that the array is empty rather than comparing objects
|
|
||||||
//assertEquals(rs.getPeriods(), emptyArray);
|
|
||||||
assertEquals(0.0, rs.getLifetimeAverageValue());
|
assertEquals(0.0, rs.getLifetimeAverageValue());
|
||||||
assertEquals(0, rs.getLifetimeEventCount());
|
assertEquals(0, rs.getLifetimeEventCount());
|
||||||
assertNull(rs.getRate(2000));
|
assertNull(rs.getRate(2000));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
@Test
|
||||||
|
public void testAddingAndRemovingThrows() throws Exception {
|
||||||
|
final long periods[] = new long[]{10};
|
||||||
|
RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", periods);
|
||||||
|
|
||||||
// Test adding and removing a period
|
try {
|
||||||
assertFalse(rs.containsRate(1000));
|
rs.addRate(1000);
|
||||||
rs.addRate(1000);
|
fail("adding periods should not be supported");
|
||||||
assertTrue(rs.containsRate(1000));
|
} catch (UnsupportedOperationException expected){}
|
||||||
rs.removeRate(1000);
|
try {
|
||||||
assertFalse(rs.containsRate(1000));
|
rs.removeRate(10);
|
||||||
|
fail("removing periods should not be supported");
|
||||||
|
} catch (UnsupportedOperationException expected){}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testRateStat() throws Exception{
|
public void testRateStat() throws Exception{
|
||||||
RateStat rs = new RateStat("moo", "moo moo moo", "cow trueisms", new long[] { 60 * 1000, 60 * 60 * 1000,
|
RateStat rs = new RateStat("moo", "moo moo moo", "cow trueisms", new long[] { 60 * 1000, 60 * 60 * 1000,
|
||||||
24 * 60 * 60 * 1000});
|
24 * 60 * 60 * 1000});
|
||||||
|
@ -3,10 +3,14 @@ package net.i2p.stat;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
|
||||||
public class RateTest extends TestCase {
|
public class RateTest extends TestCase {
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testRate() throws Exception{
|
public void testRate() throws Exception{
|
||||||
Rate rate = new Rate(5000);
|
Rate rate = new Rate(5000);
|
||||||
for (int i = 0; i < 50; i++) {
|
for (int i = 0; i < 50; i++) {
|
||||||
|
Reference in New Issue
Block a user