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:
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