Move articles out of core-java-lang part 2
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
package com.baeldung.controlstructures;
|
||||
|
||||
public class ConditionalBranches {
|
||||
|
||||
/**
|
||||
* Multiple if/else/else if statements examples. Shows different syntax usage.
|
||||
*/
|
||||
public static void ifElseStatementsExamples() {
|
||||
int count = 2; // Initial count value.
|
||||
|
||||
// Basic syntax. Only one statement follows. No brace usage.
|
||||
if (count > 1)
|
||||
System.out.println("Count is higher than 1");
|
||||
|
||||
// Basic syntax. More than one statement can be included. Braces are used (recommended syntax).
|
||||
if (count > 1) {
|
||||
System.out.println("Count is higher than 1");
|
||||
System.out.println("Count is equal to: " + count);
|
||||
}
|
||||
|
||||
// If/Else syntax. Two different courses of action can be included.
|
||||
if (count > 2) {
|
||||
System.out.println("Count is higher than 2");
|
||||
} else {
|
||||
System.out.println("Count is lower or equal than 2");
|
||||
}
|
||||
|
||||
// If/Else/Else If syntax. Three or more courses of action can be included.
|
||||
if (count > 2) {
|
||||
System.out.println("Count is higher than 2");
|
||||
} else if (count <= 0) {
|
||||
System.out.println("Count is less or equal than zero");
|
||||
} else {
|
||||
System.out.println("Count is either equal to one, or two");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ternary Operator example.
|
||||
* @see ConditionalBranches#ifElseStatementsExamples()
|
||||
*/
|
||||
public static void ternaryExample() {
|
||||
int count = 2;
|
||||
System.out.println(count > 2 ? "Count is higher than 2" : "Count is lower or equal than 2");
|
||||
}
|
||||
|
||||
/**
|
||||
* Switch structure example. Shows how to replace multiple if/else statements with one structure.
|
||||
*/
|
||||
public static void switchExample() {
|
||||
int count = 3;
|
||||
switch (count) {
|
||||
case 0:
|
||||
System.out.println("Count is equal to 0");
|
||||
break;
|
||||
case 1:
|
||||
System.out.println("Count is equal to 1");
|
||||
break;
|
||||
case 2:
|
||||
System.out.println("Count is equal to 2");
|
||||
break;
|
||||
default:
|
||||
System.out.println("Count is either negative, or higher than 2");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
package com.baeldung.controlstructures;
|
||||
|
||||
public class Loops {
|
||||
|
||||
/**
|
||||
* Dummy method. Only prints a generic message.
|
||||
*/
|
||||
private static void methodToRepeat() {
|
||||
System.out.println("Dummy method.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows how to iterate 50 times with 3 different method/control structures.
|
||||
*/
|
||||
public static void repetitionTo50Examples() {
|
||||
for (int i = 1; i <= 50; i++) {
|
||||
methodToRepeat();
|
||||
}
|
||||
|
||||
int whileCounter = 1;
|
||||
while (whileCounter <= 50) {
|
||||
methodToRepeat();
|
||||
whileCounter++;
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
do {
|
||||
methodToRepeat();
|
||||
count++;
|
||||
} while (count < 50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a sentence in words, and prints each word in a new line.
|
||||
* @param sentence Sentence to print as independent words.
|
||||
*/
|
||||
public static void printWordByWord(String sentence) {
|
||||
for (String word : sentence.split(" ")) {
|
||||
System.out.println(word);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints text an N amount of times. Shows usage of the {@code break} branching statement.
|
||||
* @param textToPrint Text to repeatedly print.
|
||||
* @param times Amount to times to print received text.
|
||||
*/
|
||||
public static void printTextNTimes(String textToPrint, int times) {
|
||||
int counter = 1;
|
||||
while (true) {
|
||||
System.out.println(textToPrint);
|
||||
if (counter == times) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints text an N amount of times, up to 50. Shows usage of the {@code break} branching statement.
|
||||
* @param textToPrint Text to repeatedly print.
|
||||
* @param times Amount to times to print received text. If times is higher than 50, textToPrint will only be printed 50 times.
|
||||
*/
|
||||
public static void printTextNTimesUpTo50(String textToPrint, int times) {
|
||||
int counter = 1;
|
||||
while (counter < 50) {
|
||||
System.out.println(textToPrint);
|
||||
if (counter == times) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the index of {@code name} in a list
|
||||
* @param name The name to look for
|
||||
* @param names The list of names
|
||||
* @return The index where the name was found or -1 otherwise
|
||||
*/
|
||||
public static int findFirstInstanceOfName(String name, String[] names) {
|
||||
int index = 0;
|
||||
for ( ; index < names.length; index++) {
|
||||
if (names[index].equals(name)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return index == names.length ? -1 : index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes several names and makes a list, skipping the specified {@code name}.
|
||||
*
|
||||
* @param name The name to skip
|
||||
* @param names The list of names
|
||||
* @return The list of names as a single string, missing the specified {@code name}.
|
||||
*/
|
||||
public static String makeListSkippingName(String name, String[] names) {
|
||||
String list = "";
|
||||
for (int i = 0; i < names.length; i++) {
|
||||
if (names[i].equals(name)) {
|
||||
continue;
|
||||
}
|
||||
list += names[i];
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints an specified amount of even numbers. Shows usage of both {@code break} and {@code continue} branching statements.
|
||||
* @param amountToPrint Amount of even numbers to print.
|
||||
*/
|
||||
public static void printEvenNumbers(int amountToPrint) {
|
||||
if (amountToPrint <= 0) { // Invalid input
|
||||
return;
|
||||
}
|
||||
int iterator = 0;
|
||||
int amountPrinted = 0;
|
||||
while (true) {
|
||||
if (iterator % 2 == 0) { // Is an even number
|
||||
System.out.println(iterator);
|
||||
amountPrinted++;
|
||||
iterator++;
|
||||
} else {
|
||||
iterator++;
|
||||
continue; // Won't print
|
||||
}
|
||||
if (amountPrinted == amountToPrint) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints an specified amount of even numbers, up to 100. Shows usage of both {@code break} and {@code continue} branching statements.
|
||||
* @param amountToPrint Amount of even numbers to print.
|
||||
*/
|
||||
public static void printEvenNumbersToAMaxOf100(int amountToPrint) {
|
||||
if (amountToPrint <= 0) { // Invalid input
|
||||
return;
|
||||
}
|
||||
int iterator = 0;
|
||||
int amountPrinted = 0;
|
||||
while (amountPrinted < 100) {
|
||||
if (iterator % 2 == 0) { // Is an even number
|
||||
System.out.println(iterator);
|
||||
amountPrinted++;
|
||||
iterator++;
|
||||
} else {
|
||||
iterator++;
|
||||
continue; // Won't print
|
||||
}
|
||||
if (amountPrinted == amountToPrint) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.nativekeyword;
|
||||
|
||||
public class DateTimeUtils {
|
||||
|
||||
public native String getSystemTime();
|
||||
|
||||
static {
|
||||
System.loadLibrary("nativedatetimeutils");
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.nativekeyword;
|
||||
|
||||
import com.baeldung.nativekeyword.DateTimeUtils;
|
||||
|
||||
public class NativeMainApp {
|
||||
public static void main(String[] args) {
|
||||
DateTimeUtils dateTimeUtils = new DateTimeUtils();
|
||||
String input = dateTimeUtils.getSystemTime();
|
||||
System.out.println("System time is : " + input);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.baeldung.packages;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import com.baeldung.packages.domain.TodoItem;
|
||||
|
||||
public class TodoApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
TodoList todoList = new TodoList();
|
||||
for (int i = 0; i < 3; i++) {
|
||||
TodoItem item = new TodoItem();
|
||||
item.setId(Long.valueOf((long)i));
|
||||
item.setDescription("Todo item " + (i + 1));
|
||||
item.setDueDate(LocalDate.now().plusDays((long)(i + 1)));
|
||||
todoList.addTodoItem(item);
|
||||
}
|
||||
|
||||
todoList.getTodoItems().forEach((TodoItem todoItem) -> System.out.println(todoItem.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.packages;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.packages.domain.TodoItem;
|
||||
|
||||
public class TodoList {
|
||||
private List<TodoItem> todoItems;
|
||||
|
||||
public void addTodoItem(TodoItem todoItem) {
|
||||
if (todoItems == null) {
|
||||
todoItems = new ArrayList<TodoItem>();
|
||||
}
|
||||
|
||||
todoItems.add(todoItem);
|
||||
}
|
||||
|
||||
public List<TodoItem> getTodoItems() {
|
||||
return todoItems;
|
||||
}
|
||||
|
||||
public void setTodoItems(List<TodoItem> todoItems) {
|
||||
this.todoItems = todoItems;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.baeldung.packages.domain;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class TodoItem {
|
||||
private Long id;
|
||||
private String description;
|
||||
private LocalDate dueDate;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public LocalDate getDueDate() {
|
||||
return dueDate;
|
||||
}
|
||||
|
||||
public void setDueDate(LocalDate dueDate) {
|
||||
this.dueDate = dueDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TodoItem [id=" + id + ", description=" + description + ", dueDate=" + dueDate + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.scope;
|
||||
|
||||
public class BracketScopeExample {
|
||||
|
||||
public void mathOperationExample() {
|
||||
Integer sum = 0;
|
||||
{
|
||||
Integer number = 2;
|
||||
sum = sum + number;
|
||||
}
|
||||
// compiler error, number cannot be solved as a variable
|
||||
// number++;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.scope;
|
||||
|
||||
public class ClassScopeExample {
|
||||
|
||||
Integer amount = 0;
|
||||
|
||||
public void exampleMethod() {
|
||||
amount++;
|
||||
}
|
||||
|
||||
public void anotherExampleMethod() {
|
||||
Integer anotherAmount = amount + 4;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.scope;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class LoopScopeExample {
|
||||
|
||||
List<String> listOfNames = Arrays.asList("Joe", "Susan", "Pattrick");
|
||||
|
||||
public void iterationOfNames() {
|
||||
String allNames = "";
|
||||
for (String name : listOfNames) {
|
||||
allNames = allNames + " " + name;
|
||||
}
|
||||
// compiler error, name cannot be resolved to a variable
|
||||
// String lastNameUsed = name;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.scope;
|
||||
|
||||
public class MethodScopeExample {
|
||||
|
||||
public void methodA() {
|
||||
Integer area = 2;
|
||||
}
|
||||
|
||||
public void methodB() {
|
||||
// compiler error, area cannot be resolved to a variable
|
||||
// area = area + 2;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.scope;
|
||||
|
||||
public class NestedScopesExample {
|
||||
|
||||
String title = "Baeldung";
|
||||
|
||||
public void printTitle() {
|
||||
System.out.println(title);
|
||||
String title = "John Doe";
|
||||
System.out.println(title);
|
||||
}
|
||||
}
|
||||
@@ -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!
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
public class EnvironmentVariables {
|
||||
public String getPath() {
|
||||
return System.getenv("PATH");
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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: ");
|
||||
}
|
||||
}
|
||||
@@ -1,111 +0,0 @@
|
||||
package com.baeldung.compoundoperators;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CompoundOperatorsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAssignmentOperatorIsUsed_thenValueIsAssigned() {
|
||||
int x = 5;
|
||||
|
||||
assertEquals(5, x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompoundAssignmentUsed_thenSameAsSimpleAssignment() {
|
||||
int a = 3, b = 3, c = -2;
|
||||
a = a * c; // Simple assignment operator
|
||||
b *= c; // Compound assignment operator
|
||||
|
||||
assertEquals(a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAssignmentOperatorIsUsed_thenValueIsReturned() {
|
||||
long x = 1;
|
||||
long y = (x+=2);
|
||||
|
||||
assertEquals(3, y);
|
||||
assertEquals(y, x);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompoundOperatorsAreUsed_thenOperationsArePerformedAndAssigned() {
|
||||
//Simple assignment
|
||||
int x = 5; //x is 5
|
||||
|
||||
//Incrementation
|
||||
x += 5; //x is 10
|
||||
assertEquals(10, x);
|
||||
|
||||
//Decrementation
|
||||
x -= 2; //x is 8
|
||||
assertEquals(8, x);
|
||||
|
||||
//Multiplication
|
||||
x *= 2; //x is 16
|
||||
assertEquals(16, x);
|
||||
|
||||
//Division
|
||||
x /= 4; //x is 4
|
||||
assertEquals(4, x);
|
||||
|
||||
//Modulus
|
||||
x %= 3; //x is 1
|
||||
assertEquals(1, x);
|
||||
|
||||
|
||||
//Binary AND
|
||||
x &= 4; //x is 0
|
||||
assertEquals(0, x);
|
||||
|
||||
//Binary exclusive OR
|
||||
x ^= 4; //x is 4
|
||||
assertEquals(4, x);
|
||||
|
||||
//Binary inclusive OR
|
||||
x |= 8; //x is 12
|
||||
assertEquals(12, x);
|
||||
|
||||
|
||||
//Binary Left Shift
|
||||
x <<= 2; //x is 48
|
||||
assertEquals(48, x);
|
||||
|
||||
//Binary Right Shift
|
||||
x >>= 2; //x is 12
|
||||
assertEquals(12, x);
|
||||
|
||||
//Shift right zero fill
|
||||
x >>>= 1; //x is 6
|
||||
assertEquals(6, x);
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void whenArrayIsNull_thenThrowNullException() {
|
||||
int[] numbers = null;
|
||||
|
||||
//Trying Incrementation
|
||||
numbers[2] += 5;
|
||||
}
|
||||
|
||||
@Test(expected = ArrayIndexOutOfBoundsException.class)
|
||||
public void whenArrayIndexNotCorrect_thenThrowArrayIndexException() {
|
||||
int[] numbers = {0, 1};
|
||||
|
||||
//Trying Incrementation
|
||||
numbers[2] += 5;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenArrayIndexIsCorrect_thenPerformOperation() {
|
||||
int[] numbers = {0, 1};
|
||||
|
||||
//Incrementation
|
||||
numbers[1] += 5;
|
||||
assertEquals(6, numbers[1]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.nativekeyword;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class DateTimeUtilsManualTest {
|
||||
|
||||
private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(DateTimeUtilsManualTest.class);
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
System.loadLibrary("msvcr100");
|
||||
System.loadLibrary("libgcc_s_sjlj-1");
|
||||
System.loadLibrary("libstdc++-6");
|
||||
System.loadLibrary("nativedatetimeutils");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNativeLibsLoaded_thenNativeMethodIsAccessible() {
|
||||
DateTimeUtils dateTimeUtils = new DateTimeUtils();
|
||||
LOG.info("System time is : " + dateTimeUtils.getSystemTime());
|
||||
assertNotNull(dateTimeUtils.getSystemTime());
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.packages;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.packages.domain.TodoItem;
|
||||
|
||||
public class PackagesUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenTodoItemAdded_ThenSizeIncreases() {
|
||||
TodoItem todoItem = new TodoItem();
|
||||
todoItem.setId(1L);
|
||||
todoItem.setDescription("Test the Todo List");
|
||||
todoItem.setDueDate(LocalDate.now());
|
||||
TodoList todoList = new TodoList();
|
||||
todoList.addTodoItem(todoItem);
|
||||
assertEquals(1, todoList.getTodoItems().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateTimeServiceUnitTest {
|
||||
|
||||
@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 EnvironmentVariablesUnitTest {
|
||||
|
||||
@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 SystemArrayCopyUnitTest {
|
||||
|
||||
@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 SystemNanoUnitTest {
|
||||
|
||||
@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,56 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
public class SystemPropertiesUnitTest {
|
||||
|
||||
@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
|
||||
@Ignore
|
||||
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