View Javadoc
1   /*
2    * Copyright (C) 2020-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  import org.junit.jupiter.params.ParameterizedTest;
16  import org.junit.jupiter.params.provider.ValueSource;
17  
18  import de.powerstat.validation.values.Port;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * Tests for Port value class.
24   */
25  @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"})
26  final class PortTests
27   {
28    /**
29     * Port should be 49152 constant.
30     */
31    private static final String PORT_SHOULD_BE_49152 = "Port should be 49152!"; //$NON-NLS-1$
32  
33    /**
34     * Port 49152 constant.
35     */
36    private static final String PORT_49152 = "49152"; //$NON-NLS-1$
37  
38  
39    /**
40     * Default constructor.
41     */
42    /* default */ PortTests()
43     {
44      super();
45     }
46  
47  
48    /**
49     * Factory string test.
50     */
51    @Test
52    /* default */ void testFactory1()
53     {
54      assertEquals(49152, Port.of(PORT_49152).intValue(), PortTests.PORT_SHOULD_BE_49152);
55     }
56  
57  
58    /**
59     * Is port.
60     */
61    @Test
62    /* default */ void testIsPort()
63     {
64      final Port port = Port.of(49152);
65      assertEquals(49152, port.intValue(), PortTests.PORT_SHOULD_BE_49152);
66     }
67  
68  
69    /**
70     * Is not a port.
71     *
72     * @param port Port
73     */
74    @ParameterizedTest
75    @ValueSource(ints = {65536, -1})
76    /* default */ void testIsPortFalse(final int port)
77     {
78      assertThrows(IndexOutOfBoundsException.class, () ->
79       {
80        /* final Port port = */ Port.of(port);
81       }, "Index out of bounds exception expected" //$NON-NLS-1$
82      );
83     }
84  
85  
86    /**
87     * Test intValue.
88     */
89    @Test
90    /* default */ void testIntValue()
91     {
92      final Port port = Port.of(49152);
93      assertEquals(49152, port.intValue(), PortTests.PORT_SHOULD_BE_49152);
94     }
95  
96  
97    /**
98     * Test stringValue.
99     */
100   @Test
101   /* default */ void testStringValue()
102    {
103     final Port port = Port.of(49152);
104     assertEquals(PORT_49152, port.stringValue(), PortTests.PORT_SHOULD_BE_49152);
105    }
106 
107 
108   /**
109    * Is system port.
110    *
111    * @param port Port
112    */
113   @ParameterizedTest
114   @ValueSource(ints = {0, 1023})
115   /* default */ void testIsSystem(final int port)
116    {
117     assertTrue(Port.of(port).isSystem(), "Should be a system port!"); //$NON-NLS-1$
118    }
119 
120 
121   /**
122    * Is not a system port.
123    */
124   @Test
125   /* default */ void testIsSystemFalse()
126    {
127     assertFalse(Port.of(49152).isSystem(), "Should not be a system port!"); //$NON-NLS-1$
128    }
129 
130 
131   /**
132    * Is registered port.
133    *
134    * @param port Port
135    */
136   @ParameterizedTest
137   @ValueSource(ints = {1024, 49151})
138   /* default */ void testIsRegistered(final int port)
139    {
140     assertTrue(Port.of(port).isRegistered(), "Should be a registered port!"); //$NON-NLS-1$
141    }
142 
143 
144   /**
145    * Is not a registered port.
146    *
147    * @param port Port
148    */
149   @ParameterizedTest
150   @ValueSource(ints = {1023, 49152})
151   /* default */ void testIsRegisteredFalse(final int port)
152    {
153     assertFalse(Port.of(port).isRegistered(), "Should not be a registered port!"); //$NON-NLS-1$
154    }
155 
156 
157   /**
158    * Is dynamic port.
159    *
160    * @param port Port
161    */
162   @ParameterizedTest
163   @ValueSource(ints = {49152, 65535})
164   /* default */ void testIsDynamic(final int port)
165    {
166     assertTrue(Port.of(port).isDynamic(), "Should be a dynamic port!"); //$NON-NLS-1$
167    }
168 
169 
170   /**
171    * Is not a dynamic port.
172    */
173   @Test
174   /* default */ void testIsDynamicFalse()
175    {
176     assertFalse(Port.of(1023).isDynamic(), "Should not be a dynamic port!"); //$NON-NLS-1$
177    }
178 
179 
180   /**
181    * Test hash code.
182    */
183   @Test
184   /* default */ void testHashCode()
185    {
186     final Port port1 = Port.of(1024);
187     final Port port2 = Port.of(1024);
188     final Port port3 = Port.of(1025);
189     assertAll("testHashCode", //$NON-NLS-1$
190       () -> assertEquals(port1.hashCode(), port2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
191       () -> assertNotEquals(port1.hashCode(), port3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
192     );
193    }
194 
195 
196   /**
197    * Test equals.
198    */
199   @Test
200   @SuppressWarnings("java:S5785")
201   /* default */ void testEquals()
202    {
203     final Port port1 = Port.of(1024);
204     final Port port2 = Port.of(1024);
205     final Port port3 = Port.of(1025);
206     final Port port4 = Port.of(1024);
207     assertAll("testEquals", //$NON-NLS-1$
208       () -> assertTrue(port1.equals(port1), "port11 is not equal"), //$NON-NLS-1$
209       () -> assertTrue(port1.equals(port2), "port12 are not equal"), //$NON-NLS-1$
210       () -> assertTrue(port2.equals(port1), "port21 are not equal"), //$NON-NLS-1$
211       () -> assertTrue(port2.equals(port4), "port24 are not equal"), //$NON-NLS-1$
212       () -> assertTrue(port1.equals(port4), "port14 are not equal"), //$NON-NLS-1$
213       () -> assertFalse(port1.equals(port3), "port13 are equal"), //$NON-NLS-1$
214       () -> assertFalse(port3.equals(port1), "port31 are equal"), //$NON-NLS-1$
215       () -> assertFalse(port1.equals(null), "port10 is equal") //$NON-NLS-1$
216     );
217    }
218 
219 
220   /**
221    * Test toString.
222    */
223   @Test
224   /* default */ void testToString()
225    {
226     final Port port = Port.of(49152);
227     assertEquals("Port[port=49152]", port.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
228    }
229 
230 
231   /**
232    * Test compareTo.
233    */
234   @Test
235   @SuppressWarnings("java:S5785")
236   /* default */ void testCompareTo()
237    {
238     final Port port1 = Port.of(1024);
239     final Port port2 = Port.of(1024);
240     final Port port3 = Port.of(1025);
241     final Port port4 = Port.of(1026);
242     final Port port5 = Port.of(1024);
243     assertAll("testCompareTo", //$NON-NLS-1$
244       () -> assertTrue(port1.compareTo(port2) == -port2.compareTo(port1), "reflexive1"), //$NON-NLS-1$
245       () -> assertTrue(port1.compareTo(port3) == -port3.compareTo(port1), "reflexive2"), //$NON-NLS-1$
246       () -> assertTrue((port4.compareTo(port3) > 0) && (port3.compareTo(port1) > 0) && (port4.compareTo(port1) > 0), "transitive1"), //$NON-NLS-1$
247       () -> assertTrue((port1.compareTo(port2) == 0) && (Math.abs(port1.compareTo(port5)) == Math.abs(port2.compareTo(port5))), "sgn1"), //$NON-NLS-1$
248       () -> assertTrue((port1.compareTo(port2) == 0) && port1.equals(port2), "equals") //$NON-NLS-1$
249     );
250    }
251 
252  }