commit
3d3dfd7970
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.java.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
class CollectionsEmpty {
|
||||
|
||||
@Test
|
||||
public void givenArrayList_whenAddingElement_addsNewElement() {
|
||||
ArrayList<String> mutableList = new ArrayList<>();
|
||||
mutableList.add("test");
|
||||
|
||||
Assert.assertEquals(mutableList.size(), 1);
|
||||
Assert.assertEquals(mutableList.get(0), "test");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void givenCollectionsEmptyList_whenAddingElement_throwsUnsupportedOperationException() {
|
||||
List<String> immutableList = Collections.emptyList();
|
||||
immutableList.add("test");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class RandomNumberTest {
|
||||
|
||||
@Test
|
||||
fun whenRandomNumberWithJavaUtilMath_thenResultIsBetween0And1() {
|
||||
val randomNumber = Math.random()
|
||||
assertTrue { randomNumber >=0 }
|
||||
assertTrue { randomNumber <= 1 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInDefaultRanges() {
|
||||
val randomDouble = ThreadLocalRandom.current().nextDouble()
|
||||
val randomInteger = ThreadLocalRandom.current().nextInt()
|
||||
val randomLong = ThreadLocalRandom.current().nextLong()
|
||||
assertTrue { randomDouble >= 0 }
|
||||
assertTrue { randomDouble <= 1 }
|
||||
assertTrue { randomInteger >= Integer.MIN_VALUE }
|
||||
assertTrue { randomInteger <= Integer.MAX_VALUE }
|
||||
assertTrue { randomLong >= Long.MIN_VALUE }
|
||||
assertTrue { randomLong <= Long.MAX_VALUE }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRandomNumberWithKotlinJSMath_thenResultIsBetween0And1() {
|
||||
val randomDouble = Math.random()
|
||||
assertTrue { randomDouble >=0 }
|
||||
assertTrue { randomDouble <= 1 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRandomNumberWithKotlinNumberRange_thenResultInGivenRange() {
|
||||
val randomInteger = (1..12).shuffled().first()
|
||||
assertTrue { randomInteger >= 1 }
|
||||
assertTrue { randomInteger <= 12 }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRandomNumberWithJavaThreadLocalRandom_thenResultsInGivenRanges() {
|
||||
val randomDouble = ThreadLocalRandom.current().nextDouble(1.0, 10.0)
|
||||
val randomInteger = ThreadLocalRandom.current().nextInt(1, 10)
|
||||
val randomLong = ThreadLocalRandom.current().nextLong(1, 10)
|
||||
assertTrue { randomDouble >= 1 }
|
||||
assertTrue { randomDouble <= 10 }
|
||||
assertTrue { randomInteger >= 1 }
|
||||
assertTrue { randomInteger <= 10 }
|
||||
assertTrue { randomLong >= 1 }
|
||||
assertTrue { randomLong <= 10 }
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.reactive.util;
|
||||
|
||||
import com.sun.management.OperatingSystemMXBean;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
public class CpuUtils {
|
||||
|
||||
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
|
||||
|
||||
static Double getUsage() {
|
||||
return (operatingSystemMXBean.getSystemCpuLoad() / operatingSystemMXBean.getAvailableProcessors()) * 100;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue