View Javadoc
1   /*
2    * Copyright (C) 2021-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.test;
5   
6   
7   import static org.junit.jupiter.api.Assertions.assertAll;
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotEquals;
11  import static org.junit.jupiter.api.Assertions.assertThrows;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import org.junit.jupiter.api.Test;
15  
16  import de.powerstat.validation.values.DisplayAspectRatio;
17  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18  
19  
20  /**
21   * Display aspect ratio tests.
22   */
23  @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
24  final class DisplayAspectRatioTests
25   {
26    /**
27     * Test aspect ration constant.
28     */
29    private static final String TEST_ASPECT_RATIO = "testAspectRatio"; //$NON-NLS-1$
30  
31    /**
32     * 1:1 constant.
33     */
34    private static final String ONE_TO_ONE = "1:1"; //$NON-NLS-1$
35  
36    /**
37     * Aspect ratio not as expected constant.
38     */
39    private static final String ASPECT_RATIO_NOT_AS_EXPECTED = "aspect ratio not as expected"; //$NON-NLS-1$
40  
41    /**
42     * Index out of bounds exception comnstant.
43     */
44    private static final String INDEX_OUT_OF_BOUNDS_EXPECTED = "Index out of bounds exception expected"; //$NON-NLS-1$
45  
46  
47    /**
48     * Default constructor.
49     */
50    /* default */ DisplayAspectRatioTests()
51     {
52      super();
53     }
54  
55  
56    /**
57     * Get aspect ratio.
58     */
59    @Test
60    /* default */ void testOf1()
61     {
62      final DisplayAspectRatio ratio = DisplayAspectRatio.of(ONE_TO_ONE);
63      assertAll(DisplayAspectRatioTests.TEST_ASPECT_RATIO,
64        () -> assertEquals(DisplayAspectRatioTests.ONE_TO_ONE, ratio.stringValue(), DisplayAspectRatioTests.ASPECT_RATIO_NOT_AS_EXPECTED)
65      );
66     }
67  
68  
69    /**
70     * Get aspect ratio.
71     */
72    @Test
73    /* default */ void testOf2()
74     {
75      assertThrows(IllegalArgumentException.class, () ->
76       {
77        /* final DisplayAspectRatio ratio = */ DisplayAspectRatio.of("11");
78       }, "Illegal argument excetion"
79      );
80     }
81  
82  
83    /**
84     * Is display aspect ratio.
85     */
86    @Test
87    /* default */ void testIsDisplayAspectRatio()
88     {
89      final DisplayAspectRatio ratio = DisplayAspectRatio.of(1, 1);
90      assertAll(DisplayAspectRatioTests.TEST_ASPECT_RATIO,
91        () -> assertEquals(1, ratio.getX(), "x ratio not as expected"), //$NON-NLS-1$
92        () -> assertEquals(1, ratio.getY(), "y ratio not as expected"), //$NON-NLS-1$
93        () -> assertEquals(DisplayAspectRatioTests.ONE_TO_ONE, ratio.stringValue(), DisplayAspectRatioTests.ASPECT_RATIO_NOT_AS_EXPECTED)
94      );
95     }
96  
97  
98    /**
99     * Is not display aspect ratio.
100    */
101   @Test
102   /* default */ void testIsNotADisplayAspectRatio1()
103    {
104     assertThrows(IndexOutOfBoundsException.class, () ->
105      {
106       /* final DisplayAspectRatio ratio = */ DisplayAspectRatio.of(0, 0);
107      }, DisplayAspectRatioTests.INDEX_OUT_OF_BOUNDS_EXPECTED
108     );
109    }
110 
111 
112   /**
113    * Is not display aspect ratio.
114    */
115   @Test
116   /* default */ void testIsNotADisplayAspectRatio2()
117    {
118     assertThrows(IndexOutOfBoundsException.class, () ->
119      {
120       /* final DisplayAspectRatio ratio = */ DisplayAspectRatio.of(73, 0);
121      }, DisplayAspectRatioTests.INDEX_OUT_OF_BOUNDS_EXPECTED
122     );
123    }
124 
125 
126   /**
127    * Is not display aspect ratio.
128    */
129   @Test
130   /* default */ void testIsNotADisplayAspectRatio3()
131    {
132     assertThrows(IndexOutOfBoundsException.class, () ->
133      {
134       /* final DisplayAspectRatio ratio = */ DisplayAspectRatio.of(1, 36);
135      }, DisplayAspectRatioTests.INDEX_OUT_OF_BOUNDS_EXPECTED
136     );
137    }
138 
139 
140   /**
141    * Is not display aspect ratio.
142    */
143   @Test
144   /* default */ void testIsNotADisplayAspectRatio4()
145    {
146     assertThrows(IndexOutOfBoundsException.class, () ->
147      {
148       /* final DisplayAspectRatio ratio = */ DisplayAspectRatio.of(1, 0);
149      }, DisplayAspectRatioTests.INDEX_OUT_OF_BOUNDS_EXPECTED
150     );
151    }
152 
153 
154   /**
155    * Test hash code.
156    */
157   @Test
158   /* default */ void testHashCode()
159    {
160     final DisplayAspectRatio ratio1 = DisplayAspectRatio.of(1, 1);
161     final DisplayAspectRatio ratio2 = DisplayAspectRatio.of(1, 1);
162     final DisplayAspectRatio ratio3 = DisplayAspectRatio.of(2, 2);
163     assertAll("testHashCode", //$NON-NLS-1$
164       () -> assertEquals(ratio1.hashCode(), ratio2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
165       () -> assertNotEquals(ratio1.hashCode(), ratio3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
166     );
167    }
168 
169 
170   /**
171    * Test equals.
172    */
173   @Test
174   @SuppressWarnings("java:S5785")
175   /* default */ void testEquals()
176    {
177     final DisplayAspectRatio ratio1 = DisplayAspectRatio.of(1, 1);
178     final DisplayAspectRatio ratio2 = DisplayAspectRatio.of(1, 1);
179     final DisplayAspectRatio ratio3 = DisplayAspectRatio.of(2, 2);
180     final DisplayAspectRatio ratio4 = DisplayAspectRatio.of(1, 1);
181     assertAll("testEquals", //$NON-NLS-1$
182       () -> assertTrue(ratio1.equals(ratio1), "ratio11 is not equal"), //$NON-NLS-1$
183       () -> assertTrue(ratio1.equals(ratio2), "ratio12 are not equal"), //$NON-NLS-1$
184       () -> assertTrue(ratio2.equals(ratio1), "ratio21 are not equal"), //$NON-NLS-1$
185       () -> assertTrue(ratio2.equals(ratio4), "ratio24 are not equal"), //$NON-NLS-1$
186       () -> assertTrue(ratio1.equals(ratio4), "ratio14 are not equal"), //$NON-NLS-1$
187       () -> assertFalse(ratio1.equals(ratio3), "ratio13 are equal"), //$NON-NLS-1$
188       () -> assertFalse(ratio3.equals(ratio1), "ratio31 are equal"), //$NON-NLS-1$
189       () -> assertFalse(ratio1.equals(null), "ratio10 is equal") //$NON-NLS-1$
190     );
191    }
192 
193 
194   /**
195    * Test notEquals.
196    */
197   @Test
198   @SuppressWarnings("java:S5785")
199   /* default */ void testNotEquals()
200    {
201     final DisplayAspectRatio ratio1 = DisplayAspectRatio.of(1, 1);
202     final DisplayAspectRatio ratio2 = DisplayAspectRatio.of(1, 2);
203     assertAll("testNotEquals", //$NON-NLS-1$
204       () -> assertFalse(ratio1.equals(ratio2), "ratio12 is equal") //$NON-NLS-1$
205     );
206    }
207 
208 
209   /**
210    * Test toString.
211    */
212   @Test
213   /* default */ void testToString()
214    {
215     final DisplayAspectRatio ratio = DisplayAspectRatio.of(1, 1);
216     assertEquals("DisplayAspectRatio[x=1, y=1]", ratio.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
217    }
218 
219 
220   /**
221    * Test compareTo.
222    */
223   @Test
224   @SuppressWarnings("java:S5785")
225   /* default */ void testCompareTo()
226    {
227     final DisplayAspectRatio ratio1 = DisplayAspectRatio.of(1, 1);
228     final DisplayAspectRatio ratio2 = DisplayAspectRatio.of(1, 1);
229     final DisplayAspectRatio ratio3 = DisplayAspectRatio.of(2, 2);
230     final DisplayAspectRatio ratio4 = DisplayAspectRatio.of(3, 2);
231     final DisplayAspectRatio ratio5 = DisplayAspectRatio.of(1, 1);
232     assertAll("testCompareTo", //$NON-NLS-1$
233       () -> assertTrue(ratio1.compareTo(ratio2) == -ratio2.compareTo(ratio1), "reflexive1"), //$NON-NLS-1$
234       () -> assertTrue(ratio1.compareTo(ratio3) == -ratio3.compareTo(ratio1), "reflexive2"), //$NON-NLS-1$
235       () -> assertTrue((ratio4.compareTo(ratio3) > 0) && (ratio3.compareTo(ratio1) > 0) && (ratio4.compareTo(ratio1) > 0), "transitive1"), //$NON-NLS-1$
236       () -> assertTrue((ratio1.compareTo(ratio2) == 0) && (Math.abs(ratio1.compareTo(ratio5)) == Math.abs(ratio2.compareTo(ratio5))), "sgn1"), //$NON-NLS-1$
237       () -> assertTrue((ratio1.compareTo(ratio2) == 0) && ratio1.equals(ratio2), "equals") //$NON-NLS-1$
238     );
239    }
240 
241  }