Skip to content

Commit

Permalink
support optional component keyword in PlantUML diagram rules (#1223)
Browse files Browse the repository at this point in the history
It is valid PlantUML syntax to prepend the `component` keyword to a
component
declaration, e.g.

```
component [MyComponent] <<..mycmp..>>
```

Since it doesn't create a big overhead to support this as an optional
part
of the component declaration, we can add it to make it easier for users
that distinguish their components from other types of objects like
databases.
  • Loading branch information
codecholeric committed Apr 10, 2024
2 parents 0daf60e + 2ec2db2 commit 6fda869
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static java.util.Collections.singletonList;

class PlantUmlPatterns {
private static final String OPTIONAL_COMPONENT_KEYWORD_FORMAT = "(?:component)?";
private static final String COMPONENT_NAME_GROUP_NAME = "componentName";
private static final String COMPONENT_NAME_FORMAT = "\\[" + capture(anythingBut("\\[\\]"), COMPONENT_NAME_GROUP_NAME) + "]";

Expand All @@ -46,7 +47,13 @@ class PlantUmlPatterns {
private static final String COLOR_FORMAT = "\\s*(?:#" + anyOf("\\w|/\\\\-") + "+)?";

private static final Pattern PLANTUML_COMPONENT_PATTERN = Pattern.compile(
"^\\s*" + COMPONENT_NAME_FORMAT + "\\s*" + STEREOTYPE_FORMAT + "*" + ALIAS_FORMAT + COLOR_FORMAT + "\\s*");
"^\\s*" + OPTIONAL_COMPONENT_KEYWORD_FORMAT
+ "\\s*" + COMPONENT_NAME_FORMAT
+ "\\s*" + STEREOTYPE_FORMAT + "*"
+ ALIAS_FORMAT
+ COLOR_FORMAT
+ "\\s*"
);

private static String capture(String pattern) {
return "(" + pattern + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ public void ignores_database_components() {
assertThat(b.getDependencies()).isEmpty();
}

@Test
public void supports_components_declared_with_component_keyword() {
File file = TestDiagram.in(temporaryFolder)
.rawLine("component [CompA] <<..c1..>>")
.rawLine("component [Comp B] <<..c2..>> as CompB")
.dependencyFrom("CompA").to("CompB")
.write();

PlantUmlDiagram diagram = createDiagram(file);

PlantUmlComponent a = getComponentWithName("CompA", diagram);
PlantUmlComponent b = getComponentWithAlias(new Alias("CompB"), diagram);

assertThat(a.getDependencies()).containsOnly(b);
assertThat(b.getDependencies()).isEmpty();
}

private PlantUmlComponent getComponentWithName(String componentName, PlantUmlDiagram diagram) {
PlantUmlComponent component = diagram.getAllComponents().stream()
.filter(c -> c.getComponentName().asString().equals(componentName))
Expand Down

0 comments on commit 6fda869

Please sign in to comment.