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.BIC;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * BIC Tests.
24   */
25  @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
26  final class BICTests
27   {
28    /**
29     * Illegal argument exception expected.
30     */
31    private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected"; //$NON-NLS-1$
32  
33    /**
34     * BIC not as expected.
35     */
36    private static final String BIC_NOT_AS_EXPECTED = "BIC not as expected"; //$NON-NLS-1$
37  
38    /**
39     * BIC for tests.
40     */
41    private static final String BIC_BELADEBEXXX = "BELADEBEXXX"; //$NON-NLS-1$
42  
43    /**
44     * BIC for tests.
45     */
46    private static final String BIC_RZTIAT22263 = "RZTIAT22263"; //$NON-NLS-1$
47  
48  
49    /**
50     * Default constructor.
51     */
52    /* default */ BICTests()
53     {
54      super();
55     }
56  
57  
58    /**
59     * Test correct BIC.
60     *
61     * @param bic BIC
62     */
63    @ParameterizedTest
64    @ValueSource(strings = {"POWSDE30XXX", "POWSDE30"})
65    /* default */ void testBicCorrect(final String bic)
66     {
67      final BIC cleanBic = BIC.of(bic);
68      assertEquals(bic, cleanBic.stringValue(), BICTests.BIC_NOT_AS_EXPECTED);
69     }
70  
71  
72    /**
73     * Test BIC with wrong lengths.
74     *
75     * @param bic BIC
76     */
77    @ParameterizedTest
78    @ValueSource(strings = {"POWSDE0", "POWSDE30X", "POWSDE30XX", "POWSDE30XXXX"})
79    /* default */ void testBicLength(final String bic)
80     {
81      assertThrows(IllegalArgumentException.class, () ->
82       {
83        /* final BIC cleanBic = */ BIC.of(bic);
84       }, BICTests.ILLEGAL_ARGUMENT
85      );
86     }
87  
88  
89    /**
90     * Test wrong BIC.
91     *
92     * @param bic BIC
93     */
94    @ParameterizedTest
95    @ValueSource(strings = {"POWSDE10XXX", "POWSZZ30XXX"})
96    /* default */ void testBicWrong(final String bic)
97     {
98      assertThrows(IllegalArgumentException.class, () ->
99       {
100       /* final BIC cleanBic = */ BIC.of(bic);
101      }, BICTests.ILLEGAL_ARGUMENT
102     );
103    }
104 
105 
106   /**
107    * Test string value.
108    */
109   @Test
110   /* default */ void testStringValue()
111    {
112     final BIC bic = BIC.of(BICTests.BIC_BELADEBEXXX);
113     assertEquals(BICTests.BIC_BELADEBEXXX, bic.stringValue(), BICTests.BIC_NOT_AS_EXPECTED);
114    }
115 
116 
117   /**
118    * Test hash code.
119    */
120   @Test
121   /* default */ void testHashCode()
122    {
123     final BIC bic1 = BIC.of(BICTests.BIC_BELADEBEXXX);
124     final BIC bic2 = BIC.of(BICTests.BIC_BELADEBEXXX);
125     final BIC bic3 = BIC.of(BICTests.BIC_RZTIAT22263);
126     assertAll("testHashCode", //$NON-NLS-1$
127       () -> assertEquals(bic1.hashCode(), bic2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
128       () -> assertNotEquals(bic1.hashCode(), bic3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
129     );
130    }
131 
132 
133   /**
134    * Test equals.
135    */
136   @Test
137   @SuppressWarnings("java:S5785")
138   /* default */ void testEquals()
139    {
140     final BIC bic1 = BIC.of(BICTests.BIC_BELADEBEXXX);
141     final BIC bic2 = BIC.of(BICTests.BIC_BELADEBEXXX);
142     final BIC bic3 = BIC.of(BICTests.BIC_RZTIAT22263);
143     final BIC bic4 = BIC.of(BICTests.BIC_BELADEBEXXX);
144     assertAll("testEquals", //$NON-NLS-1$
145       () -> assertTrue(bic1.equals(bic1), "bic11 is not equal"), //$NON-NLS-1$
146       () -> assertTrue(bic1.equals(bic2), "bic12 are not equal"), //$NON-NLS-1$
147       () -> assertTrue(bic2.equals(bic1), "bic21 are not equal"), //$NON-NLS-1$
148       () -> assertTrue(bic2.equals(bic4), "bic24 are not equal"), //$NON-NLS-1$
149       () -> assertTrue(bic1.equals(bic4), "bic14 are not equal"), //$NON-NLS-1$
150       () -> assertFalse(bic1.equals(bic3), "bic13 are equal"), //$NON-NLS-1$
151       () -> assertFalse(bic3.equals(bic1), "bic31 are equal"), //$NON-NLS-1$
152       () -> assertFalse(bic1.equals(null), "bic10 is equal") //$NON-NLS-1$
153     );
154    }
155 
156 
157   /**
158    * Test toString.
159    */
160   @Test
161   /* default */ void testToString()
162    {
163     final BIC bic = BIC.of(BICTests.BIC_BELADEBEXXX);
164     assertEquals("BIC[bic=BELADEBEXXX]", bic.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
165    }
166 
167 
168   /**
169    * Test compareTo.
170    */
171   @Test
172   @SuppressWarnings("java:S5785")
173   /* default */ void testCompareTo()
174    {
175     final BIC bic1 = BIC.of(BICTests.BIC_BELADEBEXXX);
176     final BIC bic2 = BIC.of(BICTests.BIC_BELADEBEXXX);
177     final BIC bic3 = BIC.of(BICTests.BIC_RZTIAT22263);
178     final BIC bic4 = BIC.of("UBSWCHZH80A"); //$NON-NLS-1$
179     final BIC bic5 = BIC.of(BICTests.BIC_BELADEBEXXX);
180     assertAll("testCompareTo", //$NON-NLS-1$
181       () -> assertTrue(bic1.compareTo(bic2) == -bic2.compareTo(bic1), "reflexive1"), //$NON-NLS-1$
182       () -> assertTrue(bic1.compareTo(bic3) == -bic3.compareTo(bic1), "reflexive2"), //$NON-NLS-1$
183       () -> assertTrue((bic4.compareTo(bic3) > 0) && (bic3.compareTo(bic1) > 0) && (bic4.compareTo(bic1) > 0), "transitive1"), //$NON-NLS-1$
184       () -> assertTrue((bic1.compareTo(bic2) == 0) && (Math.abs(bic1.compareTo(bic5)) == Math.abs(bic2.compareTo(bic5))), "sgn1"), //$NON-NLS-1$
185       () -> assertTrue((bic1.compareTo(bic2) == 0) && bic1.equals(bic2), "equals") //$NON-NLS-1$
186     );
187    }
188 
189  }