1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-11-05 20:37:53 +00:00

A TestSuite and new test cases

This commit is contained in:
Maxime Sinclair 2011-02-23 15:51:38 +01:00
parent 7ffbc44e08
commit 5a97a919ff
4 changed files with 142 additions and 28 deletions

View File

@ -0,0 +1,17 @@
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);
//$JUnit-END$
return suite;
}
}

View File

@ -1,6 +1,6 @@
package net.sourceforge.plantuml.servlet; package net.sourceforge.plantuml.servlet;
import junit.framework.*; import junit.framework.TestCase;
import com.meterware.httpunit.*; import com.meterware.httpunit.*;
public class TestForm extends TestCase { public class TestForm extends TestCase {
@ -10,33 +10,52 @@ public class TestForm extends TestCase {
* with the Bob --> Alice sample * with the Bob --> Alice sample
*/ */
public void testWelcomePage() throws Exception { public void testWelcomePage() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest( "http://localhost/plantuml/" ); WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl());
WebResponse response = tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request );
// Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals( 2, forms.length );
assertEquals( "url", forms[1].getParameterNames()[0] ); assertEquals( "url", forms[1].getParameterNames()[0] );
FormControl urlInput = forms[1].getParameter("url").getControl(); assertTrue( forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"));
assertEquals( "http://localhost:80/plantuml/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000",
forms[1].getParameterValue("url"));
assertEquals( "INPUT", urlInput.getTagName());
// Ensure the generated image is present // Ensure the generated image is present
assertEquals( 1, response.getImages().length); 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( TestUtils.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 * Verifies that when the UML text is empty, no image is generated
*/ */
public void testEmptyText() throws Exception { public void testEmptyText() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Fill the form and submit it // Fill the form and submit it
WebRequest request = new GetMethodWebRequest( "http://localhost/plantuml/form" ); WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl());
WebResponse response = tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request );
WebForm formUMLText = response.getForms()[0]; WebForm formUMLText = response.getForms()[0];
formUMLText.setParameter("text", ""); formUMLText.setParameter("text", "");
response = tryGetResponse( conversation, formUMLText.getRequest()); response = formUMLText.submit();
// Analyze response // Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals( 2, forms.length );
@ -49,22 +68,25 @@ public class TestForm extends TestCase {
} }
/** /**
* try getting a response for the given Conversation and Request * Verifies that when the encoded URL is empty, the default image is generated
* 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 WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception { public void testEmptyUrl() throws Exception {
WebResponse response=null; WebConversation conversation = new WebConversation();
try { // Fill the form and submit it
response = conversation.getResponse( request ); WebRequest request = new GetMethodWebRequest( "http://localhost/plantuml/" );
} catch (HttpNotFoundException nfe) { WebResponse response = TestUtils.tryGetResponse(conversation, request );
System.err.println("The URL '"+request.getURL()+"' is not active any more"); WebForm formUrl = response.getForms()[1];
throw nfe; formUrl.setParameter("url", "");
} response = formUrl.submit();
return response; // Analyze response
WebForm forms[] = response.getForms();
assertEquals( 2, forms.length );
// Ensure the Text field is empty
assertTrue( forms[0].getParameterValue("text").startsWith("Bob"));
// Ensure the URL field is correct
assertTrue( forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"));
// Ensure the image is present
assertEquals( 1, response.getImages().length);
} }
} }

View File

@ -0,0 +1,35 @@
package net.sourceforge.plantuml.servlet;
import junit.framework.TestCase;
import com.meterware.httpunit.*;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
public class TestImage extends TestCase {
/**
* Verifies the generation of the version image from an encoded URL
*/
public void testVersionImage() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest( TestUtils.getServerUrl()+"img/AqijAixCpmC0");
WebResponse response = conversation.getResource( request);
// Analyze response
// Verifies the Content-Type header
assertEquals( "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);
}
}

View File

@ -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;
}
}