Skip to content
This repository was archived by the owner on Mar 28, 2026. It is now read-only.

Commit 783ff48

Browse files
Adds support for name/value properties on element and relationship styles.
1 parent 87d42ae commit 783ff48

7 files changed

Lines changed: 210 additions & 8 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ subprojects { proj ->
1414

1515
description = 'Structurizr'
1616
group = 'com.structurizr'
17-
version = '1.12.2'
17+
version = '1.13.0'
1818

1919
repositories {
2020
mavenCentral()

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.13.0 (unreleased to Maven Central)
4+
5+
- Adds support for name/value properties on element and relationship styles.
6+
37
## 1.12.2 (30th March 2022)
48

59
- Adds support for sorting views by the order in which they are created.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.structurizr.view;
2+
3+
import com.structurizr.PropertyHolder;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public abstract class AbstractStyle implements PropertyHolder {
9+
10+
private Map<String, String> properties = new HashMap<>();
11+
12+
/**
13+
* Gets the collection of name-value property pairs associated with this workspace, as a Map.
14+
*
15+
* @return a Map (String, String) (empty if there are no properties)
16+
*/
17+
public Map<String, String> getProperties() {
18+
return new HashMap<>(properties);
19+
}
20+
21+
/**
22+
* Adds a name-value pair property to this workspace.
23+
*
24+
* @param name the name of the property
25+
* @param value the value of the property
26+
*/
27+
public void addProperty(String name, String value) {
28+
if (name == null || name.trim().length() == 0) {
29+
throw new IllegalArgumentException("A property name must be specified.");
30+
}
31+
32+
if (value == null || value.trim().length() == 0) {
33+
throw new IllegalArgumentException("A property value must be specified.");
34+
}
35+
36+
properties.put(name, value);
37+
}
38+
39+
void setProperties(Map<String, String> properties) {
40+
if (properties != null) {
41+
this.properties = new HashMap<>(properties);
42+
}
43+
}
44+
45+
}

structurizr-core/src/com/structurizr/view/ElementStyle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.structurizr.view;
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.structurizr.PropertyHolder;
45
import com.structurizr.util.ImageUtils;
56
import com.structurizr.util.StringUtils;
67

78
/**
89
* A definition of an element style.
910
*/
10-
public final class ElementStyle {
11+
public final class ElementStyle extends AbstractStyle {
1112

1213
public static final int DEFAULT_WIDTH = 450;
1314
public static final int DEFAULT_HEIGHT = 300;

structurizr-core/src/com/structurizr/view/RelationshipStyle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.structurizr.util.StringUtils;
55

6-
public final class RelationshipStyle {
6+
public final class RelationshipStyle extends AbstractStyle {
77

88
private static final int START_OF_LINE = 0;
99
private static final int END_OF_LINE = 100;

structurizr-core/test/unit/com/structurizr/view/ElementStyleTests.java

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.junit.Test;
44

5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertNull;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.junit.Assert.*;
79

810
public class ElementStyleTests {
911

@@ -203,5 +205,79 @@ public void test_Stroke_ThrowsAnException_WhenAnInvalidHexColorCodeIsSpecified()
203205
ElementStyle style = new ElementStyle();
204206
style.stroke("white");
205207
}
206-
208+
209+
@Test
210+
public void test_getProperties_ReturnsAnEmptyList_WhenNoPropertiesHaveBeenAdded() {
211+
ElementStyle style = new ElementStyle();
212+
assertEquals(0, style.getProperties().size());
213+
}
214+
215+
@Test
216+
public void test_addProperty_ThrowsAnException_WhenTheNameIsNull() {
217+
try {
218+
ElementStyle style = new ElementStyle();
219+
style.addProperty(null, "value");
220+
fail();
221+
} catch (IllegalArgumentException e) {
222+
assertEquals("A property name must be specified.", e.getMessage());
223+
}
224+
}
225+
226+
@Test
227+
public void test_addProperty_ThrowsAnException_WhenTheNameIsEmpty() {
228+
try {
229+
ElementStyle style = new ElementStyle();
230+
style.addProperty(" ", "value");
231+
fail();
232+
} catch (IllegalArgumentException e) {
233+
assertEquals("A property name must be specified.", e.getMessage());
234+
}
235+
}
236+
237+
@Test
238+
public void test_addProperty_ThrowsAnException_WhenTheValueIsNull() {
239+
try {
240+
ElementStyle style = new ElementStyle();
241+
style.addProperty("name", null);
242+
fail();
243+
} catch (IllegalArgumentException e) {
244+
assertEquals("A property value must be specified.", e.getMessage());
245+
}
246+
}
247+
248+
@Test
249+
public void test_addProperty_ThrowsAnException_WhenTheValueIsEmpty() {
250+
try {
251+
ElementStyle style = new ElementStyle();
252+
style.addProperty("name", " ");
253+
fail();
254+
} catch (IllegalArgumentException e) {
255+
assertEquals("A property value must be specified.", e.getMessage());
256+
}
257+
}
258+
259+
@Test
260+
public void test_addProperty_AddsTheProperty_WhenANameAndValueAreSpecified() {
261+
ElementStyle style = new ElementStyle();
262+
style.addProperty("name", "value");
263+
assertEquals("value", style.getProperties().get("name"));
264+
}
265+
266+
@Test
267+
public void test_setProperties_DoesNothing_WhenNullIsSpecified() {
268+
ElementStyle style = new ElementStyle();
269+
style.setProperties(null);
270+
assertEquals(0, style.getProperties().size());
271+
}
272+
273+
@Test
274+
public void test_setProperties_SetsTheProperties_WhenANonEmptyMapIsSpecified() {
275+
ElementStyle style = new ElementStyle();
276+
Map<String, String> properties = new HashMap<>();
277+
properties.put("name", "value");
278+
style.setProperties(properties);
279+
assertEquals(1, style.getProperties().size());
280+
assertEquals("value", style.getProperties().get("name"));
281+
}
282+
207283
}

structurizr-core/test/unit/com/structurizr/view/RelationshipStyleTests.java

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.junit.Test;
44

5-
import static org.junit.Assert.assertEquals;
6-
import static org.junit.Assert.assertNull;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import static org.junit.Assert.*;
79

810
public class RelationshipStyleTests {
911

@@ -126,4 +128,78 @@ public void test_color_ThrowsAnException_WhenAnInvalidHexColorCodeIsSpecified()
126128
style.color("white");
127129
}
128130

131+
@Test
132+
public void test_getProperties_ReturnsAnEmptyList_WhenNoPropertiesHaveBeenAdded() {
133+
RelationshipStyle style = new RelationshipStyle();
134+
assertEquals(0, style.getProperties().size());
135+
}
136+
137+
@Test
138+
public void test_addProperty_ThrowsAnException_WhenTheNameIsNull() {
139+
try {
140+
RelationshipStyle style = new RelationshipStyle();
141+
style.addProperty(null, "value");
142+
fail();
143+
} catch (IllegalArgumentException e) {
144+
assertEquals("A property name must be specified.", e.getMessage());
145+
}
146+
}
147+
148+
@Test
149+
public void test_addProperty_ThrowsAnException_WhenTheNameIsEmpty() {
150+
try {
151+
RelationshipStyle style = new RelationshipStyle();
152+
style.addProperty(" ", "value");
153+
fail();
154+
} catch (IllegalArgumentException e) {
155+
assertEquals("A property name must be specified.", e.getMessage());
156+
}
157+
}
158+
159+
@Test
160+
public void test_addProperty_ThrowsAnException_WhenTheValueIsNull() {
161+
try {
162+
RelationshipStyle style = new RelationshipStyle();
163+
style.addProperty("name", null);
164+
fail();
165+
} catch (IllegalArgumentException e) {
166+
assertEquals("A property value must be specified.", e.getMessage());
167+
}
168+
}
169+
170+
@Test
171+
public void test_addProperty_ThrowsAnException_WhenTheValueIsEmpty() {
172+
try {
173+
RelationshipStyle style = new RelationshipStyle();
174+
style.addProperty("name", " ");
175+
fail();
176+
} catch (IllegalArgumentException e) {
177+
assertEquals("A property value must be specified.", e.getMessage());
178+
}
179+
}
180+
181+
@Test
182+
public void test_addProperty_AddsTheProperty_WhenANameAndValueAreSpecified() {
183+
RelationshipStyle style = new RelationshipStyle();
184+
style.addProperty("name", "value");
185+
assertEquals("value", style.getProperties().get("name"));
186+
}
187+
188+
@Test
189+
public void test_setProperties_DoesNothing_WhenNullIsSpecified() {
190+
RelationshipStyle style = new RelationshipStyle();
191+
style.setProperties(null);
192+
assertEquals(0, style.getProperties().size());
193+
}
194+
195+
@Test
196+
public void test_setProperties_SetsTheProperties_WhenANonEmptyMapIsSpecified() {
197+
RelationshipStyle style = new RelationshipStyle();
198+
Map<String, String> properties = new HashMap<>();
199+
properties.put("name", "value");
200+
style.setProperties(properties);
201+
assertEquals(1, style.getProperties().size());
202+
assertEquals("value", style.getProperties().get("name"));
203+
}
204+
129205
}

0 commit comments

Comments
 (0)