View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package org.basepom.mojo.propertyhelper;
15  
16  import static org.assertj.core.api.Assertions.assertThat;
17  import static org.junit.jupiter.api.Assertions.assertNotNull;
18  import static org.junit.jupiter.api.Assertions.assertNotSame;
19  
20  import java.util.UUID;
21  
22  import org.junit.jupiter.api.Test;
23  
24  public class RandomUtilTest {
25  
26      @Test
27      public void testCreateRandomFromSeedString() {
28          String seedString = UUID.randomUUID().toString();
29  
30          var secureRandom1 = RandomUtil.createRandomFromSeed(seedString);
31          var secureRandom2 = RandomUtil.createRandomFromSeed(seedString);
32  
33          assertNotNull(secureRandom1);
34          assertNotNull(secureRandom2);
35  
36          //Verify that two different SecureRandom instances are created for each call with null parameter
37          assertNotSame(secureRandom1, secureRandom2);
38  
39          long value1 = secureRandom1.nextLong();
40          long value2 = secureRandom2.nextLong();
41          assertThat(value1).isEqualTo(value2);
42      }
43  
44      /**
45       * Tests `createRandomFromSeedString` method of `RandomUtil` class.
46       * When given a null, this method also generates a SecureRandom instance.
47       * This method specifically validates the nonnullness and the uniqueness of the returned SecureRandom instance.
48       */
49      @Test
50      public void testCreateRandomFromNull() {
51          var secureRandom1 = RandomUtil.createRandomFromSeed(null);
52          var secureRandom2 = RandomUtil.createRandomFromSeed(null);
53  
54          assertNotNull(secureRandom1);
55          assertNotNull(secureRandom2);
56  
57          //Verify that two different SecureRandom instances are created for each call with null parameter
58          assertNotSame(secureRandom1, secureRandom2);
59  
60          assertThat(secureRandom1.nextLong()).isNotEqualTo(secureRandom2.nextLong());
61      }
62  }