Skip to content

Commit 13bedab

Browse files
kashifmapivovarit
authored andcommitted
BAEL-1530 - Guide to java.lang.System (eugenp#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
1 parent ce40046 commit 13bedab

12 files changed

Lines changed: 268 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.system;
2+
3+
import java.awt.event.WindowEvent;
4+
5+
/**
6+
* Note: This class is not meant for unit-testing since it uses system
7+
* features at low level and that it uses 'System' gc() which suggests
8+
* JVM for garbage collection. But the usage below demonstrates how the
9+
* method can be used.
10+
*/
11+
public class ChatWindow {
12+
public void windowStateChanged(WindowEvent event) {
13+
if (event.getNewState() == WindowEvent.WINDOW_DEACTIVATED ) {
14+
System.gc(); // if it ends up running, great!
15+
}
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.system;
2+
3+
import java.util.Date;
4+
5+
public class DateTimeService {
6+
7+
// One hour from now
8+
public long nowPlusOneHour() {
9+
return System.currentTimeMillis() + 3600 * 1000L;
10+
}
11+
12+
// Human-readable format
13+
public String nowPrettyPrinted() {
14+
return new Date(System.currentTimeMillis()).toString();
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.system;
2+
3+
public class EnvironmentVariables {
4+
public String getPath() {
5+
return System.getenv("PATH");
6+
}
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.system;
2+
3+
/**
4+
* Note: This class is not meant for unit-testing since it uses system
5+
* features at low level and that it uses 'System' standard error stream
6+
* methods to show output on screen. Also unit-tests in CI environments
7+
* don't have console output for user to see messages. But the usage below
8+
* demonstrates how the methods can be used.
9+
*/
10+
public class SystemErrDemo {
11+
public static void main(String[] args) {
12+
// Print without 'hitting' return
13+
System.err.print("some inline error message");
14+
15+
// Print and then 'hit' return
16+
System.err.println("an error message having new line at the end");
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.system;
2+
3+
/**
4+
* Note: This class is not meant for unit-testing since it uses system
5+
* features at low level and that it uses 'System' exit() which will
6+
* exit the JVM. Also unit-tests in CI environments are not meant to
7+
* exit unit tests like that. But the usage below demonstrates how the
8+
* method can be used.
9+
*/
10+
public class SystemExitDemo {
11+
public static void main(String[] args) {
12+
boolean error = false;
13+
14+
// do something and set error value
15+
16+
if (error) {
17+
System.exit(1); // error case exit
18+
} else {
19+
System.exit(0); // normal case exit
20+
}
21+
22+
// Will not do anything after exit()
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.system;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.PrintStream;
5+
6+
/**
7+
* Note: This class is not meant for unit-testing since it uses system
8+
* features at low level and that it uses 'System' standard output stream
9+
* methods to show output on screen. Also unit-tests in CI environments
10+
* don't have console output for user to see messages. But the usage below
11+
* demonstrates how the methods can be used.
12+
*/
13+
public class SystemOutDemo {
14+
15+
public static void main(String[] args) throws FileNotFoundException {
16+
// Print without 'hitting' return
17+
System.out.print("some inline message");
18+
19+
// Print and then 'hit' return
20+
System.out.println("a message having new line at the end");
21+
22+
// Changes output stream to send messages to file.
23+
System.setOut(new PrintStream("file.txt"));
24+
}
25+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.system;
2+
3+
import java.io.BufferedReader;
4+
import java.io.Console;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
8+
/**
9+
* Note: This class is not meant for unit-testing since it uses system
10+
* features at low level and that it uses 'System' standard input stream
11+
* methods to read text from user. Also unit-tests in CI environments
12+
* don't have console input for user to type in text. But the usage below
13+
* demonstrates how the methods can be used.
14+
*/
15+
public class UserCredentials {
16+
17+
public String readUsername(int length) throws IOException {
18+
byte[] name = new byte[length];
19+
System.in.read(name, 0, length); // by default, from the console
20+
return new String(name);
21+
}
22+
23+
public String readUsername() throws IOException {
24+
BufferedReader reader =
25+
new BufferedReader(new InputStreamReader(System.in));
26+
return reader.readLine();
27+
}
28+
29+
public String readUsernameWithPrompt() {
30+
Console console = System.console();
31+
32+
return console == null ? null : // Console not available
33+
console.readLine("%s", "Enter your name: ");
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.system;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class DateTimeServiceTest {
7+
8+
@Test
9+
public void givenClass_whenCalledMethods_thenNotNullInResult() {
10+
DateTimeService dateTimeService = new DateTimeService();
11+
12+
Assert.assertNotNull(dateTimeService.nowPlusOneHour());
13+
Assert.assertNotNull(dateTimeService.nowPrettyPrinted());
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.system;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class EnvironmentVariablesTest {
7+
8+
@Test
9+
public void givenEnvVars_whenReadPath_thenGetValueinResult() {
10+
EnvironmentVariables environmentVariables = new EnvironmentVariables();
11+
12+
Assert.assertNotNull(environmentVariables.getPath());
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.system;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class SystemArrayCopyTest {
7+
8+
@Test
9+
public void givenTwoArraysAB_whenUseArrayCopy_thenArrayCopiedFromAToBInResult() {
10+
int[] a = {34, 22, 44, 2, 55, 3};
11+
int[] b = new int[a.length];
12+
13+
// copy all elements from a to b
14+
System.arraycopy(a, 0, b, 0, a.length);
15+
Assert.assertArrayEquals(a, b);
16+
}
17+
18+
@Test
19+
public void givenTwoArraysAB_whenUseArrayCopyPosition_thenArrayCopiedFromAToBInResult() {
20+
int[] a = {34, 22, 44, 2, 55, 3};
21+
int[] b = new int[a.length];
22+
23+
// copy 2 elements from a, starting at a[1] to b, starting at b[3]
24+
System.arraycopy(a, 1, b, 3, 2);
25+
Assert.assertArrayEquals(new int[] {0, 0, 0, 22, 44, 0}, b);
26+
}
27+
}

0 commit comments

Comments
 (0)