3-Third commit for articles:
* to core-java-lang: * https://www.baeldung.com/java-lang-system * https://www.baeldung.com/java-type-erasure * https://www.baeldung.com/java-assert * https://www.baeldung.com/java-pass-by-value-or-pass-by-reference * https://www.baeldung.com/java-variable-method-hiding * https://www.baeldung.com/java-access-modifiers * https://www.baeldung.com/java-super * https://www.baeldung.com/java-this * https://www.baeldung.com/java-immutable-object * https://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror * https://www.baeldung.com/infinite-loops-java (changes for this were included in commit 1, with other loop-related examples)
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class Public {
|
||||
public Public() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
public class SubClass extends SuperPublic {
|
||||
public SubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in the same package or subclass.
|
||||
SuperPublic.defaultMethod(); // Available in the same package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
//Only public or default access modifiers are permitted
|
||||
public class SuperPublic {
|
||||
// Always available from anywhere
|
||||
static public void publicMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " publicMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package
|
||||
static void defaultMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " defaultMethod()");
|
||||
}
|
||||
|
||||
// Available within the same package and subclasses
|
||||
static protected void protectedMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " protectedMethod()");
|
||||
}
|
||||
|
||||
// Available within the same class only
|
||||
static private void privateMethod() {
|
||||
System.out.println(SuperPublic.class.getName() + " privateMethod()");
|
||||
}
|
||||
|
||||
// Method in the same class = has access to all members within the same class
|
||||
private void anotherPrivateMethod() {
|
||||
privateMethod();
|
||||
defaultMethod();
|
||||
protectedMethod();
|
||||
publicMethod(); // Available in the same class only.
|
||||
}
|
||||
}
|
||||
|
||||
// Only public or default access modifiers are permitted
|
||||
class SuperDefault {
|
||||
public void publicMethod() {
|
||||
System.out.println(this.getClass().getName() + " publicMethod()");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherPublic {
|
||||
public AnotherPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSubClass extends SuperPublic {
|
||||
public AnotherSubClass() {
|
||||
SuperPublic.publicMethod(); // Available everywhere.
|
||||
SuperPublic.protectedMethod(); // Available in subclass. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.accessmodifiers.another;
|
||||
|
||||
import com.baeldung.accessmodifiers.SuperPublic;
|
||||
|
||||
public class AnotherSuperPublic {
|
||||
public AnotherSuperPublic() {
|
||||
SuperPublic.publicMethod(); // Available everywhere. Let's note different package.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.assertion;
|
||||
|
||||
/**
|
||||
* Simple demonstration of using Java assert keyword.
|
||||
*/
|
||||
public class Assertion {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Assertion assertion = new Assertion();
|
||||
assertion.setup();
|
||||
}
|
||||
|
||||
public void setup() {
|
||||
Object conn = getConnection();
|
||||
assert conn != null : "Connection is null";
|
||||
|
||||
// continue with other setup ...
|
||||
}
|
||||
|
||||
// Simulate failure to get a connection; using Object
|
||||
// to avoid dependencies on JDBC or some other heavy
|
||||
// 3rd party library
|
||||
public Object getConnection() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
public final class Currency {
|
||||
|
||||
private final String value;
|
||||
|
||||
private Currency(String currencyValue) {
|
||||
value = currencyValue;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Currency of(String value) {
|
||||
return new Currency(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
// 4. Immutability in Java
|
||||
public final class Money {
|
||||
private final double amount;
|
||||
private final Currency currency;
|
||||
|
||||
public Money(double amount, Currency currency) {
|
||||
this.amount = amount;
|
||||
this.currency = currency;
|
||||
}
|
||||
|
||||
public Currency getCurrency() {
|
||||
return currency;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.keyword;
|
||||
|
||||
import com.baeldung.keyword.superkeyword.SuperSub;
|
||||
import com.baeldung.keyword.thiskeyword.KeywordUnitTest;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/14/2018.
|
||||
*/
|
||||
public class KeywordDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
KeywordUnitTest keyword = new KeywordUnitTest();
|
||||
|
||||
SuperSub child = new SuperSub("message from the child class");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.keyword.superkeyword;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/14/2018.
|
||||
*/
|
||||
public class SuperBase {
|
||||
|
||||
String message = "super class";
|
||||
|
||||
public SuperBase() {
|
||||
}
|
||||
|
||||
public SuperBase(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.keyword.superkeyword;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/15/2018.
|
||||
*/
|
||||
public class SuperSub extends SuperBase {
|
||||
|
||||
String message = "child class";
|
||||
|
||||
public SuperSub(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public SuperSub() {
|
||||
super.printMessage();
|
||||
printMessage();
|
||||
}
|
||||
|
||||
public void getParentMessage() {
|
||||
System.out.println(super.message);
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.keyword.thiskeyword;
|
||||
|
||||
public class KeywordUnitTest {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public KeywordUnitTest() {
|
||||
this("John", 27);
|
||||
this.printMessage();
|
||||
printInstance(this);
|
||||
}
|
||||
|
||||
public KeywordUnitTest(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public void printMessage() {
|
||||
System.out.println("invoked by this");
|
||||
}
|
||||
|
||||
public void printInstance(KeywordUnitTest thisKeyword) {
|
||||
System.out.println(thisKeyword);
|
||||
}
|
||||
|
||||
public KeywordUnitTest getCurrentInstance() {
|
||||
return this;
|
||||
}
|
||||
|
||||
class ThisInnerClass {
|
||||
|
||||
boolean isInnerClass = true;
|
||||
|
||||
public ThisInnerClass() {
|
||||
KeywordUnitTest thisKeyword = KeywordUnitTest.this;
|
||||
String outerString = KeywordUnitTest.this.name;
|
||||
System.out.println(this.isInnerClass);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "KeywordTest{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.noclassdeffounderror;
|
||||
|
||||
public class ClassWithInitErrors {
|
||||
static int data = 1 / 0;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.noclassdeffounderror;
|
||||
|
||||
public class NoClassDefFoundErrorExample {
|
||||
public ClassWithInitErrors getClassWithInitErrors() {
|
||||
ClassWithInitErrors test;
|
||||
try {
|
||||
test = new ClassWithInitErrors();
|
||||
} catch (Throwable t) {
|
||||
System.out.println(t);
|
||||
}
|
||||
test = new ClassWithInitErrors();
|
||||
return test;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class NonPrimitives {
|
||||
public static void main(String[] args) {
|
||||
FooClass a = new FooClass(1);
|
||||
FooClass b = new FooClass(1);
|
||||
|
||||
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
|
||||
modify(a, b);
|
||||
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
|
||||
}
|
||||
|
||||
public static void modify(FooClass a1, FooClass b1) {
|
||||
a1.num++;
|
||||
|
||||
b1 = new FooClass(1);
|
||||
b1.num++;
|
||||
}
|
||||
}
|
||||
|
||||
class FooClass {
|
||||
public int num;
|
||||
|
||||
public FooClass(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
public class Primitives {
|
||||
public static void main(String[] args) {
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
|
||||
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
|
||||
modify(x, y);
|
||||
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
|
||||
}
|
||||
|
||||
public static void modify(int x1, int y1) {
|
||||
x1 = 5;
|
||||
y1 = 10;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("base static method");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
|
||||
public class ChildMethodClass extends BaseMethodClass {
|
||||
|
||||
public static void printMessage() {
|
||||
System.out.println("child static method");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.scope.method;
|
||||
|
||||
public class MethodHidingDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ChildMethodClass.printMessage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ChildVariable extends ParentVariable {
|
||||
|
||||
String instanceVariable = "child variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class HideVariable {
|
||||
|
||||
private String message = "this is instance variable";
|
||||
|
||||
HideVariable() {
|
||||
String message = "constructor local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printLocalVariable() {
|
||||
String message = "method local variable";
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(this.message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/7/2018.
|
||||
*/
|
||||
public class ParentVariable {
|
||||
|
||||
String instanceVariable = "parent variable";
|
||||
|
||||
public void printInstanceVariable() {
|
||||
System.out.println(instanceVariable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.scope.variable;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/6/2018.
|
||||
*/
|
||||
public class VariableHidingDemo {
|
||||
public static void main(String[] args) {
|
||||
HideVariable variable = new HideVariable();
|
||||
variable.printLocalVariable();
|
||||
variable.printInstanceVariable();
|
||||
|
||||
ParentVariable parentVariable = new ParentVariable();
|
||||
ParentVariable childVariable = new ChildVariable();
|
||||
|
||||
parentVariable.printInstanceVariable();
|
||||
childVariable.printInstanceVariable();
|
||||
}
|
||||
}
|
||||
@@ -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: ");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
public class ArrayContentPrintUtil {
|
||||
|
||||
public static <E> void printArray(E[] array) {
|
||||
for (E element : array) {
|
||||
System.out.printf("%s ", element);
|
||||
}
|
||||
}
|
||||
|
||||
public static <E extends Comparable<E>> void printArray(E[] array) {
|
||||
for (E element : array) {
|
||||
System.out.printf("%s ", element);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BoundStack<E extends Comparable<E>> {
|
||||
|
||||
private E[] stackContent;
|
||||
private int total;
|
||||
|
||||
public BoundStack(int capacity) {
|
||||
this.stackContent = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public void push(E data) {
|
||||
if (total == stackContent.length) {
|
||||
resize(2 * stackContent.length);
|
||||
}
|
||||
stackContent[total++] = data;
|
||||
}
|
||||
|
||||
public E pop() {
|
||||
if (!isEmpty()) {
|
||||
E datum = stackContent[total];
|
||||
stackContent[total--] = null;
|
||||
return datum;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
Arrays.copyOf(stackContent, capacity);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return total == 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
public class IntegerStack extends Stack<Integer> {
|
||||
|
||||
public IntegerStack(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public void push(Integer value) {
|
||||
System.out.println("Pushing into my integerStack");
|
||||
super.push(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Stack<E> {
|
||||
|
||||
private E[] stackContent;
|
||||
private int total;
|
||||
|
||||
public Stack(int capacity) {
|
||||
this.stackContent = (E[]) new Object[capacity];
|
||||
}
|
||||
|
||||
public void push(E data) {
|
||||
System.out.println("In base stack push#");
|
||||
if (total == stackContent.length) {
|
||||
resize(2 * stackContent.length);
|
||||
}
|
||||
stackContent[total++] = data;
|
||||
}
|
||||
|
||||
public E pop() {
|
||||
if (!isEmpty()) {
|
||||
E datum = stackContent[total];
|
||||
stackContent[total--] = null;
|
||||
return datum;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void resize(int capacity) {
|
||||
Arrays.copyOf(stackContent, capacity);
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return total == 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.classnotfoundexception;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ClassNotFoundExceptionUnitTest {
|
||||
|
||||
@Test(expected = ClassNotFoundException.class)
|
||||
public void givenNoDriversInClassPath_whenLoadDrivers_thenClassNotFoundException() throws ClassNotFoundException {
|
||||
Class.forName("oracle.jdbc.driver.OracleDriver");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ImmutableObjectsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCallingStringReplace_thenStringDoesNotMutate() {
|
||||
// 2. What's an Immutable Object?
|
||||
final String name = "baeldung";
|
||||
final String newName = name.replace("dung", "----");
|
||||
|
||||
assertEquals("baeldung", name);
|
||||
assertEquals("bael----", newName);
|
||||
}
|
||||
|
||||
public void whenReassignFinalValue_thenCompilerError() {
|
||||
// 3. The final Keyword in Java (1)
|
||||
final String name = "baeldung";
|
||||
// name = "bael...";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddingElementToList_thenSizeChange() {
|
||||
// 3. The final Keyword in Java (2)
|
||||
final List<String> strings = new ArrayList<>();
|
||||
assertEquals(0, strings.size());
|
||||
strings.add("baeldung");
|
||||
assertEquals(1, strings.size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.noclassdeffounderror;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class NoClassDefFoundErrorUnitTest {
|
||||
|
||||
@Test(expected = NoClassDefFoundError.class)
|
||||
public void givenInitErrorInClass_whenloadClass_thenNoClassDefFoundError() {
|
||||
NoClassDefFoundErrorExample sample = new NoClassDefFoundErrorExample();
|
||||
sample.getClassWithInitErrors();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NonPrimitivesUnitTest {
|
||||
@Test
|
||||
public void whenModifyingObjects_thenOriginalObjectChanged() {
|
||||
Foo a = new Foo(1);
|
||||
Foo b = new Foo(1);
|
||||
|
||||
// Before Modification
|
||||
Assert.assertEquals(a.num, 1);
|
||||
Assert.assertEquals(b.num, 1);
|
||||
|
||||
modify(a, b);
|
||||
|
||||
// After Modification
|
||||
Assert.assertEquals(a.num, 2);
|
||||
Assert.assertEquals(b.num, 1);
|
||||
}
|
||||
|
||||
public static void modify(Foo a1, Foo b1) {
|
||||
a1.num++;
|
||||
|
||||
b1 = new Foo(1);
|
||||
b1.num++;
|
||||
}
|
||||
}
|
||||
|
||||
class Foo {
|
||||
public int num;
|
||||
|
||||
public Foo(int num) {
|
||||
this.num = num;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.parameterpassing;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrimitivesUnitTest {
|
||||
@Test
|
||||
public void whenModifyingPrimitives_thenOriginalValuesNotModified() {
|
||||
|
||||
int x = 1;
|
||||
int y = 2;
|
||||
|
||||
// Before Modification
|
||||
Assert.assertEquals(x, 1);
|
||||
Assert.assertEquals(y, 2);
|
||||
|
||||
modify(x, y);
|
||||
|
||||
// After Modification
|
||||
Assert.assertEquals(x, 1);
|
||||
Assert.assertEquals(y, 2);
|
||||
}
|
||||
|
||||
public static void modify(int x1, int y1) {
|
||||
x1 = 5;
|
||||
y1 = 10;
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.typeerasure;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class TypeErasureUnitTest {
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void givenIntegerStack_whenStringPushedAndAssignPoppedValueToInteger_shouldFail() {
|
||||
IntegerStack integerStack = new IntegerStack(5);
|
||||
Stack stack = integerStack;
|
||||
stack.push("Hello");
|
||||
Integer data = integerStack.pop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnyArray_whenInvokedPrintArray_shouldSucceed() {
|
||||
Integer[] scores = new Integer[] { 100, 200, 10, 99, 20 };
|
||||
ArrayContentPrintUtil.printArray(scores);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user