Skip to content

#4: Implemented PackageRuleTest#17

Merged
hohwille merged 15 commits into
devonfw-sample:masterfrom
moritzLanger:master
Feb 13, 2023
Merged

#4: Implemented PackageRuleTest#17
hohwille merged 15 commits into
devonfw-sample:masterfrom
moritzLanger:master

Conversation

@moritzLanger
Copy link
Copy Markdown
Contributor

@moritzLanger moritzLanger commented Feb 6, 2023

Implements story #4.

Copy link
Copy Markdown
Contributor

@hohwille hohwille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moritzLanger thanks for your PR. Looks good to me. 👍
Can you have a look at my review comments and see if you can improve accordingly?

return new ArchCondition<JavaClass>("check for the package structure to be valid", new Object(){}) {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
Pattern pattern = Pattern.compile(DEFAULT_PATTERN);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern compilation consumes time. Use a constant for the Pattern to prevent compilation for every checked class.

Comment on lines +71 to +72
events.add(new SimpleConditionEvent(javaClass,
matcher.find(), "true if valid, flase if invalid"));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest using matcher.matches() here as find is for finding a sub-pattern that may occur multiple times in a String to check.
Further, I do not understand this event approach. So we always create and event with a boolean flag (true = OK, false = NOK). Will archunit raise an error displaying all violations then? Fine, if so - I am just curious as I have not seen this feature before...

/*Components */
public static final String COMPONENT_GENERAL = "general";

public static final String COMPONENT_TASK = "task";
Copy link
Copy Markdown
Contributor

@hohwille hohwille Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it is a good idea to hardcode the components into the regex.
We only need the actual component names in the component dependency rules but for the package rule there is no point in restricting the components and listing all of them here as constants... As a result this rule will not need to be customized by the projects.
If we need the component name we can get it from a capturing group of the regex.
When implementing #3 we will see if we want to build it on top of this and your regex or if we use archunit features directly.

+ LAYER_DATA_ACCESS + "|" + LAYER_SERVICE + "|" + LAYER_LOGIC + "|" + LAYER_BATCH + "|"
+ LAYER_CLIENT;

private static final String COMPONENT_LAYERS = COMPONENT_GENERAL + "|" + COMPONENT_TASK;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Components are orthogonal to layers so the term COMPONENT_LAYERS is IMHO missleading.


private static final String COMPONENT_LAYERS = COMPONENT_GENERAL + "|" + COMPONENT_TASK;

public static final String DEFAULT_PATTERN = "(" + PATTERN_SEGMENT + ")\\.(" + COMPONENT_LAYERS + ")\\.(" + PATTERN_LAYERS + ")\\.(" + PATTERN_SEGMENT + ")*";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The root package typically has multiple segments (e.g. com.customer.app).
I would create a separate capturing group for the entire root package as `"(" + PATTERN_SEGMENT + "(\." + PATTERN_SEGMENT + ")*)".
In case you will actually use capturing groups with according index in the regex code below, a best practice is to add a comment on top of the regex numbering the groups. Example:
https://github.com/devonfw/cobigen/blob/8db8c40c53fe2acedfe15010c69f7fcbee3bce7c/cobigen-plugins/cobigen-templateengines/cobigen-tempeng-agnostic/src/main/java/com/devonfw/cobigen/tempeng/agnostic/AgnosticTemplateEngine.java#L51-L52

Comment on lines +23 to +24


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove these blank lines :)

@Override
public void check(JavaClass javaClass, ConditionEvents events) {
Pattern pattern = Pattern.compile(DEFAULT_PATTERN);
Matcher matcher = pattern.matcher(javaClass.getName());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you should instead do javaClass.getPackageName() instead of javaClass.getName().

LAYER_LOGIC, LAYER_SERVICE, LAYER_BATCH);

/* Pattern */
private static final String PATTERN_SEGMENT = "[a-zA-Z0-9_]+";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while it is technically possible to use UPPERCASE in package segments, it is strictly discouraged.
We should not allow this explicitly as it is sick to do this in Java.

Copy link
Copy Markdown
Contributor

@hohwille hohwille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moritzLanger thanks. Now everything looks perfect 👍
Ready for merge.

p.s.: We should later see how we can align this with the layer rules that defines the same layers redundantly that you have here as constants and later also with the component rules...

@hohwille hohwille linked an issue Feb 13, 2023 that may be closed by this pull request
1 task
@hohwille hohwille merged commit 0f81313 into devonfw-sample:master Feb 13, 2023
@hohwille hohwille changed the title Implemented PackageRuleTest #4: Implemented PackageRuleTest Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement package rule

2 participants