1 package com.atlassian.plugin.web.api.model;
2
3
4 import com.atlassian.plugin.web.api.WebFragment;
5 import org.junit.Before;
6 import org.junit.Test;
7
8 import static org.hamcrest.Matchers.equalTo;
9 import static org.hamcrest.Matchers.greaterThan;
10 import static org.hamcrest.Matchers.is;
11 import static org.hamcrest.Matchers.lessThan;
12 import static org.junit.Assert.assertThat;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 public class TestWeightedComparator {
17 private static final int MAX_NEGATIVE = Integer.MIN_VALUE;
18 private static final int LITTLE_NEGATIVE = -100;
19 private static final int LITTLE_POSITIVE = 100;
20 private static final int MAX_POSITIVE = Integer.MAX_VALUE;
21
22 WebFragment w1;
23 WebFragment w2;
24 WeightedComparator comparator;
25
26 @Before
27 public void setup() {
28 w1 = mock(WebFragment.class);
29 w2 = mock(WebFragment.class);
30 comparator = new WeightedComparator();
31 }
32
33 @Test
34 public void whenEqual_compare_should_returnZero() {
35 when(w1.getWeight()).thenReturn(LITTLE_POSITIVE);
36 when(w2.getWeight()).thenReturn(LITTLE_POSITIVE);
37
38 final int comparisonResult = comparator.compare(w1, w2);
39
40 assertThat(comparisonResult, is(equalTo(0)));
41 }
42
43 @Test
44 public void whenTheFormerIsLessThanTheLatter_compare_should_returnANegativeValue() {
45 when(w1.getWeight()).thenReturn(LITTLE_NEGATIVE);
46 when(w2.getWeight()).thenReturn(LITTLE_POSITIVE);
47
48 final int comparisonResult = comparator.compare(w1, w2);
49
50 assertThat(comparisonResult, is(lessThan(0)));
51 }
52
53 @Test
54 public void whenTheFormerIsGreaterThanTheLatter_compare_should_returnAPositiveValue() {
55 when(w1.getWeight()).thenReturn(LITTLE_POSITIVE);
56 when(w2.getWeight()).thenReturn(LITTLE_NEGATIVE);
57
58 final int comparisonResult = comparator.compare(w1, w2);
59
60 assertThat(comparisonResult, is(greaterThan(0)));
61 }
62
63 @Test
64 public void compare_should_notUnderflow() {
65 when(w1.getWeight()).thenReturn(MAX_NEGATIVE);
66 when(w2.getWeight()).thenReturn(LITTLE_POSITIVE);
67
68 final int comparisonResult = comparator.compare(w1, w2);
69
70 assertThat(comparisonResult, is(lessThan(0)));
71 }
72
73 @Test
74 public void compare_should_notOverflow() {
75 when(w1.getWeight()).thenReturn(MAX_POSITIVE);
76 when(w2.getWeight()).thenReturn(LITTLE_NEGATIVE);
77
78 final int comparisonResult = comparator.compare(w1, w2);
79
80 assertThat(comparisonResult, is(greaterThan(0)));
81 }
82 }