BAEL-1530 - Guide to java.lang.System (#3616)
* Different types of bean injection in Spring - eval * Different types of bean injection in Spring - eval - changed to java config * Different types of bean injection in Spring - eval - unit tests and fixes * BAEL-1530 - Guide to java.lang.System * removed eval article stuff * updated unit tests: removed sysouts and unrelated stuff * updated examples for easiness. refactoring. * fixed typos, identified by unit tests * broken into multiple unit tests
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
ce40046339
commit
13bedabb99
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateTimeServiceTest {
|
||||
|
||||
@Test
|
||||
public void givenClass_whenCalledMethods_thenNotNullInResult() {
|
||||
DateTimeService dateTimeService = new DateTimeService();
|
||||
|
||||
Assert.assertNotNull(dateTimeService.nowPlusOneHour());
|
||||
Assert.assertNotNull(dateTimeService.nowPrettyPrinted());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EnvironmentVariablesTest {
|
||||
|
||||
@Test
|
||||
public void givenEnvVars_whenReadPath_thenGetValueinResult() {
|
||||
EnvironmentVariables environmentVariables = new EnvironmentVariables();
|
||||
|
||||
Assert.assertNotNull(environmentVariables.getPath());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SystemArrayCopyTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoArraysAB_whenUseArrayCopy_thenArrayCopiedFromAToBInResult() {
|
||||
int[] a = {34, 22, 44, 2, 55, 3};
|
||||
int[] b = new int[a.length];
|
||||
|
||||
// copy all elements from a to b
|
||||
System.arraycopy(a, 0, b, 0, a.length);
|
||||
Assert.assertArrayEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArraysAB_whenUseArrayCopyPosition_thenArrayCopiedFromAToBInResult() {
|
||||
int[] a = {34, 22, 44, 2, 55, 3};
|
||||
int[] b = new int[a.length];
|
||||
|
||||
// copy 2 elements from a, starting at a[1] to b, starting at b[3]
|
||||
System.arraycopy(a, 1, b, 3, 2);
|
||||
Assert.assertArrayEquals(new int[] {0, 0, 0, 22, 44, 0}, b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SystemNanoTest {
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledNanoTime_thenGivesTimeinResult() {
|
||||
long startTime = System.nanoTime();
|
||||
// do something that takes time
|
||||
long endTime = System.nanoTime();
|
||||
|
||||
Assert.assertTrue(endTime - startTime < 10000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class SystemPropertiesTest {
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledGetProperty_thenReturnPropertyinResult() {
|
||||
Assert.assertNotNull(System.getProperty("java.vm.vendor"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledSetProperty_thenSetPropertyasResult() {
|
||||
|
||||
// set a particular property
|
||||
System.setProperty("abckey", "abcvaluefoo");
|
||||
Assert.assertEquals("abcvaluefoo", System.getProperty("abckey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledClearProperty_thenDeletePropertyasResult() {
|
||||
|
||||
// Delete a property
|
||||
System.clearProperty("abckey");
|
||||
Assert.assertNull(System.getProperty("abckey"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledGetPropertyDefaultValue_thenReturnPropertyinResult() {
|
||||
|
||||
System.clearProperty("dbHost");
|
||||
String myKey = System.getProperty("dbHost", "db.host.com");
|
||||
Assert.assertEquals("db.host.com", myKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledGetProperties_thenReturnPropertiesinResult() {
|
||||
Properties properties = System.getProperties();
|
||||
|
||||
Assert.assertNotNull(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSystem_whenCalledClearProperties_thenDeleteAllPropertiesasResult() {
|
||||
|
||||
// Clears all system properties. Use with care!
|
||||
System.getProperties().clear();
|
||||
|
||||
Assert.assertTrue(System.getProperties().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user