Skip to content

Commit

Permalink
ArchRule: Prevent ForEach (as not functional)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfuchss committed Sep 25, 2024
1 parent 34b1cb9 commit 55717cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<autograder.version>0.6.2</autograder.version>
<jgit.version>7.0.0.202409031743-r</jgit.version>
<junit.version>5.11.1</junit.version>
<archunit.version>1.3.0</archunit.version>
<versions-maven-plugin.version>2.17.1</versions-maven-plugin.version>
</properties>

Expand Down Expand Up @@ -93,6 +94,17 @@
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit</artifactId>
<version>${archunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
<version>${archunit.version}</version>
</dependency>

<!-- Autograder -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static List<Annotation> mergeAnnotations(
}

// Further partition the annotations by their remaining classifiers:
annotationsForClassifier.stream()
Map<String, List<Annotation>> map = annotationsForClassifier.stream()
.collect(Collectors.groupingBy(
annotation -> {
List<String> classifiers = annotation.getClassifiers();
Expand All @@ -75,11 +75,11 @@ static List<Annotation> mergeAnnotations(
}
},
LinkedHashMap::new,
Collectors.toList()))
.values()
.stream()
.flatMap(list -> merge(list, targetNumberOfAnnotations, locale).stream())
.forEach(result::add);
Collectors.toList()));

for (List<Annotation> list : map.values()) {
result.addAll(merge(list, targetNumberOfAnnotations, locale));
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.sdq.artemis4j.grading;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -112,7 +111,13 @@ public void close() throws ArtemisClientException {

private static void deleteDirectory(Path path) throws IOException {
try (var dirStream = Files.walk(path)) {
dirStream.map(Path::toFile).sorted(Comparator.reverseOrder()).forEach(File::delete);
var filesInDirectory = dirStream
.map(Path::toFile)
.sorted(Comparator.reverseOrder())
.toList();
for (var file : filesInDirectory) {
file.delete();
}
}
}
}
28 changes: 28 additions & 0 deletions src/test/java/edu/kit/kastel/sdq/artemis4j/ArchitectureTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Licensed under EPL-2.0 2024. */
package edu.kit.kastel.sdq.artemis4j;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;

@AnalyzeClasses(packages = "edu.kit.kastel.sdq.artemis4j")
class ArchitectureTest {

@ArchTest
static final ArchRule noForEachInCollectionsOrStream = noClasses()
.should()
.callMethod(Stream.class, "forEach", Consumer.class)
.orShould()
.callMethod(Stream.class, "forEachOrdered", Consumer.class)
.orShould()
.callMethod(List.class, "forEach", Consumer.class)
.orShould()
.callMethod(List.class, "forEachOrdered", Consumer.class)
.because("Lambdas should be functional. ForEach is typically used for side-effects.");
}

0 comments on commit 55717cc

Please sign in to comment.