Skip to content

Commit

Permalink
Better error messages for image views.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbrowndotje committed Sep 11, 2024
1 parent 3fdab62 commit 43ec4c8
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void parsePlantUML(ImageViewDslContext context, File dslFile, Tokens tokens) {
if (!restricted) {
File file = new File(dslFile.getParentFile(), source);
new PlantUMLImporter().importDiagram(context.getView(), file);
} else {
throw new RuntimeException("PlantUML source must be specified as a URL when running in restricted mode");
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -84,6 +86,8 @@ void parseMermaid(ImageViewDslContext context, File dslFile, Tokens tokens) {
if (!restricted) {
File file = new File(dslFile.getParentFile(), source);
new MermaidImporter().importDiagram(context.getView(), file);
} else {
throw new RuntimeException("Mermaid source must be specified as a URL when running in restricted mode");
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -117,6 +121,8 @@ void parseKroki(ImageViewDslContext context, File dslFile, Tokens tokens) {
if (!restricted) {
File file = new File(dslFile.getParentFile(), source);
new KrokiImporter().importDiagram(context.getView(), format, file);
} else {
throw new RuntimeException("Kroki source must be specified as a URL when running in restricted mode");
}
}
} catch (Exception e) {
Expand Down Expand Up @@ -149,6 +155,8 @@ void parseImage(ImageViewDslContext context, File dslFile, Tokens tokens) {
File file = new File(dslFile.getParentFile(), source);
context.getView().setContent(ImageUtils.getImageAsDataUri(file));
context.getView().setTitle(file.getName());
} else {
throw new RuntimeException("Images must be specified as a URL when running in restricted mode");
}
}
} catch (Exception e) {
Expand Down
14 changes: 14 additions & 0 deletions structurizr-dsl/src/test/java/com/structurizr/dsl/DslTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1254,4 +1254,18 @@ void test_bulkOperations() throws Exception {
parser.parse(dslFile);
}

@Test
void test_ImageView_WhenParserIsInRestrictedMode() {
File dslFile = new File("src/test/resources/dsl/image-view.dsl");

try {
StructurizrDslParser parser = new StructurizrDslParser();
parser.setRestricted(true);
parser.parse(dslFile);
fail();
} catch (StructurizrDslParserException e) {
assertEquals("Images must be specified as a URL when running in restricted mode at line 5 of " + dslFile.getAbsolutePath() + ": image image.png", e.getMessage());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.structurizr.dsl;

import com.structurizr.view.ImageView;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class ImageViewContentParserTests extends AbstractTests {

private ImageViewContentParser parser;
private ImageView imageView;

@BeforeEach
void setUp() {
imageView = workspace.getViews().createImageView("key");
}

@Test
void test_parsePlantUML_ThrowsAnException_WhenUsingAFileNameInRestrictedMode() {
try {
parser = new ImageViewContentParser(true);
parser.parsePlantUML(new ImageViewDslContext(imageView), null, tokens("plantuml", "image.puml"));
fail();
} catch (Exception e) {
assertEquals("PlantUML source must be specified as a URL when running in restricted mode", e.getMessage());
}
}

@Test
void test_parseMermaid_ThrowsAnException_WhenUsingAFileNameInRestrictedMode() {
try {
parser = new ImageViewContentParser(true);
parser.parseMermaid(new ImageViewDslContext(imageView), null, tokens("mermaid", "image.puml"));
fail();
} catch (Exception e) {
assertEquals("Mermaid source must be specified as a URL when running in restricted mode", e.getMessage());
}
}

@Test
void test_parseKroki_ThrowsAnException_WhenUsingAFileNameInRestrictedMode() {
try {
parser = new ImageViewContentParser(true);
parser.parseKroki(new ImageViewDslContext(imageView), null, tokens("kroki", "plantuml", "image.puml"));
fail();
} catch (Exception e) {
assertEquals("Kroki source must be specified as a URL when running in restricted mode", e.getMessage());
}
}

@Test
void test_parseImage_ThrowsAnException_WhenUsingAFileNameInRestrictedMode() {
try {
parser = new ImageViewContentParser(true);
parser.parseImage(new ImageViewDslContext(imageView), null, tokens("image", "image.png"));
fail();
} catch (Exception e) {
assertEquals("Images must be specified as a URL when running in restricted mode", e.getMessage());
}
}

}
9 changes: 9 additions & 0 deletions structurizr-dsl/src/test/resources/dsl/image-view.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
workspace {

views {
image * "Image" {
image image.png
}
}

}

0 comments on commit 43ec4c8

Please sign in to comment.