View Javadoc
1   /*
2    * Copyright (C) 2014 Sven von Pluto - javanarior (a) gmail dot com
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.javanarior.utils.compare;
17  
18  import org.testng.Assert;
19  import org.testng.annotations.BeforeClass;
20  import org.testng.annotations.Test;
21  
22  @Test
23  public class SpeakingComparableAdapterTest {
24  
25      private static final Integer ONE = Integer.valueOf(1);
26      private static final Integer TWO = Integer.valueOf(2);
27  
28      private SpeakingComparableInteger one;
29      private SpeakingComparableInteger two;
30  
31      @BeforeClass
32      public void setUpClass() {
33          one = new SpeakingComparableInteger(1);
34          two = new SpeakingComparableInteger(2);
35      }
36  
37      public void testEqualsTo() {
38          Assert.assertTrue(one.equalsTo(ONE));
39          Assert.assertFalse(one.equalsTo(TWO));
40          Assert.assertFalse(two.equalsTo(ONE));
41          Assert.assertTrue(two.equalsTo(TWO));
42      }
43  
44      public void testNotEqualsTo() {
45          Assert.assertFalse(one.notEqualsTo(ONE));
46          Assert.assertTrue(one.notEqualsTo(TWO));
47          Assert.assertTrue(two.notEqualsTo(ONE));
48          Assert.assertFalse(two.notEqualsTo(TWO));
49      }
50  
51      public void testGreaterThan() {
52          Assert.assertFalse(one.greaterThan(ONE));
53          Assert.assertFalse(one.greaterThan(TWO));
54          Assert.assertTrue(two.greaterThan(ONE));
55          Assert.assertFalse(two.greaterThan(TWO));
56      }
57  
58      public void testGreaterOrEqualsThan() {
59          Assert.assertTrue(one.greaterOrEqualsThan(ONE));
60          Assert.assertFalse(one.greaterOrEqualsThan(TWO));
61          Assert.assertTrue(two.greaterOrEqualsThan(ONE));
62          Assert.assertTrue(two.greaterOrEqualsThan(TWO));
63      }
64  
65      public void testLessThan() {
66          Assert.assertFalse(one.lessThan(ONE));
67          Assert.assertTrue(one.lessThan(TWO));
68          Assert.assertFalse(two.lessThan(ONE));
69          Assert.assertFalse(two.lessThan(TWO));
70      }
71  
72      public void testLessOrEqualsThan() {
73          Assert.assertTrue(one.lessOrEqualsThan(ONE));
74          Assert.assertTrue(one.lessOrEqualsThan(TWO));
75          Assert.assertFalse(two.lessOrEqualsThan(ONE));
76          Assert.assertTrue(two.lessOrEqualsThan(TWO));
77      }
78  
79      // START SNIPPET: class
80      private static class SpeakingComparableInteger extends SpeakingComparableAdapter<Integer> {
81  
82          private Integer value;
83  
84          SpeakingComparableInteger(int value) {
85              this.value = Integer.valueOf(value);
86          }
87  
88          @Override
89          public int compareTo(Integer other) {
90              return value.compareTo(other);
91          }
92      }
93      // END SNIPPET: class
94  }