Changeset 2c8f2ae4


Ignore:
Timestamp:
Jan 3, 2013 8:08:54 PM (8 years ago)
Author:
zab <zab@…>
Branches:
master
Children:
8e57a2e, 94827d6d
Parents:
3eb00c5
Message:

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

Location:
core/java
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/stat/Rate.java

    r3eb00c5 r2c8f2ae4  
    534534        if (obj == this) return true;
    535535        Rate r = (Rate) obj;
    536         return _period == r.getPeriod() && _creationDate == r.getCreationDate() &&
    537                // do this the easy way to avoid NPEs.
    538                // Alternative: compare name and group name (very carefully to avoid NPEs)
    539                _stat == r._stat;
     536        if (_period != r.getPeriod() || _creationDate != r.getCreationDate())
     537            return false;
     538        if (_stat == null && r._stat == null)
     539            return true;
     540        if (_stat != null && r._stat != null)
     541            return _stat.nameGroupDescEquals(r._stat);
     542        return false;
    540543    }
    541544
  • core/java/src/net/i2p/stat/RateStat.java

    r3eb00c5 r2c8f2ae4  
    167167            return true;
    168168        RateStat rs = (RateStat) obj;
    169         if (DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
    170             && DataHelper.eq(getName(), rs.getName())) {
     169        if (nameGroupDescEquals(rs))
    171170            return deepEquals(this._rates, rs._rates);
    172         }
    173171       
    174172        return false;
     173    }
     174   
     175    boolean nameGroupDescEquals(RateStat rs) {
     176        return DataHelper.eq(getGroupName(), rs.getGroupName()) && DataHelper.eq(getDescription(), rs.getDescription())
     177                && DataHelper.eq(getName(), rs.getName());
    175178    }
    176179
  • core/java/test/junit/net/i2p/stat/RateStatTest.java

    r3eb00c5 r2c8f2ae4  
    22
    33import java.util.Properties;
     4
     5import org.junit.Test;
    46
    57import junit.framework.TestCase;
     
    79
    810public class RateStatTest extends TestCase {
     11   
     12    @Test
     13    public void testNoRates() throws Exception {
     14        final long emptyArray[] = new long[0];
     15        try {
     16            new RateStat("test", "test RateStat getters etc", "tests", emptyArray);
     17            fail("created a rate stat with no periods");
     18        } catch (IllegalArgumentException expected){}
     19    }
     20   
     21    @Test
    922    public void testGettersEtc() throws Exception{
    10         long emptyArray[] = new long[0];
    11         RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", emptyArray);
     23        final long periods[] = new long[]{10};
     24        RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", periods);
    1225
    1326        // Test basic getters
     
    1629        assertEquals("test RateStat getters etc", rs.getDescription());
    1730
    18         // There should be no periods, so other getters should return defaults
    19         // TODO: Fix this so it checks that the array is empty rather than comparing objects
    20         //assertEquals(rs.getPeriods(), emptyArray);
     31        // There should be no data, so other getters should return defaults
    2132        assertEquals(0.0, rs.getLifetimeAverageValue());
    2233        assertEquals(0, rs.getLifetimeEventCount());
    2334        assertNull(rs.getRate(2000));
     35    }
     36   
     37    @SuppressWarnings("deprecation")
     38    @Test
     39    public void testAddingAndRemovingThrows() throws Exception {
     40        final long periods[] = new long[]{10};
     41        RateStat rs = new RateStat("test", "test RateStat getters etc", "tests", periods);
    2442
    25         // Test adding and removing a period
    26         assertFalse(rs.containsRate(1000));
    27         rs.addRate(1000);
    28         assertTrue(rs.containsRate(1000));
    29         rs.removeRate(1000);
    30         assertFalse(rs.containsRate(1000));
     43        try {
     44            rs.addRate(1000);
     45            fail("adding periods should not be supported");
     46        } catch (UnsupportedOperationException expected){}
     47        try {
     48            rs.removeRate(10);
     49            fail("removing periods should not be supported");
     50        } catch (UnsupportedOperationException expected){}
    3151    }
    3252
     53    @Test
    3354    public void testRateStat() throws Exception{
    3455        RateStat rs = new RateStat("moo", "moo moo moo", "cow trueisms", new long[] { 60 * 1000, 60 * 60 * 1000,
  • core/java/test/junit/net/i2p/stat/RateTest.java

    r3eb00c5 r2c8f2ae4  
    44import java.util.Properties;
    55
     6import org.junit.Test;
     7
    68import junit.framework.TestCase;
    79
    810
    911public class RateTest extends TestCase {
     12   
     13    @Test
    1014    public void testRate() throws Exception{
    1115        Rate rate = new Rate(5000);
Note: See TracChangeset for help on using the changeset viewer.