add servlet to encode and decode diagrams

.
This commit is contained in:
Florian
2023-04-17 16:20:38 +02:00
committed by PlantUML
parent 3ef176edae
commit 763976abdd
6 changed files with 252 additions and 49 deletions

View File

@@ -0,0 +1,61 @@
package net.sourceforge.plantuml.servlet;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class TestAsciiCoder extends WebappTestCase {
/**
* Verifies the decoding for the Bob -> Alice sample
*/
public void testBobAliceSampleDiagramDecoding() throws IOException {
final URL url = new URL(getServerUrl() + "/coder/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get and verify the content
final String diagram = getContentText(conn);
assertEquals(TestUtils.SEQBOBCODE, diagram);
}
/**
* Verifies the encoding for the Bob -> Alice sample
*/
public void testBobAliceSampleDiagramEncoding() throws IOException {
final URL url = new URL(getServerUrl() + "/coder");
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-type", "text/plain");
try (final OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream())) {
writer.write(TestUtils.SEQBOBCODE);
writer.flush();
}
// Analyze response
// HTTP response 200
assertEquals(
"Bad HTTP status received",
200,
conn.getResponseCode()
);
// Verifies the Content-Type header
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
final String diagram = getContentText(conn.getInputStream());
assertEquals(TestUtils.SEQBOB, diagram);
}
}

View File

@@ -28,7 +28,7 @@ public class TestForm extends WebappTestCase {
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startuml\nBob -> Alice : hello\n@enduml", text);
assertEquals(TestUtils.SEQBOBCODE, text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
@@ -57,7 +57,7 @@ public class TestForm extends WebappTestCase {
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startuml\nversion\n@enduml", text);
assertEquals(TestUtils.VERSIONCODE, text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
@@ -86,7 +86,7 @@ public class TestForm extends WebappTestCase {
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startuml\nBob -> Alice : hello\n@enduml", text);
assertEquals(TestUtils.SEQBOBCODE, text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
@@ -116,7 +116,7 @@ public class TestForm extends WebappTestCase {
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startuml\nBob -> Alice : hello\n@enduml", text);
assertEquals(TestUtils.SEQBOBCODE, text);
// Ensure the URL field is correct
url = forms.get(1).getInputByName("url");
assertNotNull(url);
@@ -204,7 +204,7 @@ public class TestForm extends WebappTestCase {
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startuml\nBob -> Alice : hello\n@enduml", text);
assertEquals(TestUtils.SEQBOBCODE, text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);

View File

@@ -15,9 +15,19 @@ public abstract class TestUtils {
*/
public static final String VERSION = "AqijAixCpmC0";
/**
* version
*/
public static final String VERSIONCODE = "@startuml\nversion\n@enduml";
/**
* Bob -> Alice : hello
*/
public static final String SEQBOB = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000";
/**
* Bob -> Alice : hello
*/
public static final String SEQBOBCODE = "@startuml\nBob -> Alice : hello\n@enduml";
}