Adding composite specification (Issue#1093) (#1094)

* Resolution proposition to Issue#1055 (UML diagram left to do)

* Deciding not to modify the UML diagram for now

* Resolution proposition to Issue#1093

* Code reformatting
This commit is contained in:
Martin Vandenbussche 2019-11-17 13:25:32 +01:00 committed by Ilkka Seppälä
parent 19b129c28e
commit 73f9b8bef1
20 changed files with 494 additions and 75 deletions

View File

@ -31,7 +31,8 @@ Use the Specification pattern when
Real world example
> There is a pool of different creatures and we often need to select some subset of them. We can write our search specification such as "creatures that can fly" or "creatures heavier than 500 kilograms" and give it to the party that will perform the filtering.
> There is a pool of different creatures and we often need to select some subset of them.
> We can write our search specification such as "creatures that can fly", "creatures heavier than 500 kilograms", or as a combination of other search specifications, and then give it to the party that will perform the filtering.
In Plain Words
@ -44,8 +45,10 @@ Wikipedia says
**Programmatic Example**
If we look at our creature pool example from above, we have a set of creatures with certain properties.\
Those properties can be part of a pre-defined, limited set (represented here by the enums Size, Movement and Color); but they can also be discrete (e.g. the mass of a Creature). In this case, it is more appropriate to use what we call "parameterized specification", where the property value can be given as an argument when the Creature is created, allowing for more flexibility.
Those properties can be part of a pre-defined, limited set (represented here by the enums Size, Movement and Color); but they can also be continuous values (e.g. the mass of a Creature).
In this case, it is more appropriate to use what we call "parameterized specification", where the property value can be given as an argument when the Creature is instantiated, allowing for more flexibility.
A third option is to combine pre-defined and/or parameterized properties using boolean logic, allowing for near-endless selection possibilities (this is called Composite Specification, see below).
The pros and cons of each approach are detailed in the table at the end of this document.
```java
public interface Creature {
String getName();
@ -56,8 +59,7 @@ public interface Creature {
}
```
And dragon implementation looks like this.
And ``Dragon`` implementation looks like this.
```java
public class Dragon extends AbstractCreature {
@ -67,10 +69,9 @@ public class Dragon extends AbstractCreature {
}
```
Now that we want to select some subset of them, we use selectors. To select creatures that fly, we should use MovementSelector.
Now that we want to select some subset of them, we use selectors. To select creatures that fly, we should use ``MovementSelector``.
```java
public class MovementSelector implements Predicate<Creature> {
public class MovementSelector extends AbstractSelector<Creature> {
private final Movement movement;
@ -85,10 +86,9 @@ public class MovementSelector implements Predicate<Creature> {
}
```
On the other hand, we selecting creatures heavier than a chosen amount, we use MassGreaterThanSelector.
On the other hand, when selecting creatures heavier than a chosen amount, we use ``MassGreaterThanSelector``.
```java
public class MassGreaterThanSelector implements Predicate<Creature> {
public class MassGreaterThanSelector extends AbstractSelector<Creature> {
private final Mass mass;
@ -103,20 +103,84 @@ public class MassGreaterThanSelector implements Predicate<Creature> {
}
```
With these building blocks in place, we can perform a search for red and flying creatures like this.
With these building blocks in place, we can perform a search for red creatures as follows :
```java
List<Creature> redAndFlyingCreatures = creatures.stream()
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING))).collect(Collectors.toList());
List<Creature> redCreatures = creatures.stream().filter(new ColorSelector(Color.RED))
.collect(Collectors.toList());
```
But we could also use our paramterized selector like this.
But we could also use our parameterized selector like this :
```java
List<Creature> heavyCreatures = creatures.stream()
.filter(new MassGreaterThanSelector(500.0).collect(Collectors.toList());
List<Creature> heavyCreatures = creatures.stream().filter(new MassGreaterThanSelector(500.0)
.collect(Collectors.toList());
```
Our third option is to combine multiple selectors together. Performing a search for special creatures (defined as red, flying, and not small) could be done as follows :
```java
AbstractSelector specialCreaturesSelector =
new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)).and(new SizeSelector(Size.SMALL).not());
List<Creature> specialCreatures = creatures.stream().filter(specialCreaturesSelector)
.collect(Collectors.toList());
```
**More on Composite Specification**
In Composite Specification, we will create custom instances of ``AbstractSelector`` by combining other selectors (called "leaves") using the three basic logical operators.
These are implemented in ``ConjunctionSelector``, ``DisjunctionSelector`` and ``NegationSelector``.
```java
public abstract class AbstractSelector<T> implements Predicate<T> {
public AbstractSelector<T> and(AbstractSelector<T> other) {
return new ConjunctionSelector<>(this, other);
}
public AbstractSelector<T> or(AbstractSelector<T> other) {
return new DisjunctionSelector<>(this, other);
}
public AbstractSelector<T> not() {
return new NegationSelector<>(this);
}
}
```
```java
public class ConjunctionSelector<T> extends AbstractSelector<T> {
private List<AbstractSelector<T>> leafComponents;
@SafeVarargs
ConjunctionSelector(AbstractSelector<T>... selectors) {
this.leafComponents = List.of(selectors);
}
/**
* Tests if *all* selectors pass the test.
*/
@Override
public boolean test(T t) {
return leafComponents.stream().allMatch(comp -> (comp.test(t)));
}
}
```
All that is left to do is now to create leaf selectors (be it hard-coded or parameterized ones) that are as generic as possible,
and we will be able to instantiate the ``AbstractSelector`` class by combining any amount of selectors, as exemplified above.
We should be careful though, as it is easy to make a mistake when combining many logical operators; in particular, we should pay attention to the priority of the operations.\
In general, Composite Specification is a great way to write more reusable code, as there is no need to create a Selector class for each filtering operation.
Instead, we just create an instance of ``AbstractSelector`` "on the spot", using tour generic "leaf" selectors and some basic boolean logic.
**Comparison of the different approaches**
| Pattern | Usage | Pros | Cons |
|---|---|---|---|
| Hard-Coded Specification | Selection criteria are few and known in advance | + Easy to implement | - Inflexible |
| | | + Expressive |
| Parameterized Specification | Selection criteria are a large range of values (e.g. mass, speed,...) | + Some flexibility | - Still requires special-purpose classes |
| Composite Specification | There are a lot of selection criteria that can be combined in multiple ways, hence it is not feasible to create a class for each selector | + Very flexible, without requiring many specialized classes | - Somewhat more difficult to comprehend |
| | | + Supports logical operations | - You still need to create the base classes used as leaves |
## Related patterns
* Repository

View File

@ -31,9 +31,10 @@ import com.iluwatar.specification.creature.Octopus;
import com.iluwatar.specification.creature.Shark;
import com.iluwatar.specification.creature.Troll;
import com.iluwatar.specification.property.Color;
import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.selector.AbstractSelector;
import com.iluwatar.specification.selector.ColorSelector;
import com.iluwatar.specification.selector.MassEqualSelector;
import com.iluwatar.specification.selector.MassGreaterThanSelector;
import com.iluwatar.specification.selector.MassSmallerThanOrEqSelector;
import com.iluwatar.specification.selector.MovementSelector;
@ -77,13 +78,8 @@ public class App {
List<Creature> darkCreatures =
creatures.stream().filter(new ColorSelector(Color.DARK)).collect(Collectors.toList());
darkCreatures.forEach(c -> LOGGER.info(c.toString()));
// find all red and flying creatures
LOGGER.info("Find all red and flying creatures");
List<Creature> redAndFlyingCreatures =
creatures.stream()
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)))
.collect(Collectors.toList());
redAndFlyingCreatures.forEach(c -> LOGGER.info(c.toString()));
LOGGER.info("\n");
// so-called "parameterized" specification
LOGGER.info("Demonstrating parameterized specification :");
// find all creatures heavier than 500kg
@ -98,5 +94,26 @@ public class App {
creatures.stream().filter(new MassSmallerThanOrEqSelector(500.0))
.collect(Collectors.toList());
lightCreatures.forEach(c -> LOGGER.info(c.toString()));
LOGGER.info("\n");
// so-called "composite" specification
LOGGER.info("Demonstrating composite specification :");
// find all red and flying creatures
LOGGER.info("Find all red and flying creatures");
List<Creature> redAndFlyingCreatures =
creatures.stream()
.filter(new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING)))
.collect(Collectors.toList());
redAndFlyingCreatures.forEach(c -> LOGGER.info(c.toString()));
// find all creatures dark or red, non-swimming, and heavier than or equal to 400kg
LOGGER.info("Find all scary creatures");
AbstractSelector<Creature> scaryCreaturesSelector = new ColorSelector(Color.DARK)
.or(new ColorSelector(Color.RED)).and(new MovementSelector(Movement.SWIMMING).not())
.and(new MassGreaterThanSelector(400.0).or(new MassEqualSelector(400.0)));
List<Creature> scaryCreatures =
creatures.stream()
.filter(scaryCreaturesSelector)
.collect(Collectors.toList());
scaryCreatures.forEach(c -> LOGGER.info(c.toString()));
}
}

View File

@ -23,7 +23,9 @@
package com.iluwatar.specification.property;
/** Mass property. */
/**
* Mass property.
*/
public class Mass {
private double value;

View File

@ -0,0 +1,44 @@
/*
* 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.specification.selector;
import java.util.function.Predicate;
/**
* Base class for selectors.
*/
public abstract class AbstractSelector<T> implements Predicate<T> {
public AbstractSelector<T> and(AbstractSelector<T> other) {
return new ConjunctionSelector<>(this, other);
}
public AbstractSelector<T> or(AbstractSelector<T> other) {
return new DisjunctionSelector<>(this, other);
}
public AbstractSelector<T> not() {
return new NegationSelector<>(this);
}
}

View File

@ -25,12 +25,11 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Color;
import java.util.function.Predicate;
/**
* Color selector.
*/
public class ColorSelector implements Predicate<Creature> {
public class ColorSelector extends AbstractSelector<Creature> {
private final Color color;

View File

@ -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.specification.selector;
import java.util.List;
/**
* A Selector defined as the conjunction (AND) of other (leaf) selectors.
*/
public class ConjunctionSelector<T> extends AbstractSelector<T> {
private List<AbstractSelector<T>> leafComponents;
@SafeVarargs
ConjunctionSelector(AbstractSelector<T>... selectors) {
this.leafComponents = List.of(selectors);
}
/**
* Tests if *all* selectors pass the test.
*/
@Override
public boolean test(T t) {
return leafComponents.stream().allMatch(comp -> (comp.test(t)));
}
}

View File

@ -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.specification.selector;
import java.util.List;
/**
* A Selector defined as the disjunction (OR) of other (leaf) selectors.
*/
public class DisjunctionSelector<T> extends AbstractSelector<T> {
private List<AbstractSelector<T>> leafComponents;
@SafeVarargs
DisjunctionSelector(AbstractSelector<T>... selectors) {
this.leafComponents = List.of(selectors);
}
/**
* Tests if *at least one* selector passes the test.
*/
@Override
public boolean test(T t) {
return leafComponents.stream().anyMatch(comp -> comp.test(t));
}
}

View File

@ -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.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
/**
* Mass selector for values exactly equal than the parameter.
*/
public class MassEqualSelector extends AbstractSelector<Creature> {
private final Mass mass;
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
public MassEqualSelector(double mass) {
this.mass = new Mass(mass);
}
@Override
public boolean test(Creature t) {
return t.getMass().equals(mass);
}
}

View File

@ -25,14 +25,17 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
import java.util.function.Predicate;
/** Mass selector for values greater than the parameter. */
public class MassGreaterThanSelector implements Predicate<Creature> {
/**
* Mass selector for values greater than the parameter.
*/
public class MassGreaterThanSelector extends AbstractSelector<Creature> {
private final Mass mass;
/** The use of a double as a parameter will spare some typing when instantiating this class. */
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
public MassGreaterThanSelector(double mass) {
this.mass = new Mass(mass);
}

View File

@ -25,14 +25,17 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
import java.util.function.Predicate;
/** Mass selector for values smaller or equal to the parameter. */
public class MassSmallerThanOrEqSelector implements Predicate<Creature> {
/**
* Mass selector for values smaller or equal to the parameter.
*/
public class MassSmallerThanOrEqSelector extends AbstractSelector<Creature> {
private final Mass mass;
/** The use of a double as a parameter will spare some typing when instantiating this class. */
/**
* The use of a double as a parameter will spare some typing when instantiating this class.
*/
public MassSmallerThanOrEqSelector(double mass) {
this.mass = new Mass(mass);
}

View File

@ -25,12 +25,11 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Movement;
import java.util.function.Predicate;
/**
* Movement selector.
*/
public class MovementSelector implements Predicate<Creature> {
public class MovementSelector extends AbstractSelector<Creature> {
private final Movement movement;

View File

@ -0,0 +1,46 @@
/*
* 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.specification.selector;
/**
* A Selector defined as the negation (NOT) of a (leaf) selectors. This is of course only useful
* when used in combination with other composite selectors.
*/
public class NegationSelector<T> extends AbstractSelector<T> {
private AbstractSelector<T> component;
NegationSelector(AbstractSelector<T> selector) {
this.component = selector;
}
/**
* Tests if the selector fails the test (yes).
*/
@Override
public boolean test(T t) {
return !(component.test(t));
}
}

View File

@ -25,12 +25,11 @@ package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Size;
import java.util.function.Predicate;
/**
* Size selector.
*/
public class SizeSelector implements Predicate<Creature> {
public class SizeSelector extends AbstractSelector<Creature> {
private final Size size;

View File

@ -26,9 +26,7 @@ package com.iluwatar.specification.app;
import org.junit.jupiter.api.Test;
/**
*
* Application test
*
*/
public class AppTest {

View File

@ -23,18 +23,17 @@
package com.iluwatar.specification.creature;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.iluwatar.specification.property.Color;
import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import com.iluwatar.specification.property.Size;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Collection;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Date: 12/29/15 - 7:47 PM
@ -48,12 +47,18 @@ public class CreatureTest {
*/
public static Collection<Object[]> dataProvider() {
return List.of(
new Object[]{new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED, new Mass(39300.0)},
new Object[]{new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN, new Mass(30.0)},
new Object[]{new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT, new Mass(6.7)},
new Object[]{new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK, new Mass(12.0)},
new Object[]{new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT, new Mass(500.0)},
new Object[]{new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK, new Mass(4000.0)}
new Object[]{new Dragon(), "Dragon", Size.LARGE, Movement.FLYING, Color.RED,
new Mass(39300.0)},
new Object[]{new Goblin(), "Goblin", Size.SMALL, Movement.WALKING, Color.GREEN,
new Mass(30.0)},
new Object[]{new KillerBee(), "KillerBee", Size.SMALL, Movement.FLYING, Color.LIGHT,
new Mass(6.7)},
new Object[]{new Octopus(), "Octopus", Size.NORMAL, Movement.SWIMMING, Color.DARK,
new Mass(12.0)},
new Object[]{new Shark(), "Shark", Size.NORMAL, Movement.SWIMMING, Color.LIGHT,
new Mass(500.0)},
new Object[]{new Troll(), "Troll", Size.LARGE, Movement.WALKING, Color.DARK,
new Mass(4000.0)}
);
}
@ -77,23 +82,27 @@ public class CreatureTest {
@ParameterizedTest
@MethodSource("dataProvider")
public void testGetColor(Creature testedCreature, String name, Size size, Movement movement, Color color) {
public void testGetColor(Creature testedCreature, String name, Size size, Movement movement,
Color color) {
assertEquals(color, testedCreature.getColor());
}
@ParameterizedTest
@MethodSource("dataProvider")
public void testGetMass(Creature testedCreature, String name, Size size, Movement movement, Color color, Mass mass) {
public void testGetMass(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
assertEquals(mass, testedCreature.getMass());
}
@ParameterizedTest
@MethodSource("dataProvider")
public void testToString(Creature testedCreature, String name, Size size, Movement movement, Color color, Mass mass) {
public void testToString(Creature testedCreature, String name, Size size, Movement movement,
Color color, Mass mass) {
final String toString = testedCreature.toString();
assertNotNull(toString);
assertEquals(
String.format("%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color, mass),
String.format("%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color,
mass),
toString
);
}

View File

@ -23,15 +23,15 @@
package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Color;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Color;
import org.junit.jupiter.api.Test;
/**
* Date: 12/29/15 - 7:35 PM
*

View File

@ -0,0 +1,93 @@
/*
* 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.specification.selector;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Mass;
import com.iluwatar.specification.property.Movement;
import org.junit.jupiter.api.Test;
public class CompositeSelectorsTest {
/**
* Verify if the conjunction selector gives the correct results.
*/
@Test
public void testAndComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> lightAndSwimmingSelector = new MassSmallerThanOrEqSelector(
50.0).and(new MovementSelector(Movement.SWIMMING));
assertFalse(lightAndSwimmingSelector.test(swimmingHeavyCreature));
assertTrue(lightAndSwimmingSelector.test(swimmingLightCreature));
}
/**
* Verify if the disjunction selector gives the correct results.
*/
@Test
public void testOrComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> lightOrSwimmingSelector = new MassSmallerThanOrEqSelector(50.0)
.or(new MovementSelector(Movement.SWIMMING));
assertTrue(lightOrSwimmingSelector.test(swimmingHeavyCreature));
assertTrue(lightOrSwimmingSelector.test(swimmingLightCreature));
}
/**
* Verify if the negation selector gives the correct results.
*/
@Test
public void testNotComposition() {
final Creature swimmingHeavyCreature = mock(Creature.class);
when(swimmingHeavyCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingHeavyCreature.getMass()).thenReturn(new Mass(100.0));
final Creature swimmingLightCreature = mock(Creature.class);
when(swimmingLightCreature.getMovement()).thenReturn(Movement.SWIMMING);
when(swimmingLightCreature.getMass()).thenReturn(new Mass(25.0));
final AbstractSelector<Creature> heavySelector = new MassSmallerThanOrEqSelector(50.0).not();
assertTrue(heavySelector.test(swimmingHeavyCreature));
assertFalse(heavySelector.test(swimmingLightCreature));
}
}

View File

@ -34,7 +34,9 @@ import org.junit.jupiter.api.Test;
public class MassSelectorTest {
/** Verify if the mass selector gives the correct results */
/**
* Verify if the mass selector gives the correct results.
*/
@Test
public void testMass() {
final Creature lightCreature = mock(Creature.class);

View File

@ -23,15 +23,15 @@
package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Movement;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Movement;
import org.junit.jupiter.api.Test;
/**
* Date: 12/29/15 - 7:37 PM
*
@ -40,7 +40,7 @@ import static org.mockito.Mockito.when;
public class MovementSelectorTest {
/**
* Verify if the movement selector gives the correct results
* Verify if the movement selector gives the correct results.
*/
@Test
public void testMovement() {

View File

@ -23,15 +23,15 @@
package com.iluwatar.specification.selector;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Size;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.iluwatar.specification.creature.Creature;
import com.iluwatar.specification.property.Size;
import org.junit.jupiter.api.Test;
/**
* Date: 12/29/15 - 7:43 PM
*