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:
committed by
Ilkka Seppälä
parent
19b129c28e
commit
73f9b8bef1
@ -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()));
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,9 @@
|
||||
|
||||
package com.iluwatar.specification.property;
|
||||
|
||||
/** Mass property. */
|
||||
/**
|
||||
* Mass property.
|
||||
*/
|
||||
public class Mass {
|
||||
|
||||
private double value;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -26,9 +26,7 @@ package com.iluwatar.specification.app;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* Application test
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
|
@ -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,24 +82,28 @@ 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),
|
||||
toString
|
||||
String.format("%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color,
|
||||
mass),
|
||||
toString
|
||||
);
|
||||
}
|
||||
}
|
@ -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
|
||||
*
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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() {
|
||||
|
@ -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
|
||||
*
|
||||
|
Reference in New Issue
Block a user