1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-09-27 14:39:01 +00:00
plantuml-server/src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java

97 lines
3.6 KiB
Java
Raw Normal View History

2011-07-12 15:58:35 +00:00
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
2018-04-18 22:30:33 +00:00
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
2011-07-12 15:58:35 +00:00
2018-04-18 22:30:33 +00:00
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
2013-07-10 08:08:44 +00:00
import java.util.Scanner;
public class TestSVG extends WebappTestCase {
2011-07-12 15:58:35 +00:00
/**
* Verifies the generation of the SVG for the Bob -> Alice sample
*/
public void testSimpleSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "svg/" + TestUtils.SEQBOB);
2013-07-10 08:37:21 +00:00
WebResponse response = conversation.getResource(request);
2011-07-12 15:58:35 +00:00
// Analyze response
// Verifies the Content-Type header
2013-07-10 08:37:21 +00:00
assertEquals("Response content type is not SVG", "image/svg+xml", response.getContentType());
2011-07-12 15:58:35 +00:00
// Get the content and verify its size
String diagram = response.getText();
int diagramLen = diagram.length();
2013-07-10 08:37:21 +00:00
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
2011-07-12 15:58:35 +00:00
}
2013-07-10 08:37:21 +00:00
2018-04-18 22:30:33 +00:00
/**
* Verifies the generation of the SVG for the Bob -> Alice sample
*/
public void testPostedSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
PostMethodWebRequest request = new PostMethodWebRequest(
getServerUrl() + "svg/",
new ByteArrayInputStream("@startuml\nBob -> Alice\n@enduml".getBytes(Charset.defaultCharset())),
"text/plain");
WebResponse response = conversation.getResource(request);
assertEquals(200, response.getResponseCode());
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not SVG", "image/svg+xml", response.getContentType());
// Get the content and verify its size
String diagram = response.getText();
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
}
/**
* Verifies the generation of the SVG for the Bob -> Alice sample
*/
public void testPostedInvalidSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
PostMethodWebRequest request = new PostMethodWebRequest(
getServerUrl() + "svg/",
new ByteArrayInputStream("@startuml\n[Bob\n@enduml".getBytes(Charset.defaultCharset())),
"text/plain");
WebResponse response = conversation.getResource(request);
assertEquals(400, response.getResponseCode());
}
2013-07-10 08:08:44 +00:00
/**
* Check the content of the SVG
*/
public void testSequenceDiagramContent() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "svg/" + TestUtils.SEQBOB);
2013-07-10 08:37:21 +00:00
WebResponse response = conversation.getResource(request);
2013-07-10 08:08:44 +00:00
// Analyze response
// Get the data contained in the XML
Scanner s = new Scanner(response.getInputStream()).useDelimiter("(<([^<>]+)>)+");
String token;
int bobCounter = 0, aliceCounter = 0;
2013-07-10 08:37:21 +00:00
while (s.hasNext()) {
2013-07-10 08:08:44 +00:00
token = s.next();
if (token.startsWith("Bob")) {
bobCounter++;
}
if (token.startsWith("Alice")) {
aliceCounter++;
}
}
assertTrue(bobCounter == 2);
assertTrue(aliceCounter == 2);
}
2011-07-12 15:58:35 +00:00
}