View Javadoc

1   package com.google.code.jetm.maven.data;
2   
3   import static org.fest.assertions.Assertions.assertThat;
4   import static org.mockito.Mockito.mock;
5   import static org.mockito.Mockito.when;
6   
7   import org.junit.Rule;
8   import org.junit.Test;
9   import org.junit.rules.ExpectedException;
10  
11  import etm.core.aggregation.Aggregate;
12  
13  /**
14   * Unit tests for {@link AggregateSummary}.
15   * 
16   * @author jrh3k5
17   * 
18   */
19  
20  public class AggregateSummaryTest {
21      /**
22       * A {@link Rule} used to test for thrown exceptions.
23       */
24      @Rule
25      public ExpectedException expected = ExpectedException.none();
26  
27      /**
28       * Given a {@code null} name, construction should fail.
29       */
30      @Test
31      public void testConstructNullName() {
32          expected.expect(IllegalArgumentException.class);
33          expected.expectMessage("Name cannot be null.");
34          new AggregateSummary(null);
35      }
36  
37      /**
38       * Two summaries by the same name should be equal.
39       */
40      @Test
41      public void testEquals() {
42          final AggregateSummary summary = new AggregateSummary("test name");
43          final AggregateSummary other = new AggregateSummary(summary.getName());
44          assertThat(summary).isEqualTo(other);
45          assertThat(other).isEqualTo(summary);
46      }
47  
48      /**
49       * An aggregate summary should not be equal to an object that is not an
50       * aggregate summary.
51       */
52      @Test
53      public void testEqualsNotAggregateSummary() {
54          assertThat(new AggregateSummary("a summary")).isNotEqualTo(new Object());
55      }
56  
57      /**
58       * An aggregate summary should not be equal to {@code null}.
59       */
60      @Test
61      public void testEqualsNull() {
62          assertThat(new AggregateSummary("not null")).isNotEqualTo(null);
63      }
64  
65      /**
66       * An aggregate summary should be equal to itself.
67       */
68      @Test
69      public void testEqualsSelf() {
70          final AggregateSummary summary = new AggregateSummary("self");
71          assertThat(summary).isEqualTo(summary);
72      }
73  
74      /**
75       * Test the summary of two aggregates in a single {@link AggregateSummary}
76       * object.
77       */
78      @Test
79      public void testAdd() {
80          final Aggregate aggregateOne = mock(Aggregate.class);
81          when(aggregateOne.getMin()).thenReturn(Double.valueOf(1.0));
82          when(aggregateOne.getMax()).thenReturn(Double.valueOf(2.0));
83          when(aggregateOne.getTotal()).thenReturn(Double.valueOf(3.0));
84          when(aggregateOne.getMeasurements()).thenReturn(Long.valueOf(4));
85  
86          final Aggregate aggregateTwo = mock(Aggregate.class);
87          when(aggregateTwo.getMin()).thenReturn(Double.valueOf(5.0));
88          when(aggregateTwo.getMax()).thenReturn(Double.valueOf(6.0));
89          when(aggregateTwo.getTotal()).thenReturn(Double.valueOf(7.0));
90          when(aggregateTwo.getMeasurements()).thenReturn(Long.valueOf(8));
91  
92          final AggregateSummary summary = new AggregateSummary("a summary");
93          summary.add(aggregateOne);
94          summary.add(aggregateTwo);
95  
96          assertThat(summary.getMin()).isEqualTo(aggregateOne.getMin());
97          assertThat(summary.getMax()).isEqualTo(aggregateTwo.getMax());
98          assertThat(summary.getTotal()).isEqualTo(aggregateOne.getTotal() + aggregateTwo.getTotal());
99          assertThat(summary.getMeasurements()).isEqualTo(aggregateOne.getMeasurements() + aggregateTwo.getMeasurements());
100     }
101 
102     /**
103      * Test the comparison of two {@link AggregateSummary} objects. Two
104      * summaries by the same name should match; those with different names
105      * should not match.
106      */
107     @Test
108     public void testCompareTo() {
109         final AggregateSummary summaryA = new AggregateSummary("summaryA");
110         final AggregateSummary summaryACopy = new AggregateSummary(summaryA.getName());
111 
112         assertThat(summaryA.compareTo(summaryACopy)).isZero();
113         assertThat(summaryACopy.compareTo(summaryA)).isZero();
114 
115         final AggregateSummary summaryB = new AggregateSummary("summaryB");
116         assertThat(summaryA.compareTo(summaryB)).isLessThan(0);
117         assertThat(summaryB.compareTo(summaryA)).isGreaterThan(0);
118     }
119 
120     /**
121      * A summary's hash code should be simply the hash code of its name.
122      */
123     @Test
124     public void testHashCode() {
125         final String name = "this will be a hash code";
126         assertThat(new AggregateSummary(name).hashCode()).isEqualTo(name.hashCode());
127     }
128 }