From df40d580c5dba3f677c2fdddd66481103b157022 Mon Sep 17 00:00:00 2001 From: Grace Tang Date: Fri, 5 Jan 2018 18:34:58 -0500 Subject: [PATCH 1/9] Add test cases for concurrent reductions (#150) * add test cases and fail the stream * calculate LOC * Revert "calculate LOC" This reverts commit 034b677e792a90d9d9fa1d1032245b2928dc000e. * revert changing functional code * change test cases * change test * change test cases * change refactoring * delete useless importation * Remove extra space. * change name and collections * Add collector kind * remove duplication * improve format * remove overloading helper * format * remove duplication * Remove whitespace addition. --- .../core/analysis/CollectorKind.java | 6 ++ .../core/analysis/PreconditionSuccess.java | 6 ++ .../core/analysis/Refactoring.java | 3 +- .../core/analysis/Stream.java | 10 +++ .../core/analysis/TransformationAction.java | 5 +- .../testConcurrentReduction/in/A.java | 33 ++++++++ .../testConcurrentReduction1/in/A.java | 33 ++++++++ .../testConcurrentReduction2/in/A.java | 33 ++++++++ .../testConcurrentReduction3/in/A.java | 34 +++++++++ .../testConcurrentReduction4/in/A.java | 35 +++++++++ .../testConcurrentReduction5/in/A.java | 34 +++++++++ ...onvertStreamToParallelRefactoringTest.java | 76 +++++++++++++++++++ .../tests/StreamAnalysisExpectedResult.java | 21 +++++ 13 files changed, 326 insertions(+), 3 deletions(-) create mode 100644 edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/CollectorKind.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction4/in/A.java create mode 100644 edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction5/in/A.java diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/CollectorKind.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/CollectorKind.java new file mode 100644 index 00000000..5936bcc9 --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/CollectorKind.java @@ -0,0 +1,6 @@ +package edu.cuny.hunter.streamrefactoring.core.analysis; + +public enum CollectorKind { + CONCURRENT, + NONCONCURRENT +} \ No newline at end of file diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java index 00619e50..593d63cb 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java @@ -6,4 +6,10 @@ public enum PreconditionSuccess { P3, P4, P5 + P6, + P7, + P8, + P9, + P10, + P11 } diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Refactoring.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Refactoring.java index 892152c6..e5de7b5e 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Refactoring.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Refactoring.java @@ -7,5 +7,6 @@ */ public enum Refactoring { CONVERT_SEQUENTIAL_STREAM_TO_PARALLEL, - OPTIMIZE_PARALLEL_STREAM + OPTIMIZE_PARALLEL_STREAM, + OPTIMIZE_COMPLEX_MUTABLE_REDUCTION } diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Stream.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Stream.java index 34e01a6a..43faccba 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Stream.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Stream.java @@ -101,6 +101,8 @@ public class Stream { private IR enclosingMethodDeclarationIR; private final TypeDeclaration enclosingTypeDeclaration; + + private CollectorKind collectorKind; private boolean hasNoTerminalOperation; @@ -514,6 +516,10 @@ public Set getPossibleOrderings() { return this.possibleOrderings.stream().map(e -> e == null ? initialOrdering : e).collect(Collectors.toSet()); } + public CollectorKind getCollectorKind() { + return this.collectorKind; + } + public Refactoring getRefactoring() { return this.refactoring; } @@ -714,6 +720,10 @@ protected void setInitialOrdering(Ordering initialOrdering) { Objects.requireNonNull(initialOrdering); this.initialOrdering = initialOrdering; } + + protected void setCollectorKind(CollectorKind collectorKind) { + this.collectorKind = collectorKind; + } private void setPassingPrecondition(PreconditionSuccess succcess) { if (this.passingPrecondition == null) diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/TransformationAction.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/TransformationAction.java index b126de5d..03e9bfe5 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/TransformationAction.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/TransformationAction.java @@ -3,6 +3,7 @@ public enum TransformationAction { CONVERT_TO_PARALLEL, UNORDER, - CONVERT_TO_SEQUENTIAL - + CONVERT_TO_SEQUENTIAL, + CONVERT_COLLECTOR_TO_CONCURRENT, + CONVERT_COLLECTOR_TO_NON_CONCURRENT } diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java new file mode 100644 index 00000000..689a533b --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java @@ -0,0 +1,33 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P6 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.stream() + .collect(Collectors.groupingByConcurrent(Widget::getColor)); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java new file mode 100644 index 00000000..ccb9917a --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java @@ -0,0 +1,33 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P7 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.parallelStream() + .collect(Collectors.groupingBy(Widget::getColor)); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java new file mode 100644 index 00000000..88f967f4 --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java @@ -0,0 +1,33 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P8 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.parallelStream() + .collect(Collectors.groupingByConcurrent(Widget::getColor)); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java new file mode 100644 index 00000000..c4f4edcf --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java @@ -0,0 +1,34 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P9 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.stream() + .collect(Collectors.groupingBy(Widget::getColor, HashMap::new, Collectors.toSet())); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction4/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction4/in/A.java new file mode 100644 index 00000000..b9d47447 --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction4/in/A.java @@ -0,0 +1,35 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P10 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.stream().collect(Collectors.groupingByConcurrent( + Widget::getColor, ConcurrentHashMap::new, Collectors.toCollection(LinkedHashSet::new))); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction5/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction5/in/A.java new file mode 100644 index 00000000..a9b8128b --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction5/in/A.java @@ -0,0 +1,34 @@ +package p; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import edu.cuny.hunter.streamrefactoring.annotations.*; +import p.A.Widget.Color; + +public class A { + + static class Widget { + enum Color { + RED, BLUE, GREEN, PINK, ORANGE, YELLOW + }; + + public Color getColor() { + return this.getColor(); + } + } + + /** + * P11 in table 3 + */ + @EntryPoint + void m() { + Collection orderedWidgets = new ArrayList<>(); + Map> widgetsByColor = orderedWidgets.parallelStream() + .collect(Collectors.groupingBy(Widget::getColor, HashMap::new, Collectors.toSet())); + } +} diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index feb38707..71143a83 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -37,6 +37,7 @@ import org.eclipse.jdt.ui.tests.refactoring.RefactoringTest; import org.eclipse.ltk.core.refactoring.RefactoringStatus; +import edu.cuny.hunter.streamrefactoring.core.analysis.CollectorKind; import edu.cuny.hunter.streamrefactoring.core.analysis.ExecutionMode; import edu.cuny.hunter.streamrefactoring.core.analysis.Ordering; import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionFailure; @@ -367,6 +368,12 @@ private void helper(int nToUseForStreams, StreamAnalysisExpectedResult... expect Set orderings = stream.getPossibleOrderings(); assertEquals(errorMessage("orderings", result), result.getExpectedOrderings(), orderings); + + // exceptedCollecterKind is initialized in the excepted result class + if (result.getExceptedCollectorKind() != null) { + CollectorKind collectorKind = stream.getCollectorKind(); + assertEquals(errorMessage("collector kind", result), result.getExceptedCollectorKind(), collectorKind); + } assertEquals(errorMessage("side effects", result), result.isExpectingSideEffects(), stream.hasPossibleSideEffects()); @@ -921,4 +928,73 @@ public void testWithoutEntryPoint() throws Exception { this.helper(new StreamAnalysisExpectedResult("h1.stream()", null, null, false, false, false, null, null, null, RefactoringStatus.ERROR, EnumSet.of(PreconditionFailure.NO_ENTRY_POINT))); } + + /** + * Test #64. Test P6 in table3. + */ + public void testConcurrentReduction() throws Exception { + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), + EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, true, + EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), PreconditionSuccess.P6, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P7 in table 3. + */ + public void testConcurrentReduction1() throws Exception { + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), + EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, true, + EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P8 in table 3. + */ + public void testConcurrentReduction2() throws Exception { + HashSet transformations = new HashSet<>(); + transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT); + transformations.add(TransformationAction.CONVERT_TO_SEQUENTIAL); + + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), + EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, true, transformations, + PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); + } + + /** + * Test #64. Test P9 in table 3. + */ + public void testConcurrentReduction3() throws Exception { + HashSet transformations = new HashSet<>(); + transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT); + transformations.add(TransformationAction.CONVERT_TO_PARALLEL); + + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), + EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, false, transformations, + PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); + } + + /** + * Test #64. Test P10 in table 3. + */ + public void testConcurrentReduction4() throws Exception { + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), + EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, false, + EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P11 in table 3. + */ + public void testConcurrentReduction5() throws Exception { + helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), + EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, false, + EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java index eba7b220..40caff5c 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java @@ -2,6 +2,7 @@ import java.util.Set; +import edu.cuny.hunter.streamrefactoring.core.analysis.CollectorKind; import edu.cuny.hunter.streamrefactoring.core.analysis.ExecutionMode; import edu.cuny.hunter.streamrefactoring.core.analysis.Ordering; import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionFailure; @@ -18,6 +19,8 @@ class StreamAnalysisExpectedResult { private Set expectedFailures; + private CollectorKind expectedCollectorKind; + private Set expectedOrderings; private PreconditionSuccess expectedPassingPrecondition; @@ -49,11 +52,29 @@ public StreamAnalysisExpectedResult(String expectedCreation, Set this.expectedStatusSeverity = expectedStatusSeverity; this.expectedFailures = expectedFailures; } + + /** + * Overloading constructor to test complex mutable reduction + */ + public StreamAnalysisExpectedResult(String expectedCreation, Set expectedExecutionModes, + Set expectedOrderings, CollectorKind expectedCollectorKind, boolean expectingSideEffects, + boolean expectingStatefulIntermediateOperation, boolean expectingThatReduceOrderingMatters, + Set expectedActions, PreconditionSuccess expectedPassingPrecondition, + Refactoring expectedRefactoring, int expectedStatusSeverity, Set expectedFailures) { + this(expectedCreation, expectedExecutionModes, expectedOrderings, expectingSideEffects, + expectingStatefulIntermediateOperation, expectingThatReduceOrderingMatters, expectedActions, + expectedPassingPrecondition, expectedRefactoring, expectedStatusSeverity, expectedFailures); + this.expectedCollectorKind = expectedCollectorKind; + } public Set getExpectedActions() { return this.expectedActions; } + public CollectorKind getExceptedCollectorKind() { + return expectedCollectorKind; + } + public String getExpectedCreation() { return this.expectedCreation; } From 95154b1947e0b2922225100fe87a613e4b1805d4 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Mon, 29 Jan 2018 16:17:29 -0500 Subject: [PATCH 2/9] Re-architect expected result class hierarchy for #64. Really, an excellent example of when to use OOP. --- .../core/analysis/PreconditionSuccess.java | 2 +- ...onvertStreamToParallelRefactoringTest.java | 82 ++++++------------- .../tests/StreamAnalysisExpectedResult.java | 55 ++++++++----- ...alysisExpectedResultWithCollectorKind.java | 43 ++++++++++ 4 files changed, 103 insertions(+), 79 deletions(-) create mode 100644 edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResultWithCollectorKind.java diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java index 593d63cb..9870eb51 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java @@ -5,7 +5,7 @@ public enum PreconditionSuccess { P2, P3, P4, - P5 + P5, P6, P7, P8, diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index 71143a83..bb91f01c 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -362,41 +362,7 @@ private void helper(int nToUseForStreams, StreamAnalysisExpectedResult... expect expectingStreams.size()); Stream stream = expectingStreams.get(0); - - Set executionModes = stream.getPossibleExecutionModes(); - assertEquals(errorMessage("execution mode", result), result.getExpectedExecutionModes(), executionModes); - - Set orderings = stream.getPossibleOrderings(); - assertEquals(errorMessage("orderings", result), result.getExpectedOrderings(), orderings); - - // exceptedCollecterKind is initialized in the excepted result class - if (result.getExceptedCollectorKind() != null) { - CollectorKind collectorKind = stream.getCollectorKind(); - assertEquals(errorMessage("collector kind", result), result.getExceptedCollectorKind(), collectorKind); - } - - assertEquals(errorMessage("side effects", result), result.isExpectingSideEffects(), - stream.hasPossibleSideEffects()); - assertEquals(errorMessage("stateful intermediate operations", result), - result.isExpectingStatefulIntermediateOperation(), - stream.hasPossibleStatefulIntermediateOperations()); - assertEquals(errorMessage("ROM", result), result.isExpectingThatReduceOrderingMatters(), - stream.reduceOrderingPossiblyMatters()); - assertEquals(errorMessage("transformation actions", result), result.getExpectedActions(), - stream.getActions()); - assertEquals(errorMessage("passing precondition", result), result.getExpectedPassingPrecondition(), - stream.getPassingPrecondition()); - assertEquals(errorMessage("refactoring", result), result.getExpectedRefactoring(), stream.getRefactoring()); - assertEquals(errorMessage("status severity", result), result.getExpectedStatusSeverity(), - stream.getStatus().getSeverity()); - - Set actualCodes = Arrays.stream(stream.getStatus().getEntries()).map(e -> e.getCode()) - .collect(Collectors.toSet()); - - Set expectedCodes = result.getExpectedFailures().stream().map(e -> e.getCode()) - .collect(Collectors.toSet()); - - assertEquals(errorMessage("status codes", result), expectedCodes, actualCodes); + result.evaluate(stream); } } @@ -928,24 +894,25 @@ public void testWithoutEntryPoint() throws Exception { this.helper(new StreamAnalysisExpectedResult("h1.stream()", null, null, false, false, false, null, null, null, RefactoringStatus.ERROR, EnumSet.of(PreconditionFailure.NO_ENTRY_POINT))); } - + /** * Test #64. Test P6 in table3. */ public void testConcurrentReduction() throws Exception { - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), - EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, true, - EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), PreconditionSuccess.P6, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), + PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); } /** * Test #64. Test P7 in table 3. */ public void testConcurrentReduction1() throws Exception { - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), - EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, true, - EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); } @@ -957,10 +924,10 @@ public void testConcurrentReduction2() throws Exception { transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT); transformations.add(TransformationAction.CONVERT_TO_SEQUENTIAL); - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), - EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, true, transformations, - PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, - Collections.emptySet())); + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, true, transformations, PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, + RefactoringStatus.OK, Collections.emptySet())); } /** @@ -971,19 +938,19 @@ public void testConcurrentReduction3() throws Exception { transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT); transformations.add(TransformationAction.CONVERT_TO_PARALLEL); - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), - EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, false, transformations, - PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, - Collections.emptySet())); + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, false, transformations, PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, + RefactoringStatus.OK, Collections.emptySet())); } /** * Test #64. Test P10 in table 3. */ public void testConcurrentReduction4() throws Exception { - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.SEQUENTIAL), - EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, false, false, - EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, false, EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); } @@ -991,10 +958,9 @@ public void testConcurrentReduction4() throws Exception { * Test #64. Test P11 in table 3. */ public void testConcurrentReduction5() throws Exception { - helper(new StreamAnalysisExpectedResult("orderedWidgets.stream()", EnumSet.of(ExecutionMode.PARALLEL), - EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, false, - EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, + helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, false, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); } - } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java index 40caff5c..9722a30e 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java @@ -1,13 +1,17 @@ package edu.cuny.hunter.streamrefactoring.ui.tests; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; import java.util.Set; +import java.util.stream.Collectors; -import edu.cuny.hunter.streamrefactoring.core.analysis.CollectorKind; import edu.cuny.hunter.streamrefactoring.core.analysis.ExecutionMode; import edu.cuny.hunter.streamrefactoring.core.analysis.Ordering; import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionFailure; import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionSuccess; import edu.cuny.hunter.streamrefactoring.core.analysis.Refactoring; +import edu.cuny.hunter.streamrefactoring.core.analysis.Stream; import edu.cuny.hunter.streamrefactoring.core.analysis.TransformationAction; class StreamAnalysisExpectedResult { @@ -19,7 +23,6 @@ class StreamAnalysisExpectedResult { private Set expectedFailures; - private CollectorKind expectedCollectorKind; private Set expectedOrderings; @@ -52,29 +55,11 @@ public StreamAnalysisExpectedResult(String expectedCreation, Set this.expectedStatusSeverity = expectedStatusSeverity; this.expectedFailures = expectedFailures; } - - /** - * Overloading constructor to test complex mutable reduction - */ - public StreamAnalysisExpectedResult(String expectedCreation, Set expectedExecutionModes, - Set expectedOrderings, CollectorKind expectedCollectorKind, boolean expectingSideEffects, - boolean expectingStatefulIntermediateOperation, boolean expectingThatReduceOrderingMatters, - Set expectedActions, PreconditionSuccess expectedPassingPrecondition, - Refactoring expectedRefactoring, int expectedStatusSeverity, Set expectedFailures) { - this(expectedCreation, expectedExecutionModes, expectedOrderings, expectingSideEffects, - expectingStatefulIntermediateOperation, expectingThatReduceOrderingMatters, expectedActions, - expectedPassingPrecondition, expectedRefactoring, expectedStatusSeverity, expectedFailures); - this.expectedCollectorKind = expectedCollectorKind; - } public Set getExpectedActions() { return this.expectedActions; } - public CollectorKind getExceptedCollectorKind() { - return expectedCollectorKind; - } - public String getExpectedCreation() { return this.expectedCreation; } @@ -114,4 +99,34 @@ public boolean isExpectingStatefulIntermediateOperation() { public boolean isExpectingThatReduceOrderingMatters() { return this.expectingThatReduceOrderingMatters; } + + public void evaluate(Stream stream) { + Set executionModes = stream.getPossibleExecutionModes(); + assertEquals(errorMessage("execution mode"), this.getExpectedExecutionModes(), executionModes); + + Set orderings = stream.getPossibleOrderings(); + assertEquals(errorMessage("orderings"), this.getExpectedOrderings(), orderings); + + assertEquals(errorMessage("side effects"), isExpectingSideEffects(), stream.hasPossibleSideEffects()); + assertEquals(errorMessage("stateful intermediate operations"), isExpectingStatefulIntermediateOperation(), + stream.hasPossibleStatefulIntermediateOperations()); + assertEquals(errorMessage("ROM"), isExpectingThatReduceOrderingMatters(), + stream.reduceOrderingPossiblyMatters()); + assertEquals(errorMessage("transformation actions"), getExpectedActions(), stream.getActions()); + assertEquals(errorMessage("passing precondition"), getExpectedPassingPrecondition(), + stream.getPassingPrecondition()); + assertEquals(errorMessage("refactoring"), getExpectedRefactoring(), stream.getRefactoring()); + assertEquals(errorMessage("status severity"), getExpectedStatusSeverity(), stream.getStatus().getSeverity()); + + Set actualCodes = Arrays.stream(stream.getStatus().getEntries()).map(e -> e.getCode()) + .collect(Collectors.toSet()); + + Set expectedCodes = getExpectedFailures().stream().map(e -> e.getCode()).collect(Collectors.toSet()); + + assertEquals(errorMessage("status codes"), expectedCodes, actualCodes); + } + + protected String errorMessage(String attribute) { + return "Unexpected " + attribute + " for " + this.getExpectedCreation() + "."; + } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResultWithCollectorKind.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResultWithCollectorKind.java new file mode 100644 index 00000000..3f712af0 --- /dev/null +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResultWithCollectorKind.java @@ -0,0 +1,43 @@ +package edu.cuny.hunter.streamrefactoring.ui.tests; + +import static org.junit.Assert.assertEquals; + +import java.util.Set; + +import edu.cuny.hunter.streamrefactoring.core.analysis.CollectorKind; +import edu.cuny.hunter.streamrefactoring.core.analysis.ExecutionMode; +import edu.cuny.hunter.streamrefactoring.core.analysis.Ordering; +import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionFailure; +import edu.cuny.hunter.streamrefactoring.core.analysis.PreconditionSuccess; +import edu.cuny.hunter.streamrefactoring.core.analysis.Refactoring; +import edu.cuny.hunter.streamrefactoring.core.analysis.Stream; +import edu.cuny.hunter.streamrefactoring.core.analysis.TransformationAction; + +public class StreamAnalysisExpectedResultWithCollectorKind extends StreamAnalysisExpectedResult { + + private CollectorKind expectedCollectorKind; + + public StreamAnalysisExpectedResultWithCollectorKind(String expectedCreation, + Set expectedExecutionModes, Set expectedOrderings, + CollectorKind expectedCollectorKind, boolean expectingSideEffects, + boolean expectingStatefulIntermediateOperation, boolean expectingThatReduceOrderingMatters, + Set expectedActions, PreconditionSuccess expectedPassingPrecondition, + Refactoring expectedRefactoring, int expectedStatusSeverity, Set expectedFailures) { + super(expectedCreation, expectedExecutionModes, expectedOrderings, expectingSideEffects, + expectingStatefulIntermediateOperation, expectingThatReduceOrderingMatters, expectedActions, + expectedPassingPrecondition, expectedRefactoring, expectedStatusSeverity, expectedFailures); + this.expectedCollectorKind = expectedCollectorKind; + } + + @Override + public void evaluate(Stream stream) { + super.evaluate(stream); + + CollectorKind collectorKind = stream.getCollectorKind(); + assertEquals(this.errorMessage("collector kind"), this.getExpectedCollectorKind(), collectorKind); + } + + public CollectorKind getExpectedCollectorKind() { + return this.expectedCollectorKind; + } +} From d4a1e00367722755bb3163a50e9b0beca4840f23 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Sat, 24 Feb 2018 13:20:07 -0500 Subject: [PATCH 3/9] Clean up. --- ...onvertStreamToParallelRefactoringTest.java | 139 +++++++++--------- .../tests/StreamAnalysisExpectedResult.java | 63 ++++---- 2 files changed, 101 insertions(+), 101 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index bb91f01c..07c28f7a 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -7,7 +7,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; -import java.util.Arrays; import java.util.Collections; import java.util.EnumSet; import java.util.HashSet; @@ -516,6 +515,75 @@ public void testConcat() throws Exception { RefactoringStatus.ERROR, Collections.singleton(PreconditionFailure.CURRENTLY_NOT_HANDLED))); } + /** + * Test #64. Test P6 in table3. + */ + public void testConcurrentReduction() throws Exception { + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), + PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); + } + + /** + * Test #64. Test P7 in table 3. + */ + public void testConcurrentReduction1() throws Exception { + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P8 in table 3. + */ + public void testConcurrentReduction2() throws Exception { + HashSet transformations = new HashSet<>(); + transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT); + transformations.add(TransformationAction.CONVERT_TO_SEQUENTIAL); + + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, true, transformations, PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, + RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P9 in table 3. + */ + public void testConcurrentReduction3() throws Exception { + HashSet transformations = new HashSet<>(); + transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT); + transformations.add(TransformationAction.CONVERT_TO_PARALLEL); + + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, false, transformations, PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, + RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P10 in table 3. + */ + public void testConcurrentReduction4() throws Exception { + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, false, EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + + /** + * Test #64. Test P11 in table 3. + */ + public void testConcurrentReduction5() throws Exception { + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, false, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + } + public void testConstructor() throws Exception { this.helper(new StreamAnalysisExpectedResult("new ArrayList().stream()", Collections.singleton(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), false, false, false, @@ -894,73 +962,4 @@ public void testWithoutEntryPoint() throws Exception { this.helper(new StreamAnalysisExpectedResult("h1.stream()", null, null, false, false, false, null, null, null, RefactoringStatus.ERROR, EnumSet.of(PreconditionFailure.NO_ENTRY_POINT))); } - - /** - * Test #64. Test P6 in table3. - */ - public void testConcurrentReduction() throws Exception { - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, - false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), - PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, - Collections.emptySet())); - } - - /** - * Test #64. Test P7 in table 3. - */ - public void testConcurrentReduction1() throws Exception { - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); - } - - /** - * Test #64. Test P8 in table 3. - */ - public void testConcurrentReduction2() throws Exception { - HashSet transformations = new HashSet<>(); - transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT); - transformations.add(TransformationAction.CONVERT_TO_SEQUENTIAL); - - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, - false, true, transformations, PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, - RefactoringStatus.OK, Collections.emptySet())); - } - - /** - * Test #64. Test P9 in table 3. - */ - public void testConcurrentReduction3() throws Exception { - HashSet transformations = new HashSet<>(); - transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT); - transformations.add(TransformationAction.CONVERT_TO_PARALLEL); - - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, false, transformations, PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, - RefactoringStatus.OK, Collections.emptySet())); - } - - /** - * Test #64. Test P10 in table 3. - */ - public void testConcurrentReduction4() throws Exception { - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, - false, false, EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); - } - - /** - * Test #64. Test P11 in table 3. - */ - public void testConcurrentReduction5() throws Exception { - helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, false, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); - } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java index 9722a30e..12429f29 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/StreamAnalysisExpectedResult.java @@ -23,7 +23,6 @@ class StreamAnalysisExpectedResult { private Set expectedFailures; - private Set expectedOrderings; private PreconditionSuccess expectedPassingPrecondition; @@ -56,6 +55,38 @@ public StreamAnalysisExpectedResult(String expectedCreation, Set this.expectedFailures = expectedFailures; } + protected String errorMessage(String attribute) { + return "Unexpected " + attribute + " for " + this.getExpectedCreation() + "."; + } + + public void evaluate(Stream stream) { + Set executionModes = stream.getPossibleExecutionModes(); + assertEquals(this.errorMessage("execution mode"), this.getExpectedExecutionModes(), executionModes); + + Set orderings = stream.getPossibleOrderings(); + assertEquals(this.errorMessage("orderings"), this.getExpectedOrderings(), orderings); + + assertEquals(this.errorMessage("side effects"), this.isExpectingSideEffects(), stream.hasPossibleSideEffects()); + assertEquals(this.errorMessage("stateful intermediate operations"), + this.isExpectingStatefulIntermediateOperation(), stream.hasPossibleStatefulIntermediateOperations()); + assertEquals(this.errorMessage("ROM"), this.isExpectingThatReduceOrderingMatters(), + stream.reduceOrderingPossiblyMatters()); + assertEquals(this.errorMessage("transformation actions"), this.getExpectedActions(), stream.getActions()); + assertEquals(this.errorMessage("passing precondition"), this.getExpectedPassingPrecondition(), + stream.getPassingPrecondition()); + assertEquals(this.errorMessage("refactoring"), this.getExpectedRefactoring(), stream.getRefactoring()); + assertEquals(this.errorMessage("status severity"), this.getExpectedStatusSeverity(), + stream.getStatus().getSeverity()); + + Set actualCodes = Arrays.stream(stream.getStatus().getEntries()).map(e -> e.getCode()) + .collect(Collectors.toSet()); + + Set expectedCodes = this.getExpectedFailures().stream().map(e -> e.getCode()) + .collect(Collectors.toSet()); + + assertEquals(this.errorMessage("status codes"), expectedCodes, actualCodes); + } + public Set getExpectedActions() { return this.expectedActions; } @@ -99,34 +130,4 @@ public boolean isExpectingStatefulIntermediateOperation() { public boolean isExpectingThatReduceOrderingMatters() { return this.expectingThatReduceOrderingMatters; } - - public void evaluate(Stream stream) { - Set executionModes = stream.getPossibleExecutionModes(); - assertEquals(errorMessage("execution mode"), this.getExpectedExecutionModes(), executionModes); - - Set orderings = stream.getPossibleOrderings(); - assertEquals(errorMessage("orderings"), this.getExpectedOrderings(), orderings); - - assertEquals(errorMessage("side effects"), isExpectingSideEffects(), stream.hasPossibleSideEffects()); - assertEquals(errorMessage("stateful intermediate operations"), isExpectingStatefulIntermediateOperation(), - stream.hasPossibleStatefulIntermediateOperations()); - assertEquals(errorMessage("ROM"), isExpectingThatReduceOrderingMatters(), - stream.reduceOrderingPossiblyMatters()); - assertEquals(errorMessage("transformation actions"), getExpectedActions(), stream.getActions()); - assertEquals(errorMessage("passing precondition"), getExpectedPassingPrecondition(), - stream.getPassingPrecondition()); - assertEquals(errorMessage("refactoring"), getExpectedRefactoring(), stream.getRefactoring()); - assertEquals(errorMessage("status severity"), getExpectedStatusSeverity(), stream.getStatus().getSeverity()); - - Set actualCodes = Arrays.stream(stream.getStatus().getEntries()).map(e -> e.getCode()) - .collect(Collectors.toSet()); - - Set expectedCodes = getExpectedFailures().stream().map(e -> e.getCode()).collect(Collectors.toSet()); - - assertEquals(errorMessage("status codes"), expectedCodes, actualCodes); - } - - protected String errorMessage(String attribute) { - return "Unexpected " + attribute + " for " + this.getExpectedCreation() + "."; - } } From cfc216a7ef08e3ba398c0636981c7b02a6ea603f Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Tue, 30 Jan 2018 11:17:13 -0500 Subject: [PATCH 4/9] Remove FQN. --- .../edu/cuny/hunter/streamrefactoring/core/analysis/Util.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java index 7eddf3b1..41aeaf69 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java @@ -655,8 +655,7 @@ else if (typeReference.isPrimitiveType()) return true; else if (typeReference.isReferenceType()) { IClass type = typeAbstraction.getType(); - return !edu.cuny.hunter.streamrefactoring.core.analysis.Util.isIterable(type) - && type.getAllImplementedInterfaces().stream().noneMatch(Util::isIterable); + return !isIterable(type) && type.getAllImplementedInterfaces().stream().noneMatch(Util::isIterable); } else throw new IllegalArgumentException("Can't tell if type is scalar: " + typeAbstraction); } From 90fdbc9c7b650dfdedd9da36e93715a2c08eca95 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Tue, 30 Jan 2018 11:26:47 -0500 Subject: [PATCH 5/9] Enhance log. --- .../streamrefactoring/core/analysis/StreamStateMachine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java index bdd0a69d..302a3ee6 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java @@ -553,7 +553,7 @@ private boolean deriveRomForNonScalarMethod(Collection possible // default to ordered. ordering = Ordering.ORDERED; LOGGER.warning("Can't determine ordering for possible return types: " + possibleReturnTypes - + ". Defaulting to: " + ordering); + + ". Defaulting to: " + ordering + "."); } switch (ordering) { From 23549f6b83a52acc8933c5b77c7d2e19454aa4dd Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Tue, 6 Feb 2018 15:20:12 -0500 Subject: [PATCH 6/9] Trying to fix testConcurrentReduction() for #64. --- .../testConcurrentReduction/in/A.java | 21 +++++++++++++++---- ...onvertStreamToParallelRefactoringTest.java | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java index 689a533b..6f488557 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java @@ -2,18 +2,30 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentNavigableMap; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.stream.Collectors; +import java.util.Set; + +import edu.cuny.hunter.streamrefactoring.annotations.EntryPoint; -import edu.cuny.hunter.streamrefactoring.annotations.*; import p.A.Widget.Color; public class A { static class Widget { enum Color { - RED, BLUE, GREEN, PINK, ORANGE, YELLOW + RED, + BLUE, + GREEN, + PINK, + ORANGE, + YELLOW }; public Color getColor() { @@ -27,7 +39,8 @@ public Color getColor() { @EntryPoint void m() { Collection orderedWidgets = new ArrayList<>(); - Map> widgetsByColor = orderedWidgets.stream() - .collect(Collectors.groupingByConcurrent(Widget::getColor)); + + Map> widgetsByColor2 = orderedWidgets.stream().collect(Collectors.groupingByConcurrent( + Widget::getColor, ConcurrentSkipListMap::new, Collectors.toCollection(LinkedHashSet::new))); } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index de5cb9ec..8772ec6a 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -518,7 +518,7 @@ public void testConcat() throws Exception { */ public void testConcurrentReduction() throws Exception { this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, true, false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); From a3b598efedd4ea3b8726a2c2fdda2b2c956e0881 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Wed, 28 Feb 2018 18:20:10 -0500 Subject: [PATCH 7/9] Intermediate progress on #64. --- .../core/analysis/StreamStateMachine.java | 15 +++++++++++++-- .../streamrefactoring/core/analysis/Util.java | 8 ++++++++ .../testConcurrentReduction/in/A.java | 7 ++----- .../ConvertStreamToParallelRefactoringTest.java | 11 +++++------ 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java index 302a3ee6..b71affcf 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java @@ -58,6 +58,7 @@ import com.ibm.wala.ipa.callgraph.propagation.NormalAllocationInNode; import com.ibm.wala.ipa.callgraph.propagation.PointerKey; import com.ibm.wala.ipa.cfg.BasicBlockInContext; +import com.ibm.wala.ipa.cha.ClassHierarchy; import com.ibm.wala.ipa.cha.IClassHierarchy; import com.ibm.wala.ipa.modref.ModRef; import com.ibm.wala.shrikeCT.InvalidClassFileException; @@ -209,8 +210,17 @@ protected static StreamAttributeTypestateRule[] createStreamAttributeTypestateRu // @formatter:on } - private static boolean deriveRomForScalarMethod(SSAInvokeInstruction invokeInstruction) + private static boolean deriveRomForScalarMethod(Collection possibleReturnTypes, + SSAInvokeInstruction invokeInstruction, IClassHierarchy hierarchy) throws UnknownIfReduceOrderMattersException { + boolean allImplementMap = possibleReturnTypes.stream().map(TypeAbstraction::getTypeReference) + .allMatch(t -> Util.implementsMap(t, hierarchy)); + + if (allImplementMap) { + // TODO: we should be the complex mutable reduction case. Note that it may not be a + // call to collect(). It could be a call to reduce(). + } + MethodReference declaredTarget = invokeInstruction.getCallSite().getDeclaredTarget(); if (isTerminalOperationWhereReduceOrderMattersIsUnknown(declaredTarget)) @@ -606,7 +616,8 @@ private void discoverIfReduceOrderingPossiblyMatters(EclipseProjectAnalysisEngin else { boolean scalar = Util.isScalar(possibleReturnTypes); if (scalar) - rom = deriveRomForScalarMethod(invokeInstruction); + rom = deriveRomForScalarMethod(possibleReturnTypes, invokeInstruction, + engine.getClassHierarchy()); else // !scalar rom = this.deriveRomForNonScalarMethod(possibleReturnTypes, orderingInference); } diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java index 41aeaf69..cfeaf1d6 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/Util.java @@ -576,6 +576,10 @@ public static boolean implementsIterable(TypeReference reference, IClassHierarch return implementsType(reference, classHierarchy, Util::isIterable); } + public static boolean implementsMap(TypeReference reference, IClassHierarchy hierarchy) { + return implementsType(reference, hierarchy, Util::isMap); + } + public static boolean implementsType(TypeReference typeReference, IClassHierarchy classHierarchy, Predicate predicate) { IClass clazz = classHierarchy.lookupClass(typeReference); @@ -618,6 +622,10 @@ public static boolean isCollector(IClass clazz) { return Util.isType(clazz, "java/util/stream", "Collector"); } + public static boolean isMap(IClass clazz) { + return Util.isType(clazz, "java/util", "Map"); + } + /** * check whether the annotation is "EntryPoint" */ diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java index 6f488557..942fe5d1 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction/in/A.java @@ -33,14 +33,11 @@ public Color getColor() { } } - /** - * P6 in table 3 - */ @EntryPoint void m() { Collection orderedWidgets = new ArrayList<>(); - Map> widgetsByColor2 = orderedWidgets.stream().collect(Collectors.groupingByConcurrent( - Widget::getColor, ConcurrentSkipListMap::new, Collectors.toCollection(LinkedHashSet::new))); + Map> widgetsByColor = orderedWidgets.parallelStream() + .collect(Collectors.groupingBy(Widget::getColor)); } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index 8772ec6a..9046a351 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -514,14 +514,13 @@ public void testConcat() throws Exception { } /** - * Test #64. Test P6 in table3. + * Test #64. */ public void testConcurrentReduction() throws Exception { - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, true, - false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), - PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, - Collections.emptySet())); + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.parallelStream()", + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, true, + false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, + Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); } /** From e4fcadae1e81b1bbded3c04decdbb59f9c662269 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Fri, 2 Mar 2018 11:28:02 -0500 Subject: [PATCH 8/9] Progress on #64. --- .../core/analysis/PreconditionSuccess.java | 4 +- .../core/analysis/StreamStateMachine.java | 4 +- .../testConcurrentReduction1/in/A.java | 17 +++-- .../testConcurrentReduction2/in/A.java | 16 ++-- .../testConcurrentReduction3/in/A.java | 24 +++--- ...onvertStreamToParallelRefactoringTest.java | 75 ++++++++++--------- 6 files changed, 77 insertions(+), 63 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java index 9870eb51..561f4801 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/PreconditionSuccess.java @@ -11,5 +11,7 @@ public enum PreconditionSuccess { P8, P9, P10, - P11 + P11, + P12, + P13 } diff --git a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java index b71affcf..7284a6cf 100644 --- a/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java +++ b/edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamStateMachine.java @@ -217,8 +217,8 @@ private static boolean deriveRomForScalarMethod(Collection poss .allMatch(t -> Util.implementsMap(t, hierarchy)); if (allImplementMap) { - // TODO: we should be the complex mutable reduction case. Note that it may not be a - // call to collect(). It could be a call to reduce(). + // TODO: we should be the complex mutable reduction case #64. Note that it may + // not be a call to collect(). It could be a call to reduce(). } MethodReference declaredTarget = invokeInstruction.getCallSite().getDeclaredTarget(); diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java index ccb9917a..7e15e8db 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction1/in/A.java @@ -13,7 +13,12 @@ public class A { static class Widget { enum Color { - RED, BLUE, GREEN, PINK, ORANGE, YELLOW + RED, + BLUE, + GREEN, + PINK, + ORANGE, + YELLOW }; public Color getColor() { @@ -21,13 +26,13 @@ public Color getColor() { } } - /** - * P7 in table 3 - */ @EntryPoint void m() { - Collection orderedWidgets = new ArrayList<>(); - Map> widgetsByColor = orderedWidgets.parallelStream() + // an "unordered" collection of widgets. + Collection unorderedWidgets = new HashSet<>(); + // populate the collection ... + + Map> widgetsByColor = unorderedWidgets.stream() .collect(Collectors.groupingBy(Widget::getColor)); } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java index 88f967f4..3da86cb6 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction2/in/A.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.HashMap; import java.util.stream.Collectors; import edu.cuny.hunter.streamrefactoring.annotations.*; @@ -13,7 +14,12 @@ public class A { static class Widget { enum Color { - RED, BLUE, GREEN, PINK, ORANGE, YELLOW + RED, + BLUE, + GREEN, + PINK, + ORANGE, + YELLOW }; public Color getColor() { @@ -21,13 +27,11 @@ public Color getColor() { } } - /** - * P8 in table 3 - */ @EntryPoint void m() { Collection orderedWidgets = new ArrayList<>(); - Map> widgetsByColor = orderedWidgets.parallelStream() - .collect(Collectors.groupingByConcurrent(Widget::getColor)); + + Map> widgetsByColor2 = orderedWidgets.stream() + .collect(Collectors.groupingBy(Widget::getColor, HashMap::new, Collectors.toSet())); } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java index c4f4edcf..90afad7c 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java +++ b/edu.cuny.hunter.streamrefactoring.tests/resources/ConvertStreamToParallel/testConcurrentReduction3/in/A.java @@ -1,11 +1,8 @@ package p; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; +import java.util.*; +import java.util.stream.*; +import java.util.concurrent.*; import edu.cuny.hunter.streamrefactoring.annotations.*; import p.A.Widget.Color; @@ -14,7 +11,12 @@ public class A { static class Widget { enum Color { - RED, BLUE, GREEN, PINK, ORANGE, YELLOW + RED, + BLUE, + GREEN, + PINK, + ORANGE, + YELLOW }; public Color getColor() { @@ -22,13 +24,11 @@ public Color getColor() { } } - /** - * P9 in table 3 - */ @EntryPoint void m() { Collection orderedWidgets = new ArrayList<>(); - Map> widgetsByColor = orderedWidgets.stream() - .collect(Collectors.groupingBy(Widget::getColor, HashMap::new, Collectors.toSet())); + + Map> widgetsByColor2 = orderedWidgets.stream().collect(Collectors.groupingByConcurrent( + Widget::getColor, ConcurrentHashMap::new, Collectors.toCollection(LinkedHashSet::new))); } } diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index 9046a351..16cf91d9 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -518,68 +518,71 @@ public void testConcat() throws Exception { */ public void testConcurrentReduction() throws Exception { this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.parallelStream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, true, + EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); } /** - * Test #64. Test P7 in table 3. + * Test #64. */ public void testConcurrentReduction1() throws Exception { - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, true, EnumSet.of(TransformationAction.CONVERT_TO_SEQUENTIAL), PreconditionSuccess.P7, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); + this.helper(new StreamAnalysisExpectedResultWithCollectorKind("unorderedWidgets.stream()", + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.UNORDERED), CollectorKind.NONCONCURRENT, + false, false, true, + EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL, + TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), + PreconditionSuccess.P12, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); } /** - * Test #64. Test P8 in table 3. + * Test #64. */ public void testConcurrentReduction2() throws Exception { - HashSet transformations = new HashSet<>(); - transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT); - transformations.add(TransformationAction.CONVERT_TO_SEQUENTIAL); - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, - false, true, transformations, PreconditionSuccess.P8, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, - RefactoringStatus.OK, Collections.emptySet())); + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, + false, false, + EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL, + TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), + PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); } /** - * Test #64. Test P9 in table 3. + * Test #64. */ public void testConcurrentReduction3() throws Exception { - HashSet transformations = new HashSet<>(); - transformations.add(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT); - transformations.add(TransformationAction.CONVERT_TO_PARALLEL); - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, false, transformations, PreconditionSuccess.P9, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, - RefactoringStatus.OK, Collections.emptySet())); + EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, + false, true, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_NON_CONCURRENT), + PreconditionSuccess.P6, Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, + Collections.emptySet())); } /** - * Test #64. Test P10 in table 3. + * TODO: Test #64. */ - public void testConcurrentReduction4() throws Exception { - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, - false, false, EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); - } + // @formatter:off +// public void testConcurrentReduction4() throws Exception { +// this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", +// EnumSet.of(ExecutionMode.SEQUENTIAL), EnumSet.of(Ordering.ORDERED), CollectorKind.CONCURRENT, false, +// false, false, EnumSet.of(TransformationAction.CONVERT_TO_PARALLEL), PreconditionSuccess.P10, +// Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); +// } + // @formatter:on /** - * Test #64. Test P11 in table 3. + * TODO: Test #64. */ - public void testConcurrentReduction5() throws Exception { - this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", - EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, - false, false, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, - Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); - } + // @formatter:off +// public void testConcurrentReduction5() throws Exception { +// this.helper(new StreamAnalysisExpectedResultWithCollectorKind("orderedWidgets.stream()", +// EnumSet.of(ExecutionMode.PARALLEL), EnumSet.of(Ordering.ORDERED), CollectorKind.NONCONCURRENT, false, +// false, false, EnumSet.of(TransformationAction.CONVERT_COLLECTOR_TO_CONCURRENT), PreconditionSuccess.P11, +// Refactoring.OPTIMIZE_COMPLEX_MUTABLE_REDUCTION, RefactoringStatus.OK, Collections.emptySet())); +// } + // @formatter:on public void testConstructor() throws Exception { this.helper(new StreamAnalysisExpectedResult("new ArrayList().stream()", From 91352e307d1db334e69709fdacb0a315d3489029 Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Fri, 2 Mar 2018 11:28:16 -0500 Subject: [PATCH 9/9] Remove unused method. It was moved. --- .../ui/tests/ConvertStreamToParallelRefactoringTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java index 16cf91d9..25517ebb 100644 --- a/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java +++ b/edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java @@ -142,10 +142,6 @@ public static ICompilationUnit createCU(IPackageFragment pack, String name, Stri return cu; } - private static String errorMessage(String attribute, StreamAnalysisExpectedResult result) { - return "Unexpected " + attribute + " for " + result.getExpectedCreation() + "."; - } - private static Path getAbsolutePath(String fileName) { Path path = Paths.get(RESOURCE_PATH, fileName); Path absolutePath = path.toAbsolutePath();