[JAVA-14174] Renamed paterns to paterns-module (#12718)
* [JAVA-14174] Renamed paterns to paterns-module * [JAVA-14174] naming fixes Co-authored-by: panagiotiskakos <panagiotis.kakos@libra-is.com>
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.constructorsstaticfactorymethods;
|
||||
|
||||
import com.baeldung.constructorsstaticfactorymethods.entities.User;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUserClass_whenCalledcreateWithDefaultCountry_thenCorrect() {
|
||||
assertThat(User.createWithDefaultCountry("John", "john@domain.com")).isInstanceOf(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetName_thenCorrect() {
|
||||
User user = User.createWithDefaultCountry("John", "john@domain.com");
|
||||
assertThat(user.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetEmail_thenCorrect() {
|
||||
User user = User.createWithDefaultCountry("John", "john@domain.com");
|
||||
assertThat(user.getEmail()).isEqualTo("john@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetCountry_thenCorrect() {
|
||||
User user = User.createWithDefaultCountry("John", "john@domain.com");
|
||||
assertThat(user.getCountry()).isEqualTo("Argentina");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserInstanceCreatedWithcreateWithInstantiationTime_whenCalledcreateWithInstantiationTime_thenCorrect() {
|
||||
assertThat(User.createWithLoggedInstantiationTime("John", "john@domain.com", "Argentina")).isInstanceOf(User.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserInstanceCreatedWithgetSingletonIntance_whenCalledgetSingletonInstance_thenCorrect() {
|
||||
User user1 = User.getSingletonInstance("John", "john@domain.com", "Argentina");
|
||||
User user2 = User.getSingletonInstance("John", "john@domain.com", "Argentina");
|
||||
assertThat(user1).isEqualTo(user2);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.creational.abstractfactory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AbstractPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenAbstractFactory_whenGettingObjects_thenSuccessful() {
|
||||
AbstractFactory abstractFactory;
|
||||
|
||||
//creating a brown toy dog
|
||||
abstractFactory = FactoryProvider.getFactory("Toy");
|
||||
Animal toy = (Animal) abstractFactory.create("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color =(Color) abstractFactory.create("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
assertEquals("A Dog with brown color Barks", result);
|
||||
}
|
||||
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.creational.builder;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BuilderPatternIntegrationTest {
|
||||
@Test
|
||||
public void whenCreatingObjectThroughBuilder_thenObjectValid() {
|
||||
BankAccount newAccount = new BankAccount
|
||||
.BankAccountBuilder("Jon", "22738022275")
|
||||
.withEmail("jon@example.com")
|
||||
.wantNewsletter(true)
|
||||
.build();
|
||||
|
||||
assertEquals(newAccount.getName(), "Jon");
|
||||
assertEquals(newAccount.getAccountNumber(), "22738022275");
|
||||
assertEquals(newAccount.getEmail(), "jon@example.com");
|
||||
assertEquals(newAccount.isNewsletter(), true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkippingOptionalParameters_thenObjectValid() {
|
||||
BankAccount newAccount = new BankAccount
|
||||
.BankAccountBuilder("Jon", "22738022275")
|
||||
.build();
|
||||
|
||||
assertEquals(newAccount.getName(), "Jon");
|
||||
assertEquals(newAccount.getAccountNumber(), "22738022275");
|
||||
assertEquals(newAccount.getEmail(), null);
|
||||
assertEquals(newAccount.isNewsletter(), false);
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.creational.factory;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class FactoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingFactoryForSquare_thenCorrectObjectReturned() {
|
||||
Polygon p;
|
||||
PolygonFactory factory = new PolygonFactory();
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(4);
|
||||
String result = "The shape with 4 sides is a " + p.getType();
|
||||
|
||||
assertEquals("The shape with 4 sides is a Square", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFactoryForOctagon_thenCorrectObjectReturned() {
|
||||
Polygon p;
|
||||
PolygonFactory factory = new PolygonFactory();
|
||||
|
||||
//get the shape which has 4 sides
|
||||
p = factory.getPolygon(8);
|
||||
String result = "The shape with 8 sides is a " + p.getType();
|
||||
|
||||
assertEquals("The shape with 8 sides is a Octagon", result);
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.creational.singleton;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SingletonIntegrationTest {
|
||||
|
||||
@Test
|
||||
/**
|
||||
* Although there is absolutely no way to determine whether
|
||||
* a class is Singleton, in this test case, we will just
|
||||
* check for two objects if they point to same instance or
|
||||
* not. We will also check for their hashcode.
|
||||
*/
|
||||
public void whenGettingMultipleObjects_thenAllPointToSame() {
|
||||
//first object
|
||||
Singleton obj1 = Singleton.getInstance();
|
||||
|
||||
//Second object
|
||||
Singleton obj2 = Singleton.getInstance();
|
||||
|
||||
assertTrue(obj1 == obj2);
|
||||
assertEquals(obj1.hashCode(), obj2.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.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);
|
||||
}
|
||||
}
|
||||
+226
@@ -0,0 +1,226 @@
|
||||
package com.baeldung.freebuilder;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EmployeeBuilderUnitTest {
|
||||
|
||||
private static final int PIN_CODE = 223344;
|
||||
public static final String CITY_NAME = "New York";
|
||||
public static final int INPUT_SALARY_EUROS = 10000;
|
||||
public static final double EUROS_TO_USD_RATIO = 0.6;
|
||||
|
||||
@Test
|
||||
public void whenBuildEmployeeWithAddress_thenReturnEmployeeWithValidAddress() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setPermanent(true)
|
||||
.setRole("developer")
|
||||
.setAddress(address)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getAddress().getCity().equalsIgnoreCase(CITY_NAME));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapSalary_thenReturnEmployeeWithSalaryInUSD() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).setPinCode(PIN_CODE).build();
|
||||
|
||||
long salaryInEuros = INPUT_SALARY_EUROS;
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder
|
||||
.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setPermanent(true)
|
||||
.setRole("developer")
|
||||
.setAddress(address)
|
||||
.mapSalaryInUSD(sal -> salaryInEuros * EUROS_TO_USD_RATIO)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getAddress().getPinCode().get() == PIN_CODE);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptionalFields_thenReturnEmployeeWithEmptyValues() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setPermanent(true)
|
||||
.setRole("developer")
|
||||
.setAddress(address)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getPermanent().isPresent());
|
||||
assertTrue(employee.getPermanent().get());
|
||||
assertFalse(employee.getDateOfJoining().isPresent());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNullableFields_thenReturnEmployeeWithNullValueForField() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setNullablePermanent(null)
|
||||
.setDateOfJoining(Optional.empty())
|
||||
.setRole("developer")
|
||||
.setAddress(address)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertNull(employee.getCurrentProject());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCollectionFields_thenReturnEmployeeWithValues() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setNullablePermanent(null)
|
||||
.setDateOfJoining(Optional.empty())
|
||||
.setRole("developer")
|
||||
.addAccessTokens(1221819L)
|
||||
.addAccessTokens(1223441L, 134567L)
|
||||
.setAddress(address)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getAccessTokens().size() == 3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMapFields_thenReturnEmployeeWithValues() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setNullablePermanent(null)
|
||||
.setDateOfJoining(Optional.empty())
|
||||
.setRole("developer")
|
||||
.addAccessTokens(1221819L)
|
||||
.addAccessTokens(1223441L, 134567L)
|
||||
.putAssetsSerialIdMapping("Laptop", 12345L)
|
||||
.setAddress(address)
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getAssetsSerialIdMapping().size() == 1);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNestedBuilderTypes_thenReturnEmployeeWithValues() {
|
||||
|
||||
// when
|
||||
Address.Builder addressBuilder = new Address.Builder();
|
||||
Address address = addressBuilder.setCity(CITY_NAME).build();
|
||||
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setDesignation("author")
|
||||
.setEmail("abc@xyz.com")
|
||||
.setSupervisorName("Admin")
|
||||
.setPhoneNumber(4445566)
|
||||
.setNullablePermanent(null)
|
||||
.setDateOfJoining(Optional.empty())
|
||||
.setRole("developer")
|
||||
.addAccessTokens(1221819L)
|
||||
.addAccessTokens(1223441L, 134567L)
|
||||
.putAssetsSerialIdMapping("Laptop", 12345L)
|
||||
.setAddress(address)
|
||||
.mutateAddress(a -> a.setPinCode(112200))
|
||||
.build();
|
||||
|
||||
// then
|
||||
assertTrue(employee.getAssetsSerialIdMapping().size() == 1);
|
||||
|
||||
}
|
||||
|
||||
@Test()
|
||||
public void whenPartialEmployeeWithValidEmail_thenReturnEmployeeWithEmail() {
|
||||
|
||||
// when
|
||||
Employee.Builder builder = new Employee.Builder();
|
||||
|
||||
Employee employee = builder.setName("baeldung")
|
||||
.setAge(10)
|
||||
.setEmail("abc@xyz.com")
|
||||
.buildPartial();
|
||||
|
||||
assertNotNull(employee.getEmail());
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.freebuilder.builder;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
class EmployeeBuilderUnitTest {
|
||||
|
||||
public static final String NAME = "baeldung";
|
||||
|
||||
@Test
|
||||
public void whenBuildEmployee_thenReturnValidEmployee() {
|
||||
|
||||
// when
|
||||
Employee.Builder emplBuilder = new Employee.Builder();
|
||||
|
||||
Employee employee = emplBuilder
|
||||
.setName(NAME)
|
||||
.setAge(12)
|
||||
.setDepartment("Builder Pattern")
|
||||
.build();
|
||||
|
||||
//then
|
||||
Assertions.assertTrue(employee.getName().equalsIgnoreCase(NAME));
|
||||
}
|
||||
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.prototype;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class TreePrototypeUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAPlasticTreePrototypeWhenClonedThenCreateA_Clone() {
|
||||
double mass = 10.0;
|
||||
double height = 3.7;
|
||||
Position position = new Position(3, 7);
|
||||
Position otherPosition = new Position(4, 8);
|
||||
|
||||
PlasticTree plasticTree = new PlasticTree(mass, height);
|
||||
plasticTree.setPosition(position);
|
||||
PlasticTree anotherPlasticTree = (PlasticTree) plasticTree.copy();
|
||||
anotherPlasticTree.setPosition(otherPosition);
|
||||
|
||||
assertEquals(position, plasticTree.getPosition());
|
||||
assertEquals(otherPosition, anotherPlasticTree.getPosition());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAPineTreePrototypeWhenClonedThenCreateA_Clone() {
|
||||
double mass = 10.0;
|
||||
double height = 3.7;
|
||||
Position position = new Position(3, 7);
|
||||
Position otherPosition = new Position(4, 8);
|
||||
|
||||
PineTree pineTree = new PineTree(mass, height);
|
||||
pineTree.setPosition(position);
|
||||
PineTree anotherPineTree = (PineTree) pineTree.copy();
|
||||
anotherPineTree.setPosition(otherPosition);
|
||||
|
||||
assertEquals(position, pineTree.getPosition());
|
||||
assertEquals(otherPosition, anotherPineTree.getPosition());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenA_ListOfTreesWhenClonedThenCreateListOfClones() {
|
||||
double mass = 10.0;
|
||||
double height = 3.7;
|
||||
Position position = new Position(3, 7);
|
||||
Position otherPosition = new Position(4, 8);
|
||||
|
||||
PlasticTree plasticTree = new PlasticTree(mass, height);
|
||||
plasticTree.setPosition(position);
|
||||
PineTree pineTree = new PineTree(mass, height);
|
||||
pineTree.setPosition(otherPosition);
|
||||
|
||||
List<Tree> trees = Arrays.asList(plasticTree, pineTree);
|
||||
|
||||
List<Tree> treeClones = trees.stream().map(Tree::copy).collect(toList());
|
||||
|
||||
Tree plasticTreeClone = treeClones.get(0);
|
||||
|
||||
assertEquals(mass, plasticTreeClone.getMass());
|
||||
assertEquals(height, plasticTreeClone.getHeight());
|
||||
assertEquals(position, plasticTreeClone.getPosition());
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.reduceIfelse;
|
||||
|
||||
import com.baeldung.reducingIfElse.AddCommand;
|
||||
import com.baeldung.reducingIfElse.Calculator;
|
||||
import com.baeldung.reducingIfElse.Operator;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CalculatorUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenCalculateUsingStringOperator_thenReturnCorrectResult() {
|
||||
Calculator calculator = new Calculator();
|
||||
int result = calculator.calculate(3, 4, "add");
|
||||
assertEquals(7, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculateUsingEnumOperator_thenReturnCorrectResult() {
|
||||
Calculator calculator = new Calculator();
|
||||
int result = calculator.calculate(3, 4, Operator.valueOf("ADD"));
|
||||
assertEquals(7, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculateUsingCommand_thenReturnCorrectResult() {
|
||||
Calculator calculator = new Calculator();
|
||||
int result = calculator.calculate(new AddCommand(3, 7));
|
||||
assertEquals(10, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCalculateUsingFactory_thenReturnCorrectResult() {
|
||||
Calculator calculator = new Calculator();
|
||||
int result = calculator.calculateUsingFactory(3, 4, "add");
|
||||
assertEquals(7, result);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.reduceIfelse;
|
||||
|
||||
import com.baeldung.reducingIfElse.Expression;
|
||||
import com.baeldung.reducingIfElse.Operator;
|
||||
import com.baeldung.reducingIfElse.Result;
|
||||
import com.baeldung.reducingIfElse.RuleEngine;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class RuleEngineUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNumbersGivenToRuleEngine_thenReturnCorrectResult() {
|
||||
Expression expression = new Expression(5, 5, Operator.ADD);
|
||||
RuleEngine engine = new RuleEngine();
|
||||
Result result = engine.process(expression);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(10, result.getValue());
|
||||
}
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
package com.baeldung.singleton.synchronization;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the singleton synchronization package with the same name.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class SingletonSynchronizationIntegrationTest {
|
||||
|
||||
/**
|
||||
* Size of the thread pools used.
|
||||
*/
|
||||
private static final int POOL_SIZE = 1_000;
|
||||
|
||||
/**
|
||||
* Number of tasks to submit.
|
||||
*/
|
||||
private static final int TASKS_TO_SUBMIT = 1_000_000;
|
||||
|
||||
/**
|
||||
* Tests the thread-safety of {@link DraconianSingleton}.
|
||||
*/
|
||||
@Test
|
||||
public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
executor.submit(() -> resultSet.add(DraconianSingleton.getInstance()));
|
||||
}
|
||||
|
||||
// Since the instance of the object we inserted into the set is always
|
||||
// the same, the size should be one.
|
||||
Assert.assertEquals(1, resultSet.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the thread-safety of {@link DclSingleton}.
|
||||
*/
|
||||
@Test
|
||||
public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
executor.submit(() -> resultSet.add(DclSingleton.getInstance()));
|
||||
}
|
||||
|
||||
// Since the instance of the object we inserted into the set is always
|
||||
// the same, the size should be one.
|
||||
Assert.assertEquals(1, resultSet.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the thread-safety of {@link EarlyInitSingleton}.
|
||||
*/
|
||||
@Test
|
||||
public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
executor.submit(() -> resultSet.add(EarlyInitSingleton.getInstance()));
|
||||
}
|
||||
|
||||
// Since the instance of the object we inserted into the set is always
|
||||
// the same, the size should be one.
|
||||
Assert.assertEquals(1, resultSet.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the thread-safety of {@link InitOnDemandSingleton}.
|
||||
*/
|
||||
@Test
|
||||
public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
executor.submit(() -> resultSet.add(InitOnDemandSingleton.getInstance()));
|
||||
}
|
||||
|
||||
// Since the instance of the object we inserted into the set is always
|
||||
// the same, the size should be one.
|
||||
Assert.assertEquals(1, resultSet.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the thread-safety of {@link EnumSingleton}.
|
||||
*/
|
||||
@Test
|
||||
public void givenEnumSingleton_whenMultithreadInstancesEquals_thenTrue() {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
|
||||
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<>());
|
||||
|
||||
// Submits the instantiation tasks.
|
||||
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
|
||||
executor.submit(() -> resultSet.add(EnumSingleton.INSTANCE));
|
||||
}
|
||||
|
||||
// Since the instance of the object we inserted into the set is always
|
||||
// the same, the size should be one.
|
||||
Assert.assertEquals(1, resultSet.size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user