1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-12-22 08:48:54 +00:00

update junit test classes and there dependencies

This commit is contained in:
Florian 2021-10-11 12:38:37 +02:00 committed by arnaudroques
parent 221af78afc
commit 8d5be87f03
20 changed files with 700 additions and 469 deletions

22
pom.xml
View File

@ -84,7 +84,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<skipTests>true</skipTests>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
<plugin>
@ -174,6 +174,7 @@
<maven.build.timestamp.format>yyyyMMdd-HHmm
</maven.build.timestamp.format>
<timestamp>${maven.build.timestamp}</timestamp>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
@ -191,11 +192,6 @@
<artifactId>codemirror</artifactId>
<version>3.21</version>
</dependency>
<dependency>
<groupId>HTTPClient</groupId>
<artifactId>HTTPClient</artifactId>
<version>0.3-3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
@ -205,19 +201,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>httpunit</groupId>
<artifactId>httpunit</artifactId>
<version>1.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<version>1.7R2</version>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.53.0</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -23,23 +23,19 @@
*/
package net.sourceforge.plantuml.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
@ -52,54 +48,50 @@ import net.sourceforge.plantuml.SourceStringReader;
@SuppressWarnings("serial")
public class OldProxyServlet extends HttpServlet {
private static final Pattern PROXY_PATTERN = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private static final Pattern PROXY_PATTERN = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(https?://.*)");
private String format;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String uri = request.getRequestURI();
// Check if the src URL is valid
Matcher proxyMatcher = PROXY_PATTERN.matcher(uri);
if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
} else {
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "ERROR Invalid proxy syntax : " + uri);
request.removeAttribute("net.sourceforge.plantuml.servlet.encoded");
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
if (!proxyMatcher.matches()) {
// Bad URI format.
response.setStatus(400);
return;
}
String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
}
private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException {
SourceStringReader reader = new SourceStringReader(getSource(source));
int n = num == null ? 0 : Integer.parseInt(num);
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat(), false));
FileFormat fileFormat = getOutputFormat();
response.addHeader("Content-Type", fileFormat.getMimeType());
reader.outputImage(response.getOutputStream(), n, new FileFormatOption(fileFormat, false));
}
private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(uri);
if (m.find() == false) {
throw new IOException(uri);
}
private String getSource(final String uri) throws IOException {
final URL url = new URL(uri);
final HTTPConnection httpConnection = new HTTPConnection(url);
try {
final HTTPResponse resp = httpConnection.Get(m.group(1));
return resp.getText();
} catch (ModuleException e) {
throw new IOException(e.toString());
} catch (ParseException e) {
throw new IOException(e.toString());
try (
InputStream responseStream = url.openStream();
InputStreamReader isr = new InputStreamReader(responseStream);
BufferedReader br = new BufferedReader(isr);
) {
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString().trim();
}
}

View File

@ -76,6 +76,7 @@ public class ProxyServlet extends HttpServlet {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
response.setStatus(400);
return;
}

View File

@ -33,7 +33,7 @@
<!-- <script src="mode/plantuml.js"></script> -->
<script>
window.onload = function() {
var myCodeMirror = CodeMirror.fromTextArea(
document.myCodeMirror = CodeMirror.fromTextArea(
document.getElementById("text"),
{lineNumbers: true}
);

View File

@ -1,25 +1,27 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class TestAsciiArt extends WebappTestCase {
/**
* Verifies the generation of the ascii art for the Bob -> Alice sample
*/
public void testSimpleSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "txt/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/txt/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals("Response character set is not UTF-8", "UTF-8", response.getCharacterSet());
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
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 200);
assertTrue(diagramLen < 250);

View File

@ -1,24 +1,27 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class TestCharset extends WebappTestCase {
/**
* Verifies the preservation of unicode characters for the "Bob -> Alice : hell‽" sample
*/
public void testUnicodeSupport() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "txt/SyfFKj2rKt3CoKnELR1Io4ZDoNdKi1S0");
WebResponse response = conversation.getResource(request);
public void testUnicodeSupport() throws IOException {
final URL url = new URL(getServerUrl() + "/txt/SyfFKj2rKt3CoKnELR1Io4ZDoNdKi1S0");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify that the interrobang unicode character is present
String diagram = response.getText();
String diagram = getContentText(conn);
assertTrue("Interrobang unicode character is not preserved", diagram.contains(""));
}
@ -26,18 +29,22 @@ public class TestCharset extends WebappTestCase {
* Verifies the preservation of unicode characters for the
* "participant Bob [[http://www.snow.com/❄]]\nBob -> Alice" sample
*/
public void testUnicodeInCMap() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzShpiilrqlEpzL_DBSbDfOB9Azhf-2OavcS2W00");
WebResponse response = conversation.getResource(request);
public void testUnicodeInCMap() throws IOException {
final URL url = new URL(
getServerUrl() +
"/map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzShpiilrqlEpzL_DBSbDfOB9Azhf-2OavcS2W00"
);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify that the snow unicode character is present
String map = response.getText();
String map = getContentText(conn);
assertTrue("Snow unicode character is not preserved", map.contains(""));
}
}

View File

@ -1,28 +1,32 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class TestCheck extends WebappTestCase {
/**
* Verifies the generation of a syntax check for the following sample:
* Bob -> Alice : hello
*/
public void testCorrectSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "check/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testCorrectSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/check/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals("Response character set is not UTF-8", "UTF-8", response.getCharacterSet());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content, check its first characters and verify its size
String checkResult = response.getText();
assertTrue("Response content is not starting with (2 participants)",
checkResult.startsWith("(2 participants)"));
String checkResult = getContentText(conn);
assertTrue(
"Response content is not starting with (2 participants)",
checkResult.startsWith("(2 participants)")
);
int checkLen = checkResult.length();
assertTrue(checkLen > 1);
assertTrue(checkLen < 100);
@ -32,12 +36,10 @@ public class TestCheck extends WebappTestCase {
* Check the syntax of an invalid sequence diagram :
* Bob -
*/
public void testWrongDiagramSyntax() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "check/SyfFKj050000");
WebResponse response = conversation.getResource(request);
public void testWrongDiagramSyntax() throws IOException {
final URL url = new URL(getServerUrl() + "/check/SyfFKj050000");
// Analyze response
String checkResult = response.getText();
String checkResult = getContentText(url);
assertTrue("Response is not an error", checkResult.startsWith("(Error)"));
}

View File

@ -1,27 +1,30 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class TestEPS extends WebappTestCase {
/**
* Verifies the generation of the EPS for the Bob -> Alice sample
*/
public void testSimpleSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "eps/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/eps/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not EPS", "application/postscript", response.getContentType());
assertEquals(
"Response content type is not EPS",
"application/postscript",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 10000);
assertTrue(diagramLen < 12000);
}
}

View File

@ -1,155 +1,219 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import static org.junit.Assert.assertNotEquals;
import java.io.IOException;
import java.util.List;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextArea;
public class TestForm extends WebappTestCase {
/**
* Verifies that the welcome page has exactly two form with the Bob --> Alice sample
*/
public void testWelcomePage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = conversation.getResponse(request);
// Analyze response
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
assertEquals("url", forms[1].getParameterNames()[0]);
assertTrue(forms[1].getParameterValue("url").endsWith("/png/" + TestUtils.SEQBOB));
// Ensure the generated image is present
assertNotNull(response.getImageWithAltText("PlantUML diagram"));
public void testWelcomePage() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
// Analyze response
List<HtmlForm> forms = page.getForms();
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);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/" + TestUtils.SEQBOB));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 131
assertNotEquals(0, img.getImageReader().getWidth(0)); // 120
}
}
/**
* Verifies that the version image is generated
*/
public void testVersion() throws Exception {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = conversation.getResponse(request);
WebForm formUMLText = response.getForms()[0];
formUMLText.setParameter("text", "version");
response = formUMLText.submit();
// Analyze response
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
// Ensure the Text field is correct
assertEquals("version", forms[0].getParameterValue("text"));
// Ensure the URL field is correct
assertTrue(forms[1].getParameterValue("url").endsWith("/png/" + TestUtils.VERSION));
// Ensure the image is present
assertNotNull(response.getImageWithAltText("PlantUML diagram"));
public void testVersion() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
page.initialize();
// Fill the form and submit it
page.executeJavaScript("document.myCodeMirror.setValue('version')");
HtmlForm form = page.getForms().get(0);
HtmlSubmitInput btn = form.getFirstByXPath("//input[contains(@type, 'submit')]");
page = btn.click();
// Analyze response
List<HtmlForm> forms = page.getForms();
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);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/" + TestUtils.VERSION));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 186
assertNotEquals(0, img.getImageReader().getWidth(0)); // 519
}
}
/**
* Verifies that when the UML text is empty, no image is generated
* Verifies that when the UML text is empty, default page and image is generated
*/
public void testEmptyText() throws Exception {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = conversation.getResponse(request);
WebForm formUMLText = response.getForms()[0];
formUMLText.setParameter("text", "");
response = formUMLText.submit();
// Analyze response
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
// Ensure the Text field is empty
assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty
assertTrue(forms[1].getParameterValue("url").isEmpty());
// Ensure there is no image
assertNull(response.getImageWithAltText("PlantUML diagram"));
public void testEmptyText() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
page.initialize();
// Fill the form and submit it
page.executeJavaScript("document.myCodeMirror.setValue('')");
HtmlForm form = page.getForms().get(0);
HtmlSubmitInput btn = form.getFirstByXPath("//input[contains(@type, 'submit')]");
page = btn.click();
// Analyze response
List<HtmlForm> forms = page.getForms();
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);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/" + TestUtils.SEQBOB));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 131
assertNotEquals(0, img.getImageReader().getWidth(0)); // 120
}
}
/**
* Verifies that when the encoded URL is empty, no image is generated
* Verifies that when the encoded URL is empty, default page and image is generated
*/
public void testEmptyUrl() throws Exception {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = conversation.getResponse(request);
WebForm formUrl = response.getForms()[1];
formUrl.setParameter("url", "");
response = formUrl.submit();
// Analyze response
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
// Ensure the Text field is empty
assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty
assertTrue(forms[1].getParameterValue("url").isEmpty());
// Ensure there is no image
assertNull(response.getImageWithAltText("PlantUML diagram"));
public void testEmptyUrl() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
page.initialize();
// Fill the form and submit it
List<HtmlForm> forms = page.getForms();
HtmlInput url = forms.get(1).getInputByName("url");
url.setAttribute("value", "");
HtmlSubmitInput btn = forms.get(1).getFirstByXPath("//input[contains(@type, 'submit')]");
page = btn.click();
// Analyze response
forms = page.getForms();
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);
// Ensure the URL field is correct
url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/" + TestUtils.SEQBOB));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 131
assertNotEquals(0, img.getImageReader().getWidth(0)); // 120
}
}
/**
* 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(getServerUrl());
WebResponse response = conversation.getResponse(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("/png/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00"));
// Ensure the image is present
assertNotNull(response.getImageWithAltText("PlantUML diagram"));
public void testDitaaText() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
page.initialize();
// Fill the form and submit it
page.executeJavaScript("document.myCodeMirror.setValue(`@startditaa \n*--> \n@endditaa`)");
HtmlForm form = page.getForms().get(0);
HtmlSubmitInput btn = form.getFirstByXPath("//input[contains(@type, 'submit')]");
page = btn.click();
// Analyze response
List<HtmlForm> forms = page.getForms();
assertEquals(2, forms.size());
// Ensure the Text field is correct
String text = ((HtmlTextArea)(forms.get(0).getFirstByXPath("//textarea[contains(@name, 'text')]"))).getTextContent();
assertEquals("@startditaa \n*--> \n@endditaa", text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00"));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 70
assertNotEquals(0, img.getImageReader().getWidth(0)); // 90
}
}
/**
* Verifies that an image map is produced if the diagram contains a link
*/
public void testImageMap() throws Exception {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = conversation.getResponse(request);
WebForm formText = response.getForms()[0];
formText.setParameter("text", "@startuml \nBob -> Alice : [[http://yahoo.com]] Hello \n@enduml");
response = formText.submit();
// Analyze response
// Ensure the generated image is present
assertNotNull(response.getImageWithAltText("PlantUML diagram"));
// Ensure the image map is present
HTMLElement[] maps = response.getElementsByTagName("map");
assertEquals(1, maps.length);
public void testImageMap() throws IOException {
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage(getServerUrl());
page.initialize();
// Fill the form and submit it
page.executeJavaScript("document.myCodeMirror.setValue(`@startuml\nBob -> Alice : [[http://yahoo.com]] Hello\n@enduml`)");
HtmlForm form = page.getForms().get(0);
HtmlSubmitInput btn = form.getFirstByXPath("//input[contains(@type, 'submit')]");
page = btn.click();
// Analyze response
List<HtmlForm> forms = page.getForms();
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 : [[http://yahoo.com]] Hello\n@enduml", text);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/SyfFKj2rKt3CoKnELR1IY8xEA2afiDBNhqpCoC_NIyxFZOrLy4ZDoSa70000"));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 131
assertNotEquals(0, img.getImageReader().getWidth(0)); // 231
// TODO: Ensure the image map is present
//DomElement map = page.getElementById("plantuml_map");
//assertNotNull(map);
//assertEquals(1, map.getChildElementCount());
}
}
/**
* Verifies that when the encoded source is specified as an URL parameter
* the diagram is displayed and the source is decoded
*/
public void testUrlParameter() throws Exception {
WebConversation conversation = new WebConversation();
// Submit the request with a url parameter
WebRequest request = new GetMethodWebRequest(getServerUrl() + "form?url=" + TestUtils.SEQBOB);
WebResponse response = conversation.getResponse(request);
// Analyze response
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
// Ensure the Text field is filled
assertEquals(forms[0].getParameterValue("text"), "@startuml\nBob -> Alice : hello\n@enduml");
// Ensure the URL field is filled
assertEquals(forms[1].getParameterValue("url"), getServerUrl() + "png/" + TestUtils.SEQBOB);
// Ensure the image is present
assertNotNull(response.getImageWithAltText("PlantUML diagram"));
public void testUrlParameter() throws IOException {
try (final WebClient webClient = new WebClient()) {
// Submit the request with a url parameter
HtmlPage page = webClient.getPage(getServerUrl() + "/form?url=" + TestUtils.SEQBOB);
page.initialize();
// Analyze response
List<HtmlForm> forms = page.getForms();
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);
// Ensure the URL field is correct
HtmlInput url = forms.get(1).getInputByName("url");
assertNotNull(url);
assertTrue(url.getAttribute("value").endsWith("/png/" + TestUtils.SEQBOB));
// Ensure the generated image is present
HtmlImage img = page.getFirstByXPath("//img[contains(@alt, 'PlantUML diagram')]");
assertNotEquals(0, img.getImageReader().getHeight(0)); // 131
assertNotEquals(0, img.getImageReader().getWidth(0)); // 120
}
}
}

View File

@ -1,84 +1,76 @@
package net.sourceforge.plantuml.servlet;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestImage extends WebappTestCase {
/**
* Verifies the generation of the version image from an encoded URL
*/
public void testVersionImage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "png/" + TestUtils.VERSION);
WebResponse response = conversation.getResource(request);
public void testVersionImage() throws IOException {
final URL url = new URL(getServerUrl() + "/png/" + TestUtils.VERSION);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
assertEquals(
"Response content type is not PNG",
"image/png",
conn.getContentType().toLowerCase()
);
// Get the image and verify its size
InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
}
imageStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
byte[] inMemoryImage = getContentAsBytes(conn);
int diagramLen = inMemoryImage.length;
assertTrue(diagramLen > 10000);
assertTrue(diagramLen < 20000);
responseStream.close();
}
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testDiagramHttpHeader() throws Exception {
WebConversation conversation = new WebConversation();
// Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "png/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testDiagramHttpHeader() throws IOException, ParseException {
final URL url = new URL(getServerUrl() + "/png/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
assertEquals(
"Response content type is not PNG",
"image/png",
conn.getContentType().toLowerCase()
);
// Verifies the availability of the Expires entry in the response header
assertNotNull(response.getHeaderField("Expires"));
assertNotNull(conn.getHeaderField("Expires"));
// Verifies the availability of the Last-Modified entry in the response header
assertNotNull(response.getHeaderField("Last-Modified"));
assertNotNull(conn.getHeaderField("Last-Modified"));
// Verifies the Last-Modified value is in the past
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ", Locale.ENGLISH);
Date lastModified = format.parse(response.getHeaderField("Last-Modified"));
Date lastModified = format.parse(conn.getHeaderField("Last-Modified"));
assertTrue("Last-Modified is not in the past", lastModified.before(new Date()));
// Consume the response
InputStream responseStream = response.getInputStream();
while (responseStream.read() != -1) {
// Do nothing
}
// Consume the response but do nothing with it
getContentAsBytes(conn);
}
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testOldImgURL() throws Exception {
WebConversation conversation = new WebConversation();
// Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testOldImgURL() throws IOException {
final URL url = new URL(getServerUrl() + "/img/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
// Consume the response
InputStream responseStream = response.getInputStream();
while (responseStream.read() != -1) {
// Do nothing
}
assertEquals(
"Response content type is not PNG",
"image/png",
conn.getContentType().toLowerCase()
);
// Consume the response but do nothing with it
getContentAsBytes(conn);
}
}

View File

@ -1,11 +1,8 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
public class TestLanguage extends WebappTestCase {
@ -13,10 +10,8 @@ public class TestLanguage extends WebappTestCase {
* Tests that the language for the current PlantUML server can be obtained through HTTP
*/
public void testRetrieveLanguage() throws IOException {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "/language");
WebResponse response = conversation.getResource(request);
String languageText = response.getText();
final URL url = new URL(getServerUrl() + "/language");
String languageText = getContentText(url);
assertTrue("Language contains @startuml", languageText.indexOf("@startuml") > 0);
}

View File

@ -1,29 +1,41 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class TestMap extends WebappTestCase {
/**
* Verifies the generation of the MAP for the following sample:
*
* participant Bob [[http://www.yahoo.com]]
* Bob -> Alice : [[http://www.google.com]] hello
*/
public void testSimpleSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzUhJCp8pzTBpi-DZUK2IUhQAJZcP2QdAbYXgalFpq_FIOKeLCX8pSd91m00");
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(
getServerUrl() +
"/map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzUhJCp8pzTBpi-DZUK2IUhQAJZcP2QdAbYXgalFpq_FIOKeLCX8pSd91m00"
);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals("Response character set is not UTF-8", "UTF-8", response.getCharacterSet());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content, check its first characters and verify its size
String diagram = response.getText();
assertTrue("Response content is not starting with <area", diagram.startsWith("<area"));
String diagram = getContentText(conn);
assertTrue(
"Response content is not starting with <map",
diagram.startsWith("<map")
);
assertTrue(
"Response content (2. line) is not starting with <area",
diagram.split("\\n", 2)[1].startsWith("<area")
);
int diagramLen = diagram.length();
assertTrue(diagramLen > 200);
assertTrue(diagramLen < 300);
@ -33,33 +45,43 @@ public class TestMap extends WebappTestCase {
* Check the content of the MAP for the sequence diagram sample
* Verify structure of the area tags
*/
public void testSequenceDiagramContent() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzUhJCp8pzTBpi-DZUK2IUhQAJZcP2QdAbYXgalFpq_FIOKeLCX8pSd91m00");
WebResponse response = conversation.getResource(request);
public void testSequenceDiagramContent() throws IOException {
final URL url = new URL(
getServerUrl() +
"/map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzUhJCp8pzTBpi-DZUK2IUhQAJZcP2QdAbYXgalFpq_FIOKeLCX8pSd91m00"
);
// Analyze response
// Get the data contained in the XML
String map = response.getText();
assertTrue("Response is not a list of tags", map.matches("(<([^<>]+)>)+"));
assertTrue("Response doesn't contain the area structure",
map.matches(".*(area shape=\".+\" id=\".+\" href=\".+\").*"));
String map = getContentText(url);
// Verify shape:
// <map id="..." name="...">
// <area shape="..." id="..." href="..." ... />
// <area shape="..." id="..." href="..." ... />
// </map>
assertTrue(
"Response doesn't match shape",
map.matches("^<map id=\".+\" name=\".+\">\n(<area shape=\".+\" id=\".+\" href=\".+\".*/>\n){2}</map>\n*$")
);
}
/**
* Check the empty MAP of a sequence diagram without link
* This test uses the simple Bob -> Alice
*/
public void testSequenceDiagramWithoutLink() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "map/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSequenceDiagramWithoutLink() throws IOException {
final URL url = new URL(getServerUrl() + "/map/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
// Get the content, check it's an empty response
String diagram = response.getText();
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the data contained in the XML
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertEquals(0, diagramLen);
}
}

View File

@ -1,52 +1,45 @@
package net.sourceforge.plantuml.servlet;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestOldProxy extends WebappTestCase {
/**
* Verifies the proxified reception of the default Bob and Alice diagram
*/
public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/" + getServerUrl()
+ "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
public void testDefaultProxy() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy/" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
// assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
assertEquals(
"Response content type is not PNG",
"image/png",
conn.getContentType().toLowerCase()
);
// Get the image and verify its size (~2000 bytes)
InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
}
imageStream.close();
responseStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
byte[] inMemoryImage = getContentAsBytes(conn);
int diagramLen = inMemoryImage.length;
assertTrue(diagramLen > 1500);
assertTrue(diagramLen < 2500);
assertTrue(diagramLen > 2000);
assertTrue(diagramLen < 3000);
}
public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" + getServerUrl()
+ "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
public void testProxyWithFormat() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy/svg/" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
// TODO assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType());
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
@ -55,15 +48,22 @@ public class TestOldProxy extends WebappTestCase {
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testInvalidUrl() throws Exception {
WebConversation conversation = new WebConversation();
// Try to proxify an invalid address
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL");
WebResponse response = conversation.getResource(request);
// Analyze response, it must be the empty form
// Verifies the Content-Type header
assertEquals("Response content type is not HTML", "text/html", response.getContentType());
WebForm[] forms = response.getForms();
assertEquals(2, forms.length);
public void testInvalidUrl() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy/invalidURL");
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Analyze response
// Get the content and verify its size
assertEquals(
"Response is not empty",
0,
conn.getContentLength()
);
// Check if status code is 400
assertEquals(
"Bad HTTP status received",
400,
conn.getResponseCode()
);
}
}

View File

@ -1,65 +1,105 @@
package net.sourceforge.plantuml.servlet;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
public class TestProxy extends WebappTestCase {
/**
* Verifies the proxified reception of the default Bob and Alice diagram
*/
public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "proxy?src=" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
public void testDefaultProxy() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy?src=" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not PNG", "image/png", response.getContentType());
assertEquals(
"Response content type is not PNG",
"image/png",
conn.getContentType().toLowerCase()
);
// Get the image and verify its size (~2000 bytes)
InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
}
imageStream.close();
responseStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
byte[] inMemoryImage = getContentAsBytes(conn);
int diagramLen = inMemoryImage.length;
assertTrue(diagramLen > 1500);
assertTrue(diagramLen < 2500);
assertTrue(diagramLen > 2000);
assertTrue(diagramLen < 3000);
}
/*
public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "proxy?format=svg&src=" + getServerUrl() + "resource/test2diagrams.txt");
WebResponse response = conversation.getResource(request);
/**
* Verifies the proxified reception of the default Bob and Alice diagram with defined format.
*/
public void testProxyWithFormat() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy?fmt=svg&src=" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType());
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen > 2000);
assertTrue(diagramLen < 3000);
}
/**
* Verifies the proxified reception of the default Bob and Alice diagram with defined format and format (idx=0).
*/
public void testProxyWithFormatIdx0() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy?fmt=svg&idx=0&src=" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 2000);
assertTrue(diagramLen < 3000);
}
/**
* Verifies the proxified reception of the default Bob and Alice diagram with defined format and format (idx=1).
*/
public void testProxyWithFormatIdx1() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy?fmt=svg&idx=1&src=" + getServerUrl() + "/resource/test2diagrams.txt");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 5000);
assertTrue(diagramLen < 6000);
}
/**
* Verifies that the HTTP header of a diagram incites the browser to cache it.
*/
public void testInvalidUrl() throws Exception {
WebConversation conversation = new WebConversation();
// Try to proxify an invalid address
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy?src=invalidURL");
WebResponse response = conversation.getResource(request);
// Analyze response, it must be HTTP error 500
//assertEquals("Bad HTTP status received", 500, response.getResponseCode());
public void testInvalidUrl() throws IOException {
final URL url = new URL(getServerUrl() + "/proxy?src=invalidURL");
final HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// Analyze response, it must be HTTP error 400
assertEquals(
"Bad HTTP status received",
400,
conn.getResponseCode()
);
}
}

View File

@ -1,28 +1,31 @@
package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class TestSVG extends WebappTestCase {
/**
* 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);
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/svg/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not SVG", "image/svg+xml", response.getContentType());
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
@ -31,24 +34,31 @@ public class TestSVG extends WebappTestCase {
/**
* 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());
public void testPostedSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/svg");
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("@startuml\nBob -> Alice\n@enduml");
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 SVG", "image/svg+xml", response.getContentType());
assertEquals(
"Response content type is not SVG",
"image/svg+xml",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn.getInputStream());
int diagramLen = diagram.length();
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
@ -57,40 +67,51 @@ public class TestSVG extends WebappTestCase {
/**
* 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());
public void testPostedInvalidSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/svg");
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("@startuml\n[Bob\n@enduml");
writer.flush();
}
// Analyze response
// HTTP response 400
assertEquals(
"Bad HTTP status received",
400,
conn.getResponseCode()
);
}
/**
* Check the content of the SVG
*/
public void testSequenceDiagramContent() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "svg/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSequenceDiagramContent() throws IOException {
final URL url = new URL(getServerUrl() + "/svg/" + TestUtils.SEQBOB);
// Analyze response
// Get the data contained in the XML
Scanner s = new Scanner(response.getInputStream()).useDelimiter("(<([^<>]+)>)+");
String token;
int bobCounter = 0, aliceCounter = 0;
while (s.hasNext()) {
token = s.next();
if (token.startsWith("Bob")) {
bobCounter++;
}
if (token.startsWith("Alice")) {
aliceCounter++;
try (
final InputStream responseStream = url.openStream();
final Scanner scanner = new Scanner(responseStream).useDelimiter("(<([^<>]+)>)+")
) {
String token;
int bobCounter = 0;
int aliceCounter = 0;
while (scanner.hasNext()) {
token = scanner.next();
if (token.startsWith("Bob")) {
bobCounter++;
}
if (token.startsWith("Alice")) {
aliceCounter++;
}
}
assertTrue(bobCounter == 2);
assertTrue(aliceCounter == 2);
}
assertTrue(bobCounter == 2);
assertTrue(aliceCounter == 2);
}
}

View File

@ -4,7 +4,7 @@ package net.sourceforge.plantuml.servlet;
/**
* Utility class for the unit tests
*/
public class TestUtils {
public abstract class TestUtils {
/*
* Theses strings are the compressed form of a PlantUML diagram.
@ -20,8 +20,4 @@ public class TestUtils {
*/
public static final String SEQBOB = "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000";
protected TestUtils() {
// prevents calls from subclass
throw new UnsupportedOperationException();
}
}

View File

@ -1,22 +1,47 @@
package net.sourceforge.plantuml.servlet;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import junit.framework.TestCase;
import net.sourceforge.plantuml.servlet.server.EmbeddedJettyServer;
import net.sourceforge.plantuml.servlet.server.ExternalServer;
import net.sourceforge.plantuml.servlet.server.ServerUtils;
public abstract class WebappTestCase extends TestCase {
private ServerUtils serverUtils;
private final ServerUtils serverUtils;
public WebappTestCase() {
super();
this(null);
}
public WebappTestCase(String name) {
super(name);
String uri = System.getProperty("system.test.server", "");
//uri = "http://localhost:8080/plantuml";
if (!uri.isEmpty()) {
// mvn test -DskipTests=false -DargLine="-Dsystem.test.server=http://localhost:8080/plantuml"
System.out.println("Test against external server: " + uri);
serverUtils = new ExternalServer(uri);
return;
}
// mvn test -DskipTests=false
System.out.println("Test against embedded jetty server.");
serverUtils = new EmbeddedJettyServer();
}
@Override
public void setUp() throws Exception {
serverUtils = new ServerUtils(true);
serverUtils.startServer();
}
@Override
@ -28,4 +53,54 @@ public abstract class WebappTestCase extends TestCase {
return serverUtils.getServerUrl();
}
public String getContentText(final URL url) throws IOException {
try (final InputStream responseStream = url.openStream()) {
return getContentText(responseStream);
}
}
public String getContentText(final URLConnection conn) throws IOException {
try (final InputStream responseStream = conn.getInputStream()) {
return getContentText(responseStream);
}
}
public String getContentText(final InputStream stream) throws IOException {
try (
final InputStreamReader isr = new InputStreamReader(stream);
final BufferedReader br = new BufferedReader(isr);
) {
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString().trim();
}
}
public byte[] getContentAsBytes(final URL url) throws IOException {
try (final InputStream responseStream = url.openStream()) {
return getContentAsBytes(responseStream);
}
}
public byte[] getContentAsBytes(final URLConnection conn) throws IOException {
try (final InputStream responseStream = conn.getInputStream()) {
return getContentAsBytes(responseStream);
}
}
public byte[] getContentAsBytes(final InputStream stream) throws IOException {
try (final ByteArrayOutputStream byteStream = new ByteArrayOutputStream()) {
byte[] buf = new byte[1024];
int n = 0;
while ((n = stream.read(buf)) != -1) {
byteStream.write(buf, 0, n);
}
return byteStream.toByteArray();
}
}
}

View File

@ -1,4 +1,4 @@
package net.sourceforge.plantuml.servlet;
package net.sourceforge.plantuml.servlet.server;
import java.net.InetSocketAddress;
@ -6,20 +6,14 @@ import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class ServerUtils {
public class EmbeddedJettyServer implements ServerUtils {
private Server server;
public ServerUtils(boolean start) throws Exception {
public EmbeddedJettyServer() {
server = new Server(new InetSocketAddress("127.0.0.1", 0));
server.addBean(new WebAppContext(server, "src/main/webapp", "/plantuml"));
if (start) {
startServer();
}
}
public ServerUtils() throws Exception {
this(false);
}
public void startServer() throws Exception {
@ -32,7 +26,7 @@ public class ServerUtils {
public String getServerUrl() {
Connector connector = server.getConnectors()[0];
return String.format("http://%s:%d/plantuml/", connector.getHost(), connector.getLocalPort());
return String.format("http://%s:%d/plantuml", connector.getHost(), connector.getLocalPort());
}
}

View File

@ -0,0 +1,23 @@
package net.sourceforge.plantuml.servlet.server;
public class ExternalServer implements ServerUtils {
private final String uri;
public ExternalServer(String uri) {
this.uri = uri;
}
@Override
public void startServer() throws Exception { }
@Override
public void stopServer() throws Exception { }
@Override
public String getServerUrl() {
return this.uri;
}
}

View File

@ -0,0 +1,12 @@
package net.sourceforge.plantuml.servlet.server;
public interface ServerUtils {
public void startServer() throws Exception;
public void stopServer() throws Exception;
public abstract String getServerUrl();
}