File tree Expand file tree Collapse file tree 3 files changed +46
-12
lines changed
main/java/com/structurizr/dsl
test/java/com/structurizr/dsl Expand file tree Collapse file tree 3 files changed +46
-12
lines changed Original file line number Diff line number Diff line change 44import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
55import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
66import org .apache .hc .client5 .http .impl .classic .HttpClients ;
7- import org .apache .hc .core5 .http .ContentType ;
8- import org .apache .hc .core5 .http .Header ;
97import org .apache .hc .core5 .http .io .entity .EntityUtils ;
108
11- import java .util .regex .Pattern ;
12-
139abstract class AbstractParser {
1410
1511 private static final int HTTP_OK_STATUS = 200 ;
1612
17- private static final Pattern VIEW_KEY_PATTERN = Pattern .compile ("[\\ w-]+" );
18-
19- void validateViewKey (String key ) {
20- if (!VIEW_KEY_PATTERN .matcher (key ).matches ()) {
21- throw new RuntimeException ("View keys can only contain the following characters: a-zA-0-9_-" );
22- }
23- }
24-
2513 String removeNonWordCharacters (String name ) {
2614 return name .replaceAll ("\\ W" , "" );
2715 }
Original file line number Diff line number Diff line change 11package com .structurizr .dsl ;
22
3+ import java .util .regex .Pattern ;
4+
35abstract class AbstractViewParser extends AbstractParser {
6+
7+ private static final String PERMITTED_CHARACTERS_IN_VIEW_KEY = "a-zA-Z0-9_-" ;
8+ private static final Pattern VIEW_KEY_PATTERN = Pattern .compile ("[" + PERMITTED_CHARACTERS_IN_VIEW_KEY + "]+" );
9+
10+ void validateViewKey (String key ) {
11+ if (!VIEW_KEY_PATTERN .matcher (key ).matches ()) {
12+ throw new RuntimeException ("View keys can only contain the following characters: a-zA-Z0-9_-" );
13+ }
14+ }
15+
416}
Original file line number Diff line number Diff line change 1+ package com .structurizr .dsl ;
2+
3+ import org .junit .jupiter .api .Test ;
4+
5+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6+ import static org .junit .jupiter .api .Assertions .fail ;
7+
8+ class AbstractViewParserTests {
9+
10+ private final AbstractViewParser parser = new SystemLandscapeViewParser ();
11+
12+ @ Test
13+ void test_validateViewKey () {
14+ parser .validateViewKey ("key" );
15+ parser .validateViewKey ("key123" );
16+ parser .validateViewKey ("Key123" );
17+ parser .validateViewKey ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" );
18+
19+ try {
20+ parser .validateViewKey ("abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/0123456789" );
21+ fail ();
22+ } catch (Exception e ) {
23+ assertEquals ("View keys can only contain the following characters: a-zA-Z0-9_-" , e .getMessage ());
24+ }
25+
26+ try {
27+ parser .validateViewKey ("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789" );
28+ fail ();
29+ } catch (Exception e ) {
30+ assertEquals ("View keys can only contain the following characters: a-zA-Z0-9_-" , e .getMessage ());
31+ }
32+ }
33+
34+ }
You can’t perform that action at this time.
0 commit comments