Split or move core-java-modules/core-java-lang-oop module (#7767)
This commit is contained in:
committed by
Josh Cummings
parent
71818a79cc
commit
ec23ab367e
@@ -8,3 +8,8 @@
|
||||
- [Anonymous Classes in Java](https://www.baeldung.com/java-anonymous-classes)
|
||||
- [Raw Types in Java](https://www.baeldung.com/raw-types-java)
|
||||
- [Java ‘private’ Access Modifier](https://www.baeldung.com/java-private-keyword)
|
||||
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)
|
||||
- [Java equals() and hashCode() Contracts](https://www.baeldung.com/java-equals-hashcode-contracts)
|
||||
- [Immutable Objects in Java](http://www.baeldung.com/java-immutable-object)
|
||||
- [Inheritance and Composition (Is-a vs Has-a relationship) in Java](http://www.baeldung.com/java-inheritance-composition)
|
||||
- [A Guide to Constructors in Java](https://www.baeldung.com/java-constructors)
|
||||
|
||||
@@ -15,16 +15,26 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nl.jqno.equalsverifier</groupId>
|
||||
<artifactId>equalsverifier</artifactId>
|
||||
<version>${equalsverifier.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<h2.version>1.4.199</h2.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class ListOfThree<E> extends AbstractList<E> {
|
||||
|
||||
private static final int LENGTH = 3;
|
||||
private Object[] elements;
|
||||
|
||||
public ListOfThree(E[] data) {
|
||||
if(data == null
|
||||
|| data.length != LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
this.elements = Arrays.copyOf(data, data.length); //shallow copy
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public E get(int index) {
|
||||
return (E)elements[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return LENGTH;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
public class SpecialCharacters {
|
||||
|
||||
public static final String SLASH = "/";
|
||||
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
package com.baeldung.accessmodifiers.publicmodifier;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
|
||||
public class Student {
|
||||
|
||||
private StudentGrade grade; //new data representation
|
||||
// private int grade; //old data representation
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public void setGrade(int grade) {
|
||||
this.grade = new StudentGrade(grade);
|
||||
}
|
||||
|
||||
public int getGrade() {
|
||||
return this.grade.getGrade().intValue(); //int is returned for backward compatibility
|
||||
}
|
||||
|
||||
public Connection getConnection() throws SQLException {
|
||||
|
||||
final String URL = "jdbc:h2:~/test";
|
||||
return DriverManager.getConnection(URL, "sa", "");
|
||||
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
if (age < 0 || age > 150) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
private class StudentGrade {
|
||||
private BigDecimal grade = BigDecimal.ZERO;
|
||||
private Date updatedAt;
|
||||
|
||||
public StudentGrade(int grade) {
|
||||
this.grade = new BigDecimal(grade);
|
||||
this.updatedAt = new Date();
|
||||
}
|
||||
|
||||
public BigDecimal getGrade() {
|
||||
return grade;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return updatedAt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class BankAccount {
|
||||
String name;
|
||||
LocalDateTime opened;
|
||||
double balance;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public LocalDateTime getOpened() {
|
||||
return opened;
|
||||
}
|
||||
|
||||
public double getBalance() {
|
||||
return this.balance;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountEmptyConstructor extends BankAccount {
|
||||
public BankAccountEmptyConstructor() {
|
||||
this.name = "";
|
||||
this.opened = LocalDateTime.now();
|
||||
this.balance = 0.0d;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountParameterizedConstructor extends BankAccount {
|
||||
public BankAccountParameterizedConstructor(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountCopyConstructor extends BankAccount {
|
||||
public BankAccountCopyConstructor(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public BankAccountCopyConstructor(BankAccount other) {
|
||||
this.name = other.name;
|
||||
this.opened = LocalDateTime.now();
|
||||
this.balance = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
class BankAccountChainedConstructors extends BankAccount {
|
||||
public BankAccountChainedConstructors(String name, LocalDateTime opened, double balance) {
|
||||
this.name = name;
|
||||
this.opened = opened;
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public BankAccountChainedConstructors(String name) {
|
||||
this(name, LocalDateTime.now(), 0.0f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
class Transaction {
|
||||
final BankAccountEmptyConstructor bankAccount;
|
||||
final LocalDateTime date;
|
||||
final double amount;
|
||||
|
||||
public Transaction(BankAccountEmptyConstructor account, LocalDateTime date, double amount) {
|
||||
this.bankAccount = account;
|
||||
this.date = date;
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
/*
|
||||
* Compilation Error :'(, all final variables must be explicitly initialised.
|
||||
* public Transaction() {
|
||||
* }
|
||||
*/
|
||||
|
||||
public void invalidMethod() {
|
||||
// this.amount = 102.03; // Results in a compiler error. You cannot change the value of a final variable.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
class Money {
|
||||
|
||||
int amount;
|
||||
String currencyCode;
|
||||
|
||||
Money(int amount, String currencyCode) {
|
||||
this.amount = amount;
|
||||
this.currencyCode = currencyCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Money))
|
||||
return false;
|
||||
Money other = (Money)o;
|
||||
boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null)
|
||||
|| (this.currencyCode != null && this.currencyCode.equals(other.currencyCode));
|
||||
return this.amount == other.amount
|
||||
&& currencyCodeEquals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + amount;
|
||||
if (currencyCode != null) {
|
||||
result = 31 * result + currencyCode.hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
class Team {
|
||||
|
||||
final String city;
|
||||
final String department;
|
||||
|
||||
Team(String city, String department) {
|
||||
this.city = city;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Team))
|
||||
return false;
|
||||
Team otherTeam = (Team)o;
|
||||
boolean cityEquals = (this.city == null && otherTeam.city == null)
|
||||
|| this.city != null && this.city.equals(otherTeam.city);
|
||||
boolean departmentEquals = (this.department == null && otherTeam.department == null)
|
||||
|| this.department != null && this.department.equals(otherTeam.department);
|
||||
return cityEquals && departmentEquals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int hashCode() {
|
||||
int result = 17;
|
||||
if (city != null) {
|
||||
result = 31 * result + city.hashCode();
|
||||
}
|
||||
if (department != null) {
|
||||
result = 31 * result + department.hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
class Voucher {
|
||||
|
||||
private Money value;
|
||||
private String store;
|
||||
|
||||
Voucher(int amount, String currencyCode, String store) {
|
||||
this.value = new Money(amount, currencyCode);
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof Voucher))
|
||||
return false;
|
||||
Voucher other = (Voucher)o;
|
||||
boolean valueEquals = (this.value == null && other.value == null)
|
||||
|| (this.value != null && this.value.equals(other.value));
|
||||
boolean storeEquals = (this.store == null && other.store == null)
|
||||
|| (this.store != null && this.store.equals(other.store));
|
||||
return valueEquals && storeEquals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
if (this.value != null) {
|
||||
result = 31 * result + value.hashCode();
|
||||
}
|
||||
if (this.store != null) {
|
||||
result = 31 * result + store.hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* This class overrides equals, but it doesn't override hashCode.
|
||||
*
|
||||
* To see which problems this leads to:
|
||||
* TeamUnitTest.givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue
|
||||
*/
|
||||
class WrongTeam {
|
||||
|
||||
String city;
|
||||
String department;
|
||||
|
||||
WrongTeam(String city, String department) {
|
||||
this.city = city;
|
||||
this.department = department;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof WrongTeam))
|
||||
return false;
|
||||
WrongTeam otherTeam = (WrongTeam)o;
|
||||
return this.city == otherTeam.city
|
||||
&& this.department == otherTeam.department;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* This class extends the Money class that has overridden the equals method and once again overrides the equals method.
|
||||
*
|
||||
* To see which problems this leads to:
|
||||
* MoneyUnitTest.givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric
|
||||
*/
|
||||
class WrongVoucher extends Money {
|
||||
|
||||
private String store;
|
||||
|
||||
WrongVoucher(int amount, String currencyCode, String store) {
|
||||
super(amount, currencyCode);
|
||||
|
||||
this.store = store;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this)
|
||||
return true;
|
||||
if (!(o instanceof WrongVoucher))
|
||||
return false;
|
||||
WrongVoucher other = (WrongVoucher)o;
|
||||
boolean currencyCodeEquals = (this.currencyCode == null && other.currencyCode == null)
|
||||
|| (this.currencyCode != null && this.currencyCode.equals(other.currencyCode));
|
||||
boolean storeEquals = (this.store == null && other.store == null)
|
||||
|| (this.store != null && this.store.equals(other.store));
|
||||
return this.amount == other.amount
|
||||
&& currencyCodeEquals
|
||||
&& storeEquals;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 17;
|
||||
result = 31 * result + amount;
|
||||
if (this.currencyCode != null) {
|
||||
result = 31 * result + currencyCode.hashCode();
|
||||
}
|
||||
if (this.store != null) {
|
||||
result = 31 * result + store.hashCode();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ComplexClass {
|
||||
|
||||
private List<?> genericList;
|
||||
private Set<Integer> integerSet;
|
||||
|
||||
public ComplexClass(List<?> genericArrayList, Set<Integer> integerHashSet) {
|
||||
super();
|
||||
this.genericList = genericArrayList;
|
||||
this.integerSet = integerHashSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((genericList == null) ? 0 : genericList.hashCode());
|
||||
result = prime * result + ((integerSet == null) ? 0 : integerSet.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (!(obj instanceof ComplexClass))
|
||||
return false;
|
||||
ComplexClass other = (ComplexClass) obj;
|
||||
if (genericList == null) {
|
||||
if (other.genericList != null)
|
||||
return false;
|
||||
} else if (!genericList.equals(other.genericList))
|
||||
return false;
|
||||
if (integerSet == null) {
|
||||
if (other.integerSet != null)
|
||||
return false;
|
||||
} else if (!integerSet.equals(other.integerSet))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected List<?> getGenericList() {
|
||||
return genericList;
|
||||
}
|
||||
|
||||
protected void setGenericArrayList(List<?> genericList) {
|
||||
this.genericList = genericList;
|
||||
}
|
||||
|
||||
protected Set<Integer> getIntegerSet() {
|
||||
return integerSet;
|
||||
}
|
||||
|
||||
protected void setIntegerSet(Set<Integer> integerSet) {
|
||||
this.integerSet = integerSet;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public class PrimitiveClass {
|
||||
|
||||
private boolean primitiveBoolean;
|
||||
private int primitiveInt;
|
||||
|
||||
public PrimitiveClass(boolean primitiveBoolean, int primitiveInt) {
|
||||
super();
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
|
||||
protected boolean isPrimitiveBoolean() {
|
||||
return primitiveBoolean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + (primitiveBoolean ? 1231 : 1237);
|
||||
result = prime * result + primitiveInt;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
PrimitiveClass other = (PrimitiveClass) obj;
|
||||
if (primitiveBoolean != other.primitiveBoolean)
|
||||
return false;
|
||||
if (primitiveInt != other.primitiveInt)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void setPrimitiveBoolean(boolean primitiveBoolean) {
|
||||
this.primitiveBoolean = primitiveBoolean;
|
||||
}
|
||||
|
||||
protected int getPrimitiveInt() {
|
||||
return primitiveInt;
|
||||
}
|
||||
|
||||
protected void setPrimitiveInt(int primitiveInt) {
|
||||
this.primitiveInt = primitiveInt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public class Rectangle extends Shape {
|
||||
private double width;
|
||||
private double length;
|
||||
|
||||
public Rectangle(double width, double length) {
|
||||
this.width = width;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double area() {
|
||||
return width * length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double perimeter() {
|
||||
return 2 * (width + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(length);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
temp = Double.doubleToLongBits(width);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Rectangle other = (Rectangle) obj;
|
||||
if (Double.doubleToLongBits(length) != Double.doubleToLongBits(other.length))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(width) != Double.doubleToLongBits(other.width))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected double getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
protected double getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
public abstract class Shape {
|
||||
public abstract double area();
|
||||
|
||||
public abstract double perimeter();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class Square extends Rectangle {
|
||||
|
||||
private Color color;
|
||||
|
||||
public Square(double width, Color color) {
|
||||
super(width, width);
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = super.hashCode();
|
||||
result = prime * result + ((color == null) ? 0 : color.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!super.equals(obj)) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof Square)) {
|
||||
return false;
|
||||
}
|
||||
Square other = (Square) obj;
|
||||
if (color == null) {
|
||||
if (other.color != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!color.equals(other.color)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
protected void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,21 @@
|
||||
package com.baeldung.inheritancecomposition.application;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.*;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Person person = new Person("John", "john@domain.com", 35);
|
||||
Waitress waitress = new Waitress("Mary", "mary@domain.com", 22);
|
||||
System.out.println(waitress.serveStarter("mixed salad"));
|
||||
System.out.println(waitress.serveMainCourse("steak"));
|
||||
System.out.println(waitress.serveDessert("cup of cofee"));
|
||||
Actress actress = new Actress("Susan", "susan@domain.com", 30);
|
||||
System.out.println(actress.readScript("Psycho"));
|
||||
System.out.println(actress.performRole());
|
||||
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
|
||||
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
|
||||
System.out.println(computer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Actress extends Person {
|
||||
|
||||
public Actress(String name, String email, int age) {
|
||||
super(name, email, age);
|
||||
}
|
||||
|
||||
public String readScript(String movie) {
|
||||
return "Reading the script of " + movie;
|
||||
}
|
||||
|
||||
public String performRole() {
|
||||
return "Performing a role";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Computer {
|
||||
|
||||
private Processor processor;
|
||||
private Memory memory;
|
||||
private SoundCard soundCard;
|
||||
|
||||
public Computer(Processor processor, Memory memory) {
|
||||
this.processor = processor;
|
||||
this.memory = memory;
|
||||
}
|
||||
|
||||
public void setSoundCard(SoundCard soundCard) {
|
||||
this.soundCard = soundCard;
|
||||
}
|
||||
|
||||
public Processor getProcessor() {
|
||||
return processor;
|
||||
}
|
||||
|
||||
public Memory getMemory() {
|
||||
return memory;
|
||||
}
|
||||
|
||||
public Optional<SoundCard> getSoundCard() {
|
||||
return Optional.ofNullable(soundCard);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface Memory {
|
||||
|
||||
String getBrand();
|
||||
|
||||
String getSize();
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final String email;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, String email, int age) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface Processor {
|
||||
|
||||
String getModel();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface SoundCard {
|
||||
|
||||
String getBrand();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardMemory implements Memory {
|
||||
|
||||
private String brand;
|
||||
private String size;
|
||||
|
||||
public StandardMemory(String brand, String size) {
|
||||
this.brand = brand;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public String getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardProcessor implements Processor {
|
||||
|
||||
private String model;
|
||||
|
||||
public StandardProcessor(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Processor{" + "model=" + model + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardSoundCard implements SoundCard {
|
||||
|
||||
private String brand;
|
||||
|
||||
public StandardSoundCard(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SoundCard{" + "brand=" + brand + "}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Waitress extends Person {
|
||||
|
||||
public Waitress(String name, String email, int age) {
|
||||
super(name, email, age);
|
||||
}
|
||||
|
||||
public String serveStarter(String starter) {
|
||||
return "Serving a " + starter;
|
||||
}
|
||||
|
||||
public String serveMainCourse(String mainCourse) {
|
||||
return "Serving a " + mainCourse;
|
||||
}
|
||||
|
||||
public String serveDessert(String dessert) {
|
||||
return "Serving a " + dessert;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.markerinterface;
|
||||
|
||||
public interface DeletableShape extends Shape {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.markerinterface;
|
||||
|
||||
public class Rectangle implements DeletableShape {
|
||||
|
||||
private double width;
|
||||
private double height;
|
||||
|
||||
public Rectangle(double width, double height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getArea() {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getCircumference() {
|
||||
return 2 * (width + height);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.markerinterface;
|
||||
|
||||
public interface Shape {
|
||||
double getArea();
|
||||
double getCircumference();
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.markerinterface;
|
||||
|
||||
public class ShapeDao {
|
||||
|
||||
public boolean delete(Object object) {
|
||||
if (!(object instanceof DeletableShape)) {
|
||||
return false;
|
||||
}
|
||||
// Calling the code that deletes the entity from the database
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Car {
|
||||
|
||||
private List<Wheel> wheels;
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CarWithStaticInnerWheel {
|
||||
|
||||
private List<Wheel> wheels;
|
||||
|
||||
public static class Wheel {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.relationships.aggregation;
|
||||
|
||||
public class Wheel {
|
||||
|
||||
private Car car;
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.relationships.association;
|
||||
|
||||
public class Child {
|
||||
|
||||
private Mother mother;
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.relationships.association;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Mother {
|
||||
|
||||
private List<Child> children;
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.relationships.composition;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Building {
|
||||
|
||||
private String address;
|
||||
private List<Room> rooms;
|
||||
|
||||
public class Room {
|
||||
|
||||
public String getBuildingAddress() {
|
||||
return Building.this.address;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.relationships.composition;
|
||||
|
||||
public class BuildingWithDefinitionRoomInMethod {
|
||||
|
||||
public Room createAnonymousRoom() {
|
||||
return new Room() {
|
||||
@Override
|
||||
public void doInRoom() {}
|
||||
};
|
||||
}
|
||||
|
||||
public Room createInlineRoom() {
|
||||
class InlineRoom implements Room {
|
||||
@Override
|
||||
public void doInRoom() {}
|
||||
}
|
||||
return new InlineRoom();
|
||||
}
|
||||
|
||||
public Room createLambdaRoom() {
|
||||
return () -> {};
|
||||
}
|
||||
|
||||
public interface Room {
|
||||
void doInRoom();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Department {
|
||||
|
||||
private List<Professor> professors;
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Professor {
|
||||
|
||||
private List<Department> department;
|
||||
private List<Professor> friends;
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.relationships.university;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class University {
|
||||
|
||||
private List<Department> department;
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
package com.baeldung.accessmodifiers;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
import com.baeldung.accessmodifiers.publicmodifier.ListOfThree;
|
||||
import com.baeldung.accessmodifiers.publicmodifier.Student;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
public class PublicAccessModifierUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingBigDecimalIntValueMethod_correspondingIntIsReturned() {
|
||||
assertEquals(0, new BigDecimal(0).intValue()); //instance member
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingIntegerMaxValueField_maxPossibleIntValueIsReturned() {
|
||||
assertEquals(2147483647, Integer.MAX_VALUE); //static field
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenChangingStudentInternalRepresentation_clientCodeWillNotBreak() {
|
||||
|
||||
Student student = new Student();
|
||||
student.setGrade(100);
|
||||
|
||||
assertEquals(100, student.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEntrySet_keyValuePairsAreReturned() {
|
||||
|
||||
Map<String, String> mapObject = new HashMap<String, String>();
|
||||
mapObject.put("name", "Alex");
|
||||
|
||||
for(Map.Entry<String, String> entry : mapObject.entrySet()) {
|
||||
assertEquals("name", entry.getKey());
|
||||
assertEquals("Alex", entry.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStringToLowerCase_stringTurnsToLowerCase() {
|
||||
assertEquals("alex", "ALEX".toLowerCase());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParsingStringOne_parseIntReturns1() {
|
||||
assertEquals(1, Integer.parseInt("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenConnectingToH2_connectionInstanceIsReturned() throws SQLException {
|
||||
|
||||
final String url = "jdbc:h2:~/test";
|
||||
Connection conn = DriverManager.getConnection(url, "sa", "");
|
||||
assertNotNull(conn);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreatingCustomList_concreteAndInheritedMethodsWork() {
|
||||
|
||||
String[] dataSet1 = new String[] {"zero", "one", "two"};
|
||||
|
||||
List<String> list1 = new ListOfThree<String>(dataSet1);
|
||||
|
||||
//our implemented methods
|
||||
assertEquals("one", list1.get(1));
|
||||
assertEquals(3, list1.size());
|
||||
|
||||
//inherited implementations
|
||||
assertEquals(1, list1.indexOf("one"));
|
||||
|
||||
String[] dataSet2 = new String[] {"two", "zero", "one"};
|
||||
List<String> list2 = new ListOfThree<String>(dataSet2);
|
||||
|
||||
assertTrue(list1.containsAll(list2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.constructors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Month;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class ConstructorUnitTest {
|
||||
final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName());
|
||||
|
||||
@Test
|
||||
public void givenNoExplicitContructor_whenUsed_thenFails() {
|
||||
BankAccount account = new BankAccount();
|
||||
assertThatThrownBy(() -> {
|
||||
account.toString();
|
||||
}).isInstanceOf(Exception.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNoArgumentConstructor_whenUsed_thenSucceeds() {
|
||||
BankAccountEmptyConstructor account = new BankAccountEmptyConstructor();
|
||||
assertThatCode(() -> {
|
||||
account.toString();
|
||||
}).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParameterisedConstructor_whenUsed_thenSucceeds() {
|
||||
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
|
||||
BankAccountParameterizedConstructor account =
|
||||
new BankAccountParameterizedConstructor("Tom", opened, 1000.0f);
|
||||
|
||||
assertThatCode(() -> {
|
||||
account.toString();
|
||||
}).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyContructor_whenUser_thenMaintainsLogic() {
|
||||
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
|
||||
BankAccountCopyConstructor account = new BankAccountCopyConstructor("Tim", opened, 1000.0f);
|
||||
BankAccountCopyConstructor newAccount = new BankAccountCopyConstructor(account);
|
||||
|
||||
assertThat(account.getName()).isEqualTo(newAccount.getName());
|
||||
assertThat(account.getOpened()).isNotEqualTo(newAccount.getOpened());
|
||||
|
||||
assertThat(newAccount.getBalance()).isEqualTo(0.0f);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenChainedConstructor_whenUsed_thenMaintainsLogic() {
|
||||
BankAccountChainedConstructors account = new BankAccountChainedConstructors("Tim");
|
||||
BankAccountChainedConstructors newAccount = new BankAccountChainedConstructors("Tim", LocalDateTime.now(), 0.0f);
|
||||
|
||||
assertThat(account.getName()).isEqualTo(newAccount.getName());
|
||||
assertThat(account.getBalance()).isEqualTo(newAccount.getBalance());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class MoneyUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMoneyInstancesWithSameAmountAndCurrency_whenEquals_thenReturnsTrue() {
|
||||
Money income = new Money(55, "USD");
|
||||
Money expenses = new Money(55, "USD");
|
||||
|
||||
assertTrue(income.equals(expenses));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMoneyAndVoucherInstances_whenEquals_thenReturnValuesArentSymmetric() {
|
||||
Money cash = new Money(42, "USD");
|
||||
WrongVoucher voucher = new WrongVoucher(42, "USD", "Amazon");
|
||||
|
||||
assertFalse(voucher.equals(cash));
|
||||
assertTrue(cash.equals(voucher));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.equalshashcode;
|
||||
|
||||
import nl.jqno.equalsverifier.EqualsVerifier;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class TeamUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMapKeyWithHashCode_whenSearched_thenReturnsCorrectValue() {
|
||||
Map<Team,String> leaders = new HashMap<>();
|
||||
leaders.put(new Team("New York", "development"), "Anne");
|
||||
leaders.put(new Team("Boston", "development"), "Brian");
|
||||
leaders.put(new Team("Boston", "marketing"), "Charlie");
|
||||
|
||||
Team myTeam = new Team("New York", "development");
|
||||
String myTeamleader = leaders.get(myTeam);
|
||||
|
||||
assertEquals("Anne", myTeamleader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMapKeyWithoutHashCode_whenSearched_thenReturnsWrongValue() {
|
||||
Map<WrongTeam,String> leaders = new HashMap<>();
|
||||
leaders.put(new WrongTeam("New York", "development"), "Anne");
|
||||
leaders.put(new WrongTeam("Boston", "development"), "Brian");
|
||||
leaders.put(new WrongTeam("Boston", "marketing"), "Charlie");
|
||||
|
||||
WrongTeam myTeam = new WrongTeam("New York", "development");
|
||||
String myTeamleader = leaders.get(myTeam);
|
||||
|
||||
assertFalse("Anne".equals(myTeamleader));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsContract() {
|
||||
EqualsVerifier.forClass(Team.class).verify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
public class ComplexClassUnitTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
List<String> strArrayList = new ArrayList<String>();
|
||||
strArrayList.add("abc");
|
||||
strArrayList.add("def");
|
||||
ComplexClass aObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
|
||||
ComplexClass bObject = new ComplexClass(strArrayList, new HashSet<Integer>(45, 67));
|
||||
|
||||
List<String> strArrayListD = new ArrayList<String>();
|
||||
strArrayListD.add("lmn");
|
||||
strArrayListD.add("pqr");
|
||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
|
||||
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrimitiveClassUnitTest {
|
||||
|
||||
@Test
|
||||
public void testTwoEqualsObjects() {
|
||||
|
||||
PrimitiveClass aObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.equalshashcode.entities;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class SquareClassUnitTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsAndHashcodes() {
|
||||
Square aObject = new Square(10, Color.BLUE);
|
||||
Square bObject = new Square(10, Color.BLUE);
|
||||
|
||||
Square dObject = new Square(20, Color.BLUE);
|
||||
|
||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||
|
||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||
|
||||
Assert.assertFalse(aObject.equals(dObject));
|
||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.immutableobjects;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
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,42 @@
|
||||
package com.baeldung.inheritancecomposition;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Actress;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class ActressUnitTest {
|
||||
|
||||
private static Actress actress;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpActressInstance() {
|
||||
actress = new Actress("Susan", "susan@domain.com", 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetName_thenEqual() {
|
||||
assertThat(actress.getName()).isEqualTo("Susan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetEmail_thenEqual() {
|
||||
assertThat(actress.getEmail()).isEqualTo("susan@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetAge_thenEqual() {
|
||||
assertThat(actress.getAge()).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledreadScript_thenEqual() {
|
||||
assertThat(actress.readScript("Psycho")).isEqualTo("Reading the script of Psycho");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledperfomRole_thenEqual() {
|
||||
assertThat(actress.performRole()).isEqualTo("Performing a role");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.inheritancecomposition;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CompositionUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenComputerInstance_whenExtractedEachField_thenThreeAssertions() {
|
||||
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
|
||||
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
|
||||
assertThat(computer.getProcessor()).isInstanceOf(Processor.class);
|
||||
assertThat(computer.getMemory()).isInstanceOf(Memory.class);
|
||||
assertThat(computer.getSoundCard()).isInstanceOf(Optional.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.inheritancecomposition;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Actress;
|
||||
import com.baeldung.inheritancecomposition.model.Person;
|
||||
import com.baeldung.inheritancecomposition.model.Waitress;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class InheritanceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCheckedType_thenIsInstanceOfPerson() {
|
||||
assertThat(new Waitress("Mary", "mary@domain.com", 22)).isInstanceOf(Person.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCheckedType_thenIsInstanceOfPerson() {
|
||||
assertThat(new Actress("Susan", "susan@domain.com", 30)).isInstanceOf(Person.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.inheritancecomposition;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Person;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class PersonUnitTest {
|
||||
|
||||
private static Person person;
|
||||
|
||||
@BeforeClass
|
||||
public static void setPersonInstance() {
|
||||
person = new Person("John", "john@domain.com", 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetName_thenEqual() {
|
||||
assertThat(person.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetEmail_thenEqual() {
|
||||
assertThat(person.getEmail()).isEqualTo("john@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetAge_thenEqual() {
|
||||
assertThat(person.getAge()).isEqualTo(35);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.inheritancecomposition;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Waitress;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class WaitressUnitTest {
|
||||
|
||||
private static Waitress waitress;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpWaitressInstance() {
|
||||
waitress = new Waitress("Mary", "mary@domain.com", 22);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetName_thenOneAssertion() {
|
||||
assertThat(waitress.getName()).isEqualTo("Mary");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetEmail_thenOneAssertion() {
|
||||
assertThat(waitress.getEmail()).isEqualTo("mary@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetAge_thenOneAssertion() {
|
||||
assertThat(waitress.getAge()).isEqualTo(22);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveStarter_thenOneAssertion() {
|
||||
assertThat(waitress.serveStarter("mixed salad")).isEqualTo("Serving a mixed salad");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveMainCourse_thenOneAssertion() {
|
||||
assertThat(waitress.serveMainCourse("steak")).isEqualTo("Serving a steak");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveDessert_thenOneAssertion() {
|
||||
assertThat(waitress.serveDessert("cup of coffee")).isEqualTo("Serving a cup of coffee");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.markerinterface;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MarkerInterfaceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDeletableObjectThenTrueReturned() {
|
||||
ShapeDao shapeDao = new ShapeDao();
|
||||
Object rectangle = new Rectangle(2, 3);
|
||||
|
||||
boolean result = shapeDao.delete(rectangle);
|
||||
assertEquals(true, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonDeletableObjectThenFalseReturned() {
|
||||
ShapeDao shapeDao = new ShapeDao();
|
||||
Object object = new Object();
|
||||
|
||||
boolean result = shapeDao.delete(object);
|
||||
assertEquals(false, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user