diff --git a/core-java/src/main/java/com/baeldung/system/ChatWindow.java b/core-java/src/main/java/com/baeldung/system/ChatWindow.java new file mode 100644 index 0000000000..38bb9c63be --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/ChatWindow.java @@ -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! + } + } +} diff --git a/core-java/src/main/java/com/baeldung/system/DateTimeService.java b/core-java/src/main/java/com/baeldung/system/DateTimeService.java new file mode 100644 index 0000000000..ee13462d8a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/DateTimeService.java @@ -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(); + } +} diff --git a/core-java/src/main/java/com/baeldung/system/EnvironmentVariables.java b/core-java/src/main/java/com/baeldung/system/EnvironmentVariables.java new file mode 100644 index 0000000000..53dc02a6f0 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/EnvironmentVariables.java @@ -0,0 +1,7 @@ +package com.baeldung.system; + +public class EnvironmentVariables { + public String getPath() { + return System.getenv("PATH"); + } +} diff --git a/core-java/src/main/java/com/baeldung/system/SystemErrDemo.java b/core-java/src/main/java/com/baeldung/system/SystemErrDemo.java new file mode 100644 index 0000000000..0b1aeb95be --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/SystemErrDemo.java @@ -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"); + } +} diff --git a/core-java/src/main/java/com/baeldung/system/SystemExitDemo.java b/core-java/src/main/java/com/baeldung/system/SystemExitDemo.java new file mode 100644 index 0000000000..245d5724b8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/SystemExitDemo.java @@ -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() + } +} diff --git a/core-java/src/main/java/com/baeldung/system/SystemOutDemo.java b/core-java/src/main/java/com/baeldung/system/SystemOutDemo.java new file mode 100644 index 0000000000..474c4649c9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/SystemOutDemo.java @@ -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")); + } +} diff --git a/core-java/src/main/java/com/baeldung/system/UserCredentials.java b/core-java/src/main/java/com/baeldung/system/UserCredentials.java new file mode 100644 index 0000000000..6f7debab24 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/system/UserCredentials.java @@ -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: "); + } +} diff --git a/core-java/src/test/java/com/baeldung/system/DateTimeServiceTest.java b/core-java/src/test/java/com/baeldung/system/DateTimeServiceTest.java new file mode 100644 index 0000000000..587cd4ef20 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/DateTimeServiceTest.java @@ -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()); + } +} diff --git a/core-java/src/test/java/com/baeldung/system/EnvironmentVariablesTest.java b/core-java/src/test/java/com/baeldung/system/EnvironmentVariablesTest.java new file mode 100644 index 0000000000..3722fea88f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/EnvironmentVariablesTest.java @@ -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()); + } +} diff --git a/core-java/src/test/java/com/baeldung/system/SystemArrayCopyTest.java b/core-java/src/test/java/com/baeldung/system/SystemArrayCopyTest.java new file mode 100644 index 0000000000..f54e3702c8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/SystemArrayCopyTest.java @@ -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); + } +} diff --git a/core-java/src/test/java/com/baeldung/system/SystemNanoTest.java b/core-java/src/test/java/com/baeldung/system/SystemNanoTest.java new file mode 100644 index 0000000000..bf6590da0a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/SystemNanoTest.java @@ -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); + } +} diff --git a/core-java/src/test/java/com/baeldung/system/SystemPropertiesTest.java b/core-java/src/test/java/com/baeldung/system/SystemPropertiesTest.java new file mode 100644 index 0000000000..bbae75091a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/system/SystemPropertiesTest.java @@ -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()); + } +}