diff --git a/pom.xml b/pom.xml index 93e0fc03b..f23369107 100644 --- a/pom.xml +++ b/pom.xml @@ -190,6 +190,7 @@ combinator update-method leader-followers + strangler arrange-act-assert diff --git a/strangler/README.md b/strangler/README.md new file mode 100644 index 000000000..2f157f1d2 --- /dev/null +++ b/strangler/README.md @@ -0,0 +1,31 @@ +--- +layout: pattern +title: Strangler +folder: strangler +permalink: /patterns/strangler/ +categories: Structural +tags: + - Extensibility +--- + +## Intent +Incrementally migrate a legacy system by gradually replacing specific pieces of functionality +with new applications and services. As features from the legacy system are replaced, the new +system eventually covers all the old system's features and may has its own new features, then +strangling the old system and allowing you to decommission it. + +## Class diagram +![alt text](./etc/strangler.png "Strangler") + +## Applicability +This strangler pattern is a safe way to phase one thing out for something better, cheaper, or +more expandable. Especially when you want to update legacy system with new techniques and need +continuously develop new features at the same time. Note that this pattern indeed need extra effort, +so usually use it when the system is not so simple. + +## Credits + +* [Strangler pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/strangler#context-and-problem) +* [Legacy Application Strangulation : Case Studies](https://paulhammant.com/2013/07/14/legacy-application-strangulation-case-studies/) + + diff --git a/strangler/etc/strangler.png b/strangler/etc/strangler.png new file mode 100644 index 000000000..c69305e5d Binary files /dev/null and b/strangler/etc/strangler.png differ diff --git a/strangler/etc/strangler.puml b/strangler/etc/strangler.puml new file mode 100644 index 000000000..cb2c266a1 --- /dev/null +++ b/strangler/etc/strangler.puml @@ -0,0 +1,61 @@ +@startuml + +package com.iluwatar.strangler { + class App { + + main(args : String[]) {static} + } + + class OldArithmetic { + - LOGGER : Logger {static} + - VERSION : String {static} + - source : OldSource + + sum(nums : int...) + + mul(nums : int...) + } + + class HalfArithmetic { + - LOGGER : Logger {static} + - VERSION : String {static} + - oldSource : OldSource + - newSource : HalfSource + + sum(nums : int...) + + mul(nums : int...) + + ifHasZero(nums : int...) + } + + class NewArithmetic { + - LOGGER : Logger {static} + - VERSION : String {static} + - source : NewSource + + sum(nums : int...) + + mul(nums : int...) + + ifHasZero(nums : int...) + } + + class OldSource { + - LOGGER : Logger {static} + - VERSION : String {static} + + accumulateSum(nums : int...) + + accumulateMul(nums : int...) + } + + class HalfSource { + - LOGGER : Logger {static} + - VERSION : String {static} + + accumulateSum(nums : int...) + + ifNonZero(nums : int...) + } + + class NewSource { + - LOGGER : Logger {static} + - VERSION : String {static} + + accumulateSum(nums : int...) + + accumulateMul(nums : int...) + + ifNonZero(nums : int...) + } +} +OldArithmetic o--> OldSource +HalfArithmetic o--> OldSource +HalfArithmetic o--> HalfSource +NewArithmetic o--> NewSource +@enduml \ No newline at end of file diff --git a/strangler/pom.xml b/strangler/pom.xml new file mode 100644 index 000000000..4a1fb42ba --- /dev/null +++ b/strangler/pom.xml @@ -0,0 +1,66 @@ + + + + + java-design-patterns + com.iluwatar + 1.23.0-SNAPSHOT + + 4.0.0 + + strangler + + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.strangler.App + + + + + + + + + + + \ No newline at end of file diff --git a/strangler/src/main/java/com/iluwatar/strangler/App.java b/strangler/src/main/java/com/iluwatar/strangler/App.java new file mode 100644 index 000000000..bba0f7f74 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/App.java @@ -0,0 +1,69 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +/** + * + *

The Strangler pattern is a software design pattern that incrementally migrate a legacy + * system by gradually replacing specific pieces of functionality with new applications and + * services. As features from the legacy system are replaced, the new system eventually + * replaces all of the old system's features, strangling the old system and allowing you + * to decommission it.

+ * + *

This pattern is not only about updating but also enhancement.

+ * + *

In this example, {@link OldArithmetic} indicates old system and its implementation depends + * on its source ({@link OldSource}). Now we tend to update system with new techniques and + * new features. In reality, the system may too complex, so usually need gradual migration. + * {@link HalfArithmetic} indicates system in the process of migration, its implementation + * depends on old one ({@link OldSource}) and under development one ({@link HalfSource}). The + * {@link HalfSource} covers part of {@link OldSource} and add new functionality. You can release + * this version system with new features, which also supports old version system functionalities. + * After whole migration, the new system ({@link NewArithmetic}) only depends on new source + * ({@link NewSource}).

+ * + */ +public class App { + /** + * Program entry point. + * @param args command line args + */ + public static void main(final String[] args) { + final var nums = new int[]{1, 2, 3, 4, 5}; + //Before migration + final var oldSystem = new OldArithmetic(new OldSource()); + oldSystem.sum(nums); + oldSystem.mul(nums); + //In process of migration + final var halfSystem = new HalfArithmetic(new HalfSource(), new OldSource()); + halfSystem.sum(nums); + halfSystem.mul(nums); + halfSystem.ifHasZero(nums); + //After migration + final var newSystem = new NewArithmetic(new NewSource()); + newSystem.sum(nums); + newSystem.mul(nums); + newSystem.ifHasZero(nums); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/HalfArithmetic.java b/strangler/src/main/java/com/iluwatar/strangler/HalfArithmetic.java new file mode 100644 index 000000000..be9c15ec5 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/HalfArithmetic.java @@ -0,0 +1,74 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * System under migration. Depends on old version source ({@link OldSource}) and + * developing one ({@link HalfSource}). + */ +public class HalfArithmetic { + private static final Logger LOGGER = LoggerFactory.getLogger(HalfArithmetic.class); + private static final String VERSION = "1.5"; + + private final HalfSource newSource; + private final OldSource oldSource; + + public HalfArithmetic(HalfSource newSource, OldSource oldSource) { + this.newSource = newSource; + this.oldSource = oldSource; + } + + /** + * Accumulate sum. + * @param nums numbers need to add together + * @return accumulate sum + */ + public int sum(int... nums) { + LOGGER.info("Arithmetic sum {}", VERSION); + return newSource.accumulateSum(nums); + } + + /** + * Accumulate multiplication. + * @param nums numbers need to multiply together + * @return accumulate multiplication + */ + public int mul(int... nums) { + LOGGER.info("Arithmetic mul {}", VERSION); + return oldSource.accumulateMul(nums); + } + + /** + * Chech if has any zero. + * @param nums numbers need to check + * @return if has any zero, return true, else, return false + */ + public boolean ifHasZero(int... nums) { + LOGGER.info("Arithmetic check zero {}", VERSION); + return !newSource.ifNonZero(nums); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/HalfSource.java b/strangler/src/main/java/com/iluwatar/strangler/HalfSource.java new file mode 100644 index 000000000..b83293335 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/HalfSource.java @@ -0,0 +1,54 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import java.util.Arrays; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Source under development. Replace part of old source and has added some new features. + */ +public class HalfSource { + private static final Logger LOGGER = LoggerFactory.getLogger(HalfSource.class); + private static final String VERSION = "1.5"; + + /** + * Implement accumulate sum with new technique. + * Replace old one in {@link OldSource} + */ + public int accumulateSum(int... nums) { + LOGGER.info("Source module {}", VERSION); + return Arrays.stream(nums).reduce(0, Integer::sum); + } + + /** + * Check if all number is not zero. + * New feature. + */ + public boolean ifNonZero(int... nums) { + LOGGER.info("Source module {}", VERSION); + return Arrays.stream(nums).allMatch(num -> num != 0); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/NewArithmetic.java b/strangler/src/main/java/com/iluwatar/strangler/NewArithmetic.java new file mode 100644 index 000000000..8e482c8b3 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/NewArithmetic.java @@ -0,0 +1,71 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * System after whole migration. Only depends on new version source ({@link NewSource}). + */ +public class NewArithmetic { + private static final Logger LOGGER = LoggerFactory.getLogger(NewArithmetic.class); + private static final String VERSION = "2.0"; + + private final NewSource source; + + public NewArithmetic(NewSource source) { + this.source = source; + } + + /** + * Accumulate sum. + * @param nums numbers need to add together + * @return accumulate sum + */ + public int sum(int... nums) { + LOGGER.info("Arithmetic sum {}", VERSION); + return source.accumulateSum(nums); + } + + /** + * Accumulate multiplication. + * @param nums numbers need to multiply together + * @return accumulate multiplication + */ + public int mul(int... nums) { + LOGGER.info("Arithmetic mul {}", VERSION); + return source.accumulateMul(nums); + } + + /** + * Chech if has any zero. + * @param nums numbers need to check + * @return if has any zero, return true, else, return false + */ + public boolean ifHasZero(int... nums) { + LOGGER.info("Arithmetic check zero {}", VERSION); + return !source.ifNonZero(nums); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/NewSource.java b/strangler/src/main/java/com/iluwatar/strangler/NewSource.java new file mode 100644 index 000000000..f53a31bd8 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/NewSource.java @@ -0,0 +1,56 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import java.util.Arrays; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * New source. Completely covers functionalities of old source with new techniques + * and also has some new features. + */ +public class NewSource { + private static final Logger LOGGER = LoggerFactory.getLogger(NewSource.class); + private static final String VERSION = "2.0"; + + public int accumulateSum(int... nums) { + LOGGER.info("Source module {}", VERSION); + return Arrays.stream(nums).reduce(0, Integer::sum); + } + + /** + * Implement accumulate multiply with new technique. + * Replace old one in {@link OldSource} + */ + public int accumulateMul(int... nums) { + LOGGER.info("Source module {}", VERSION); + return Arrays.stream(nums).reduce(1, (a, b) -> a * b); + } + + public boolean ifNonZero(int... nums) { + LOGGER.info("Source module {}", VERSION); + return Arrays.stream(nums).allMatch(num -> num != 0); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/OldArithmetic.java b/strangler/src/main/java/com/iluwatar/strangler/OldArithmetic.java new file mode 100644 index 000000000..e9d57987a --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/OldArithmetic.java @@ -0,0 +1,61 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Old version system depends on old version source ({@link OldSource}). + */ +public class OldArithmetic { + private static final Logger LOGGER = LoggerFactory.getLogger(OldArithmetic.class); + private static final String VERSION = "1.0"; + + private final OldSource source; + + public OldArithmetic(OldSource source) { + this.source = source; + } + + /** + * Accumulate sum. + * @param nums numbers need to add together + * @return accumulate sum + */ + public int sum(int... nums) { + LOGGER.info("Arithmetic sum {}", VERSION); + return source.accumulateSum(nums); + } + + /** + * Accumulate multiplication. + * @param nums numbers need to multiply together + * @return accumulate multiplication + */ + public int mul(int... nums) { + LOGGER.info("Arithmetic mul {}", VERSION); + return source.accumulateMul(nums); + } +} diff --git a/strangler/src/main/java/com/iluwatar/strangler/OldSource.java b/strangler/src/main/java/com/iluwatar/strangler/OldSource.java new file mode 100644 index 000000000..0ac0b5a07 --- /dev/null +++ b/strangler/src/main/java/com/iluwatar/strangler/OldSource.java @@ -0,0 +1,59 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Old source with techniques out of date. + */ +public class OldSource { + private static final Logger LOGGER = LoggerFactory.getLogger(OldSource.class); + private static final String VERSION = "1.0"; + + /** + * Implement accumulate sum with old technique. + */ + public int accumulateSum(int... nums) { + LOGGER.info("Source module {}", VERSION); + var sum = 0; + for (final var num : nums) { + sum += num; + } + return sum; + } + + /** + * Implement accumulate multiply with old technique. + */ + public int accumulateMul(int... nums) { + LOGGER.info("Source module {}", VERSION); + var sum = 1; + for (final var num : nums) { + sum *= num; + } + return sum; + } +} diff --git a/strangler/src/test/java/com/iluwatar/strangler/AppTest.java b/strangler/src/test/java/com/iluwatar/strangler/AppTest.java new file mode 100644 index 000000000..a9e878a94 --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/AppTest.java @@ -0,0 +1,36 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +/** + * Application test + */ +public class AppTest { + @Test + public void test() { + App.main(new String[]{}); + } +} diff --git a/strangler/src/test/java/com/iluwatar/strangler/HalfArithmeticTest.java b/strangler/src/test/java/com/iluwatar/strangler/HalfArithmeticTest.java new file mode 100644 index 000000000..004631966 --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/HalfArithmeticTest.java @@ -0,0 +1,50 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test methods in HalfArithmetic + */ +class HalfArithmeticTest { + private static final HalfArithmetic arithmetic = new HalfArithmetic(new HalfSource(), new OldSource()); + + @Test + public void testSum() { + assertEquals(0, arithmetic.sum(-1, 0, 1)); + } + + @Test + public void testMul() { + assertEquals(0, arithmetic.mul(-1, 0, 1)); + } + + @Test + public void testIfHasZero() { + assertTrue(arithmetic.ifHasZero(-1, 0, 1)); + } +} \ No newline at end of file diff --git a/strangler/src/test/java/com/iluwatar/strangler/HalfSourceTest.java b/strangler/src/test/java/com/iluwatar/strangler/HalfSourceTest.java new file mode 100644 index 000000000..a8f9fc6ac --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/HalfSourceTest.java @@ -0,0 +1,47 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test methods in HalfSource + */ +public class HalfSourceTest { + private static final HalfSource source = new HalfSource(); + + @Test + public void testAccumulateSum() { + assertEquals(0, source.accumulateSum(-1, 0, 1)); + } + + @Test + public void testIfNonZero() { + assertFalse(source.ifNonZero(-1, 0, 1)); + } +} diff --git a/strangler/src/test/java/com/iluwatar/strangler/NewArithmeticTest.java b/strangler/src/test/java/com/iluwatar/strangler/NewArithmeticTest.java new file mode 100644 index 000000000..edb20241b --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/NewArithmeticTest.java @@ -0,0 +1,50 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test methods in NewArithmetic + */ +class NewArithmeticTest { + private static final NewArithmetic arithmetic = new NewArithmetic(new NewSource()); + + @Test + public void testSum() { + assertEquals(0, arithmetic.sum(-1, 0, 1)); + } + + @Test + public void testMul() { + assertEquals(0, arithmetic.mul(-1, 0, 1)); + } + + @Test + public void testIfHasZero() { + assertTrue(arithmetic.ifHasZero(-1, 0, 1)); + } +} \ No newline at end of file diff --git a/strangler/src/test/java/com/iluwatar/strangler/NewSourceTest.java b/strangler/src/test/java/com/iluwatar/strangler/NewSourceTest.java new file mode 100644 index 000000000..577cc0daf --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/NewSourceTest.java @@ -0,0 +1,51 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +/** + * Test methods in NewSource + */ +public class NewSourceTest { + private static final NewSource source = new NewSource(); + + @Test + public void testAccumulateSum() { + assertEquals(0, source.accumulateSum(-1, 0, 1)); + } + + @Test + public void testAccumulateMul() { + assertEquals(0, source.accumulateMul(-1, 0, 1)); + } + + @Test + public void testIfNonZero() { + assertFalse(source.ifNonZero(-1, 0, 1)); + } +} diff --git a/strangler/src/test/java/com/iluwatar/strangler/OldArithmeticTest.java b/strangler/src/test/java/com/iluwatar/strangler/OldArithmeticTest.java new file mode 100644 index 000000000..7f653d311 --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/OldArithmeticTest.java @@ -0,0 +1,45 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test methods in OldArithmetic + */ +class OldArithmeticTest { + private static final OldArithmetic arithmetic = new OldArithmetic(new OldSource()); + + @Test + public void testSum() { + assertEquals(0, arithmetic.sum(-1, 0, 1)); + } + + @Test + public void testMul() { + assertEquals(0, arithmetic.mul(-1, 0, 1)); + } +} \ No newline at end of file diff --git a/strangler/src/test/java/com/iluwatar/strangler/OldSourceTest.java b/strangler/src/test/java/com/iluwatar/strangler/OldSourceTest.java new file mode 100644 index 000000000..12d6a6c14 --- /dev/null +++ b/strangler/src/test/java/com/iluwatar/strangler/OldSourceTest.java @@ -0,0 +1,45 @@ +/* + * The MIT License + * Copyright © 2014-2019 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.iluwatar.strangler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Test methods in OldSource + */ +public class OldSourceTest { + private static final OldSource source = new OldSource(); + + @Test + public void testAccumulateSum() { + assertEquals(0, source.accumulateSum(-1, 0, 1)); + } + + @Test + public void testAccumulateMul() { + assertEquals(0, source.accumulateMul(-1, 0, 1)); + } +}