Merge pull request #4760 from MajewskiKrzysztof/master

BAEL-1912
This commit is contained in:
Tom Hombergs 2018-08-19 16:41:59 +02:00 committed by GitHub
commit 3d3dfd7970
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 0 deletions

View File

@ -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");
}
}

View File

@ -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 }
}
}

View File

@ -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;
}
}