From b75cf0b7cf0c839db8040d8bb483dce4ec345a56 Mon Sep 17 00:00:00 2001 From: Maxime Sinclair Date: Tue, 30 Aug 2011 16:39:12 +0200 Subject: [PATCH] Better support of ditaa diagrams. --- .../plantuml/servlet/UmlDiagramService.java | 25 ++++++++++++------- .../plantuml/servlet/TestForm.java | 24 +++++++++++++++++- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/net/sourceforge/plantuml/servlet/UmlDiagramService.java b/src/net/sourceforge/plantuml/servlet/UmlDiagramService.java index 5051aaa..1da4fff 100644 --- a/src/net/sourceforge/plantuml/servlet/UmlDiagramService.java +++ b/src/net/sourceforge/plantuml/servlet/UmlDiagramService.java @@ -47,21 +47,28 @@ public abstract class UmlDiagramService extends HttpServlet { throws IOException, ServletException { // build the UML source from the compressed request parameter - String text = URLDecoder.decode( getSource( request.getRequestURI()), "UTF-8"); + String text = URLDecoder.decode( getSource(request.getRequestURI()), "UTF-8"); Transcoder transcoder = getTranscoder(); text = transcoder.decode(text); - StringBuilder plantUmlSource = new StringBuilder(); - plantUmlSource.append("@startuml\n"); - plantUmlSource.append( text); - if (text.endsWith("\n") == false) { - plantUmlSource.append("\n"); + + // encapsulate the UML syntax if necessary + String uml; + if (text.startsWith("@start")) { + uml = text; + } else { + StringBuilder plantUmlSource = new StringBuilder(); + plantUmlSource.append( "@startuml\n"); + plantUmlSource.append( text); + if (text.endsWith( "\n") == false) { + plantUmlSource.append( "\n"); + } + plantUmlSource.append( "@enduml"); + uml = plantUmlSource.toString(); } - plantUmlSource.append("@enduml"); - final String uml = plantUmlSource.toString(); // generate the response DiagramResponse dr = new DiagramResponse( response, getOutputFormat()); - dr.sendDiagram( uml); + dr.sendDiagram(uml); dr = null; } diff --git a/test/src/net/sourceforge/plantuml/servlet/TestForm.java b/test/src/net/sourceforge/plantuml/servlet/TestForm.java index d8811dd..8c5e69b 100644 --- a/test/src/net/sourceforge/plantuml/servlet/TestForm.java +++ b/test/src/net/sourceforge/plantuml/servlet/TestForm.java @@ -68,7 +68,7 @@ public class TestForm extends TestCase { } /** - * Verifies that when the encoded URL is empty, the default image is generated + * Verifies that when the encoded URL is empty, no image is generated */ public void testEmptyUrl() throws Exception { WebConversation conversation = new WebConversation(); @@ -88,5 +88,27 @@ public class TestForm extends TestCase { // Ensure there is no image assertEquals( 0, response.getImages().length); } + + /** + * Verifies that a ditaa diagram is generated + */ + public void testDitaaText() throws Exception { + WebConversation conversation = new WebConversation(); + // Fill the form and submit it + WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl() ); + WebResponse response = TestUtils.tryGetResponse(conversation, request ); + WebForm formDitaaText = response.getForms()[0]; + formDitaaText.setParameter("text", "@startditaa \n*--> \n@endditaa"); + response = formDitaaText.submit(); + // Analyze response + WebForm forms[] = response.getForms(); + assertEquals( 2, forms.length ); + // Ensure the Text field is correct + assertTrue( forms[0].getParameterValue("text").startsWith( "@startditaa")); + // Ensure the URL field is correct + assertTrue( forms[1].getParameterValue("url").endsWith("/img/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00")); + // Ensure the image is present + assertEquals( 1, response.getImages().length); + } }