Merge remote-tracking branch 'upstream/master'
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class Find2ndLargestInArrayTest {
|
||||
@Test
|
||||
public void givenAnIntArray_thenFind2ndLargestElement() {
|
||||
int[] array = { 1, 3, 24, 16, 87, 20 };
|
||||
int expected2ndLargest = 24;
|
||||
|
||||
int actualSecondLargestElement = Find2ndLargestInArray.find2ndLargestElement(array);
|
||||
|
||||
Assert.assertEquals(expected2ndLargest, actualSecondLargestElement);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FindElementInArrayTest {
|
||||
@Test
|
||||
public void givenAnIntArray_whenNotUsingStream_thenFindAnElement() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
int element = 19;
|
||||
boolean expectedResult = true;
|
||||
boolean actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
|
||||
Assert.assertEquals(expectedResult, actualResult);
|
||||
|
||||
element = 78;
|
||||
expectedResult = false;
|
||||
actualResult = FindElementInArray.findGivenElementInArrayWithoutUsingStream(array, element);
|
||||
Assert.assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenUsingStream_thenFindAnElement() {
|
||||
int[] array = { 15, 16, 12, 18 };
|
||||
int element = 16;
|
||||
boolean expectedResult = true;
|
||||
boolean actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
|
||||
Assert.assertEquals(expectedResult, actualResult);
|
||||
|
||||
element = 20;
|
||||
expectedResult = false;
|
||||
actualResult = FindElementInArray.findGivenElementInArrayUsingStream(array, element);
|
||||
Assert.assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.array;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SumAndAverageInArrayTest {
|
||||
@Test
|
||||
public void givenAnIntArray_whenNotUsingStream_thenFindSum() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
int expectedSumOfArray = 55;
|
||||
int actualSumOfArray = SumAndAverageInArray.findSumWithoutUsingStream(array);
|
||||
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenUsingStream_thenFindSum() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
int expectedSumOfArray = 55;
|
||||
int actualSumOfArray = SumAndAverageInArray.findSumUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedSumOfArray, actualSumOfArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenNotUsingStream_thenFindAverage() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
double expectedAvgOfArray = 9.17;
|
||||
double actualAvgOfArray = SumAndAverageInArray.findAverageWithoutUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAnIntArray_whenUsingStream_thenFindAverage() {
|
||||
int[] array = { 1, 3, 4, 8, 19, 20 };
|
||||
double expectedAvgOfArray = 9.17;
|
||||
double actualAvgOfArray = SumAndAverageInArray.findAverageUsingStream(array);
|
||||
|
||||
Assert.assertEquals(expectedAvgOfArray, actualAvgOfArray, 0.0034);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.asciiart;
|
||||
|
||||
import java.awt.Font;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.asciiart.AsciiArt.Settings;
|
||||
|
||||
public class AsciiArtTest {
|
||||
|
||||
@Test
|
||||
public void givenTextWithAsciiCharacterAndSettings_shouldPrintAsciiArt() {
|
||||
AsciiArt asciiArt = new AsciiArt();
|
||||
String text = "BAELDUNG";
|
||||
Settings settings = asciiArt.new Settings(new Font("SansSerif", Font.BOLD, 24), text.length() * 30, 30); // 30 pixel width per character
|
||||
|
||||
asciiArt.drawString(text, "*", settings);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.casting;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CastingTest {
|
||||
|
||||
@Test
|
||||
public void whenPrimitiveConverted_thenValueChanged() {
|
||||
double myDouble = 1.1;
|
||||
int myInt = (int) myDouble;
|
||||
|
||||
assertNotEquals(myDouble, myInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcast_thenInstanceUnchanged() {
|
||||
Cat cat = new Cat();
|
||||
Animal animal = cat;
|
||||
animal = (Animal) cat;
|
||||
assertTrue(animal instanceof Cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToObject_thenInstanceUnchanged() {
|
||||
Object object = new Animal();
|
||||
assertTrue(object instanceof Animal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToInterface_thenInstanceUnchanged() {
|
||||
Mew mew = new Cat();
|
||||
assertTrue(mew instanceof Cat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpcastToAnimal_thenOverridenMethodsCalled() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
new AnimalFeeder().feed(animals);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDowncastToCat_thenMeowIsCalled() {
|
||||
Animal animal = new Cat();
|
||||
((Cat) animal).meow();
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void whenDownCastWithoutCheck_thenExceptionThrown() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
new AnimalFeeder().uncheckedFeed(animals);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDowncastToCatWithCastMethod_thenMeowIsCalled() {
|
||||
Animal animal = new Cat();
|
||||
if (Cat.class.isInstance(animal)) {
|
||||
Cat cat = Cat.class.cast(animal);
|
||||
cat.meow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenParameterCat_thenOnlyCatsFed() {
|
||||
List<Animal> animals = new ArrayList<>();
|
||||
animals.add(new Cat());
|
||||
animals.add(new Dog());
|
||||
AnimalFeederGeneric<Cat> catFeeder = new AnimalFeederGeneric<Cat>(Cat.class);
|
||||
List<Cat> fedAnimals = catFeeder.feed(animals);
|
||||
|
||||
assertTrue(fedAnimals.size() == 1);
|
||||
assertTrue(fedAnimals.get(0) instanceof Cat);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.classloader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class CustomClassLoaderTest {
|
||||
|
||||
@Test
|
||||
public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
|
||||
|
||||
CustomClassLoader customClassLoader = new CustomClassLoader();
|
||||
Class<?> c = customClassLoader.getClass(PrintClassLoader.class.getName());
|
||||
|
||||
Object ob = c.newInstance();
|
||||
|
||||
Method md = c.getMethod("printClassLoaders");
|
||||
md.invoke(ob);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.classloader;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PrintClassLoaderTest {
|
||||
@Test(expected = ClassNotFoundException.class)
|
||||
public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception {
|
||||
PrintClassLoader sampleClassLoader = (PrintClassLoader) Class.forName(PrintClassLoader.class.getName()).newInstance();
|
||||
sampleClassLoader.printClassLoaders();
|
||||
Class.forName(PrintClassLoader.class.getName(), true, PrintClassLoader.class.getClassLoader().getParent());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.baeldung.deepcopy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.commons.lang.SerializationUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
public class DeepCopyUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCreatingDeepCopyWithCopyConstructor_thenObjectsShouldNotBeSame() {
|
||||
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
|
||||
User deepCopy = new User(pm);
|
||||
|
||||
assertThat(deepCopy).isNotSameAs(pm);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenConstructorCopyShouldNotChange() {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
User deepCopy = new User(pm);
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenCloneCopyShouldNotChange() {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
User deepCopy = (User) pm.clone();
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenCommonsCloneShouldNotChange() {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
User deepCopy = (User) SerializationUtils.clone(pm);
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenGsonCloneShouldNotChange() {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
Gson gson = new Gson();
|
||||
User deepCopy = gson.fromJson(gson.toJson(pm), User.class);
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenJacksonCopyShouldNotChange() throws IOException {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
User deepCopy = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(deepCopy.getAddress().getCountry()).isNotEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void whenMakingCopies_thenShowHowLongEachMethodTakes() throws CloneNotSupportedException, IOException {
|
||||
int times = 1000000;
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
for (int i = 0; i < times; i++) {
|
||||
User primeMinisterClone = (User) SerializationUtils.clone(pm);
|
||||
}
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("Cloning with Apache Commons Lang took " + (end - start) + " milliseconds.");
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
Gson gson = new Gson();
|
||||
for (int i = 0; i < times; i++) {
|
||||
User primeMinisterClone = gson.fromJson(gson.toJson(pm), User.class);
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Cloning with Gson took " + (end - start) + " milliseconds.");
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
for (int i = 0; i < times; i++) {
|
||||
User primeMinisterClone = new User(pm);
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Cloning with the copy constructor took " + (end - start) + " milliseconds.");
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
for (int i = 0; i < times; i++) {
|
||||
User primeMinisterClone = (User) pm.clone();
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Cloning with Cloneable interface took " + (end - start) + " milliseconds.");
|
||||
|
||||
start = System.currentTimeMillis();
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
for (int i = 0; i < times; i++) {
|
||||
User primeMinisterClone = objectMapper.readValue(objectMapper.writeValueAsString(pm), User.class);
|
||||
}
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Cloning with Jackson took " + (end - start) + " milliseconds.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.deepcopy;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ShallowCopyUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void whenShallowCopying_thenObjectsShouldNotBeSame() {
|
||||
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
|
||||
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
|
||||
|
||||
assertThat(shallowCopy)
|
||||
.isNotSameAs(pm);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenModifyingOriginalObject_thenCopyShouldChange() {
|
||||
Address address = new Address("Downing St 10", "London", "England");
|
||||
User pm = new User("Prime", "Minister", address);
|
||||
User shallowCopy = new User(pm.getFirstName(), pm.getLastName(), pm.getAddress());
|
||||
|
||||
address.setCountry("Great Britain");
|
||||
|
||||
assertThat(shallowCopy.getAddress().getCountry())
|
||||
.isEqualTo(pm.getAddress().getCountry());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.designpatterns.flyweight;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for {@link VehicleFactory}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*/
|
||||
public class FlyweightUnitTest {
|
||||
|
||||
/**
|
||||
* Checks that when the {@link VehicleFactory} is asked to provide two
|
||||
* vehicles of different colors, the objects returned are different.
|
||||
*/
|
||||
@Test
|
||||
public void givenDifferentFlyweightObjects_whenEquals_thenFalse() {
|
||||
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
|
||||
Vehicle blueVehicle = VehicleFactory.createVehicle(Color.BLUE);
|
||||
|
||||
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
|
||||
Assert.assertNotNull("Object returned by the factory is null!", blueVehicle);
|
||||
Assert.assertNotEquals("Objects returned by the factory are equals!", blackVehicle, blueVehicle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that when the {@link VehicleFactory} is asked to provide two
|
||||
* vehicles of the same colors, the same object is returned twice.
|
||||
*/
|
||||
@Test
|
||||
public void givenSameFlyweightObjects_whenEquals_thenTrue() {
|
||||
Vehicle blackVehicle = VehicleFactory.createVehicle(Color.BLACK);
|
||||
Vehicle anotherBlackVehicle = VehicleFactory.createVehicle(Color.BLACK);
|
||||
|
||||
Assert.assertNotNull("Object returned by the factory is null!", blackVehicle);
|
||||
Assert.assertNotNull("Object returned by the factory is null!", anotherBlackVehicle);
|
||||
Assert.assertEquals("Objects returned by the factory are not equals!", blackVehicle, anotherBlackVehicle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.inheritance;
|
||||
|
||||
import com.baeldung.inheritance.*;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
public AppTest(String testName) {
|
||||
super( testName );
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(AppTest.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingBaseClassVariable() {
|
||||
Car first = new ArmoredCar();
|
||||
assertEquals("Car", first.msg());
|
||||
}
|
||||
|
||||
@SuppressWarnings("static-access")
|
||||
public void testStaticMethodUsingDerivedClassVariable() {
|
||||
ArmoredCar second = new ArmoredCar();
|
||||
assertEquals("ArmoredCar", second.msg());
|
||||
}
|
||||
|
||||
public void testAssignArmoredCarToCar() {
|
||||
Employee e1 = new Employee("Shreya", new ArmoredCar());
|
||||
assertNotNull(e1.getCar());
|
||||
}
|
||||
|
||||
public void testAssignSpaceCarToCar() {
|
||||
Employee e2 = new Employee("Paul", new SpaceCar());
|
||||
assertNotNull(e2.getCar());
|
||||
}
|
||||
|
||||
public void testBMWToCar() {
|
||||
Employee e3 = new Employee("Pavni", new BMW());
|
||||
assertNotNull(e3.getCar());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CustomListUnitTest {
|
||||
@Test
|
||||
public void givenEmptyList_whenIsEmpty_thenTrueIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonEmptyList_whenIsEmpty_thenFalseIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add(null);
|
||||
|
||||
assertFalse(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenSize_thenOneIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add(null);
|
||||
|
||||
assertEquals(1, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenGet_thenThatElementIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
Object element = list.get(0);
|
||||
|
||||
assertEquals("baeldung", element);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyList_whenElementIsAdded_thenGetReturnsThatElement() {
|
||||
List<Object> list = new CustomList<>();
|
||||
boolean succeeded = list.add(null);
|
||||
|
||||
assertTrue(succeeded);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenAnotherIsAdded_thenGetReturnsBoth() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.add(".com");
|
||||
Object element1 = list.get(0);
|
||||
Object element2 = list.get(1);
|
||||
|
||||
assertEquals("baeldung", element1);
|
||||
assertEquals(".com", element2);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenAddToSpecifiedIndex_thenExceptionIsThrown() {
|
||||
new CustomList<>().add(0, null);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenAddAllToTheEnd_thenExceptionIsThrown() {
|
||||
Collection<Object> collection = new ArrayList<>();
|
||||
List<Object> list = new CustomList<>();
|
||||
list.addAll(collection);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenAddAllToSpecifiedIndex_thenExceptionIsThrown() {
|
||||
Collection<Object> collection = new ArrayList<>();
|
||||
List<Object> list = new CustomList<>();
|
||||
list.addAll(0, collection);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenRemoveAtSpecifiedIndex_thenExceptionIsThrown() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.remove(0);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenRemoveSpecifiedElement_thenExceptionIsThrown() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.remove("baeldung");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenRemoveAll_thenExceptionIsThrown() {
|
||||
Collection<Object> collection = new ArrayList<>();
|
||||
collection.add("baeldung");
|
||||
List<Object> list = new CustomList<>();
|
||||
list.removeAll(collection);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void whenRetainAll_thenExceptionIsThrown() {
|
||||
Collection<Object> collection = new ArrayList<>();
|
||||
collection.add("baeldung");
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.retainAll(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyList_whenContains_thenFalseIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
|
||||
assertFalse(list.contains(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenContains_thenTrueIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
|
||||
assertTrue(list.contains("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenContainsAll_thenTrueIsReturned() {
|
||||
Collection<Object> collection = new ArrayList<>();
|
||||
collection.add("baeldung");
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
|
||||
assertTrue(list.containsAll(collection));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenContainsAll_thenEitherTrueOrFalseIsReturned() {
|
||||
Collection<Object> collection1 = new ArrayList<>();
|
||||
collection1.add("baeldung");
|
||||
collection1.add(".com");
|
||||
Collection<Object> collection2 = new ArrayList<>();
|
||||
collection2.add("baeldung");
|
||||
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
|
||||
assertFalse(list.containsAll(collection1));
|
||||
assertTrue(list.containsAll(collection2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenSet_thenOldElementIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
Object element = list.set(0, null);
|
||||
|
||||
assertEquals("baeldung", element);
|
||||
assertNull(list.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenClear_thenAllElementsAreRemoved() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.clear();
|
||||
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenIndexOf_thenIndexZeroIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
|
||||
assertEquals(0, list.indexOf("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenIndexOf_thenPositiveIndexOrMinusOneIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.add(".com");
|
||||
list.add(".com");
|
||||
|
||||
assertEquals(1, list.indexOf(".com"));
|
||||
assertEquals(-1, list.indexOf("com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLastIndexOf_thenIndexZeroIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
|
||||
assertEquals(0, list.lastIndexOf("baeldung"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLastIndexOf_thenPositiveIndexOrMinusOneIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.add("baeldung");
|
||||
list.add(".com");
|
||||
|
||||
assertEquals(1, list.lastIndexOf("baeldung"));
|
||||
assertEquals(-1, list.indexOf("com"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSubListZeroToOne_thenListContainingFirstElementIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
List<Object> subList = list.subList(0, 1);
|
||||
|
||||
assertEquals("baeldung", subList.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSubListOneToTwo_thenListContainingSecondElementIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.add(".");
|
||||
list.add("com");
|
||||
List<Object> subList = list.subList(1, 2);
|
||||
|
||||
assertEquals(1, subList.size());
|
||||
assertEquals(".", subList.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithElements_whenToArray_thenArrayContainsThose() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
list.add(".com");
|
||||
Object[] array = list.toArray();
|
||||
|
||||
assertArrayEquals(new Object[] { "baeldung", ".com" }, array);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithAnElement_whenToArray_thenInputArrayIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
String[] input = new String[1];
|
||||
String[] output = list.toArray(input);
|
||||
|
||||
assertArrayEquals(new String[] { "baeldung" }, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToArrayIsCalledWithEmptyInputArray_thenNewArrayIsReturned() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
String[] input = {};
|
||||
String[] output = list.toArray(input);
|
||||
|
||||
assertArrayEquals(new String[] { "baeldung" }, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToArrayIsCalledWithLargerInput_thenOutputHasTrailingNull() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
String[] input = new String[2];
|
||||
String[] output = list.toArray(input);
|
||||
|
||||
assertArrayEquals(new String[] { "baeldung", null }, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListWithOneElement_whenIterator_thenThisElementIsNext() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
Iterator<Object> iterator = list.iterator();
|
||||
|
||||
assertTrue(iterator.hasNext());
|
||||
assertEquals("baeldung", iterator.next());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIteratorHasNextIsCalledTwice_thenTheSecondReturnsFalse() {
|
||||
List<Object> list = new CustomList<>();
|
||||
list.add("baeldung");
|
||||
Iterator<Object> iterator = list.iterator();
|
||||
|
||||
iterator.next();
|
||||
assertFalse(iterator.hasNext());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.methodoverloadingoverriding.test;
|
||||
|
||||
import com.baeldung.methodoverloadingoverriding.util.Multiplier;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class MethodOverloadingUnitTest {
|
||||
|
||||
private static Multiplier multiplier;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpMultiplierInstance() {
|
||||
multiplier = new Multiplier();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() {
|
||||
assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() {
|
||||
assertThat(multiplier.multiply(10, 10)).isEqualTo(100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.methodoverloadingoverriding.test;
|
||||
|
||||
import com.baeldung.methodoverloadingoverriding.model.Car;
|
||||
import com.baeldung.methodoverloadingoverriding.model.Vehicle;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class MethodOverridingUnitTest {
|
||||
|
||||
private static Vehicle vehicle;
|
||||
private static Car car;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpVehicleInstance() {
|
||||
vehicle = new Vehicle();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpCarInstance() {
|
||||
car = new Car();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() {
|
||||
assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledRun_thenOneAssertion() {
|
||||
assertThat(vehicle.run()).isEqualTo("The vehicle is running.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleInstance_whenCalledStop_thenOneAssertion() {
|
||||
assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() {
|
||||
assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledRun_thenOneAssertion() {
|
||||
assertThat(car.run()).isEqualTo("The vehicle is running.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCarInstance_whenCalledStop_thenOneAssertion() {
|
||||
assertThat(car.stop()).isEqualTo("The vehicle has stopped.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() {
|
||||
assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledRun_thenEqual() {
|
||||
assertThat(vehicle.run()).isEqualTo(car.run());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenVehicleCarInstances_whenCalledStop_thenEqual() {
|
||||
assertThat(vehicle.stop()).isEqualTo(car.stop());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.recursion;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RecursionExampleTest {
|
||||
|
||||
RecursionExample recursion = new RecursionExample();
|
||||
|
||||
@Test
|
||||
public void testPowerOf10() {
|
||||
int p0 = recursion.powerOf10(0);
|
||||
int p1 = recursion.powerOf10(1);
|
||||
int p4 = recursion.powerOf10(4);
|
||||
|
||||
Assert.assertEquals(1, p0);
|
||||
Assert.assertEquals(10, p1);
|
||||
Assert.assertEquals(10000, p4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFibonacci() {
|
||||
int n0 = recursion.fibonacci(0);
|
||||
int n1 = recursion.fibonacci(1);
|
||||
int n7 = recursion.fibonacci(7);
|
||||
|
||||
Assert.assertEquals(0, n0);
|
||||
Assert.assertEquals(1, n1);
|
||||
Assert.assertEquals(13, n7);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToBinary() {
|
||||
String b0 = recursion.toBinary(0);
|
||||
String b1 = recursion.toBinary(1);
|
||||
String b10 = recursion.toBinary(10);
|
||||
|
||||
Assert.assertEquals("0", b0);
|
||||
Assert.assertEquals("1", b1);
|
||||
Assert.assertEquals("1010", b10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCalculateTreeHeight() {
|
||||
BinaryNode root = new BinaryNode(1);
|
||||
root.setLeft(new BinaryNode(1));
|
||||
root.setRight(new BinaryNode(1));
|
||||
|
||||
root.getLeft().setLeft(new BinaryNode(1));
|
||||
root.getLeft().getLeft().setRight(new BinaryNode(1));
|
||||
root.getLeft().getLeft().getRight().setLeft(new BinaryNode(1));
|
||||
|
||||
root.getRight().setLeft(new BinaryNode(1));
|
||||
root.getRight().getLeft().setRight(new BinaryNode(1));
|
||||
|
||||
int height = recursion.calculateTreeHeight(root);
|
||||
|
||||
Assert.assertEquals(4, height);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.resourcebundle;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ExampleResourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenGetBundleExampleResourceForLocalePlPl_thenItShouldInheritPropertiesGreetingAndLanguage() {
|
||||
Locale plLocale = new Locale("pl", "PL");
|
||||
|
||||
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", plLocale);
|
||||
|
||||
assertTrue(exampleBundle.keySet()
|
||||
.containsAll(Arrays.asList("toUsdRate", "cities", "greeting", "currency", "language")));
|
||||
assertEquals(exampleBundle.getString("greeting"), "cześć");
|
||||
assertEquals(exampleBundle.getObject("toUsdRate"), new BigDecimal("3.401"));
|
||||
assertArrayEquals(exampleBundle.getStringArray("cities"), new String[] { "Warsaw", "Cracow" });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBundleExampleResourceForLocaleUs_thenItShouldContainOnlyGreeting() {
|
||||
Locale usLocale = Locale.US;
|
||||
|
||||
ResourceBundle exampleBundle = ResourceBundle.getBundle("com.baeldung.resourcebundle.ExampleResource", usLocale);
|
||||
|
||||
assertFalse(exampleBundle.keySet()
|
||||
.containsAll(Arrays.asList("toUsdRate", "cities", "currency", "language")));
|
||||
assertTrue(exampleBundle.keySet()
|
||||
.containsAll(Arrays.asList("greeting")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.resourcebundle;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PropertyResourceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenLocaleUsAsDefualt_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("backButton", "helloLabel", "cancelButton", "continueButton", "helloLabelNoEncoding")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleUsAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldContainKeys1To3AndKey4() {
|
||||
Locale.setDefault(Locale.US);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("deleteButton", "helloLabel", "cancelButton", "continueButton")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFr_thenItShouldOnlyContainKeys1To3() {
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"));
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("continueButton", "helloLabel", "cancelButton")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLocaleChinaAsDefualt_whenGetBundleForLocaleFrFrAndExampleControl_thenItShouldOnlyContainKey5() {
|
||||
Locale.setDefault(Locale.CHINA);
|
||||
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("fr", "FR"), new ExampleControl());
|
||||
|
||||
assertTrue(bundle.keySet()
|
||||
.containsAll(Arrays.asList("backButton", "helloLabel")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValuesDifferentlyEncoded_whenGetBundleForLocalePlPl_thenItShouldContain3ButtonsAnd1Label() {
|
||||
ResourceBundle bundle = ResourceBundle.getBundle("resourcebundle.resource", new Locale("pl", "PL"));
|
||||
|
||||
assertEquals(bundle.getString("helloLabel"), "cześć");
|
||||
assertEquals(bundle.getString("helloLabelNoEncoding"), "czeÅ\u009BÄ\u0087");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class StringComparisonTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingComparisonOperator_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using comparison operator";
|
||||
String string2 = "using comparison operator";
|
||||
String string3 = new String("using comparison operator");
|
||||
|
||||
assertThat(string1 == string2).isTrue();
|
||||
assertThat(string1 == string3).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsMethod_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using equals method";
|
||||
String string2 = "using equals method";
|
||||
|
||||
String string3 = "using EQUALS method";
|
||||
String string4 = new String("using equals method");
|
||||
|
||||
assertThat(string1.equals(string2)).isTrue();
|
||||
assertThat(string1.equals(string4)).isTrue();
|
||||
|
||||
assertThat(string1.equals(null)).isFalse();
|
||||
assertThat(string1.equals(string3)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using equals ignore case";
|
||||
String string2 = "USING EQUALS IGNORE CASE";
|
||||
|
||||
assertThat(string1.equalsIgnoreCase(string2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareTo_ThenComparingStrings(){
|
||||
|
||||
String author = "author";
|
||||
String book = "book";
|
||||
String duplicateBook = "book";
|
||||
|
||||
assertThat(author.compareTo(book)).isEqualTo(-1);
|
||||
assertThat(book.compareTo(author)).isEqualTo(1);
|
||||
assertThat(duplicateBook.compareTo(book)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareToIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
String author = "Author";
|
||||
String book = "book";
|
||||
String duplicateBook = "BOOK";
|
||||
|
||||
assertThat(author.compareToIgnoreCase(book)).isEqualTo(-1);
|
||||
assertThat(book.compareToIgnoreCase(author)).isEqualTo(1);
|
||||
assertThat(duplicateBook.compareToIgnoreCase(book)).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingObjectsEqualsMethod_ThenComparingStrings(){
|
||||
|
||||
String string1 = "using objects equals";
|
||||
String string2 = "using objects equals";
|
||||
String string3 = new String("using objects equals");
|
||||
|
||||
assertThat(Objects.equals(string1, string2)).isTrue();
|
||||
assertThat(Objects.equals(string1, string3)).isTrue();
|
||||
|
||||
assertThat(Objects.equals(null, null)).isTrue();
|
||||
assertThat(Objects.equals(null, string1)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsOfApacheCommons_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equals(null, null)).isTrue();
|
||||
assertThat(StringUtils.equals(null, "equals method")).isFalse();
|
||||
assertThat(StringUtils.equals("equals method", "equals method")).isTrue();
|
||||
assertThat(StringUtils.equals("equals method", "EQUALS METHOD")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsIgnoreCaseOfApacheCommons_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsIgnoreCase(null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsIgnoreCase(null, "equals method")).isFalse();
|
||||
assertThat(StringUtils.equalsIgnoreCase("equals method", "equals method")).isTrue();
|
||||
assertThat(StringUtils.equalsIgnoreCase("equals method", "EQUALS METHOD")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsAnyOf_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsAny(null, null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsAny("equals any", "equals any", "any")).isTrue();
|
||||
assertThat(StringUtils.equalsAny("equals any", null, "equals any")).isTrue();
|
||||
assertThat(StringUtils.equalsAny(null, "equals", "any")).isFalse();
|
||||
assertThat(StringUtils.equalsAny("equals any", "EQUALS ANY", "ANY")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingEqualsAnyIgnoreCase_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(null, null, null)).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase("equals any", "equals any", "any")).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase("equals any", null, "equals any")).isTrue();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(null, "equals", "any")).isFalse();
|
||||
assertThat(StringUtils.equalsAnyIgnoreCase(
|
||||
"equals any ignore case", "EQUALS ANY IGNORE CASE", "any")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompare_thenComparingStringsWithNulls(){
|
||||
|
||||
assertThat(StringUtils.compare(null, null)).isEqualTo(0);
|
||||
assertThat(StringUtils.compare(null, "abc")).isEqualTo(-1);
|
||||
|
||||
assertThat(StringUtils.compare("abc", "bbc")).isEqualTo(-1);
|
||||
assertThat(StringUtils.compare("bbc", "abc")).isEqualTo(1);
|
||||
assertThat(StringUtils.compare("abc", "abc")).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareIgnoreCase_ThenComparingStringsWithNulls(){
|
||||
|
||||
assertThat(StringUtils.compareIgnoreCase(null, null)).isEqualTo(0);
|
||||
assertThat(StringUtils.compareIgnoreCase(null, "abc")).isEqualTo(-1);
|
||||
|
||||
assertThat(StringUtils.compareIgnoreCase("Abc", "bbc")).isEqualTo(-1);
|
||||
assertThat(StringUtils.compareIgnoreCase("bbc", "ABC")).isEqualTo(1);
|
||||
assertThat(StringUtils.compareIgnoreCase("abc", "ABC")).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingCompareWithNullIsLessOption_ThenComparingStrings(){
|
||||
|
||||
assertThat(StringUtils.compare(null, "abc", true)).isEqualTo(-1);
|
||||
assertThat(StringUtils.compare(null, "abc", false)).isEqualTo(1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.system;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@Ignore
|
||||
public class WhenDetectingOSTest {
|
||||
|
||||
private DetectOS os = new DetectOS();
|
||||
|
||||
@Test
|
||||
public void whenUsingSystemProperty_shouldReturnOS() {
|
||||
String expected = "Windows 10";
|
||||
String actual = os.getOperatingSystem();
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingSystemUtils_shouldReturnOS() {
|
||||
String expected = "Windows 10";
|
||||
String actual = os.getOperatingSystemSystemUtils();
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user