BAEL-1212 Guide to Creational Design Patterns (#2932)
* Initial commit for 'Introduction to Creational Design Patterns' * second commit with some minor fixes and test case additions * second commit with some minor fixes and test case additions * BAEL-1212 Guide to Creational Patterns - Made changes as per latest review * Renamed Toy family to AAnimal family.
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
2814a5f8d0
commit
8de9f342e4
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.designpatterns.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 = abstractFactory.getAnimal("Dog");
|
||||
|
||||
abstractFactory = FactoryProvider.getFactory("Color");
|
||||
Color color = abstractFactory.getColor("Brown");
|
||||
|
||||
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
|
||||
assertEquals("A Dog with brown color Barks", result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.designpatterns.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.designpatterns.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.designpatterns.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());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user