View Javadoc
1   /*
2    * Copyright (C) 2024 Dipl.-Inform. Kai Hofmann. All rights reserved!
3    */
4   package de.powerstat.validation.values.test;
5   
6   import static org.junit.jupiter.api.Assertions.assertAll;
7   import static org.junit.jupiter.api.Assertions.assertEquals;
8   import static org.junit.jupiter.api.Assertions.assertFalse;
9   import static org.junit.jupiter.api.Assertions.assertNotEquals;
10  import static org.junit.jupiter.api.Assertions.assertThrows;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.params.ParameterizedTest;
15  import org.junit.jupiter.params.provider.ValueSource;
16  
17  import de.powerstat.validation.values.Percent;
18  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19  
20  /**
21   * Tests for Percent value class.
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 PercentTests
25   {
26    /**
27     * Default constructor.
28     */
29    /* default */ PercentTests()
30     {
31      super();
32     }
33  
34  
35    /**
36     * Is percent.
37     */
38    @Test
39    /* default */ void testIsPercent()
40     {
41      final Percent percent = Percent.of(50);
42      assertEquals(50, percent.intValue(), "Percent should be 50");
43     }
44  
45  
46    /**
47     * Is not a percent.
48     *
49     * @param percent Percent
50     */
51    @ParameterizedTest
52    @ValueSource(ints = {101, -1})
53    /* default */ void testIsPercentFalse(final int percent)
54     {
55      assertThrows(IndexOutOfBoundsException.class, () ->
56       {
57        /* final Percent percent = */ Percent.of(percent);
58       }, "Index out of bounds exception expected" //$NON-NLS-1$
59      );
60     }
61  
62  
63    /**
64     * Factory string test.
65     */
66    @Test
67    /* default */ void testFactory1()
68     {
69      assertEquals(50, Percent.of("50").intValue(), "Percent should be 50");
70     }
71  
72  
73    /**
74     * Test intValue.
75     */
76    @Test
77    /* default */ void testIntValue()
78     {
79      final Percent percent = Percent.of(50);
80      assertEquals(50, percent.intValue(), "Percent should be 50");
81     }
82  
83  
84    /**
85     * Test stringValue.
86     */
87    @Test
88    /* default */ void testStringValue()
89     {
90      final Percent percent = Percent.of(50);
91      assertEquals("50", percent.stringValue(), "Percent should be 50");
92     }
93  
94  
95    /**
96     * Test hash code.
97     */
98    @Test
99    /* default */ void testHashCode()
100    {
101     final Percent percent1 = Percent.of(50);
102     final Percent percent2 = Percent.of(50);
103     final Percent percent3 = Percent.of(51);
104     assertAll("testHashCode", //$NON-NLS-1$
105       () -> assertEquals(percent1.hashCode(), percent2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
106       () -> assertNotEquals(percent1.hashCode(), percent3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
107     );
108    }
109 
110 
111   /**
112    * Test equals.
113    */
114   @Test
115   @SuppressWarnings("java:S5785")
116   /* default */ void testEquals()
117    {
118     final Percent percent1 = Percent.of(50);
119     final Percent percent2 = Percent.of(50);
120     final Percent percent3 = Percent.of(51);
121     final Percent percent4 = Percent.of(50);
122     assertAll("testEquals", //$NON-NLS-1$
123       () -> assertTrue(percent1.equals(percent1), "percent11 is not equal"), //$NON-NLS-1$
124       () -> assertTrue(percent1.equals(percent2), "percent12 are not equal"), //$NON-NLS-1$
125       () -> assertTrue(percent2.equals(percent1), "percent21 are not equal"), //$NON-NLS-1$
126       () -> assertTrue(percent2.equals(percent4), "percent24 are not equal"), //$NON-NLS-1$
127       () -> assertTrue(percent1.equals(percent4), "percent14 are not equal"), //$NON-NLS-1$
128       () -> assertFalse(percent1.equals(percent3), "percent13 are equal"), //$NON-NLS-1$
129       () -> assertFalse(percent3.equals(percent1), "percent31 are equal"), //$NON-NLS-1$
130       () -> assertFalse(percent1.equals(null), "percent10 is equal") //$NON-NLS-1$
131     );
132    }
133 
134 
135   /**
136    * Test toString.
137    */
138   @Test
139   /* default */ void testToString()
140    {
141     final Percent percent = Percent.of(50);
142     assertEquals("Percent[percent=50]", percent.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
143    }
144 
145 
146   /**
147    * Test compareTo.
148    */
149   @Test
150   @SuppressWarnings("java:S5785")
151   /* default */ void testCompareTo()
152    {
153     final Percent percent1 = Percent.of(50);
154     final Percent percent2 = Percent.of(50);
155     final Percent percent3 = Percent.of(51);
156     final Percent percent4 = Percent.of(52);
157     final Percent percent5 = Percent.of(50);
158     assertAll("testCompareTo", //$NON-NLS-1$
159       () -> assertTrue(percent1.compareTo(percent2) == -percent2.compareTo(percent1), "reflexive1"), //$NON-NLS-1$
160       () -> assertTrue(percent1.compareTo(percent3) == -percent3.compareTo(percent1), "reflexive2"), //$NON-NLS-1$
161       () -> assertTrue((percent4.compareTo(percent3) > 0) && (percent3.compareTo(percent1) > 0) && (percent4.compareTo(percent1) > 0), "transitive1"), //$NON-NLS-1$
162       () -> assertTrue((percent1.compareTo(percent2) == 0) && (Math.abs(percent1.compareTo(percent5)) == Math.abs(percent2.compareTo(percent5))), "sgn1"), //$NON-NLS-1$
163       () -> assertTrue((percent1.compareTo(percent2) == 0) && percent1.equals(percent2), "equals") //$NON-NLS-1$
164     );
165    }
166 
167  }