这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions core-java-modules/core-java-arrays-operations-basic-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@
<groupId>com.baeldung.core-java-modules</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<properties>
<commons-rng.version>1.6</commons-rng.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<version>${commons-lang3.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-rng-simple</artifactId>
<version>${commons-rng.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.baeldung.array.randombytes;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
import org.junit.jupiter.api.Test;

import java.security.SecureRandom;
import java.util.Random;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RandomByteArrayTest {

int SIZE = 16;

@Test
public void givenSizeWhenGenerateUsingRandomThenOK() {
byte[] byteArray = new byte[SIZE];
Random random = new Random();
random.nextBytes(byteArray);

assertEquals(SIZE, byteArray.length);
}

@Test
public void givenSizeWhenGenerateUsingSecureRandomThenOK() {
byte[] byteArray = new byte[SIZE];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(byteArray);

assertEquals(SIZE, byteArray.length);
}

@Test
public void givenSizeWhenGenerateUsingRandomUtilsThenOK() {
byte[] byteArray = RandomUtils.nextBytes(SIZE);

assertEquals(SIZE, byteArray.length);
}

@Test
public void givenSizeWhenGenerateUsingUniformRandomProviderThenOK() {
byte[] byteArray = new byte[SIZE];
UniformRandomProvider randomProvider = RandomSource.XO_RO_SHI_RO_128_PP.create();
randomProvider.nextBytes(byteArray);

assertEquals(SIZE, byteArray.length);
}

}