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:
Kashif Masood 2018-04-07 20:35:45 +05:00 committed by Grzegorz Piwowarek
parent ce40046339
commit 13bedabb99
12 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.system;
import java.awt.event.WindowEvent;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' gc() which suggests
* JVM for garbage collection. But the usage below demonstrates how the
* method can be used.
*/
public class ChatWindow {
public void windowStateChanged(WindowEvent event) {
if (event.getNewState() == WindowEvent.WINDOW_DEACTIVATED ) {
System.gc(); // if it ends up running, great!
}
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.system;
import java.util.Date;
public class DateTimeService {
// One hour from now
public long nowPlusOneHour() {
return System.currentTimeMillis() + 3600 * 1000L;
}
// Human-readable format
public String nowPrettyPrinted() {
return new Date(System.currentTimeMillis()).toString();
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.system;
public class EnvironmentVariables {
public String getPath() {
return System.getenv("PATH");
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.system;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard error stream
* methods to show output on screen. Also unit-tests in CI environments
* don't have console output for user to see messages. But the usage below
* demonstrates how the methods can be used.
*/
public class SystemErrDemo {
public static void main(String[] args) {
// Print without 'hitting' return
System.err.print("some inline error message");
// Print and then 'hit' return
System.err.println("an error message having new line at the end");
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.system;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' exit() which will
* exit the JVM. Also unit-tests in CI environments are not meant to
* exit unit tests like that. But the usage below demonstrates how the
* method can be used.
*/
public class SystemExitDemo {
public static void main(String[] args) {
boolean error = false;
// do something and set error value
if (error) {
System.exit(1); // error case exit
} else {
System.exit(0); // normal case exit
}
// Will not do anything after exit()
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.system;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard output stream
* methods to show output on screen. Also unit-tests in CI environments
* don't have console output for user to see messages. But the usage below
* demonstrates how the methods can be used.
*/
public class SystemOutDemo {
public static void main(String[] args) throws FileNotFoundException {
// Print without 'hitting' return
System.out.print("some inline message");
// Print and then 'hit' return
System.out.println("a message having new line at the end");
// Changes output stream to send messages to file.
System.setOut(new PrintStream("file.txt"));
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.system;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Note: This class is not meant for unit-testing since it uses system
* features at low level and that it uses 'System' standard input stream
* methods to read text from user. Also unit-tests in CI environments
* don't have console input for user to type in text. But the usage below
* demonstrates how the methods can be used.
*/
public class UserCredentials {
public String readUsername(int length) throws IOException {
byte[] name = new byte[length];
System.in.read(name, 0, length); // by default, from the console
return new String(name);
}
public String readUsername() throws IOException {
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
return reader.readLine();
}
public String readUsernameWithPrompt() {
Console console = System.console();
return console == null ? null : // Console not available
console.readLine("%s", "Enter your name: ");
}
}

View File

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

View File

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

View File

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

View File

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

View File

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