1
0
mirror of https://github.com/octoleo/plantuml-server.git synced 2024-09-22 03:59:03 +00:00

[TASK] Full coding style clean up

This commit is contained in:
Maxime Sinclair 2013-07-10 17:07:24 +02:00
parent ded3a5578a
commit ce168633ad
14 changed files with 130 additions and 137 deletions

View File

@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
public class AsciiServlet extends UmlDiagramService { public class AsciiServlet extends UmlDiagramService {
@Override @Override
public String getSource( String uri) { public String getSource(String uri) {
String[] result = uri.split("/txt/", 2); String[] result = uri.split("/txt/", 2);
if (result.length != 2) { if (result.length != 2) {
return ""; return "";
} else { } else {
return result[1]; return result[1];
} }
} }

View File

@ -35,10 +35,9 @@ import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader; import net.sourceforge.plantuml.SourceStringReader;
import net.sourceforge.plantuml.StringUtils; import net.sourceforge.plantuml.StringUtils;
/** /**
* Delegates the diagram generation from the UML source and * Delegates the diagram generation from the UML source and the filling of the HTTP response with the diagram in the
* the filling of the HTTP response with the diagram in the right format. * right format. Its own responsibility is to produce the right HTTP headers.
* Its own responsibility is to produce the right HTTP headers.
*/ */
class DiagramResponse { class DiagramResponse {
private HttpServletResponse response; private HttpServletResponse response;
@ -52,31 +51,29 @@ class DiagramResponse {
contentType = Collections.unmodifiableMap(map); contentType = Collections.unmodifiableMap(map);
} }
DiagramResponse( HttpServletResponse r, FileFormat f) { DiagramResponse(HttpServletResponse r, FileFormat f) {
response = r; response = r;
format = f; format = f;
} }
void sendDiagram(String uml) throws IOException {
void sendDiagram( String uml) throws IOException {
long today = System.currentTimeMillis(); long today = System.currentTimeMillis();
if ( StringUtils.isDiagramCacheable( uml)) { if (StringUtils.isDiagramCacheable(uml)) {
// Add http headers to force the browser to cache the image // Add http headers to force the browser to cache the image
response.addDateHeader( "Expires", today + 31536000000L); response.addDateHeader("Expires", today + 31536000000L);
// today + 1 year // today + 1 year
response.addDateHeader( "Last-Modified", 1261440000000L); response.addDateHeader("Last-Modified", 1261440000000L);
// 2009 dec 22 constant date in the past // 2009 dec 22 constant date in the past
response.addHeader( "Cache-Control", "public"); response.addHeader("Cache-Control", "public");
} }
response.setContentType( getContentType()); response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader( uml); SourceStringReader reader = new SourceStringReader(uml);
reader.generateImage( response.getOutputStream(), new FileFormatOption(format)); reader.generateImage(response.getOutputStream(), new FileFormatOption(format));
response.flushBuffer(); response.flushBuffer();
} }
private String getContentType() { private String getContentType() {
return contentType.get( format); return contentType.get(format);
} }
} }

View File

@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
public class ImgServlet extends UmlDiagramService { public class ImgServlet extends UmlDiagramService {
@Override @Override
public String getSource( String uri) { public String getSource(String uri) {
String[] result = uri.split("/img/", 2); String[] result = uri.split("/img/", 2);
if (result.length != 2) { if (result.length != 2) {
return ""; return "";
} else { } else {
return result[1]; return result[1];
} }
} }

View File

@ -64,7 +64,8 @@ import HTTPClient.ParseException;
public class PlantUmlServlet extends HttpServlet { public class PlantUmlServlet extends HttpServlet {
private static final Pattern urlPattern = Pattern.compile(".*/(.*)"); // Last part of the URL private static final Pattern urlPattern = Pattern.compile(".*/(.*)"); // Last part of the URL
private static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); // Format of a compressed diagram private static final Pattern encodedPattern = Pattern.compile("^[a-zA-Z0-9\\-\\_]+$"); // Format of a compressed
// diagram
private static final Pattern startumlPattern = Pattern.compile("/\\w+/start/(.*)"); private static final Pattern startumlPattern = Pattern.compile("/\\w+/start/(.*)");
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)"); private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)"); private static final Pattern oldStartumlPattern = Pattern.compile("/\\w+/uml/startuml/(.*)");

View File

@ -54,10 +54,9 @@ public class ProxyServlet extends HttpServlet {
private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)"); private static final Pattern proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private String format; private String format;
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throws IOException, ServletException {
final String uri = request.getRequestURI(); final String uri = request.getRequestURI();
@ -76,15 +75,15 @@ public class ProxyServlet extends HttpServlet {
dispatcher.forward(request, response); dispatcher.forward(request, response);
} }
} }
private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException { private void handleImageProxy(HttpServletResponse response, String num, String source) throws IOException {
SourceStringReader reader = new SourceStringReader(getSource(source)); SourceStringReader reader = new SourceStringReader(getSource(source));
int n = num == null ? 0 : Integer.parseInt(num); int n = num == null ? 0 : Integer.parseInt(num);
reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat())); reader.generateImage(response.getOutputStream(), n, new FileFormatOption(getOutputFormat()));
} }
private String getSource( String uri) throws IOException { private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null); CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)"); final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
@ -105,7 +104,7 @@ public class ProxyServlet extends HttpServlet {
} }
private FileFormat getOutputFormat() { private FileFormat getOutputFormat() {
if (format==null) { if (format == null) {
return FileFormat.PNG; return FileFormat.PNG;
} }
if (format.equals("svg")) { if (format.equals("svg")) {
@ -115,6 +114,6 @@ public class ProxyServlet extends HttpServlet {
return FileFormat.ATXT; return FileFormat.ATXT;
} }
return FileFormat.PNG; return FileFormat.PNG;
} }
} }

View File

@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
public class SvgServlet extends UmlDiagramService { public class SvgServlet extends UmlDiagramService {
@Override @Override
public String getSource( String uri) { public String getSource(String uri) {
String[] result = uri.split("/svg/", 2); String[] result = uri.split("/svg/", 2);
if (result.length != 2) { if (result.length != 2) {
return ""; return "";
} else { } else {
return result[1]; return result[1];
} }
} }

View File

@ -37,60 +37,60 @@ import net.sourceforge.plantuml.code.Transcoder;
import net.sourceforge.plantuml.code.TranscoderUtil; import net.sourceforge.plantuml.code.TranscoderUtil;
/** /**
* Common service servlet to produce diagram from compressed UML source * Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI.
* contained in the end part of the requested URI.
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public abstract class UmlDiagramService extends HttpServlet { public abstract class UmlDiagramService extends HttpServlet {
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throws IOException, ServletException {
// build the UML source from the compressed request parameter // build the UML source from the compressed request parameter
String text = URLDecoder.decode( getSource(request.getRequestURI()), "UTF-8"); String text = URLDecoder.decode(getSource(request.getRequestURI()), "UTF-8");
Transcoder transcoder = getTranscoder(); Transcoder transcoder = getTranscoder();
text = transcoder.decode(text); text = transcoder.decode(text);
// encapsulate the UML syntax if necessary // encapsulate the UML syntax if necessary
String uml; String uml;
if (text.startsWith("@start")) { if (text.startsWith("@start")) {
uml = text; uml = text;
} else { } else {
StringBuilder plantUmlSource = new StringBuilder(); StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append( "@startuml\n"); plantUmlSource.append("@startuml\n");
plantUmlSource.append( text); plantUmlSource.append(text);
if (text.endsWith( "\n") == false) { if (text.endsWith("\n") == false) {
plantUmlSource.append( "\n"); plantUmlSource.append("\n");
} }
plantUmlSource.append( "@enduml"); plantUmlSource.append("@enduml");
uml = plantUmlSource.toString(); uml = plantUmlSource.toString();
} }
// generate the response // generate the response
DiagramResponse dr = new DiagramResponse( response, getOutputFormat()); DiagramResponse dr = new DiagramResponse(response, getOutputFormat());
try { try {
dr.sendDiagram(uml); dr.sendDiagram(uml);
} catch (IIOException iioe) { } catch (IIOException iioe) {
// Browser has closed the connection, do nothing // Browser has closed the connection, do nothing
} }
dr = null; dr = null;
} }
/** /**
* Extracts the compressed UML source from the HTTP URI. * Extracts the compressed UML source from the HTTP URI.
* @param uri the complete URI as returned by request.getRequestURI() *
* @param uri
* the complete URI as returned by request.getRequestURI()
* @return the compressed UML source * @return the compressed UML source
*/ */
abstract public String getSource( String uri); abstract public String getSource(String uri);
/** /**
* Gives the wished output format of the diagram. * Gives the wished output format of the diagram. This value is used by the DiagramResponse class.
* This value is used by the DiagramResponse class. *
* @return the format * @return the format
*/ */
abstract public FileFormat getOutputFormat(); abstract public FileFormat getOutputFormat();
private Transcoder getTranscoder() { private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder(); return TranscoderUtil.getDefaultTranscoder();
} }

View File

@ -38,17 +38,16 @@ import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class Welcome extends HttpServlet { public class Welcome extends HttpServlet {
@Override @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throws IOException, ServletException {
// set the sample // set the sample
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "Bob -> Alice : hello"); request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "Bob -> Alice : hello");
request.setAttribute("net.sourceforge.plantuml.servlet.encoded", "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"); request.setAttribute("net.sourceforge.plantuml.servlet.encoded", "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
// forward to index.jsp // forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp"); RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} }
} }

View File

@ -7,13 +7,13 @@ public class AllTests extends TestSuite {
public static Test suite() { public static Test suite() {
TestSuite suite = new TestSuite(AllTests.class.getName()); TestSuite suite = new TestSuite(AllTests.class.getName());
//$JUnit-BEGIN$ // $JUnit-BEGIN$
suite.addTestSuite(TestForm.class); suite.addTestSuite(TestForm.class);
suite.addTestSuite(TestImage.class); suite.addTestSuite(TestImage.class);
suite.addTestSuite(TestAsciiArt.class); suite.addTestSuite(TestAsciiArt.class);
suite.addTestSuite(TestSVG.class); suite.addTestSuite(TestSVG.class);
suite.addTestSuite(TestProxy.class); suite.addTestSuite(TestProxy.class);
//$JUnit-END$ // $JUnit-END$
return suite; return suite;
} }

View File

@ -1,6 +1,5 @@
package net.sourceforge.plantuml.servlet; package net.sourceforge.plantuml.servlet;
import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest; import com.meterware.httpunit.WebRequest;

View File

@ -8,23 +8,22 @@ import com.meterware.httpunit.WebResponse;
public class TestForm extends WebappTestCase { public class TestForm extends WebappTestCase {
/** /**
* Verifies that the welcome page has exactly two form * Verifies that the welcome page has exactly two form 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(getServerUrl()); WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request);
// Analyze response // 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]);
assertTrue( forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000")); assertTrue(forms[1].getParameterValue("url").endsWith("/img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"));
// 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 * Verifies that the version image is generated
@ -33,19 +32,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Fill the form and submit it // Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl()); WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request);
WebForm formUMLText = response.getForms()[0]; WebForm formUMLText = response.getForms()[0];
formUMLText.setParameter("text", "version"); formUMLText.setParameter("text", "version");
response = formUMLText.submit(); response = formUMLText.submit();
// Analyze response // Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals(2, forms.length);
// Ensure the Text field is correct // Ensure the Text field is correct
assertEquals( "version", forms[0].getParameterValue("text")); assertEquals("version", forms[0].getParameterValue("text"));
// Ensure the URL field is correct // Ensure the URL field is correct
assertTrue( forms[1].getParameterValue("url").endsWith("/img/AqijAixCpmC0")); assertTrue(forms[1].getParameterValue("url").endsWith("/img/AqijAixCpmC0"));
// Ensure the image is present // Ensure the image is present
assertEquals( 1, response.getImages().length); assertEquals(1, response.getImages().length);
} }
/** /**
@ -55,19 +54,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Fill the form and submit it // Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl()); WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.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 = formUMLText.submit(); response = formUMLText.submit();
// Analyze response // Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals(2, forms.length);
// Ensure the Text field is empty // Ensure the Text field is empty
assertNull( forms[0].getParameterValue("text")); assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty // Ensure the URL field is empty
assertTrue( forms[1].getParameterValue("url").isEmpty()); assertTrue(forms[1].getParameterValue("url").isEmpty());
// Ensure there is no image // Ensure there is no image
assertEquals( 0, response.getImages().length); assertEquals(0, response.getImages().length);
} }
/** /**
@ -77,19 +76,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Fill the form and submit it // Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl()); WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request);
WebForm formUrl = response.getForms()[1]; WebForm formUrl = response.getForms()[1];
formUrl.setParameter("url", ""); formUrl.setParameter("url", "");
response = formUrl.submit(); response = formUrl.submit();
// Analyze response // Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals(2, forms.length);
// Ensure the Text field is empty // Ensure the Text field is empty
assertNull( forms[0].getParameterValue("text")); assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty // Ensure the URL field is empty
assertTrue( forms[1].getParameterValue("url").isEmpty()); assertTrue(forms[1].getParameterValue("url").isEmpty());
// Ensure there is no image // Ensure there is no image
assertEquals( 0, response.getImages().length); assertEquals(0, response.getImages().length);
} }
/** /**
@ -99,19 +98,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Fill the form and submit it // Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl()); WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request ); WebResponse response = TestUtils.tryGetResponse(conversation, request);
WebForm formDitaaText = response.getForms()[0]; WebForm formDitaaText = response.getForms()[0];
formDitaaText.setParameter("text", "@startditaa \n*--> \n@endditaa"); formDitaaText.setParameter("text", "@startditaa \n*--> \n@endditaa");
response = formDitaaText.submit(); response = formDitaaText.submit();
// Analyze response // Analyze response
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals(2, forms.length);
// Ensure the Text field is correct // Ensure the Text field is correct
assertTrue( forms[0].getParameterValue("text").startsWith( "@startditaa")); assertTrue(forms[0].getParameterValue("text").startsWith("@startditaa"));
// Ensure the URL field is correct // Ensure the URL field is correct
assertTrue( forms[1].getParameterValue("url").endsWith("/img/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00")); assertTrue(forms[1].getParameterValue("url").endsWith("/img/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00"));
// Ensure the image is present // Ensure the image is present
assertEquals( 1, response.getImages().length); assertEquals(1, response.getImages().length);
} }
} }

View File

@ -18,23 +18,23 @@ public class TestImage extends WebappTestCase {
public void testVersionImage() throws Exception { public void testVersionImage() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/AqijAixCpmC0"); WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/AqijAixCpmC0");
WebResponse response = conversation.getResource( request); WebResponse response = conversation.getResource(request);
// Analyze response // Analyze response
// Verifies the Content-Type header // 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", response.getContentType());
// Get the image and verify its size // Get the image and verify its size
InputStream responseStream = response.getInputStream(); InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
while( ( n = responseStream.read( buf)) != -1) { while ((n = responseStream.read(buf)) != -1) {
imageStream.write( buf, 0, n); imageStream.write(buf, 0, n);
} }
imageStream.close(); imageStream.close();
byte[] inMemoryImage = imageStream.toByteArray(); byte[] inMemoryImage = imageStream.toByteArray();
int diagramLen = inMemoryImage.length; int diagramLen = inMemoryImage.length;
assertTrue( diagramLen > 10000); assertTrue(diagramLen > 10000);
assertTrue( diagramLen < 20000); assertTrue(diagramLen < 20000);
responseStream.close(); responseStream.close();
} }
@ -45,21 +45,21 @@ public class TestImage extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Bob -> Alice : hello // Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000"); WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
WebResponse response = conversation.getResource( request); WebResponse response = conversation.getResource(request);
// Analyze response // Analyze response
// Verifies the Content-Type header // 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", response.getContentType());
// Verifies the availability of the Expires entry in the response header // Verifies the availability of the Expires entry in the response header
assertNotNull( response.getHeaderField( "Expires")); assertNotNull(response.getHeaderField("Expires"));
// Verifies the availability of the Last-Modified entry in the response header // Verifies the availability of the Last-Modified entry in the response header
assertNotNull( response.getHeaderField( "Last-Modified")); assertNotNull(response.getHeaderField("Last-Modified"));
// Verifies the Last-Modified value is in the past // Verifies the Last-Modified value is in the past
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ", Locale.ENGLISH); 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(response.getHeaderField("Last-Modified"));
assertTrue( "Last-Modified is not in the past", lastModified.before( new Date())); assertTrue("Last-Modified is not in the past", lastModified.before(new Date()));
// Consume the response // Consume the response
InputStream responseStream = response.getInputStream(); InputStream responseStream = response.getInputStream();
while( responseStream.read() != -1) { while (responseStream.read() != -1) {
; // Do nothing ; // Do nothing
} }
} }

View File

@ -15,41 +15,39 @@ public class TestProxy extends WebappTestCase {
*/ */
public void testDefaultProxy() throws Exception { public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/" WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/" + getServerUrl() + "welcome");
+ getServerUrl() + "welcome"); WebResponse response = conversation.getResource(request);
WebResponse response = conversation.getResource( request);
// Analyze response // Analyze response
// Verifies the Content-Type header // 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", response.getContentType());
// Get the image and verify its size (~2000 bytes) // Get the image and verify its size (~2000 bytes)
InputStream responseStream = response.getInputStream(); InputStream responseStream = response.getInputStream();
ByteArrayOutputStream imageStream = new ByteArrayOutputStream(); ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int n = 0; int n = 0;
while( ( n = responseStream.read( buf)) != -1) { while ((n = responseStream.read(buf)) != -1) {
imageStream.write( buf, 0, n); imageStream.write(buf, 0, n);
} }
imageStream.close(); imageStream.close();
responseStream.close(); responseStream.close();
byte[] inMemoryImage = imageStream.toByteArray(); byte[] inMemoryImage = imageStream.toByteArray();
int diagramLen = inMemoryImage.length; int diagramLen = inMemoryImage.length;
assertTrue( diagramLen > 1500); assertTrue(diagramLen > 1500);
assertTrue( diagramLen < 2500); assertTrue(diagramLen < 2500);
} }
public void testProxyWithFormat() throws Exception { public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" + getServerUrl() + "welcome");
+ getServerUrl() + "welcome"); WebResponse response = conversation.getResource(request);
WebResponse response = conversation.getResource( request);
// Analyze response // Analyze response
// Verifies the Content-Type header // Verifies the Content-Type header
// TODO assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType()); // TODO assertEquals( "Response content type is not SVG", "image/svg+xml", response.getContentType());
// Get the content and verify its size // Get the content and verify its size
String diagram = response.getText(); String diagram = response.getText();
int diagramLen = diagram.length(); int diagramLen = diagram.length();
assertTrue( diagramLen > 1000); assertTrue(diagramLen > 1000);
assertTrue( diagramLen < 3000); assertTrue(diagramLen < 3000);
} }
/** /**
@ -59,11 +57,11 @@ public class TestProxy extends WebappTestCase {
WebConversation conversation = new WebConversation(); WebConversation conversation = new WebConversation();
// Try to proxify an invalid address // Try to proxify an invalid address
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL"); WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/invalidURL");
WebResponse response = conversation.getResource( request); WebResponse response = conversation.getResource(request);
// Analyze response, it must be the empty form // Analyze response, it must be the empty form
// Verifies the Content-Type header // Verifies the Content-Type header
assertEquals( "Response content type is not HTML", "text/html", response.getContentType()); assertEquals("Response content type is not HTML", "text/html", response.getContentType());
WebForm forms[] = response.getForms(); WebForm forms[] = response.getForms();
assertEquals( 2, forms.length ); assertEquals(2, forms.length);
} }
} }

View File

@ -8,33 +8,34 @@ import com.meterware.httpunit.*;
public class TestUtils { public class TestUtils {
/** /**
* Return the URL of the PlantUMLServlet, deployed on the testing web server * Return the URL of the PlantUMLServlet, deployed on the testing web server in the following form
* in the following form http://server/contextroot/ * http://server/contextroot/ Note the trailing slash (/)
* Note the trailing slash (/) *
* @return the URL * @return the URL
*/ */
public static String getServerUrl() { public static String getServerUrl() {
return "http://localhost/plantuml/"; return "http://localhost/plantuml/";
} }
/** /**
* Try getting a response for the given Conversation and Request * Try getting a response for the given Conversation and Request show an error message if a 404 error appears
* show an error message if a 404 error appears *
* @param conversation - the conversation to use * @param conversation
* - the conversation to use
* @param request * @param request
* @return the response * @return the response
* @throws an Exception if getting the response fails * @throws an
* Exception if getting the response fails
*/ */
public static WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception { public static WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception {
WebResponse response=null; WebResponse response = null;
try { try {
response = conversation.getResponse( request ); response = conversation.getResponse(request);
} catch (HttpNotFoundException nfe) { } catch (HttpNotFoundException nfe) {
System.err.println("The URL '"+request.getURL()+"' is not active any more"); System.err.println("The URL '" + request.getURL() + "' is not active any more");
throw nfe; throw nfe;
} }
return response; return response;
} }
} }