From 53884c4dd0aba81d07b455177a03101ae10b5c6d Mon Sep 17 00:00:00 2001 From: Julia Gustafsson Date: Tue, 23 May 2023 17:21:44 +0200 Subject: [PATCH] Add tests for terminal operation optimization #31 --- .../IntermediateOperationFactory.java | 5 +- .../StandardIntermediateOperationFactory.java | 1 + .../InternalIntermediateOperationFactory.java | 2 +- provider/termopoptimizer-standard/pom.xml | 5 +- .../StandardTerminalOperationOptimizer.java | 56 ++++-- .../src/main/java9/module-info.java | 1 + .../standard/internal/AnyMatchTest.java | 53 +++-- .../standard/internal/Film$.java | 111 ++++++++++ .../standard/internal/Film.java | 190 ++++++++++++++++++ .../standard/internal/FindAnyTest.java | 55 ++++- .../standard/internal/FindFirstTest.java | 54 ++++- .../standard/internal/NoneMatchTest.java | 73 ++++++- 12 files changed, 567 insertions(+), 39 deletions(-) create mode 100644 provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film$.java create mode 100644 provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film.java diff --git a/pipeline/src/main/java/com/speedment/jpastreamer/pipeline/intermediate/IntermediateOperationFactory.java b/pipeline/src/main/java/com/speedment/jpastreamer/pipeline/intermediate/IntermediateOperationFactory.java index c12aa5483..0fdf4173a 100644 --- a/pipeline/src/main/java/com/speedment/jpastreamer/pipeline/intermediate/IntermediateOperationFactory.java +++ b/pipeline/src/main/java/com/speedment/jpastreamer/pipeline/intermediate/IntermediateOperationFactory.java @@ -12,6 +12,8 @@ */ package com.speedment.jpastreamer.pipeline.intermediate; +import com.speedment.jpastreamer.pipeline.terminal.TerminalOperation; + import java.util.Comparator; import java.util.function.*; import java.util.stream.DoubleStream; @@ -22,8 +24,7 @@ public interface IntermediateOperationFactory { IntermediateOperation, Stream> createFilter(Predicate predicate); - - + IntermediateOperation, Stream> createMap(Function mapper); IntermediateOperation, IntStream> createMapToInt(ToIntFunction mapper); diff --git a/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/intermediate/StandardIntermediateOperationFactory.java b/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/intermediate/StandardIntermediateOperationFactory.java index 04e4750bf..0efd4cee0 100644 --- a/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/intermediate/StandardIntermediateOperationFactory.java +++ b/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/intermediate/StandardIntermediateOperationFactory.java @@ -15,6 +15,7 @@ import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperation; import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperationFactory; import com.speedment.jpastreamer.pipeline.standard.internal.intermediate.InternalIntermediateOperationFactory; +import com.speedment.jpastreamer.pipeline.terminal.TerminalOperation; import java.util.Comparator; import java.util.function.*; diff --git a/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/internal/intermediate/InternalIntermediateOperationFactory.java b/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/internal/intermediate/InternalIntermediateOperationFactory.java index 63a42d8e1..cc2fa6400 100644 --- a/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/internal/intermediate/InternalIntermediateOperationFactory.java +++ b/provider/pipeline-standard/src/main/java/com/speedment/jpastreamer/pipeline/standard/internal/intermediate/InternalIntermediateOperationFactory.java @@ -16,6 +16,7 @@ import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperation; import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperationFactory; import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperationType; +import com.speedment.jpastreamer.pipeline.terminal.TerminalOperation; import java.util.Comparator; import java.util.function.*; @@ -53,7 +54,6 @@ public IntermediateOperation, Stream> createFilter(final Predic Stream.class, function, predicate); - } @Override diff --git a/provider/termopoptimizer-standard/pom.xml b/provider/termopoptimizer-standard/pom.xml index 2e12c4ccb..d3d3d5d7d 100644 --- a/provider/termopoptimizer-standard/pom.xml +++ b/provider/termopoptimizer-standard/pom.xml @@ -55,7 +55,10 @@ pipeline-standard test - + + com.speedment.jpastreamer + field + diff --git a/provider/termopoptimizer-standard/src/main/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/StandardTerminalOperationOptimizer.java b/provider/termopoptimizer-standard/src/main/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/StandardTerminalOperationOptimizer.java index 5f5b0bd87..57cbf11b9 100644 --- a/provider/termopoptimizer-standard/src/main/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/StandardTerminalOperationOptimizer.java +++ b/provider/termopoptimizer-standard/src/main/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/StandardTerminalOperationOptimizer.java @@ -14,21 +14,28 @@ import static java.util.Objects.requireNonNull; +import com.speedment.jpastreamer.criteria.PredicateFactory; +import com.speedment.jpastreamer.field.predicate.SpeedmentPredicate; import com.speedment.jpastreamer.pipeline.Pipeline; import com.speedment.jpastreamer.pipeline.intermediate.IntermediateOperationFactory; +import com.speedment.jpastreamer.pipeline.terminal.TerminalOperation; import com.speedment.jpastreamer.pipeline.terminal.TerminalOperationFactory; import com.speedment.jpastreamer.pipeline.terminal.TerminalOperationType; import com.speedment.jpastreamer.rootfactory.RootFactory; import com.speedment.jpastreamer.termopoptimizer.TerminalOperationOptimizer; +import java.util.Optional; import java.util.ServiceLoader; final class StandardTerminalOperationOptimizer implements TerminalOperationOptimizer { + + private final IntermediateOperationFactory intermediateOperationFactory; + private final TerminalOperationFactory terminalOperationFactory; - final IntermediateOperationFactory iof = RootFactory - .getOrThrow(IntermediateOperationFactory.class, ServiceLoader::load); - final TerminalOperationFactory tof = - RootFactory.getOrThrow(TerminalOperationFactory.class, ServiceLoader::load); + public StandardTerminalOperationOptimizer() { + this.intermediateOperationFactory = RootFactory.getOrThrow(IntermediateOperationFactory.class, ServiceLoader::load); + this.terminalOperationFactory = RootFactory.getOrThrow(TerminalOperationFactory.class, ServiceLoader::load); + } @Override public Pipeline optimize(Pipeline pipeline) { @@ -52,32 +59,49 @@ public Pipeline optimize(Pipeline pipeline) { } private Pipeline optimizeAnyMatch(Pipeline pipeline) { - pipeline.intermediateOperations().add(iof.createLimit(1)); - pipeline.intermediateOperations().add( - iof.createFilter(pipeline.terminatingOperation().predicate())); - pipeline.terminatingOperation(tof.createAnyMatch(p -> true)); + this.getPredicate(pipeline.terminatingOperation()).ifPresent(speedmentPredicate -> { + pipeline.intermediateOperations().add(intermediateOperationFactory.createLimit(1)); + pipeline.intermediateOperations().add( + intermediateOperationFactory.createFilter(speedmentPredicate)); + pipeline.terminatingOperation(terminalOperationFactory.createAnyMatch(p -> true)); + }); return pipeline; } private Pipeline optimizeNoneMatch(Pipeline pipeline) { - pipeline.intermediateOperations().add(iof.createLimit(1)); - pipeline.intermediateOperations().add( - iof.createFilter(pipeline.terminatingOperation().predicate())); - // NoneMatch() - If the stream is empty then true is returned and the predicate is not evaluated. - // If the expression is evaluated => There is a match and the expression is always false. - pipeline.terminatingOperation(tof.createNoneMatch(e -> false)); + this.getPredicate(pipeline.terminatingOperation()).ifPresent(speedmentPredicate -> { + pipeline.intermediateOperations().add(intermediateOperationFactory.createLimit(1)); + pipeline.intermediateOperations().add(intermediateOperationFactory.createFilter(speedmentPredicate)); + // NoneMatch() - If the stream is empty then true is returned and the predicate is not evaluated. + // If the expression is evaluated => There is a match and the expression is always false. + pipeline.terminatingOperation(terminalOperationFactory.createNoneMatch(e -> false)); + }); return pipeline; } private Pipeline optimizeFindFirst(Pipeline pipeline) { - pipeline.intermediateOperations().add(iof.createLimit(1)); + pipeline.intermediateOperations().add(intermediateOperationFactory.createLimit(1)); return pipeline; } private Pipeline optimizeFindAny(Pipeline pipeline) { pipeline.ordered(false); - pipeline.intermediateOperations().add(iof.createLimit(1)); + pipeline.intermediateOperations().add(intermediateOperationFactory.createLimit(1)); return pipeline; } + + private Optional> getPredicate(final TerminalOperation operation) { + final Object[] arguments = operation.arguments(); + + if (arguments.length != 1) { + return Optional.empty(); + } + + if (arguments[0] instanceof SpeedmentPredicate) { + return Optional.of((SpeedmentPredicate) arguments[0]); + } + + return Optional.empty(); + } } diff --git a/provider/termopoptimizer-standard/src/main/java9/module-info.java b/provider/termopoptimizer-standard/src/main/java9/module-info.java index dcb9426ac..169c0eb0f 100644 --- a/provider/termopoptimizer-standard/src/main/java9/module-info.java +++ b/provider/termopoptimizer-standard/src/main/java9/module-info.java @@ -13,6 +13,7 @@ module jpastreamer.termopoptimizer.standard { requires transitive jpastreamer.termopoptimizer; requires jpastreamer.rootfactory; + requires jpastreamer.predicate; exports com.speedment.jpastreamer.termopoptimizer.standard; diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/AnyMatchTest.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/AnyMatchTest.java index 94e28c7e8..f6095a13b 100644 --- a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/AnyMatchTest.java +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/AnyMatchTest.java @@ -1,55 +1,76 @@ package com.speedment.jpastreamer.termopoptimizer.standard.internal; +import com.speedment.jpastreamer.field.ComparableField; +import com.speedment.jpastreamer.field.ShortField; +import com.speedment.jpastreamer.field.predicate.SpeedmentPredicate; import com.speedment.jpastreamer.pipeline.Pipeline; +import java.math.BigDecimal; import java.util.function.Predicate; import java.util.stream.Stream; -final class AnyMatchTest extends StandardTerminalOperationOptimizerTest { +final class AnyMatchTest extends StandardTerminalOperationOptimizerTest { @Override - Class getEntityClass() { - return String.class; + Class getEntityClass() { + return Film.class; } @Override - protected Stream> pipelines() { + protected Stream> pipelines() { return Stream.of( noAnyMatch(), - anyMatch() + anyMatchLambda(), + anyMatchSpeedmentPredicate() ); } - private PipelineTestCase noAnyMatch() { - final Pipeline noAnyMatch = createPipeline( + private PipelineTestCase noAnyMatch() { + final Pipeline noAnyMatch = createPipeline( tof.createAllMatch(s -> s.equals("test")), - iof.createLimit(1) + iof.createLimit(100) ); - final Pipeline noAnyMatchExpected = createPipeline( + final Pipeline noAnyMatchExpected = createPipeline( tof.createAllMatch(s -> s.equals("test")), - iof.createLimit(1) + iof.createLimit(100) ); return new PipelineTestCase<>("No Any Match", noAnyMatch, noAnyMatchExpected); } - private PipelineTestCase anyMatch() { - final Predicate p = s -> (s.equals("test")); + private PipelineTestCase anyMatchLambda() { + final Predicate p = f -> f.getTitle().startsWith("A"); - final Pipeline anyMatch = createPipeline( + final Pipeline anyMatch = createPipeline( tof.createAnyMatch(p), iof.createLimit(100) ); - final Pipeline anyMatchExpected = createPipeline( + final Pipeline anyMatchExpected = createPipeline( + tof.createAnyMatch(s -> true), + iof.createLimit(100) + ); + + return new PipelineTestCase<>("Any Match Lambda", anyMatch, anyMatchExpected); + } + + private PipelineTestCase anyMatchSpeedmentPredicate() { + SpeedmentPredicate predicate = Film$.title.startsWith("A"); + + final Pipeline anyMatch = createPipeline( + tof.createAnyMatch(predicate), + iof.createLimit(100) + ); + + final Pipeline anyMatchExpected = createPipeline( tof.createAnyMatch(s -> true), iof.createLimit(100), iof.createLimit(1), - iof.createFilter(p) + iof.createFilter(predicate) ); - return new PipelineTestCase<>("Any Match", anyMatch, anyMatchExpected); + return new PipelineTestCase<>("Any Match Speedment Predicate", anyMatch, anyMatchExpected); } } diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film$.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film$.java new file mode 100644 index 000000000..389820db3 --- /dev/null +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film$.java @@ -0,0 +1,111 @@ +package com.speedment.jpastreamer.termopoptimizer.standard.internal; + +import com.speedment.jpastreamer.field.ComparableField; +import com.speedment.jpastreamer.field.ReferenceField; +import com.speedment.jpastreamer.field.ShortField; +import com.speedment.jpastreamer.field.StringField; + +import java.math.BigDecimal; +import java.sql.Timestamp; + +/** + * The generated base for entity {@link Film} representing entities of the + * {@code film}-table in the database. + *

This file has been automatically generated by JPAStreamer. + * + * @author JPAStreamer + */ +public final class Film$ { + + /** + * This Field corresponds to the {@link Film} field "length". + */ + public static final ComparableField length = ComparableField.create( + Film.class, + "length", + Film::getLength, + false + ); + /** + * This Field corresponds to the {@link Film} field "filmId". + */ + public static final ShortField filmId = ShortField.create( + Film.class, + "filmId", + Film::getFilmId, + false + ); + /** + * This Field corresponds to the {@link Film} field "replacementCost". + */ + public static final ComparableField replacementCost = ComparableField.create( + Film.class, + "replacementCost", + Film::getReplacementCost, + false + ); + + /** + * This Field corresponds to the {@link Film} field "rating". + */ + public static final StringField rating = StringField.create( + Film.class, + "rating", + Film::getRating, + false + ); + /** + * This Field corresponds to the {@link Film} field "title". + */ + public static final StringField title = StringField.create( + Film.class, + "title", + Film::getTitle, + false + ); + /** + * This Field corresponds to the {@link Film} field "languageId". + */ + public static final ShortField languageId = ShortField.create( + Film.class, + "languageId", + Film::getLanguageId, + false + ); + /** + * This Field corresponds to the {@link Film} field "description". + */ + public static final StringField description = StringField.create( + Film.class, + "description", + Film::getDescription, + false + ); + /** + * This Field corresponds to the {@link Film} field "rentalRate". + */ + public static final ComparableField rentalRate = ComparableField.create( + Film.class, + "rentalRate", + Film::getRentalRate, + false + ); + /** + * This Field corresponds to the {@link Film} field "rentalDuration". + */ + public static final ShortField rentalDuration = ShortField.create( + Film.class, + "rentalDuration", + Film::getRentalDuration, + false + ); + /** + * This Field corresponds to the {@link Film} field "lastUpdate". + */ + public static final ComparableField lastUpdate = ComparableField.create( + Film.class, + "lastUpdate", + Film::getLastUpdate, + false + ); +} diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film.java new file mode 100644 index 000000000..b37d66b29 --- /dev/null +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/Film.java @@ -0,0 +1,190 @@ +package com.speedment.jpastreamer.termopoptimizer.standard.internal; + +import jakarta.persistence.*; +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.HashSet; + +@Entity +@Table(name = "film", schema = "sakila") +public class Film { + public Film() {} + public Film(short filmId) { + this.filmId = filmId; + } + public Film(String description) { + this.description = description; + } + + public Film(short filmId, String title, String description) { + this.filmId = filmId; + this.title = title; + this.description = description; + } + + public Film(short filmId, String description) { + this.filmId = filmId; + this.description = description; + } + + @Id + @Column(name = "film_id", columnDefinition = "SMALLINT UNSIGNED") + private short filmId; + + @Basic + @Column(name = "title", columnDefinition = "VARCHAR(255)") + private String title; + + @Basic + @Column(name = "description", columnDefinition = "TEXT") + private String description; + @Basic + @Column(name = "rental_duration", columnDefinition = "YEAR") + private short rentalDuration; + @Basic + @Column(name = "language_id", columnDefinition = "SMALLINT UNSIGNED") + private short languageId; + @Basic + @Column(name = "rental_rate", columnDefinition = "DECIMAL(4,2)") + private BigDecimal rentalRate; + + @Basic + @Column(name = "length", columnDefinition = "SMALL UNSIGNED") + private Short length; + + @Basic + @Column(name = "rating", columnDefinition = "enum('G','PG','PG-13','R','NC-17')") + private String rating; + + @Basic + @Column(name = "replacement_cost") + private BigDecimal replacementCost; + + @Basic + @Column(name = "last_update") + private Timestamp lastUpdate; + + public short getFilmId() { + return filmId; + } + + public void setFilmId(short filmId) { + this.filmId = filmId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public short getLanguageId() { + return languageId; + } + + public void setLanguageId(short languageId) { + this.languageId = languageId; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public short getRentalDuration() { + return rentalDuration; + } + + public void setRentalDuration(short rentalDuration) { + this.rentalDuration = rentalDuration; + } + + public String getRating() { return rating; } + + public void setRating(String rating) { this.rating = rating; } + + public BigDecimal getRentalRate() { + return rentalRate; + } + + public void setRentalRate(BigDecimal rentalRate) { + this.rentalRate = rentalRate; + } + + public Short getLength() { + return length; + } + + public void setLength(Short length) { + this.length = length; + } + + public BigDecimal getReplacementCost() { + return replacementCost; + } + + public void setReplacementCost(BigDecimal replacementCost) { + this.replacementCost = replacementCost; + } + public Timestamp getLastUpdate() { + return lastUpdate; + } + + public void setLastUpdate(Timestamp lastUpdate) { + this.lastUpdate = lastUpdate; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Film film = (Film) o; + + if (filmId != film.filmId) return false; + if (rentalDuration != film.rentalDuration) return false; + if (title != null ? !title.equals(film.title) : film.title != null) return false; + if (description != null ? !description.equals(film.description) : film.description != null) return false; + if (rentalRate != null ? !rentalRate.equals(film.rentalRate) : film.rentalRate != null) return false; + if (length != null ? !length.equals(film.length) : film.length != null) return false; + if (replacementCost != null ? !replacementCost.equals(film.replacementCost) : film.replacementCost != null) + return false; + if (lastUpdate != null ? !lastUpdate.equals(film.lastUpdate) : film.lastUpdate != null) return false; + + return true; + } + + @Override + public int hashCode() { + int result = (int) filmId; + result = 31 * result + (title != null ? title.hashCode() : 0); + result = 31 * result + (description != null ? description.hashCode() : 0); + result = 31 * result + (int) rentalDuration; + result = 31 * result + (rentalRate != null ? rentalRate.hashCode() : 0); + result = 31 * result + (length != null ? length.hashCode() : 0); + result = 31 * result + (replacementCost != null ? replacementCost.hashCode() : 0); + result = 31 * result + (lastUpdate != null ? lastUpdate.hashCode() : 0); + return result; + } + + @Override + public String toString() { + return "Film{" + + "filmId=" + filmId + + ", title='" + title + '\'' + + ", description='" + description + '\'' + + ", rentalDuration=" + rentalDuration + + ", languageId=" + languageId + + ", rentalRate=" + rentalRate + + ", length=" + length + + ", rating='" + rating + '\'' + + ", replacementCost=" + replacementCost + + ", lastUpdate=" + lastUpdate + + '}'; + } +} diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindAnyTest.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindAnyTest.java index 78412ab19..4986b7791 100644 --- a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindAnyTest.java +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindAnyTest.java @@ -1,4 +1,57 @@ package com.speedment.jpastreamer.termopoptimizer.standard.internal; -public class FindAnyTest { +import com.speedment.jpastreamer.field.predicate.SpeedmentPredicate; +import com.speedment.jpastreamer.pipeline.Pipeline; + +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class FindAnyTest extends StandardTerminalOperationOptimizerTest { + + @Override + Class getEntityClass() { + return Film.class; + } + + @Override + protected Stream> pipelines() { + return Stream.of( + noFindAny(), + findAny() + ); + } + + private PipelineTestCase noFindAny() { + final Pipeline noFindAny = createPipeline( + tof.createAllMatch(Film$.title.startsWith("A")), + iof.createLimit(100) + ); + + final Pipeline noFindAnyExpected = createPipeline( + tof.createAllMatch(Film$.title.startsWith("A")), + iof.createLimit(100) + ); + + return new PipelineTestCase<>("No Find Any", noFindAny, noFindAnyExpected); + } + + private PipelineTestCase findAny() { + final Predicate p = f -> f.getTitle().startsWith("A"); + + final Pipeline findAny = createPipeline( + tof.acquireFindAny(), + iof.createFilter(p), + iof.createLimit(100) + ); + + final Pipeline findAnyExpected = createPipeline( + tof.acquireFindAny(), + iof.createFilter(p), + iof.createLimit(100), + iof.createLimit(1) + ); + + return new PipelineTestCase<>("Find Any", findAny, findAnyExpected); + } + } diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindFirstTest.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindFirstTest.java index 979a74030..0bd058518 100644 --- a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindFirstTest.java +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/FindFirstTest.java @@ -1,4 +1,56 @@ package com.speedment.jpastreamer.termopoptimizer.standard.internal; -public class FindFirstTest { +import com.speedment.jpastreamer.pipeline.Pipeline; + +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class FindFirstTest extends StandardTerminalOperationOptimizerTest { + + @Override + Class getEntityClass() { + return Film.class; + } + + @Override + protected Stream> pipelines() { + return Stream.of( + noFindFirst(), + findFirst() + ); + } + + private PipelineTestCase noFindFirst() { + final Pipeline noFindFirst = createPipeline( + tof.createAllMatch(Film$.title.startsWith("A")), + iof.createLimit(100) + ); + + final Pipeline noFindFirstExpected = createPipeline( + tof.createAllMatch(Film$.title.startsWith("A")), + iof.createLimit(100) + ); + + return new PipelineTestCase<>("No Find First", noFindFirst, noFindFirstExpected); + } + + private PipelineTestCase findFirst() { + final Predicate p = f -> f.getTitle().startsWith("A"); + + final Pipeline findFirst = createPipeline( + tof.acquireFindFirst(), + iof.createFilter(p), + iof.createLimit(100) + ); + + final Pipeline findFirstExpected = createPipeline( + tof.acquireFindFirst(), + iof.createFilter(p), + iof.createLimit(100), + iof.createLimit(1) + ); + + return new PipelineTestCase<>("Find First", findFirst, findFirstExpected); + } + } diff --git a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/NoneMatchTest.java b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/NoneMatchTest.java index 6140ee182..ec624ea84 100644 --- a/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/NoneMatchTest.java +++ b/provider/termopoptimizer-standard/src/test/java/com/speedment/jpastreamer/termopoptimizer/standard/internal/NoneMatchTest.java @@ -1,4 +1,75 @@ package com.speedment.jpastreamer.termopoptimizer.standard.internal; -public class NoneMatchTest { +import com.speedment.jpastreamer.field.predicate.SpeedmentPredicate; +import com.speedment.jpastreamer.pipeline.Pipeline; + +import java.util.function.Predicate; +import java.util.stream.Stream; + +public class NoneMatchTest extends StandardTerminalOperationOptimizerTest{ + + @Override + Class getEntityClass() { + return Film.class; + } + + @Override + protected Stream> pipelines() { + return Stream.of( + noNoneMatch(), + noneMatchLambda(), + noneMatchSpeedmentPredicate() + ); + } + + private PipelineTestCase noNoneMatch() { + final Predicate p = f -> f.getTitle().startsWith("A"); + + final Pipeline noAnyMatch = createPipeline( + tof.createAllMatch(p), + iof.createLimit(100) + ); + + final Pipeline noAnyMatchExpected = createPipeline( + tof.createAllMatch(p), + iof.createLimit(100) + ); + + return new PipelineTestCase<>("No None Match", noAnyMatch, noAnyMatchExpected); + } + + private PipelineTestCase noneMatchLambda() { + final Predicate p = f -> f.getTitle().startsWith("A"); + + final Pipeline anyMatch = createPipeline( + tof.createNoneMatch(p), + iof.createLimit(100) + ); + + final Pipeline anyMatchExpected = createPipeline( + tof.createNoneMatch(p), + iof.createLimit(100) + ); + + return new PipelineTestCase<>("None Match Lambda", anyMatch, anyMatchExpected); + } + + private PipelineTestCase noneMatchSpeedmentPredicate() { + SpeedmentPredicate p = Film$.title.startsWith("A"); + + final Pipeline anyMatch = createPipeline( + tof.createNoneMatch(p), + iof.createLimit(100) + ); + + final Pipeline anyMatchExpected = createPipeline( + tof.createNoneMatch(s -> true), + iof.createLimit(100), + iof.createLimit(1), + iof.createFilter(p) + ); + + return new PipelineTestCase<>("None Match Speedment Predicate", anyMatch, anyMatchExpected); + } + }