Skip to content

Commit

Permalink
Added a way to clear styles, and some checks to ensure that multiple …
Browse files Browse the repository at this point in the history
…styles for the same tag cannot be added.
  • Loading branch information
simonbrowndotje committed Oct 29, 2019
1 parent 3dfbea5 commit c9d3aa5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
22 changes: 22 additions & 0 deletions structurizr-core/src/com/structurizr/view/Styles.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public ElementStyle addElementStyle(String tag) {
ElementStyle elementStyle = null;

if (tag != null) {
if (elements.stream().anyMatch(es -> es.getTag().equals(tag))) {
throw new IllegalArgumentException("An element style for the tag \"" + tag + "\" already exists.");
}

elementStyle = new ElementStyle();
elementStyle.setTag(tag);
add(elementStyle);
Expand All @@ -33,6 +37,20 @@ public ElementStyle addElementStyle(String tag) {
return elementStyle;
}

/**
* Removes all element styles.
*/
public void clearElementStyles() {
this.elements = new LinkedList<>();
}

/**
* Removes all relationship styles.
*/
public void clearRelationshipStyles() {
this.relationships = new LinkedList<>();
}

public Collection<RelationshipStyle> getRelationships() {
return relationships;
}
Expand All @@ -47,6 +65,10 @@ public RelationshipStyle addRelationshipStyle(String tag) {
RelationshipStyle relationshipStyle = null;

if (tag != null) {
if (relationships.stream().anyMatch(rs -> rs.getTag().equals(tag))) {
throw new IllegalArgumentException("A relationship style for the tag \"" + tag + "\" already exists.");
}

relationshipStyle = new RelationshipStyle();
relationshipStyle.setTag(tag);
add(relationshipStyle);
Expand Down
43 changes: 43 additions & 0 deletions structurizr-core/test/unit/com/structurizr/view/StyleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class StyleTests extends AbstractWorkspaceTestBase {

Expand Down Expand Up @@ -70,4 +71,46 @@ public void test_findRelationshipStyle_ReturnsTheCorrectStyle_WhenStylesAreDefin
assertEquals("#0000ff", style.getColor());
}

@Test
public void test_addElementStyle_ThrowsAnException_WhenAStyleWithTheSameTagExistsAlready() {
try {
styles.addElementStyle(Tags.SOFTWARE_SYSTEM).color("#ff0000");
styles.addElementStyle(Tags.SOFTWARE_SYSTEM).color("#ff0000");

fail();
} catch (IllegalArgumentException iae) {
assertEquals("An element style for the tag \"Software System\" already exists.", iae.getMessage());
}
}

@Test
public void test_addRelationshipStyle_ThrowsAnException_WhenAStyleWithTheSameTagExistsAlready() {
try {
styles.addRelationshipStyle(Tags.RELATIONSHIP).color("#ff0000");
styles.addRelationshipStyle(Tags.RELATIONSHIP).color("#ff0000");

fail();
} catch (IllegalArgumentException iae) {
assertEquals("A relationship style for the tag \"Relationship\" already exists.", iae.getMessage());
}
}

@Test
public void test_clearElementStyles_RemovesAllElementStyles() {
styles.addElementStyle(Tags.SOFTWARE_SYSTEM).color("#ff0000");
assertEquals(1, styles.getElements().size());

styles.clearElementStyles();
assertEquals(0, styles.getElements().size());
}

@Test
public void test_clearRelationshipStyles_RemovesAllRelationshipStyles() {
styles.addRelationshipStyle(Tags.RELATIONSHIP).color("#ff0000");
assertEquals(1, styles.getRelationships().size());

styles.clearRelationshipStyles();
assertEquals(0, styles.getRelationships().size());
}

}

0 comments on commit c9d3aa5

Please sign in to comment.