Project fully mavenized with functionality exceeding previous Ant build.xml implemented (except deployment to servlet container, which can be easily added).
Improved unit tests to be independent of externally running servlet container, now every test runs its own embedded jetty server (and stops it afterward). Removed all eclipse artifacts (.project, .classpath, .settings, etc.) and added to .gitignore to be independent of development environment (can be autogenerated by maven, or better yet use m2eclipse). Removed embedded dependency jars since they are handled automatically by maven.
This commit is contained in:
3
src/main/webapp/META-INF/MANIFEST.MF
Normal file
3
src/main/webapp/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
||||
65
src/main/webapp/WEB-INF/web.xml
Normal file
65
src/main/webapp/WEB-INF/web.xml
Normal file
@@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
|
||||
<display-name>plantuml</display-name>
|
||||
<servlet>
|
||||
<servlet-name>plantumlservlet</servlet-name>
|
||||
<servlet-class>net.sourceforge.plantuml.servlet.PlantUmlServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>welcome</servlet-name>
|
||||
<servlet-class>net.sourceforge.plantuml.servlet.Welcome</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>imgservlet</servlet-name>
|
||||
<servlet-class>net.sourceforge.plantuml.servlet.ImgServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>svgservlet</servlet-name>
|
||||
<servlet-class>net.sourceforge.plantuml.servlet.SvgServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>asciiservlet</servlet-name>
|
||||
<servlet-class>net.sourceforge.plantuml.servlet.AsciiServlet</servlet-class>
|
||||
</servlet>
|
||||
<!-- Patterns of the servlet -->
|
||||
<servlet-mapping>
|
||||
<servlet-name>welcome</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>plantumlservlet</servlet-name>
|
||||
<url-pattern>/uml/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>plantumlservlet</servlet-name>
|
||||
<url-pattern>/form</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>imgservlet</servlet-name>
|
||||
<url-pattern>/img/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>svgservlet</servlet-name>
|
||||
<url-pattern>/svg/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>asciiservlet</servlet-name>
|
||||
<url-pattern>/txt/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>plantumlservlet</servlet-name>
|
||||
<url-pattern>/start/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>plantumlservlet</servlet-name>
|
||||
<url-pattern>/proxy/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<error-page>
|
||||
<exception-type>java.lang.Throwable</exception-type>
|
||||
<location>/error.jsp</location>
|
||||
</error-page>
|
||||
<error-page>
|
||||
<error-code>500</error-code>
|
||||
<location>/error.jsp</location>
|
||||
</error-page>
|
||||
</web-app>
|
||||
32
src/main/webapp/error.jsp
Normal file
32
src/main/webapp/error.jsp
Normal file
@@ -0,0 +1,32 @@
|
||||
<%@ page isErrorPage="true" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="false" %>
|
||||
<%
|
||||
String contextRoot = request.getContextPath();
|
||||
%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="expires" content="0" />
|
||||
<meta http-equiv="pragma" content="no-cache" />
|
||||
<meta http-equiv="cache-control" content="no-cache, must-revalidate" />
|
||||
<link rel="stylesheet" href="<%=contextRoot %>/plantuml.css" type="text/css"/>
|
||||
<link rel="icon" href="<%=contextRoot %>/favicon.ico" type="image/x-icon"/>
|
||||
<link rel="shortcut icon" href="<%=contextRoot %>/favicon.ico" type="image/x-icon"/>
|
||||
<title>PlantUMLServer Error</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>
|
||||
Sorry, but things didn't work out as planned.
|
||||
</p>
|
||||
<hr/>
|
||||
<jsp:useBean id="now" class="java.util.Date" />
|
||||
<ul>
|
||||
<li><%=now.toString() %></li>
|
||||
<li>Request that failed: <%=pageContext.getErrorData().getRequestURI() %></li>
|
||||
<li>Status code: <%=pageContext.getErrorData().getStatusCode() %></li>
|
||||
<li>Exception: <%=pageContext.getErrorData().getThrowable() %></li>
|
||||
</ul>
|
||||
<hr/>
|
||||
</body>
|
||||
</html>
|
||||
BIN
src/main/webapp/favicon.ico
Normal file
BIN
src/main/webapp/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
5
src/main/webapp/footer.jspf
Normal file
5
src/main/webapp/footer.jspf
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
<div id="footer">
|
||||
<p>PlantUML Server @timestamp@ / version <%= net.sourceforge.plantuml.version.Version.version() %>
|
||||
</p>
|
||||
</div>
|
||||
79
src/main/webapp/index.jsp
Normal file
79
src/main/webapp/index.jsp
Normal file
@@ -0,0 +1,79 @@
|
||||
<%@ page info="index" contentType="text/html; charset=utf-8" pageEncoding="utf-8" session="false" %>
|
||||
|
||||
<%
|
||||
String contextRoot = request.getContextPath();
|
||||
String host = "http://" + request.getServerName() + ":" + request.getServerPort();
|
||||
String encoded = "";
|
||||
String umltext = "";
|
||||
String imgurl = "";
|
||||
String svgurl = "";
|
||||
String txturl = "";
|
||||
Object encodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.encoded");
|
||||
if (encodedAttribute != null) {
|
||||
encoded = encodedAttribute.toString();
|
||||
if (!encoded.isEmpty()) {
|
||||
imgurl = host + contextRoot + "/img/" + encoded;
|
||||
svgurl = host + contextRoot + "/svg/" + encoded;
|
||||
txturl = host + contextRoot + "/txt/" + encoded;
|
||||
}
|
||||
}
|
||||
Object decodedAttribute = request.getAttribute("net.sourceforge.plantuml.servlet.decoded");
|
||||
if (decodedAttribute != null) {
|
||||
umltext = decodedAttribute.toString();
|
||||
}
|
||||
%>
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="expires" content="0" />
|
||||
<meta http-equiv="pragma" content="no-cache" />
|
||||
<meta http-equiv="cache-control" content="no-cache, must-revalidate" />
|
||||
<link rel="stylesheet" href="<%=contextRoot %>/plantuml.css" type="text/css"/>
|
||||
<link rel="icon" href="<%=contextRoot %>/favicon.ico" type="image/x-icon"/>
|
||||
<link rel="shortcut icon" href="<%=contextRoot %>/favicon.ico" type="image/x-icon"/>
|
||||
<title>PlantUMLServer</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="header">
|
||||
<%-- PAGE TITLE --%>
|
||||
<h1>PlantUML Server</h1>
|
||||
<p>This application provides a servlet which serves images created by <a href="http://plantuml.sourceforge.net">PlantUML</a>.</p>
|
||||
</div>
|
||||
<div id="content">
|
||||
<%-- CONTENT --%>
|
||||
<form method="post" accept-charset="UTF-8" action="<%=contextRoot %>/form">
|
||||
<p>
|
||||
<textarea name="text" cols="120" rows="10"><%=umltext %></textarea>
|
||||
<br/>
|
||||
<input type="submit" />
|
||||
</p>
|
||||
</form>
|
||||
<hr/>
|
||||
You can enter here a previously generated URL:
|
||||
<form method="post" action="<%=contextRoot %>/form">
|
||||
<p>
|
||||
<input name="url" type="text" size="150" value="<%=imgurl %>" />
|
||||
<br/>
|
||||
<input type="submit"/>
|
||||
</p>
|
||||
</form>
|
||||
<% if ( !imgurl.isEmpty()) { %>
|
||||
<hr/>
|
||||
<a href="<%=svgurl%>">View as SVG</a>
|
||||
<a href="<%=txturl%>">View as ASCII Art</a>
|
||||
<p id="diagram">
|
||||
<img src="<%=imgurl %>" alt="PlantUML diagram"/>
|
||||
</p>
|
||||
<% } //endif %>
|
||||
</div>
|
||||
<!-- This comment is used by the TestProxy class
|
||||
@startuml
|
||||
Bob -> Alice : hello
|
||||
@enduml
|
||||
-->
|
||||
<%-- FOOTER --%>
|
||||
<%@ include file="footer.jspf" %>
|
||||
</body>
|
||||
</html>
|
||||
55
src/main/webapp/plantuml.css
Normal file
55
src/main/webapp/plantuml.css
Normal file
@@ -0,0 +1,55 @@
|
||||
/******************************
|
||||
* PlantUMLServlet style sheet *
|
||||
******************************/
|
||||
|
||||
/* Font */
|
||||
* {
|
||||
font-family: arial,helvetica,sans-serif;
|
||||
}
|
||||
|
||||
/* Header */
|
||||
#header {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* Content */
|
||||
#content {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
width: 95%;
|
||||
}
|
||||
|
||||
/* Form inputs */
|
||||
#content textarea, #content input[type=text] {
|
||||
background-color: #ffc;
|
||||
font-family: monospace;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* XHTML tag */
|
||||
#content code {
|
||||
font-family: 'courier new',courier,monospace;
|
||||
letter-spacing: -1pt;
|
||||
}
|
||||
|
||||
/* Diagram */
|
||||
#content #diagram {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
#content #diagram img {
|
||||
border: medium solid green;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
/* Footer */
|
||||
#footer p {
|
||||
background-color: #eee;
|
||||
color: #666;
|
||||
font-size: 0.7em;
|
||||
padding: 2px;
|
||||
text-align: center;
|
||||
width: 100%;
|
||||
}
|
||||
20
src/test/java/net/sourceforge/plantuml/servlet/AllTests.java
Normal file
20
src/test/java/net/sourceforge/plantuml/servlet/AllTests.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
public class AllTests extends TestSuite {
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(AllTests.class.getName());
|
||||
//$JUnit-BEGIN$
|
||||
suite.addTestSuite(TestForm.class);
|
||||
suite.addTestSuite(TestImage.class);
|
||||
suite.addTestSuite(TestAsciiArt.class);
|
||||
suite.addTestSuite(TestSVG.class);
|
||||
suite.addTestSuite(TestProxy.class);
|
||||
//$JUnit-END$
|
||||
return suite;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.webapp.WebAppContext;
|
||||
|
||||
public class ServerUtils {
|
||||
|
||||
Server server;
|
||||
|
||||
public ServerUtils(boolean start) throws Exception {
|
||||
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 {
|
||||
server.start();
|
||||
}
|
||||
|
||||
public void stopServer() throws Exception {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
public String getServerUrl() {
|
||||
Connector connector = server.getConnectors()[0];
|
||||
return String.format("http://%s:%d/plantuml/", connector.getHost(), connector.getLocalPort());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
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;
|
||||
|
||||
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/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
|
||||
WebResponse response = conversation.getResource(request);
|
||||
// Analyze response
|
||||
// Verifies the Content-Type header
|
||||
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
|
||||
// Get the content and verify its size
|
||||
String diagram = response.getText();
|
||||
int diagramLen = diagram.length();
|
||||
assertTrue(diagramLen > 200);
|
||||
assertTrue(diagramLen < 250);
|
||||
}
|
||||
|
||||
}
|
||||
117
src/test/java/net/sourceforge/plantuml/servlet/TestForm.java
Normal file
117
src/test/java/net/sourceforge/plantuml/servlet/TestForm.java
Normal file
@@ -0,0 +1,117 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
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 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 = TestUtils.tryGetResponse(conversation, request );
|
||||
// Analyze response
|
||||
WebForm forms[] = response.getForms();
|
||||
assertEquals( 2, forms.length );
|
||||
assertEquals( "url", forms[1].getParameterNames()[0] );
|
||||
assertTrue( forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"));
|
||||
// Ensure the generated image is present
|
||||
assertEquals( 1, response.getImages().length);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = TestUtils.tryGetResponse(conversation, 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("/img/AqijAixCpmC0"));
|
||||
// Ensure the image is present
|
||||
assertEquals( 1, response.getImages().length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that when the UML text is empty, no 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 = TestUtils.tryGetResponse(conversation, 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
|
||||
assertEquals( 0, response.getImages().length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that when the encoded URL is empty, no 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 = TestUtils.tryGetResponse(conversation, 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
|
||||
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(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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
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() + "img/AqijAixCpmC0");
|
||||
WebResponse response = conversation.getResource( request);
|
||||
// Analyze response
|
||||
// Verifies the Content-Type header
|
||||
assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
|
||||
// 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();
|
||||
responseStream.close();
|
||||
byte[] inMemoryImage = imageStream.toByteArray();
|
||||
int diagramLen = inMemoryImage.length;
|
||||
assertTrue( diagramLen > 10000);
|
||||
assertTrue( diagramLen < 20000);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() + "img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
|
||||
WebResponse response = conversation.getResource( request);
|
||||
// Analyze response
|
||||
// Verifies the Content-Type header
|
||||
assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
|
||||
// Verifies the availability of the Expires entry in the response header
|
||||
assertNotNull( response.getHeaderField( "Expires"));
|
||||
// Verifies the availability of the Last-Modified entry in the response header
|
||||
assertNotNull( response.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"));
|
||||
assertTrue( "Last-Modified is not in the past", lastModified.before( new Date()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
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 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/"
|
||||
+ getServerUrl() + "welcome");
|
||||
WebResponse response = conversation.getResource( request);
|
||||
// Analyze response
|
||||
// Verifies the Content-Type header
|
||||
//assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
|
||||
// Get the image and verify its size (~1533 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();
|
||||
int diagramLen = inMemoryImage.length;
|
||||
assertTrue( diagramLen > 1500);
|
||||
assertTrue( diagramLen < 1600);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
26
src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java
Normal file
26
src/test/java/net/sourceforge/plantuml/servlet/TestSVG.java
Normal file
@@ -0,0 +1,26 @@
|
||||
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;
|
||||
|
||||
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/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
|
||||
WebResponse response = conversation.getResource( request);
|
||||
// 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 > 1700);
|
||||
assertTrue( diagramLen < 1800);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import com.meterware.httpunit.*;
|
||||
|
||||
/**
|
||||
* Utility class for HttpUnit conversations
|
||||
*/
|
||||
public class TestUtils {
|
||||
|
||||
/**
|
||||
* Return the URL of the PlantUMLServlet, deployed on the testing web server
|
||||
* in the following form http://server/contextroot/
|
||||
* Note the trailing slash (/)
|
||||
* @return the URL
|
||||
*/
|
||||
public static String getServerUrl() {
|
||||
return "http://localhost/plantuml/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Try getting a response for the given Conversation and Request
|
||||
* show an error message if a 404 error appears
|
||||
* @param conversation - the conversation to use
|
||||
* @param request
|
||||
* @return the response
|
||||
* @throws an Exception if getting the response fails
|
||||
*/
|
||||
public static WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception {
|
||||
WebResponse response=null;
|
||||
try {
|
||||
response = conversation.getResponse( request );
|
||||
} catch (HttpNotFoundException nfe) {
|
||||
System.err.println("The URL '"+request.getURL()+"' is not active any more");
|
||||
throw nfe;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package net.sourceforge.plantuml.servlet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public abstract class WebappTestCase extends TestCase {
|
||||
|
||||
private ServerUtils serverUtils;
|
||||
|
||||
public WebappTestCase() {
|
||||
super();
|
||||
}
|
||||
|
||||
public WebappTestCase(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
serverUtils = new ServerUtils(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tearDown() throws Exception {
|
||||
serverUtils.stopServer();
|
||||
}
|
||||
|
||||
protected String getServerUrl() {
|
||||
return serverUtils.getServerUrl();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user