Skip to content

Commit 3d3dfd7

Browse files
authored
Merge pull request eugenp#4760 from MajewskiKrzysztof/master
BAEL-1912
2 parents c246f17 + 9ea9b7a commit 3d3dfd7

3 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.java.collections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
class CollectionsEmpty {
10+
11+
@Test
12+
public void givenArrayList_whenAddingElement_addsNewElement() {
13+
ArrayList<String> mutableList = new ArrayList<>();
14+
mutableList.add("test");
15+
16+
Assert.assertEquals(mutableList.size(), 1);
17+
Assert.assertEquals(mutableList.get(0), "test");
18+
}
19+
20+
@Test(expected = UnsupportedOperationException.class)
21+
public void givenCollectionsEmptyList_whenAddingElement_throwsUnsupportedOperationException() {
22+
List<String> immutableList = Collections.emptyList();
23+
immutableList.add("test");
24+
}
25+
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
import org.junit.jupiter.api.Test
3+
import java.util.concurrent.ThreadLocalRandom
4+
import kotlin.test.assertTrue
5+
6+
class RandomNumberTest {
7+
8+
@Test
9+
fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() {
10+
val randomNumber = Math.random()
11+
assertTrue { randomNumber >=0 }
12+
assertTrue { randomNumber <= 1 }
13+
}
14+
15+
@Test
16+
fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInDefaultRanges() {
17+
val randomDouble = ThreadLocalRandom.current().nextDouble()
18+
val randomInteger = ThreadLocalRandom.current().nextInt()
19+
val randomLong = ThreadLocalRandom.current().nextLong()
20+
assertTrue { randomDouble >= 0 }
21+
assertTrue { randomDouble <= 1 }
22+
assertTrue { randomInteger >= Integer.MIN_VALUE }
23+
assertTrue { randomInteger <= Integer.MAX_VALUE }
24+
assertTrue { randomLong >= Long.MIN_VALUE }
25+
assertTrue { randomLong <= Long.MAX_VALUE }
26+
}
27+
28+
@Test
29+
fun whenRandomNumberWithKotlinJSMath_thenResultIsBetween0And1() {
30+
val randomDouble = Math.random()
31+
assertTrue { randomDouble >=0 }
32+
assertTrue { randomDouble <= 1 }
33+
}
34+
35+
@Test
36+
fun whenRandomNumberWithKotlinNumberRange_thenResultInGivenRange() {
37+
val randomInteger = (1..12).shuffled().first()
38+
assertTrue { randomInteger >= 1 }
39+
assertTrue { randomInteger <= 12 }
40+
}
41+
42+
@Test
43+
fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInGivenRanges() {
44+
val randomDouble = ThreadLocalRandom.current().nextDouble(1.0, 10.0)
45+
val randomInteger = ThreadLocalRandom.current().nextInt(1, 10)
46+
val randomLong = ThreadLocalRandom.current().nextLong(1, 10)
47+
assertTrue { randomDouble >= 1 }
48+
assertTrue { randomDouble <= 10 }
49+
assertTrue { randomInteger >= 1 }
50+
assertTrue { randomInteger <= 10 }
51+
assertTrue { randomLong >= 1 }
52+
assertTrue { randomLong <= 10 }
53+
}
54+
55+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.reactive.util;
2+
3+
import com.sun.management.OperatingSystemMXBean;
4+
5+
import java.lang.management.ManagementFactory;
6+
7+
public class CpuUtils {
8+
9+
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
10+
11+
static Double getUsage() {
12+
return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100;
13+
}
14+
15+
}

0 commit comments

Comments
 (0)