-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #290 from lucanonym/tooFewPackages
Too few packages
- Loading branch information
Showing
7 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...r-core/src/main/java/de/firemage/autograder/core/check/structure/TooFewPackagesCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.firemage.autograder.core.check.structure; | ||
|
||
import de.firemage.autograder.core.LocalizedMessage; | ||
import de.firemage.autograder.core.ProblemType; | ||
import de.firemage.autograder.core.check.ExecutableCheck; | ||
import de.firemage.autograder.core.dynamic.DynamicAnalysis; | ||
import de.firemage.autograder.core.integrated.IntegratedCheck; | ||
import de.firemage.autograder.core.integrated.StaticAnalysis; | ||
import spoon.reflect.declaration.CtPackage; | ||
import spoon.reflect.declaration.CtType; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@ExecutableCheck(reportedProblems = {ProblemType.TOO_FEW_PACKAGES}) | ||
public class TooFewPackagesCheck extends IntegratedCheck { | ||
public static final int MAX_CLASSES_PER_PACKAGE = 8; | ||
public static final String LOCALIZED_MESSAGE_KEY = "too-few-packages"; | ||
|
||
@Override | ||
protected void check(StaticAnalysis staticAnalysis, DynamicAnalysis dynamicAnalysis) { | ||
List<CtPackage> packages = staticAnalysis.getModel().getAllPackages() | ||
.stream() | ||
.filter(CtPackage::hasTypes) | ||
.toList(); | ||
|
||
if (packages.size() == 1 | ||
&& packages.stream().anyMatch(ctPackage -> ctPackage.getTypes().size() > MAX_CLASSES_PER_PACKAGE)) { | ||
|
||
Optional<CtType<?>> ctType = packages.get(0).getTypes().stream().findFirst(); | ||
ctType.ifPresent(type -> | ||
this.addLocalProblem( | ||
type, | ||
new LocalizedMessage(LOCALIZED_MESSAGE_KEY, Map.of("max", MAX_CLASSES_PER_PACKAGE)), | ||
ProblemType.TOO_FEW_PACKAGES | ||
)); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...re/src/test/java/de/firemage/autograder/core/check/structure/TestTooFewPackagesCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package de.firemage.autograder.core.check.structure; | ||
|
||
import de.firemage.autograder.core.LinterException; | ||
import de.firemage.autograder.core.LocalizedMessage; | ||
import de.firemage.autograder.core.Problem; | ||
import de.firemage.autograder.core.ProblemType; | ||
import de.firemage.autograder.core.check.AbstractCheckTest; | ||
import de.firemage.autograder.core.compiler.JavaVersion; | ||
import de.firemage.autograder.core.file.StringSourceInfo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class TestTooFewPackagesCheck extends AbstractCheckTest { | ||
private static final String LOCALIZED_MESSAGE_KEY = "too-few-packages"; | ||
|
||
|
||
void assertEqualsTooFewPackages(Problem problem) { | ||
assertEquals(ProblemType.TOO_FEW_PACKAGES, problem.getProblemType()); | ||
assertEquals( | ||
this.linter.translateMessage(new LocalizedMessage( | ||
LOCALIZED_MESSAGE_KEY, | ||
Map.of("max", TooFewPackagesCheck.MAX_CLASSES_PER_PACKAGE))), | ||
this.linter.translateMessage(problem.getExplanation()) | ||
); | ||
assertEquals(ProblemType.TOO_FEW_PACKAGES, problem.getProblemType(), "Wrong problem type"); | ||
} | ||
@Test | ||
void test() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceStrings( | ||
JavaVersion.JAVA_17, | ||
Map.ofEntries( | ||
dummySourceEntry("edu.kit", "First"), | ||
dummySourceEntry("edu.kit", "Second"), | ||
dummySourceEntry("edu.kit", "Third"), | ||
dummySourceEntry("edu.kit", "Fourth"), | ||
dummySourceEntry("edu.kit", "Fifth"), | ||
dummySourceEntry("edu.kit", "Sixth"), | ||
dummySourceEntry("edu.kit", "Seventh"), | ||
dummySourceEntry("edu.kit", "Eighth"), | ||
dummySourceEntry("edu.kit", "Ninth") | ||
) | ||
), List.of(ProblemType.TOO_FEW_PACKAGES)); | ||
|
||
assertTrue(problems.hasNext(), "At least one problem should be reported"); | ||
assertEqualsTooFewPackages(problems.next()); | ||
problems.assertExhausted(); | ||
|
||
} | ||
|
||
// if there are multiple packages, the check should not be triggered | ||
@Test | ||
void testWithMultiplePackages() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceStrings( | ||
JavaVersion.JAVA_17, | ||
Map.ofEntries( | ||
dummySourceEntry("edu.kit", "First"), | ||
dummySourceEntry("edu.kit", "Second"), | ||
dummySourceEntry("edu.kit", "Third"), | ||
dummySourceEntry("edu.kit", "Fourth"), | ||
dummySourceEntry("edu.kit", "Fifth"), | ||
dummySourceEntry("edu.kit", "Sixth"), | ||
dummySourceEntry("edu.kit", "Seventh"), | ||
dummySourceEntry("edu.kit", "Eighth"), | ||
dummySourceEntry("edu.kit", "Ninth"), | ||
dummySourceEntry("test", "Test") | ||
) | ||
), List.of(ProblemType.TOO_FEW_PACKAGES)); | ||
problems.assertExhausted(); | ||
} | ||
@Test | ||
void testWithAllowedNumberOfClasses() throws IOException, LinterException { | ||
ProblemIterator problems = this.checkIterator(StringSourceInfo.fromSourceStrings( | ||
JavaVersion.JAVA_17, | ||
Map.ofEntries( | ||
dummySourceEntry("edu.kit", "First"), | ||
dummySourceEntry("edu.kit", "Second"), | ||
dummySourceEntry("edu.kit", "Third"), | ||
dummySourceEntry("edu.kit", "Fourth"), | ||
dummySourceEntry("edu.kit", "Fifth"), | ||
dummySourceEntry("edu.kit", "Sixth"), | ||
dummySourceEntry("edu.kit", "Seventh"), | ||
dummySourceEntry("edu.kit", "Eighth") | ||
) | ||
), List.of(ProblemType.TOO_FEW_PACKAGES)); | ||
problems.assertExhausted(); | ||
|
||
|
||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters