Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 2c2b228

Browse files
Fixes an issue when nested groups are configured, but groups are not nested.
1 parent ee9b222 commit 2c2b228

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ Once the layout has been applied, you can upload your workspace to the Structuri
2525

2626
## Changelog
2727

28-
### 1.8.1 (unreleased)
28+
### 1.8.1 (13th March 2023)
2929

3030
- Fixes warning about clusters with the same name.
31+
- Fixes an issue when nested groups are configured, but groups are not nested.
3132

3233
### 1.8.0 (12th March 2023)
3334

src/main/java/com/structurizr/graphviz/DotFileWriter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ void write(ContainerView view) throws Exception {
163163
softwareSystems.add((SoftwareSystem)elementView.getElement().getParent());
164164
}
165165
}
166+
List<SoftwareSystem> sortedSoftwareSystems = new ArrayList<>(softwareSystems);
167+
sortedSoftwareSystems.sort(Comparator.comparing(Element::getId));
166168

167-
for (SoftwareSystem softwareSystem : softwareSystems) {
169+
for (SoftwareSystem softwareSystem : sortedSoftwareSystems) {
168170
fileWriter.write(String.format(locale, " subgraph cluster_%s {\n", softwareSystem.getId()));
169171
fileWriter.write(" margin=" + CLUSTER_INTERNAL_MARGIN + "\n");
170172

@@ -202,8 +204,10 @@ void write(ComponentView view) throws Exception {
202204
containers.add((Container)elementView.getElement().getParent());
203205
}
204206
}
207+
List<Container> sortedContainers = new ArrayList<>(containers);
208+
sortedContainers.sort(Comparator.comparing(Element::getId));
205209

206-
for (Container container : containers) {
210+
for (Container container : sortedContainers) {
207211
fileWriter.write(String.format(locale, " subgraph cluster_%s {\n", container.getId()));
208212
fileWriter.write(" margin=" + CLUSTER_INTERNAL_MARGIN + "\n");
209213

@@ -378,7 +382,7 @@ private void writeElements(ModelView view, String padding, Set<GroupableElement>
378382
} else if (groupCount == contextCount) {
379383
// moved from a/b to a/c
380384
// - close off previous subgraph
381-
if (groupCount > 1) {
385+
if (context.length() > 0) {
382386
writer.write(padding + "}\n");
383387
}
384388
} else {

src/test/java/com/structurizr/graphviz/DotFileWriterTests.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,4 +536,57 @@ public void test_writeComponentViewWithGroupedElements() throws Exception {
536536
"}", content);
537537
}
538538

539+
@Test
540+
public void test_writeContainerViewWithGroupedElements_WithAndWithoutAGroupSeparator() throws Exception {
541+
Workspace workspace = new Workspace("Name", "");
542+
SoftwareSystem softwareSystem = workspace.getModel().addSoftwareSystem("Software System", "");
543+
Container container1 = softwareSystem.addContainer("Container 1");
544+
container1.setGroup("Group 1");
545+
Container container2 = softwareSystem.addContainer("Container 2");
546+
container2.setGroup("Group 2");
547+
548+
ContainerView view = workspace.getViews().createContainerView(softwareSystem, "Containers", "");
549+
view.addAllElements();
550+
551+
DotFileWriter dotFileWriter = new DotFileWriter(PATH, RankDirection.TopBottom, 300, 300);
552+
dotFileWriter.write(view);
553+
554+
File file = new File(PATH, "Containers.dot");
555+
assertTrue(file.exists());
556+
557+
String expectedResult = "digraph {\n" +
558+
" compound=true\n" +
559+
" graph [splines=polyline,rankdir=TB,ranksep=1.0,nodesep=1.0,fontsize=5]\n" +
560+
" node [shape=box,fontsize=5]\n" +
561+
" edge []\n" +
562+
"\n" +
563+
" subgraph cluster_1 {\n" +
564+
" margin=25\n" +
565+
" subgraph cluster_group_1 {\n" +
566+
" margin=25\n" +
567+
" 2 [width=1.500000,height=1.000000,fixedsize=true,id=2,label=\"2: Container 1\"]\n" +
568+
" }\n" +
569+
" subgraph cluster_group_2 {\n" +
570+
" margin=25\n" +
571+
" 3 [width=1.500000,height=1.000000,fixedsize=true,id=3,label=\"3: Container 2\"]\n" +
572+
" }\n" +
573+
" }\n" +
574+
"\n" +
575+
"}";
576+
577+
String content = new String(Files.readAllBytes(file.toPath()));
578+
assertEquals(expectedResult, content);
579+
580+
// this should be the same
581+
workspace.getModel().addProperty("structurizr.groupSeparator", "/");
582+
dotFileWriter = new DotFileWriter(PATH, RankDirection.TopBottom, 300, 300);
583+
dotFileWriter.write(view);
584+
585+
file = new File(PATH, "Containers.dot");
586+
assertTrue(file.exists());
587+
588+
content = new String(Files.readAllBytes(file.toPath()));
589+
assertEquals(expectedResult, content);
590+
}
591+
539592
}

0 commit comments

Comments
 (0)