BAEL3236 Fixing test cases and updating the missed version in pom.xml

This commit is contained in:
Chirag Dewan
2019-10-26 18:46:18 +05:30
parent db85c8f275
commit f52e3c9e60
20349 changed files with 1637694 additions and 0 deletions
@@ -0,0 +1,6 @@
## Relevant Articles:
- [Guide to the Diamond Operator in Java](https://www.baeldung.com/java-diamond-operator)
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof)
- [A Guide to Increment and Decrement Unary Operators in Java](https://www.baeldung.com/java-unary-operators)
@@ -0,0 +1,43 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>core-java-lang-operators</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-operators</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-operators</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>
@@ -0,0 +1,36 @@
package com.baeldung.booleanoperators;
public class Car {
private boolean diesel;
private boolean manual;
public Car(boolean diesel, boolean manual) {
this.diesel = diesel;
this.manual = manual;
}
public boolean isDiesel() {
return diesel;
}
public boolean isManual() {
return manual;
}
static Car dieselAndManualCar() {
return new Car(true, true);
}
static Car dieselAndAutomaticCar() {
return new Car(true, false);
}
static Car oilAndManualCar() {
return new Car(false, true);
}
static Car oilAndAutomaticCar() {
return new Car(false, false);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.keyword;
public class Circle extends Round implements Shape {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Ring extends Round {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Round {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public interface Shape {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Triangle implements Shape {
}
@@ -0,0 +1,69 @@
package com.baeldung.booleanoperators;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class XorUnitTest {
@Test
void givenDieselManualCar_whenXorOldSchool_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXorOldSchool_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselManualCar_whenXor_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXor_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXor_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXor_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenNumbersOneAndThree_whenXor_thenTwo() {
assertThat(1 ^ 3).isEqualTo(2);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.java.diamond;
public class Car<T extends Engine> implements Vehicle<T> {
}
@@ -0,0 +1,13 @@
package com.baeldung.java.diamond;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
public class DiamondOperatorUnitTest {
@Test
public void whenCreateCarUsingDiamondOperator_thenSuccess() {
Car<Diesel> myCar = new Car<>();
assertNotNull(myCar);
}
}
@@ -0,0 +1,10 @@
package com.baeldung.java.diamond;
public class Diesel implements Engine {
@Override
public void start() {
System.out.println("Started Diesel...");
}
}
@@ -0,0 +1,6 @@
package com.baeldung.java.diamond;
public interface Engine {
void start();
}
@@ -0,0 +1,5 @@
package com.baeldung.java.diamond;
public interface Vehicle<T extends Engine> {
}
@@ -0,0 +1,46 @@
package com.baeldung.java.doublebrace;
import org.junit.Test;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toSet;
import static org.junit.Assert.assertTrue;
public class DoubleBraceUnitTest {
@Test
public void whenInitializeSetWithoutDoubleBraces_containsElements() {
final Set<String> countries = new HashSet<>();
countries.add("India");
countries.add("USSR");
countries.add("USA");
assertTrue(countries.contains("India"));
}
@Test
public void whenInitializeSetWithDoubleBraces_containsElements() {
final Set<String> countries = new HashSet<String>() {
{
add("India");
add("USSR");
add("USA");
}
};
assertTrue(countries.contains("India"));
}
@Test
public void whenInitializeUnmodifiableSetWithDoubleBrace_containsElements() {
Set<String> countries = Stream.of("India", "USSR", "USA")
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
assertTrue(countries.contains("India"));
}
}
@@ -0,0 +1,55 @@
package com.baeldung.keyword.test;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import com.baeldung.keyword.Circle;
import com.baeldung.keyword.Ring;
import com.baeldung.keyword.Round;
import com.baeldung.keyword.Shape;
import com.baeldung.keyword.Triangle;
public class InstanceOfUnitTest {
@Test
public void giveWhenInstanceIsCorrect_thenReturnTrue() {
Ring ring = new Ring();
Assert.assertTrue("ring is instance of Round ", ring instanceof Round);
}
@Test
public void giveWhenObjectIsInstanceOfType_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle);
}
@Test
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Round", circle instanceof Round);
}
@Test
public void giveWhenTypeIsInterface_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Shape", circle instanceof Shape);
}
@Test
public void giveWhenTypeIsOfObjectType_thenReturnTrue() {
Thread thread = new Thread();
Assert.assertTrue("thread is instance of Object", thread instanceof Object);
}
@Test
public void giveWhenInstanceValueIsNull_thenReturnFalse() {
Circle circle = null;
Assert.assertFalse("circle is instance of Round", circle instanceof Round);
}
@Test
public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() {
// Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle);
}
}
@@ -0,0 +1,54 @@
package com.baeldung.modulo;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;
public class ModuloUnitTest {
@Test
public void whenIntegerDivision_thenLosesRemainder(){
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder(){
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder(){
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException(){
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException(){
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
int QUEUE_CAPACITY= 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
}
}
}
@@ -0,0 +1,43 @@
package com.baeldung.ternaryoperator;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TernaryOperatorUnitTest {
@Test
public void givenACondition_whenUsingTernaryOperator_thenItEvaluatesConditionAndReturnsAValue() {
int number = 10;
String msg = number > 10 ? "Number is greater than 10" : "Number is less than or equal to 10";
assertThat(msg).isEqualTo("Number is less than or equal to 10");
}
@Test
public void givenATrueCondition_whenUsingTernaryOperator_thenOnlyExpression1IsEvaluated() {
int exp1 = 0, exp2 = 0;
int result = 12 > 10 ? ++exp1 : ++exp2;
assertThat(exp1).isEqualTo(1);
assertThat(exp2).isEqualTo(0);
assertThat(result).isEqualTo(1);
}
@Test
public void givenAFalseCondition_whenUsingTernaryOperator_thenOnlyExpression2IsEvaluated() {
int exp1 = 0, exp2 = 0;
int result = 8 > 10 ? ++exp1 : ++exp2;
assertThat(exp1).isEqualTo(0);
assertThat(exp2).isEqualTo(1);
assertThat(result).isEqualTo(1);
}
@Test
public void givenANestedCondition_whenUsingTernaryOperator_thenCorrectValueIsReturned() {
int number = 6;
String msg = number > 10 ? "Number is greater than 10" : number > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
assertThat(msg).isEqualTo("Number is greater than 5");
}
}
@@ -0,0 +1,64 @@
package com.baeldung.unaryoperators;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class IncrementDecrementUnaryOperatorUnitTest {
@Test
public void givenAnOperand_whenUsingPreIncrementUnaryOperator_thenOperandIsIncrementedByOne() {
int operand = 1;
++operand;
assertThat(operand).isEqualTo(2);
}
@Test
public void givenANumber_whenUsingPreIncrementUnaryOperatorInEvaluation_thenNumberIsIncrementedByOne() {
int operand = 1;
int number = ++operand;
assertThat(number).isEqualTo(2);
}
@Test
public void givenAnOperand_whenUsingPreDecrementUnaryOperator_thenOperandIsDecrementedByOne() {
int operand = 1;
--operand;
assertThat(operand).isEqualTo(0);
}
@Test
public void givenANumber_whenUsingPreDecrementUnaryOperatorInEvaluation_thenNumberIsDecrementedByOne() {
int operand = 1;
int number = --operand;
assertThat(number).isEqualTo(0);
}
@Test
public void givenAnOperand_whenUsingPostIncrementUnaryOperator_thenOperandIsIncrementedByOne() {
int operand = 1;
operand++;
assertThat(operand).isEqualTo(2);
}
@Test
public void givenANumber_whenUsingPostIncrementUnaryOperatorInEvaluation_thenNumberIsSameAsOldValue() {
int operand = 1;
int number = operand++;
assertThat(number).isEqualTo(1);
}
@Test
public void givenAnOperand_whenUsingPostDecrementUnaryOperator_thenOperandIsDecrementedByOne() {
int operand = 1;
operand--;
assertThat(operand).isEqualTo(0);
}
@Test
public void givenANumber_whenUsingPostDecrementUnaryOperatorInEvaluation_thenNumberIsSameAsOldValue() {
int operand = 1;
int number = operand--;
assertThat(number).isEqualTo(1);
}
}