From e872f156be613dc5765a0d621c97201ec49f7734 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 27 Jul 2018 21:27:38 +0200 Subject: [PATCH 01/11] BAEL-1997 state design pattern in Java --- .../com/baeldung/state/ExplicitStateDemo.java | 21 ++++++++++++++ .../com/baeldung/state/InternalStateDemo.java | 15 ++++++++++ .../baeldung/state/context/TradeContext.java | 29 +++++++++++++++++++ .../state/states/DayTradeBehavior.java | 11 +++++++ .../states/FallbackDayTradeBehavior.java | 16 ++++++++++ .../state/states/PositionTradeBehavior.java | 11 +++++++ .../state/states/SwingTradeBehavior.java | 11 +++++++ .../baeldung/state/states/TradeBehavior.java | 7 +++++ 8 files changed, 121 insertions(+) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java new file mode 100644 index 0000000000..de7b54d70e --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java @@ -0,0 +1,21 @@ +package com.baeldung.state; + +import com.baeldung.state.context.TradeContext; +import com.baeldung.state.states.DayTradeBehavior; +import com.baeldung.state.states.PositionTradeBehavior; +import com.baeldung.state.states.SwingTradeBehavior; + +public class ExplicitStateDemo { + + public static void main(String[] args) { + + TradeContext tradeContext = new TradeContext(new SwingTradeBehavior()); + tradeContext.trade(); + + tradeContext.setBehavior(new PositionTradeBehavior()); + tradeContext.trade(); + + tradeContext.setBehavior(new DayTradeBehavior()); + tradeContext.trade(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java new file mode 100644 index 0000000000..3148940f17 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java @@ -0,0 +1,15 @@ +package com.baeldung.state; + +import com.baeldung.state.context.TradeContext; +import com.baeldung.state.states.FallbackDayTradeBehavior; + +public class InternalStateDemo { + + public static void main(String[] args) { + + TradeContext tradeContext = new TradeContext(new FallbackDayTradeBehavior()); + tradeContext.trade(); + tradeContext.trade(); + tradeContext.trade(); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java new file mode 100644 index 0000000000..0f12d050db --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java @@ -0,0 +1,29 @@ +package com.baeldung.state.context; + +import com.baeldung.state.states.TradeBehavior; + +public class TradeContext { + + private TradeBehavior behavior; + + public TradeContext(TradeBehavior behavior) { + this.behavior = behavior; + } + + public TradeBehavior getBehavior() { + return behavior; + } + + public void setBehavior(TradeBehavior behavior) { + this.behavior = behavior; + } + + public void trade() { + behavior.trade(this); + } + + @Override + public String toString() { + return "TradeContext{" + "behavior=" + behavior + '}'; + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java new file mode 100644 index 0000000000..540530e50b --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class DayTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Day Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java new file mode 100644 index 0000000000..cf6e3ea695 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java @@ -0,0 +1,16 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class FallbackDayTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Day Trade"); + + if (Math.random() > 0.4) { + ctx.setBehavior(new SwingTradeBehavior()); + System.out.println("Day trading would not go well, falling back to Swing Trade"); + } + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java new file mode 100644 index 0000000000..0b1facb143 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class PositionTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Position Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java new file mode 100644 index 0000000000..bbe8091bed --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java @@ -0,0 +1,11 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public class SwingTradeBehavior implements TradeBehavior { + + @Override + public void trade(TradeContext ctx) { + System.out.println("Trading method: Swing Trade"); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java new file mode 100644 index 0000000000..ef5554ab50 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java @@ -0,0 +1,7 @@ +package com.baeldung.state.states; + +import com.baeldung.state.context.TradeContext; + +public interface TradeBehavior { + void trade(TradeContext ctx); +} From 26a28dd663a1dd8ca1a692c8973d3f4427ab3d36 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 7 Aug 2018 21:28:06 +0200 Subject: [PATCH 02/11] BAEL-1997 different example code --- .../com/baeldung/state/DeliveredState.java | 19 +++++++++++ .../com/baeldung/state/ExplicitStateDemo.java | 21 ------------ .../com/baeldung/state/InternalStateDemo.java | 15 --------- .../java/com/baeldung/state/OrderedState.java | 19 +++++++++++ .../main/java/com/baeldung/state/Package.java | 22 +++++++++++++ .../java/com/baeldung/state/PackageState.java | 8 +++++ .../com/baeldung/state/ReceivedState.java | 23 +++++++++++++ .../baeldung/state/context/TradeContext.java | 29 ---------------- .../state/states/DayTradeBehavior.java | 11 ------- .../states/FallbackDayTradeBehavior.java | 16 --------- .../state/states/PositionTradeBehavior.java | 11 ------- .../state/states/SwingTradeBehavior.java | 11 ------- .../baeldung/state/states/TradeBehavior.java | 7 ---- .../baeldung/state/StatePatternUnitTest.java | 33 +++++++++++++++++++ 14 files changed, 124 insertions(+), 121 deletions(-) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/Package.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java delete mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java create mode 100644 patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java new file mode 100644 index 0000000000..adb0fd3f5a --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class DeliveredState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new ReceivedState()); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new OrderedState()); + } + + @Override + public String toString() { + return "DeliveredState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java deleted file mode 100644 index de7b54d70e..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ExplicitStateDemo.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.state; - -import com.baeldung.state.context.TradeContext; -import com.baeldung.state.states.DayTradeBehavior; -import com.baeldung.state.states.PositionTradeBehavior; -import com.baeldung.state.states.SwingTradeBehavior; - -public class ExplicitStateDemo { - - public static void main(String[] args) { - - TradeContext tradeContext = new TradeContext(new SwingTradeBehavior()); - tradeContext.trade(); - - tradeContext.setBehavior(new PositionTradeBehavior()); - tradeContext.trade(); - - tradeContext.setBehavior(new DayTradeBehavior()); - tradeContext.trade(); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java deleted file mode 100644 index 3148940f17..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/InternalStateDemo.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.baeldung.state; - -import com.baeldung.state.context.TradeContext; -import com.baeldung.state.states.FallbackDayTradeBehavior; - -public class InternalStateDemo { - - public static void main(String[] args) { - - TradeContext tradeContext = new TradeContext(new FallbackDayTradeBehavior()); - tradeContext.trade(); - tradeContext.trade(); - tradeContext.trade(); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java new file mode 100644 index 0000000000..56a267f160 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class OrderedState implements PackageState { + + @Override + public void next(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public void prev(Package pkg) { + System.out.println("The package is in it's root state."); + } + + @Override + public String toString() { + return "OrderedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java new file mode 100644 index 0000000000..ff04d543b8 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -0,0 +1,22 @@ +package com.baeldung.state; + +public class Package { + + private PackageState state = new OrderedState(); + + public PackageState getState() { + return state; + } + + public void setState(PackageState state) { + this.state = state; + } + + public void previousState() { + state.prev(this); + } + + public void nextState() { + state.next(this); + } +} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java new file mode 100644 index 0000000000..d7f7a5c110 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -0,0 +1,8 @@ +package com.baeldung.state; + +public interface PackageState { + + void next(Package pkg); + + void prev(Package pkg); +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java new file mode 100644 index 0000000000..be76937159 --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -0,0 +1,23 @@ +package com.baeldung.state; + +public class ReceivedState implements PackageState { + + @Override + public void next(Package pkg) { + if (pkg.getState() instanceof ReceivedState) { + System.out.println("This package is already received by a client."); + return; + } + pkg.setState(this); + } + + @Override + public void prev(Package pkg) { + pkg.setState(new DeliveredState()); + } + + @Override + public String toString() { + return "ReceivedState{}"; + } +} \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java b/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java deleted file mode 100644 index 0f12d050db..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/context/TradeContext.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.state.context; - -import com.baeldung.state.states.TradeBehavior; - -public class TradeContext { - - private TradeBehavior behavior; - - public TradeContext(TradeBehavior behavior) { - this.behavior = behavior; - } - - public TradeBehavior getBehavior() { - return behavior; - } - - public void setBehavior(TradeBehavior behavior) { - this.behavior = behavior; - } - - public void trade() { - behavior.trade(this); - } - - @Override - public String toString() { - return "TradeContext{" + "behavior=" + behavior + '}'; - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java deleted file mode 100644 index 540530e50b..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/DayTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class DayTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Day Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java deleted file mode 100644 index cf6e3ea695..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/FallbackDayTradeBehavior.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class FallbackDayTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Day Trade"); - - if (Math.random() > 0.4) { - ctx.setBehavior(new SwingTradeBehavior()); - System.out.println("Day trading would not go well, falling back to Swing Trade"); - } - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java deleted file mode 100644 index 0b1facb143..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/PositionTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class PositionTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Position Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java deleted file mode 100644 index bbe8091bed..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/SwingTradeBehavior.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public class SwingTradeBehavior implements TradeBehavior { - - @Override - public void trade(TradeContext ctx) { - System.out.println("Trading method: Swing Trade"); - } -} diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java b/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java deleted file mode 100644 index ef5554ab50..0000000000 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/states/TradeBehavior.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.state.states; - -import com.baeldung.state.context.TradeContext; - -public interface TradeBehavior { - void trade(TradeContext ctx); -} diff --git a/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java new file mode 100644 index 0000000000..731974f92b --- /dev/null +++ b/patterns/design-patterns/src/test/java/com/baeldung/state/StatePatternUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.state; + +import com.baeldung.state.Package; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; + +import org.junit.Test; + +public class StatePatternUnitTest { + + @Test + public void givenNewPackage_whenPackageReceived_thenStateReceived() { + Package pkg = new Package(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(DeliveredState.class)); + pkg.nextState(); + + assertThat(pkg.getState(), instanceOf(ReceivedState.class)); + } + + @Test + public void givenDeliveredPackage_whenPrevState_thenStateOrdered() { + Package pkg = new Package(); + pkg.setState(new DeliveredState()); + pkg.previousState(); + + assertThat(pkg.getState(), instanceOf(OrderedState.class)); + } +} From 7c422c70ad16e7783ff5a919d6a330f9a45307ce Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 9 Aug 2018 21:06:30 +0200 Subject: [PATCH 03/11] BAEL-1997 add additional method to the states --- .../com/baeldung/state/DeliveredState.java | 6 ++++++ .../java/com/baeldung/state/OrderedState.java | 5 +++++ .../main/java/com/baeldung/state/Package.java | 4 ++++ .../java/com/baeldung/state/PackageState.java | 2 ++ .../com/baeldung/state/ReceivedState.java | 5 +++++ .../java/com/baeldung/state/StateDemo.java | 19 +++++++++++++++++++ 6 files changed, 41 insertions(+) create mode 100644 patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java index adb0fd3f5a..9f5e4d8fc4 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/DeliveredState.java @@ -12,8 +12,14 @@ public class DeliveredState implements PackageState { pkg.setState(new OrderedState()); } + @Override + public void printStatus() { + System.out.println("Package delivered to post office, not received yet."); + } + @Override public String toString() { return "DeliveredState{}"; } + } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java index 56a267f160..0642c4c73c 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/OrderedState.java @@ -12,6 +12,11 @@ public class OrderedState implements PackageState { System.out.println("The package is in it's root state."); } + @Override + public void printStatus() { + System.out.println("Package ordered, not delivered to the office yet."); + } + @Override public String toString() { return "OrderedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java index ff04d543b8..f3dfbb3fa7 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/Package.java @@ -19,4 +19,8 @@ public class Package { public void nextState() { state.next(this); } + + public void printStatus() { + state.printStatus(); + } } diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java index d7f7a5c110..d6656c78ac 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/PackageState.java @@ -5,4 +5,6 @@ public interface PackageState { void next(Package pkg); void prev(Package pkg); + + void printStatus(); } \ No newline at end of file diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java index be76937159..de25672e67 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -16,6 +16,11 @@ public class ReceivedState implements PackageState { pkg.setState(new DeliveredState()); } + @Override + public void printStatus() { + System.out.println("Package was received by client."); + } + @Override public String toString() { return "ReceivedState{}"; diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java new file mode 100644 index 0000000000..1a63ea3ddf --- /dev/null +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/StateDemo.java @@ -0,0 +1,19 @@ +package com.baeldung.state; + +public class StateDemo { + + public static void main(String[] args) { + + Package pkg = new Package(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + + pkg.nextState(); + pkg.printStatus(); + } +} From 1a1ea1560dfcae9b472a2e046ce5de1cc72c6186 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 10 Aug 2018 23:28:16 +0200 Subject: [PATCH 04/11] BAEL-1997 clean up in ReceivedState --- .../src/main/java/com/baeldung/state/ReceivedState.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java index de25672e67..84136fa48e 100644 --- a/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java +++ b/patterns/design-patterns/src/main/java/com/baeldung/state/ReceivedState.java @@ -4,11 +4,7 @@ public class ReceivedState implements PackageState { @Override public void next(Package pkg) { - if (pkg.getState() instanceof ReceivedState) { - System.out.println("This package is already received by a client."); - return; - } - pkg.setState(this); + System.out.println("This package is already received by a client."); } @Override From b2644c5cfcf112f548fa3766207aa168a152bc7d Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 28 Aug 2018 17:28:41 +0200 Subject: [PATCH 05/11] BAEL-2144 sort string alphabetically --- .../string/anagram/AnagramValidator.java | 29 +++++++++++++++++ .../string/AnagramValidatorUnitTest.java | 26 ++++++++++++++++ .../sorting/string/SortStringUnitTest.java | 31 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java create mode 100644 core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java new file mode 100644 index 0000000000..a12571b2e2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java @@ -0,0 +1,29 @@ +package com.baeldung.sorting.string.anagram; + +import java.util.Arrays; + +public class AnagramValidator { + + public static boolean isValid(String text, String anagram) { + text = prepare(text); + anagram = prepare(anagram); + + String sortedText = sort(text); + String sortedAnagram = sort(anagram); + + return sortedText.equals(sortedAnagram); + } + + private static String sort(String text) { + char[] chars = prepare(text).toCharArray(); + + Arrays.sort(chars); + return new String(chars); + } + + private static String prepare(String text) { + return text.toLowerCase() + .trim() + .replaceAll("\\s+", ""); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java b/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java new file mode 100644 index 0000000000..7759e929ae --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java @@ -0,0 +1,26 @@ +package org.baeldung.java.sorting.string; + + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +import com.baeldung.sorting.string.anagram.AnagramValidator; + +class AnagramValidatorUnitTest { + + @Test + void givenValidAnagrams_whenSorted_thenEqual() { + boolean isValidAnagram = AnagramValidator.isValid("Avida Dollars", "Salvador Dali"); + + assertTrue(isValidAnagram); + } + + @Test + void givenNotValidAnagrams_whenSorted_thenNotEqual() { + boolean isValidAnagram = AnagramValidator.isValid("abc", "def"); + + assertFalse(isValidAnagram); + } +} diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java b/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java new file mode 100644 index 0000000000..73fd29f0d3 --- /dev/null +++ b/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java @@ -0,0 +1,31 @@ +package org.baeldung.java.sorting.string; + +import java.util.Arrays; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class SortStringUnitTest { + + @Test + void givenString_whenSort_thenSorted() { + String abcd = "bdca"; + char[] chars = abcd.toCharArray(); + + Arrays.sort(chars); + String sorted = new String(chars); + + assertThat(sorted).isEqualTo("abcd"); + } + + @Test + void givenString_whenSortJava8_thenSorted() { + String sorted = "bdca".chars() + .sorted() + .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) + .toString(); + + assertThat(sorted).isEqualTo("abcd"); + } +} From 0782ae03c0f13911a6627f4fabebad2107955d3b Mon Sep 17 00:00:00 2001 From: Tom Hombergs Date: Wed, 29 Aug 2018 22:01:34 +0200 Subject: [PATCH 06/11] moved spring-jersey-rx into spring-jersey --- jersey-client-rx/pom.xml | 67 ------------------- jersey-client-rx/readme.md | 3 - spring-jersey/pom.xml | 45 ++++++++++++- .../ClientOrchestrationIntegrationTest.java | 2 +- 4 files changed, 45 insertions(+), 72 deletions(-) delete mode 100644 jersey-client-rx/pom.xml delete mode 100644 jersey-client-rx/readme.md rename {jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx => spring-jersey/src/test/java/com/baeldung/clientrx}/ClientOrchestrationIntegrationTest.java (99%) diff --git a/jersey-client-rx/pom.xml b/jersey-client-rx/pom.xml deleted file mode 100644 index 24894869a5..0000000000 --- a/jersey-client-rx/pom.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - 4.0.0 - com.baeldung.samples - jersey-client-rx - 1.0 - jar - - - org.glassfish.jersey.inject - jersey-hk2 - 2.27 - - - org.glassfish.jersey.core - jersey-client - 2.27 - - - org.glassfish.jersey.ext.rx - jersey-rx-client-rxjava - 2.27 - - - org.glassfish.jersey.ext.rx - jersey-rx-client-rxjava2 - 2.27 - - - com.github.tomakehurst - wiremock - 1.58 - test - - - org.junit.vintage - junit-vintage-engine - 5.2.0 - - - org.glassfish.jersey.media - jersey-media-json-jackson - 2.25 - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - 2.4.1 - - - org.slf4j - slf4j-jdk14 - 1.7.25 - - - org.assertj - assertj-core - 3.10.0 - test - - - - UTF-8 - 1.8 - 1.8 - - \ No newline at end of file diff --git a/jersey-client-rx/readme.md b/jersey-client-rx/readme.md deleted file mode 100644 index d1bc4e950b..0000000000 --- a/jersey-client-rx/readme.md +++ /dev/null @@ -1,3 +0,0 @@ -# Fluent, Reactive Jersey Client Orchestration # - -### Sample code demonstrating the options for asynchronous, reactive RESTful service consumption with JAX-RS ### diff --git a/spring-jersey/pom.xml b/spring-jersey/pom.xml index 22adf265b1..40dc91a88d 100644 --- a/spring-jersey/pom.xml +++ b/spring-jersey/pom.xml @@ -76,6 +76,49 @@ test + + org.glassfish.jersey.inject + jersey-hk2 + ${jersey.version} + + + org.glassfish.jersey.ext.rx + jersey-rx-client-rxjava + ${jersey.version} + + + org.glassfish.jersey.ext.rx + jersey-rx-client-rxjava2 + ${jersey.version} + + + com.github.tomakehurst + wiremock + 1.58 + test + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + 2.6.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.6.0 + + + org.slf4j + slf4j-jdk14 + 1.7.25 + + + org.assertj + assertj-core + 3.10.0 + test + + @@ -170,7 +213,7 @@ - 2.26 + 2.27 3.2.0 1.6.1 4.4.9 diff --git a/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationIntegrationTest.java b/spring-jersey/src/test/java/com/baeldung/clientrx/ClientOrchestrationIntegrationTest.java similarity index 99% rename from jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationIntegrationTest.java rename to spring-jersey/src/test/java/com/baeldung/clientrx/ClientOrchestrationIntegrationTest.java index 88a8d67a7d..990279a481 100644 --- a/jersey-client-rx/src/test/java/com/baeldung/samples/jerseyrx/ClientOrchestrationIntegrationTest.java +++ b/spring-jersey/src/test/java/com/baeldung/clientrx/ClientOrchestrationIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.samples.jerseyrx; +package com.baeldung.clientrx; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.assertj.core.api.Assertions.*; From 7b639b69cd8e04f0958774fe762b27b3347450b9 Mon Sep 17 00:00:00 2001 From: Tom Hombergs Date: Wed, 29 Aug 2018 22:05:08 +0200 Subject: [PATCH 07/11] removed spring-jersey-rx from pom.xml --- pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/pom.xml b/pom.xml index ad2f607141..a4537c6d55 100644 --- a/pom.xml +++ b/pom.xml @@ -584,7 +584,6 @@ spring-security-thymeleaf persistence-modules/java-jdbi jersey - jersey-client-rx java-spi performance-tests twilio From 61de6251ec763ab3aadbb64e2e061f2ce3849410 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 30 Aug 2018 21:58:38 +0200 Subject: [PATCH 08/11] BAEL-2144 move code to different module --- .../string/sorting/AnagramValidator.java | 29 +++++++++++++++++++ .../sorting}/AnagramValidatorUnitTest.java | 4 +-- .../string/sorting}/SortStringUnitTest.java | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java rename {core-java/src/test/java/org/baeldung/java/sorting/string => java-strings/src/test/java/com/baeldung/string/sorting}/AnagramValidatorUnitTest.java (85%) rename {core-java/src/test/java/org/baeldung/java/sorting/string => java-strings/src/test/java/com/baeldung/string/sorting}/SortStringUnitTest.java (94%) diff --git a/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java b/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java new file mode 100644 index 0000000000..c4f684383d --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/sorting/AnagramValidator.java @@ -0,0 +1,29 @@ +package com.baeldung.string.sorting; + +import java.util.Arrays; + +public class AnagramValidator { + + public static boolean isValid(String text, String anagram) { + text = prepare(text); + anagram = prepare(anagram); + + String sortedText = sort(text); + String sortedAnagram = sort(anagram); + + return sortedText.equals(sortedAnagram); + } + + private static String sort(String text) { + char[] chars = prepare(text).toCharArray(); + + Arrays.sort(chars); + return new String(chars); + } + + private static String prepare(String text) { + return text.toLowerCase() + .trim() + .replaceAll("\\s+", ""); + } +} \ No newline at end of file diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java b/java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java similarity index 85% rename from core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java rename to java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java index 7759e929ae..07d31c7187 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/string/AnagramValidatorUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/sorting/AnagramValidatorUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.java.sorting.string; +package com.baeldung.string.sorting; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -6,7 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import org.junit.jupiter.api.Test; -import com.baeldung.sorting.string.anagram.AnagramValidator; +import com.baeldung.string.sorting.AnagramValidator; class AnagramValidatorUnitTest { diff --git a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java b/java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java similarity index 94% rename from core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java rename to java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java index 73fd29f0d3..90d1dad554 100644 --- a/core-java/src/test/java/org/baeldung/java/sorting/string/SortStringUnitTest.java +++ b/java-strings/src/test/java/com/baeldung/string/sorting/SortStringUnitTest.java @@ -1,4 +1,4 @@ -package org.baeldung.java.sorting.string; +package com.baeldung.string.sorting; import java.util.Arrays; From db0caea58884a3c573d08a1e4290e73743c3b4b5 Mon Sep 17 00:00:00 2001 From: Denis Date: Thu, 30 Aug 2018 22:08:34 +0200 Subject: [PATCH 09/11] BAEL-2144 remove duplicated anagram validator from old location --- .../string/anagram/AnagramValidator.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java diff --git a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java b/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java deleted file mode 100644 index a12571b2e2..0000000000 --- a/core-java/src/main/java/com/baeldung/sorting/string/anagram/AnagramValidator.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.sorting.string.anagram; - -import java.util.Arrays; - -public class AnagramValidator { - - public static boolean isValid(String text, String anagram) { - text = prepare(text); - anagram = prepare(anagram); - - String sortedText = sort(text); - String sortedAnagram = sort(anagram); - - return sortedText.equals(sortedAnagram); - } - - private static String sort(String text) { - char[] chars = prepare(text).toCharArray(); - - Arrays.sort(chars); - return new String(chars); - } - - private static String prepare(String text) { - return text.toLowerCase() - .trim() - .replaceAll("\\s+", ""); - } -} \ No newline at end of file From 382459037568fea792202b36d1179b1e9ec922f5 Mon Sep 17 00:00:00 2001 From: myluckagain Date: Fri, 31 Aug 2018 02:42:16 +0500 Subject: [PATCH 10/11] float to double (#5118) --- .../LinesIntersectionService.java | 10 +++++----- .../LinesIntersectionServiceUnitTest.java | 20 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java b/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java index e4fed5a22e..a2cce53c8f 100644 --- a/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java +++ b/core-java/src/main/java/com/baeldung/linesintersection/LinesIntersectionService.java @@ -5,17 +5,17 @@ import java.util.Optional; public class LinesIntersectionService { - public Optional calculateIntersectionPoint(float m1, float b1, float m2, float b2) { + public Optional calculateIntersectionPoint(double m1, double b1, double m2, double b2) { if (m1 == m2) { return Optional.empty(); } - - float x = (b2 - b1) / (m1 - m2); - float y = m1 * x + b1; - Point point = new Point(Math.round(x), Math.round(y)); + double x = (b2 - b1) / (m1 - m2); + double y = m1 * x + b1; + Point point = new Point(); + point.setLocation(x, y); return Optional.of(point); } } diff --git a/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java b/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java index 90c93fe050..47f1fd45a1 100644 --- a/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java +++ b/core-java/src/test/java/com/baeldung/linesintersection/LinesIntersectionServiceUnitTest.java @@ -14,24 +14,24 @@ public class LinesIntersectionServiceUnitTest { @Test public void givenNotParallelLines_whenCalculatePoint_thenPresent() { - float m1 = 0; - float b1 = 0; - float m2 = 1; - float b2 = -1; + double m1 = 0; + double b1 = 0; + double m2 = 1; + double b2 = -1; Optional point = service.calculateIntersectionPoint(m1, b1, m2, b2); assertTrue(point.isPresent()); - assertEquals(point.get().x, 1); - assertEquals(point.get().y, 0); + assertEquals(point.get().getX(), 1, 0.001); + assertEquals(point.get().getX(), 1, 0.001); } @Test public void givenParallelLines_whenCalculatePoint_thenEmpty() { - float m1 = 1; - float b1 = 0; - float m2 = 1; - float b2 = -1; + double m1 = 1; + double b1 = 0; + double m2 = 1; + double b2 = -1; Optional point = service.calculateIntersectionPoint(m1, b1, m2, b2); From d7edc9a7c2812cf5ef7aeac6c0ad452185b29fe0 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 30 Aug 2018 22:31:26 -0300 Subject: [PATCH 11/11] BAEL-2102 - Configure a Tomcat Connection Pool in Spring Boot (#5095) * Initial Commit * Initial Commit * Initial Commit * Update CommandLineCrudRunner.java * Update pom.xml * Update Customer.java * Update application.properties * Update pom.xml * Update SpringBootConsoleApplication.java * Update SpringBootConsoleApplication.java * Update SpringBootConsoleApplication.java * Update CommandLineCrudRunner.java * Update pom.xml --- spring-boot-persistence/pom.xml | 128 ++++++++++-------- .../SpringBootConsoleApplication.java | 22 +++ .../application/entities/Customer.java | 53 ++++++++ .../repositories/CustomerRepository.java | 12 ++ .../runners/CommandLineCrudRunner.java | 37 +++++ .../src/main/resources/application.properties | 18 ++- ...otTomcatConnectionPoolIntegrationTest.java | 22 +++ 7 files changed, 235 insertions(+), 57 deletions(-) create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java create mode 100644 spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java create mode 100644 spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java diff --git a/spring-boot-persistence/pom.xml b/spring-boot-persistence/pom.xml index a16d953581..08989edfa9 100644 --- a/spring-boot-persistence/pom.xml +++ b/spring-boot-persistence/pom.xml @@ -1,57 +1,75 @@ - - 4.0.0 + + + 4.0.0 + + com.baeldung + spring-boot-persistence + 0.1.0 + + + parent-boot-2 com.baeldung - spring-boot-persistence 0.0.1-SNAPSHOT - jar - spring-boot-persistence - This is a simple Spring Data Repositories test - - - parent-boot-2 - com.baeldung - 0.0.1-SNAPSHOT - ../parent-boot-2 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - - - org.springframework.boot - spring-boot-starter - - - - - spring-boot-persistence - - - src/main/resources - true - - - - - org.apache.maven.plugins - maven-war-plugin - - - pl.project13.maven - git-commit-id-plugin - ${git-commit-id-plugin.version} - - - - - - 2.2.4 - - \ No newline at end of file + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.zaxxer + HikariCP + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.apache.tomcat + tomcat-jdbc + ${tomcat-jdbc.version} + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + + + com.h2database + h2 + ${h2database.version} + runtime + + + + + UTF-8 + 1.8 + 8.0.12 + 9.0.10 + 1.4.197 + + + + spring-boot-persistence + + + src/main/resources + true + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java new file mode 100644 index 0000000000..ff37442cd4 --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/SpringBootConsoleApplication.java @@ -0,0 +1,22 @@ +package com.baeldung.tomcatconnectionpool.application; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@SpringBootApplication +@EnableAutoConfiguration +@ComponentScan(basePackages={"com.baeldung.tomcatconnectionpool.application"}) +@EnableJpaRepositories(basePackages="com.baeldung.tomcatconnectionpool.application.repositories") +@EnableTransactionManagement +@EntityScan(basePackages="com.baeldung.tomcatconnectionpool.application.entities") +public class SpringBootConsoleApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootConsoleApplication.class); + } +} diff --git a/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java new file mode 100644 index 0000000000..4003d5aca9 --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/entities/Customer.java @@ -0,0 +1,53 @@ +package com.baeldung.tomcatconnectionpool.application.entities; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "customers") +public class Customer { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + @Column(name = "first_name") + private String firstName; + @Column(name = "last_name") + private String lastName; + + public Customer() {} + + public Customer(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public long getId() { + return id; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + @Override + public String toString() { + return "Customer{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}'; + } +} diff --git a/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java new file mode 100644 index 0000000000..770906439c --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/repositories/CustomerRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.tomcatconnectionpool.application.repositories; + +import com.baeldung.tomcatconnectionpool.application.entities.Customer; +import java.util.List; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CustomerRepository extends CrudRepository { + + List findByLastName(String lastName); +} diff --git a/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java new file mode 100644 index 0000000000..9666bac5a5 --- /dev/null +++ b/spring-boot-persistence/src/main/java/com/baeldung/tomcatconnectionpool/application/runners/CommandLineCrudRunner.java @@ -0,0 +1,37 @@ +package com.baeldung.tomcatconnectionpool.application.runners; + +import com.baeldung.tomcatconnectionpool.application.entities.Customer; +import com.baeldung.tomcatconnectionpool.application.repositories.CustomerRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +@Component +public class CommandLineCrudRunner implements CommandLineRunner { + + private static final Logger logger = LoggerFactory.getLogger(CommandLineCrudRunner.class); + + @Autowired + private CustomerRepository customerRepository; + + @Override + public void run(String... args) throws Exception { + customerRepository.save(new Customer("John", "Doe")); + customerRepository.save(new Customer("Jennifer", "Wilson")); + + logger.info("Customers found with findAll():"); + customerRepository.findAll().forEach(c -> logger.info(c.toString())); + + logger.info("Customer found with findById(1L):"); + Customer customer = customerRepository.findById(1L) + .orElseGet(() -> new Customer("Non-existing customer", "")); + logger.info(customer.toString()); + + logger.info("Customer found with findByLastName('Wilson'):"); + customerRepository.findByLastName("Wilson").forEach(c -> { + logger.info(c.toString()); + }); + } +} diff --git a/spring-boot-persistence/src/main/resources/application.properties b/spring-boot-persistence/src/main/resources/application.properties index d0fb785fe8..303ce33c25 100644 --- a/spring-boot-persistence/src/main/resources/application.properties +++ b/spring-boot-persistence/src/main/resources/application.properties @@ -1,2 +1,16 @@ -spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=none \ No newline at end of file +spring.datasource.tomcat.initial-size=15 +spring.datasource.tomcat.max-wait=20000 +spring.datasource.tomcat.max-active=50 +spring.datasource.tomcat.max-idle=15 +spring.datasource.tomcat.min-idle=8 +spring.datasource.tomcat.default-auto-commit=true +spring.datasource.url=jdbc:h2:mem:test +spring.datasource.username=root +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver + +spring.jpa.show-sql=false +spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect +spring.jpa.properties.hibernate.id.new_generator_mappings=false diff --git a/spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java b/spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java new file mode 100644 index 0000000000..c68e137fb0 --- /dev/null +++ b/spring-boot-persistence/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.tomcatconnectionpool.test.application; + +import javax.sql.DataSource; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringRunner; +import static org.assertj.core.api.Assertions.*; +import org.springframework.boot.test.context.SpringBootTest; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootTomcatConnectionPoolIntegrationTest { + + @Autowired + private DataSource dataSource; + + @Test + public void givenTomcatConnectionPoolInstance_whenCheckedPoolClassName_thenCorrect() { + assertThat(dataSource.getClass().getName()).isEqualTo("org.apache.tomcat.jdbc.pool.DataSource"); + } +}