Skip to content

Commit

Permalink
better links renderer (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Boz <[email protected]>
  • Loading branch information
jboz and Julien Boz authored Jul 21, 2023
1 parent 84983ec commit 4d571e6
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 18 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/ch/ifocusit/plantuml/PlantUmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ch.ifocusit.plantuml.classdiagram.model.Association;
import ch.ifocusit.plantuml.classdiagram.model.Association.AssociationType;
import ch.ifocusit.plantuml.classdiagram.model.Cardinality;
import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.classdiagram.model.Package;
import ch.ifocusit.plantuml.classdiagram.model.attribute.Attribute;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz;
Expand Down Expand Up @@ -177,7 +178,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
.append(String.join(", ", stereotypes))
.append(STEREOTYPE_CLOSE));
// class link
clazz.getLink().ifPresent(link -> content.append(SPACE).append(link));
clazz.getLink().ifPresent(link -> content.append(SPACE).append(link.render(Link.LinkContext.CLASS)));
// class color
clazz.getBackgroundColor().ifPresent(color -> content.append(SPACE).append(color(color)));

Expand All @@ -192,7 +193,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
attribute.getTypeName().ifPresent(
type -> content.append(SPACE).append(SEMICOLON).append(SPACE).append(type));
// field link
attribute.getLink().ifPresent(link -> content.append(SPACE).append(link));
attribute.getLink().ifPresent(link -> content.append(SPACE).append(link.render(Link.LinkContext.FIELD)));
content.append(NEWLINE);
}
// add methods
Expand All @@ -211,7 +212,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
method.getReturnTypeName().ifPresent(
type -> content.append(SPACE).append(SEMICOLON).append(SPACE).append(type));
// method link
method.getLink().ifPresent(link -> content.append(SPACE).append(link));
method.getLink().ifPresent(link -> content.append(SPACE).append(link.render(Link.LinkContext.METHOD)));
content.append(NEWLINE);
});
if (clazz.hasContent()) {
Expand Down
55 changes: 44 additions & 11 deletions src/main/java/ch/ifocusit/plantuml/classdiagram/model/Link.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
package ch.ifocusit.plantuml.classdiagram.model;

import org.apache.commons.lang3.StringUtils;

@SuppressWarnings("unused")
public class Link {
private String url;
private String label;

public String getUrl() {
return url;
}
private String tooltip;

public void setUrl(String url) {
this.url = url;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}

@Override
public String toString() {
return String.format("[[%s{%s}]]", url, label);
public void setTooltip(String tooltip) {
this.tooltip = tooltip;
}

public String render(LinkContext context) {
if (LinkContext.CLASS.equals(context)) {
if (StringUtils.isNotBlank(tooltip) && StringUtils.isNotBlank(url)) {
return String.format("[[%s{%s}]]", url, tooltip);
}
if (StringUtils.isNotBlank(tooltip) && StringUtils.isBlank(url)) {
return String.format("[[{%s}]]", tooltip);
}
if (StringUtils.isNotBlank(url)) {
return String.format("[[%s]]", url);
}
return StringUtils.EMPTY;
}
if (StringUtils.isNotBlank(tooltip) && StringUtils.isNotBlank(url) && StringUtils.isNotBlank(label)) {
return String.format("[[[%s{%s} %s]]]", url, tooltip, label);
}
if (StringUtils.isNotBlank(tooltip) && StringUtils.isNotBlank(url) && StringUtils.isBlank(label)) {
return String.format("[[[%s{%s}]]]", url, tooltip);
}
if (StringUtils.isBlank(tooltip) && StringUtils.isNotBlank(url) && StringUtils.isNotBlank(label)) {
return String.format("[[[%s %s]]]", url, label);
}
if (StringUtils.isNotBlank(tooltip) && StringUtils.isBlank(url) && StringUtils.isBlank(label)) {
return String.format("[[[{%s}]]]", tooltip);
}
if (StringUtils.isNotBlank(url) && StringUtils.isBlank(tooltip) && StringUtils.isBlank(label)) {
return String.format("[[[%s]]]", url);
}
if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(label) && StringUtils.isBlank(tooltip)) {
return String.format("[[[%s %s]]]", url, label);
}
if (StringUtils.isNotBlank(tooltip) && StringUtils.isBlank(url)) {
return String.format("[[[{%s}]]]", tooltip);
}
return StringUtils.EMPTY;
}

public enum LinkContext {
CLASS, FIELD, METHOD
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -31,9 +32,11 @@
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;

Expand All @@ -47,10 +50,7 @@ class ClassDiagramBuilderTest {

@Test
void buildShouldGenerateDiagram() throws Exception {
String expected = IOUtils.toString(
Objects.requireNonNull(
this.getClass().getResourceAsStream("/domain-diagram.plantuml")),
Charset.defaultCharset());
String expected = IOUtils.toString(Objects.requireNonNull(this.getClass().getResourceAsStream("/domain-diagram.plantuml")), Charset.defaultCharset());

// tag::createSimple[]
String diagram =
Expand Down Expand Up @@ -137,4 +137,49 @@ public String getFieldName(Field field) {
+ " 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ch.ifocusit.plantuml.classdiagram.model;

import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import static org.assertj.core.api.Assertions.assertThat;

class LinkTest {

public static final String URL = "https://link.com";
public static final String LABEL = "label";
public static final String TOOLTIP = "tooltip";

@ParameterizedTest
@CsvSource({
",,,,",
URL + ",,,[[" + URL + "]]",
URL + "," + LABEL + ",,[[" + URL + "]]",
URL + "," + LABEL + "," + TOOLTIP + ",[[" + URL + "{" + TOOLTIP + "}]]",
",," + TOOLTIP + ",[[{" + TOOLTIP + "}]]",
})
void render_class(String url, String label, String tooltip, String expected) {
// given
Link link = new Link();
link.setUrl(url);
link.setLabel(label);
link.setTooltip(tooltip);
// when
String actual = link.render(Link.LinkContext.CLASS);
// then
assertThat(actual).isEqualTo(expected == null ? StringUtils.EMPTY : expected);
}

@ParameterizedTest
@CsvSource({
",,,,",
URL + ",,,[[[" + URL + "]]]",
"," + LABEL + ",,",
",," + TOOLTIP + ",[[[{" + TOOLTIP + "}]]]",
URL + "," + LABEL + ",,[[[" + URL + " " + LABEL + "]]]",
URL + ",," + TOOLTIP + ",[[[" + URL + "{" + TOOLTIP + "}]]]",
URL + "," + LABEL + "," + TOOLTIP + ",[[[" + URL + "{" + TOOLTIP + "} " + LABEL + "]]]",
"," + LABEL + "," + TOOLTIP + ",[[[{" + TOOLTIP + "}]]]",
})
void render_field(String url, String label, String tooltip, String expected) {
// given
Link link = new Link();
link.setUrl(url);
link.setLabel(label);
link.setTooltip(tooltip);
// when
String actual = link.render(Link.LinkContext.FIELD);
// then
assertThat(actual).isEqualTo(expected == null ? StringUtils.EMPTY : expected);
}
}
23 changes: 23 additions & 0 deletions src/test/resources/links.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@startuml

class "Car" [[https://link.com/car]] {
brand : String [[[https://link.com/car/brand lien]]]
model : String [[[https://link.com/car/model{Show all cars' models} Car models]]]
drivers : Set<Driver> [[[https://link.com/car/drivers]]]
price : Price [[[https://link.com/car/price{Show details}]]]
wheels : Collection<Wheel>
addDriver(Driver) : Car
addWheel(Wheel)
buyBy(Driver, BigDecimal, Devise) : Driver
}

class "Driver" [[https://link.com/driver{Taxi Driver}]] {
name : String
cars : List<Car>
addCar(Car)
buy(Car)
}

"Car" "*" <-> "*" "Driver" : drivers/cars

@enduml

0 comments on commit 4d571e6

Please sign in to comment.