BAEL-16653 (#7691)
* added module 'design-patterns-behavioral' * added module 'design-patterns-structural' * added module 'design-patterns-creational' * added module 'design-patterns-architectural' * added module 'design-patterns-functional' * moved facade code examples from design-patterns to design-patterns-structural * moved singleton examples from design-patterns to design-patterns-creational * moved bridge examples from design-patterns to design-patterns-structural * moved creational examples from design-patterns to design-patterns-creational * moved observer examples from design-patterns to design-patterns-behavioral * moved flyweight examples from design-patterns to design-patterns-creational * moved service locator examples from design-patterns to design-patterns-architectural * moved double checked locking singleton examples from design-patterns to design-patterns-creational * moved composite examples from design-patterns to design-patterns-structural * moved visitor examples from design-patterns to design-patterns-behavioral * moved dao examples from design-patterns to design-patterns-architectural * moved interpreter examples from design-patterns to design-patterns-behavioral * moved state examples from design-patterns to design-patterns-behavioral * moved decorator examples from design-patterns to design-patterns-structural * renamed LogerUtil to LoggerUtil * moved template method examples from design-patterns to design-patterns-behavioral * moved chain of responsibility examples from design-patterns to design-patterns-behavioral * moved command examples from design-patterns to design-patterns-behavioral * moved constructor vs static factory method examples from design-patterns to design-patterns-creational * moved adapter examples from design-patterns to design-patterns-structural * moved currying examples from design-patterns to design-patterns-functional * moved proxy examples from design-patterns to design-patterns-structural * moved persistence.xml from design-patterns to design-patterns-architectural * deleted empty module: design-patterns * moved mediator examples from design-patterns-2 to design-patterns-behavioral * moved null object examples from design-patterns-2 to design-patterns-behavioral * moved null check examples from design-patterns to design-patterns-behavioral * moved freebuilder examples from design-patterns-2 to design-patterns-creational * added module design-patterns-behavioral-2 * moved memento examples from design-patterns-2 to design-patterns-behavioral-2 * removed empty module design-patterns-2 * changed http to https in readmes in modules design-patterns-*
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.adapter;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AdapterPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenMovableAdapter_WhenConvertingMPHToKMPH_thenSuccessfullyConverted() {
|
||||
Movable bugattiVeyron = new BugattiVeyron();
|
||||
MovableAdapter bugattiVeyronAdapter = new MovableAdapterImpl(bugattiVeyron);
|
||||
assertEquals(bugattiVeyronAdapter.getSpeed(), 431.30312, 0.00001);
|
||||
|
||||
Movable mcLaren = new McLaren();
|
||||
MovableAdapter mcLarenAdapter = new MovableAdapterImpl(mcLaren);
|
||||
assertEquals(mcLarenAdapter.getSpeed(), 387.85094, 0.00001);
|
||||
|
||||
Movable astonMartin = new AstonMartin();
|
||||
MovableAdapter astonMartinAdapter = new MovableAdapterImpl(astonMartin);
|
||||
assertEquals(astonMartinAdapter.getSpeed(), 354.0548, 0.00001);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.bridge;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BridgePatternIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void whenBridgePatternInvoked_thenConfigSuccess() {
|
||||
//a square with red color
|
||||
Shape square = new Square(new Red());
|
||||
assertEquals(square.draw(), "Square drawn. Color is Red");
|
||||
|
||||
//a triangle with blue color
|
||||
Shape triangle = new Triangle(new Blue());
|
||||
assertEquals(triangle.draw(), "Triangle drawn. Color is Blue");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.decorator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DecoratorPatternIntegrationTest {
|
||||
@Test
|
||||
public void givenDecoratorPattern_WhenDecoratorsInjectedAtRuntime_thenConfigSuccess() {
|
||||
ChristmasTree tree1 = new Garland(new ChristmasTreeImpl());
|
||||
assertEquals(tree1.decorate(),
|
||||
"Christmas tree with Garland");
|
||||
|
||||
ChristmasTree tree2 = new BubbleLights(
|
||||
new Garland(new Garland(new ChristmasTreeImpl())));
|
||||
assertEquals(tree2.decorate(),
|
||||
"Christmas tree with Garland with Garland with Bubble Lights");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.facade;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.AppenderBase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class CarEngineFacadeIntegrationTest {
|
||||
|
||||
|
||||
private InMemoryCustomTestAppender appender;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new InMemoryCustomTestAppender();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
appender.stop();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenStartEngine_thenAllNecessaryActionsPerformed() {
|
||||
CarEngineFacade carEngineFacade = new CarEngineFacade();
|
||||
carEngineFacade.startEngine();
|
||||
assertTrue(appender.logContains("Fuel injector ready to inject fuel."));
|
||||
assertTrue(appender.logContains("Getting air measurements.."));
|
||||
assertTrue(appender.logContains("Air provided!"));
|
||||
assertTrue(appender.logContains("Fuel injector ready to inject fuel."));
|
||||
assertTrue(appender.logContains("Fuel Pump is pumping fuel.."));
|
||||
assertTrue(appender.logContains("Fuel injected."));
|
||||
assertTrue(appender.logContains("Starting.."));
|
||||
assertTrue(appender.logContains("Setting temperature upper limit to 90"));
|
||||
assertTrue(appender.logContains("Cooling Controller ready!"));
|
||||
assertTrue(appender.logContains("Setting radiator speed to 10"));
|
||||
assertTrue(appender.logContains("Catalytic Converter switched on!"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenStopEngine_thenAllNecessaryActionsPerformed() {
|
||||
CarEngineFacade carEngineFacade = new CarEngineFacade();
|
||||
carEngineFacade.stopEngine();
|
||||
assertTrue(appender.logContains("Stopping Fuel injector.."));
|
||||
assertTrue(appender.logContains("Catalytic Converter switched off!"));
|
||||
assertTrue(appender.logContains("Scheduled cooling with maximum allowed temperature 50"));
|
||||
assertTrue(appender.logContains("Getting temperature from the sensor.."));
|
||||
assertTrue(appender.logContains("Radiator switched on!"));
|
||||
assertTrue(appender.logContains("Stopping Cooling Controller.."));
|
||||
assertTrue(appender.logContains("Radiator switched off!"));
|
||||
assertTrue(appender.logContains("Air controller switched off."));
|
||||
}
|
||||
|
||||
private class InMemoryCustomTestAppender extends AppenderBase<ILoggingEvent> {
|
||||
|
||||
private List<ILoggingEvent> logs = new ArrayList<>();
|
||||
|
||||
public InMemoryCustomTestAppender() {
|
||||
((Logger) LoggerFactory.getLogger("root")).addAppender(this);
|
||||
start();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(ILoggingEvent eventObject) {
|
||||
logs.add(eventObject);
|
||||
}
|
||||
|
||||
public boolean logContains(String message) {
|
||||
return logs.stream().anyMatch(event -> event.getFormattedMessage().equals(message));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
import static com.baeldung.util.LoggerUtil.LOG;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ProxyPatternIntegrationTest {
|
||||
public static TestAppenderDP appender;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
appender = new TestAppenderDP();
|
||||
LOG.addAppender(appender);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExpensiveObjectProxy_WhenObjectInitialized_thenInitializedOnlyOnce() {
|
||||
ExpensiveObject object = new ExpensiveObjectProxy();
|
||||
object.process();
|
||||
object.process();
|
||||
|
||||
final List<LoggingEvent> log = appender.getLog();
|
||||
|
||||
assertThat((String) log.get(0).getMessage(), is("Loading initial configuration.."));
|
||||
assertThat((String) log.get(1).getMessage(), is("processing complete."));
|
||||
assertThat((String) log.get(2).getMessage(), is("processing complete."));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
LOG.removeAppender(appender);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.proxy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.AppenderSkeleton;
|
||||
import org.apache.log4j.spi.LoggingEvent;
|
||||
|
||||
public class TestAppenderDP extends AppenderSkeleton {
|
||||
private final List<LoggingEvent> log = new ArrayList<LoggingEvent>();
|
||||
|
||||
@Override
|
||||
public boolean requiresLayout() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void append(final LoggingEvent loggingEvent) {
|
||||
log.add(loggingEvent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public List<LoggingEvent> getLog() {
|
||||
return new ArrayList<LoggingEvent>(log);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user