-
Notifications
You must be signed in to change notification settings - Fork 10
/
ClassDiagramBuilderTest.java
191 lines (168 loc) · 7.95 KB
/
ClassDiagramBuilderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Plantuml builder
*
* Copyright (C) 2024 Focus IT
*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package ch.ifocusit.plantuml.classdiagram;
import ch.ifocusit.plantuml.PlantUmlBuilder;
import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.test.helper.domain.Devise;
import ch.ifocusit.plantuml.test.helper.domain.Driver;
import ch.ifocusit.plantuml.test.helper.domain.Price;
import ch.ifocusit.plantuml.test.helper.domain.material.Car;
import ch.ifocusit.plantuml.test.helper.domain.material.Machine;
import ch.ifocusit.plantuml.test.helper.domain.material.Vehicule;
import ch.ifocusit.plantuml.test.helper.domain.material.Wheel;
import ch.ifocusit.plantuml.test.helper.service.AccessDataService;
import ch.ifocusit.plantuml.utils.ClassUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Objects;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Julien Boz
*/
@SuppressWarnings("rawtypes")
class ClassDiagramBuilderTest {
private static final String CR = PlantUmlBuilder.NEWLINE;
@Test
void buildShouldGenerateDiagram() throws Exception {
String expected = IOUtils.toString(
Objects.requireNonNull(this.getClass().getResourceAsStream("/domain-diagram.plantuml")),
Charset.defaultCharset());
// tag::createSimple[]
String diagram = new ClassDiagramBuilder().<ClassDiagramBuilder>excludes(".*\\.ignored", "Machine")
.addPackage(Vehicule.class.getPackage()).addClasses(Vehicule.class,
Car.class, Driver.class, Price.class, Wheel.class, Devise.class)
.build();
// end::createSimple[]
assertThat(diagram).isEqualTo(expected);
}
@Test
public void buildShouldGenerateDiagramFromAggregateMaster() throws Exception {
String expected = IOUtils.toString(
Objects.requireNonNull(this.getClass().getResourceAsStream("/domain-aggregate-diagram.plantuml")),
Charset.defaultCharset());
// tag::createSimple[]
String diagram = new ClassDiagramBuilder()
.<ClassDiagramBuilder>excludes(".*\\.ignored")
.addClasses(Car.class)
.withDependencies()
.setStartOptions("!pragma layout smetana")
.build();
// end::createSimple[]
assertThat(diagram).isEqualTo(expected);
}
@Test
public void buildShouldGenerateDiagramWithDepth() throws Exception {
String expected = IOUtils.toString(
Objects.requireNonNull(this.getClass().getResourceAsStream("/service-diagram.plantuml")),
Charset.defaultCharset());
// tag::createFromOneClassWithDependencies[]
String diagram = new ClassDiagramBuilder().addClasses(AccessDataService.class)
.withDependencies()
.setStartOptions("skinparam backgroundColor lightgray")
.setHeader("class diagram type")
.setTitle("Service diagram")
.setFooter("page 1/1")
.setEndOptions("show methods", "hide fields")
.build();
// end::createFromOneClassWithDependencies[]
assertThat(diagram).isEqualTo(expected);
}
@Test
public void buildShouldExportOnlyAnnotatedClassAndField() {
String diagram = new ClassDiagramBuilder().excludes(".*\\.ignored")
// only annotated
.addFieldPredicate(attribute -> attribute.getField().isAnnotationPresent(Machine.class))
// no method
.<ClassDiagramBuilder>addMethodPredicate(classMethod -> false)
.addClasses(Car.class)
.build();
assertThat(diagram).isEqualTo("@startuml" + CR + CR + "class \"Car\" {" + CR
+ " brand : String" + CR + " model : String" + CR + " wheels : Collection<Wheel>"
+ CR + "}" + CR + CR + CR + "@enduml");
}
@Test
public void testOverrideNames() {
String diagram = new ClassDiagramBuilder().excludes(".*\\.ignored")
// only annotated
.addFieldPredicate(attribute -> attribute.getField().isAnnotationPresent(Machine.class))
// no method
.<ClassDiagramBuilder>addMethodPredicate(classMethod -> false)
.addClasses(Car.class)
.withNamesMapper(new NamesMapper() {
@Override
public String getClassName(Class aClass) {
return "domain." + ClassUtils.getSimpleName(aClass);
}
@Override
public String getFieldName(Field field) {
return "attr." + field.getName();
}
})
.build();
assertThat(diagram).isEqualTo("@startuml" + CR + CR + "class \"domain.Car\" {" + CR
+ " attr.brand : String" + CR + " attr.model : String" + CR
+ " attr.wheels : Collection<Wheel>" + CR + "}" + CR + CR + CR + "@enduml");
}
@Test
public void testLinkRenderer() throws IOException {
String expected = IOUtils.toString(Objects.requireNonNull(this.getClass().getResourceAsStream("/links.puml")),
Charset.defaultCharset());
String diagram = new ClassDiagramBuilder()
.addClasses(Car.class, Driver.class)
.withLinkMaker(new LinkMaker() {
@Override
public Optional<Link> getClassLink(Class aClass) {
String label = aClass.getSimpleName();
Link link = new Link();
link.setLabel(label);
link.setUrl("https://link.com/" + label.toLowerCase());
if (aClass.equals(Driver.class)) {
link.setTooltip("Taxi Driver");
}
return Optional.of(link);
}
@Override
public Optional<Link> getFieldLink(Field field) {
if (field.getName().equals("wheels") || field.getDeclaringClass().equals(Driver.class)) {
return Optional.empty();
}
Link link = new Link();
link.setUrl("https://link.com/car/" + field.getName().toLowerCase());
if (field.getName().equals("brand")) {
link.setLabel("lien");
}
if (field.getName().equals("price")) {
link.setTooltip("Show details");
}
if (field.getName().equals("model")) {
link.setLabel("Car models");
link.setTooltip("Show all cars' models");
}
return Optional.of(link);
}
})
.excludes(".*\\.ignored")
.build();
assertThat(diagram).isEqualTo(expected);
}
}