BAEL-3184 (#7505)
- Moved operator related modules from core-java-lang-syntax to core-java-lang-operators
This commit is contained in:
@@ -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,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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user