[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 {
@Override
public String getSource( String uri) {
public String getSource(String uri) {
String[] result = uri.split("/txt/", 2);
if (result.length != 2) {
return "";
} 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.StringUtils;
/**
* Delegates the diagram generation from the UML source and
* the filling of the HTTP response with the diagram in the right format.
* Its own responsibility is to produce the right HTTP headers.
/**
* Delegates the diagram generation from the UML source and the filling of the HTTP response with the diagram in the
* right format. Its own responsibility is to produce the right HTTP headers.
*/
class DiagramResponse {
private HttpServletResponse response;
@ -52,31 +51,29 @@ class DiagramResponse {
contentType = Collections.unmodifiableMap(map);
}
DiagramResponse( HttpServletResponse r, FileFormat f) {
DiagramResponse(HttpServletResponse r, FileFormat f) {
response = r;
format = f;
}
void sendDiagram( String uml) throws IOException {
void sendDiagram(String uml) throws IOException {
long today = System.currentTimeMillis();
if ( StringUtils.isDiagramCacheable( uml)) {
if (StringUtils.isDiagramCacheable(uml)) {
// Add http headers to force the browser to cache the image
response.addDateHeader( "Expires", today + 31536000000L);
response.addDateHeader("Expires", today + 31536000000L);
// today + 1 year
response.addDateHeader( "Last-Modified", 1261440000000L);
response.addDateHeader("Last-Modified", 1261440000000L);
// 2009 dec 22 constant date in the past
response.addHeader( "Cache-Control", "public");
response.addHeader("Cache-Control", "public");
}
response.setContentType( getContentType());
SourceStringReader reader = new SourceStringReader( uml);
reader.generateImage( response.getOutputStream(), new FileFormatOption(format));
response.setContentType(getContentType());
SourceStringReader reader = new SourceStringReader(uml);
reader.generateImage(response.getOutputStream(), new FileFormatOption(format));
response.flushBuffer();
}
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 {
@Override
public String getSource( String uri) {
public String getSource(String uri) {
String[] result = uri.split("/img/", 2);
if (result.length != 2) {
return "";
} else {
return result[1];
return result[1];
}
}

View File

@ -64,7 +64,8 @@ import HTTPClient.ParseException;
public class PlantUmlServlet extends HttpServlet {
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 proxyPattern = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
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 String format;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String uri = request.getRequestURI();
@ -76,15 +75,15 @@ public class ProxyServlet extends HttpServlet {
dispatcher.forward(request, response);
}
}
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()));
}
private String getSource( String uri) throws IOException {
private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null);
final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
@ -105,7 +104,7 @@ public class ProxyServlet extends HttpServlet {
}
private FileFormat getOutputFormat() {
if (format==null) {
if (format == null) {
return FileFormat.PNG;
}
if (format.equals("svg")) {
@ -115,6 +114,6 @@ public class ProxyServlet extends HttpServlet {
return FileFormat.ATXT;
}
return FileFormat.PNG;
}
}
}

View File

@ -33,12 +33,12 @@ import net.sourceforge.plantuml.FileFormat;
public class SvgServlet extends UmlDiagramService {
@Override
public String getSource( String uri) {
public String getSource(String uri) {
String[] result = uri.split("/svg/", 2);
if (result.length != 2) {
return "";
} 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;
/**
* Common service servlet to produce diagram from compressed UML source
* contained in the end part of the requested URI.
* Common service servlet to produce diagram from compressed UML source contained in the end part of the requested URI.
*/
@SuppressWarnings("serial")
public abstract class UmlDiagramService extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// 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();
text = transcoder.decode(text);
// encapsulate the UML syntax if necessary
// encapsulate the UML syntax if necessary
String uml;
if (text.startsWith("@start")) {
uml = text;
} else {
StringBuilder plantUmlSource = new StringBuilder();
plantUmlSource.append( "@startuml\n");
plantUmlSource.append( text);
if (text.endsWith( "\n") == false) {
plantUmlSource.append( "\n");
plantUmlSource.append("@startuml\n");
plantUmlSource.append(text);
if (text.endsWith("\n") == false) {
plantUmlSource.append("\n");
}
plantUmlSource.append( "@enduml");
plantUmlSource.append("@enduml");
uml = plantUmlSource.toString();
}
// generate the response
DiagramResponse dr = new DiagramResponse( response, getOutputFormat());
DiagramResponse dr = new DiagramResponse(response, getOutputFormat());
try {
dr.sendDiagram(uml);
} catch (IIOException iioe) {
// Browser has closed the connection, do nothing
// Browser has closed the connection, do nothing
}
dr = null;
}
/**
* 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
*/
abstract public String getSource( String uri);
abstract public String getSource(String uri);
/**
* Gives the wished output format of the diagram.
* This value is used by the DiagramResponse class.
* Gives the wished output format of the diagram. This value is used by the DiagramResponse class.
*
* @return the format
*/
abstract public FileFormat getOutputFormat();
private Transcoder getTranscoder() {
return TranscoderUtil.getDefaultTranscoder();
}

View File

@ -38,17 +38,16 @@ import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class Welcome extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) 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.encoded", "SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
}
}
}

View File

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

View File

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

View File

@ -8,23 +8,22 @@ import com.meterware.httpunit.WebResponse;
public class TestForm extends WebappTestCase {
/**
* Verifies that the welcome page has exactly two form
* with the Bob --> Alice sample
*/
/**
* 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 );
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"));
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);
assertEquals(1, response.getImages().length);
}
}
/**
* Verifies that the version image is generated
@ -33,19 +32,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request );
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 );
assertEquals(2, forms.length);
// Ensure the Text field is correct
assertEquals( "version", forms[0].getParameterValue("text"));
assertEquals("version", forms[0].getParameterValue("text"));
// 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
assertEquals( 1, response.getImages().length);
assertEquals(1, response.getImages().length);
}
/**
@ -55,19 +54,19 @@ public class TestForm extends WebappTestCase {
WebConversation conversation = new WebConversation();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request );
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 );
assertEquals(2, forms.length);
// Ensure the Text field is empty
assertNull( forms[0].getParameterValue("text"));
assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty
assertTrue( forms[1].getParameterValue("url").isEmpty());
assertTrue(forms[1].getParameterValue("url").isEmpty());
// 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();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request );
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 );
assertEquals(2, forms.length);
// Ensure the Text field is empty
assertNull( forms[0].getParameterValue("text"));
assertNull(forms[0].getParameterValue("text"));
// Ensure the URL field is empty
assertTrue( forms[1].getParameterValue("url").isEmpty());
assertTrue(forms[1].getParameterValue("url").isEmpty());
// 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();
// Fill the form and submit it
WebRequest request = new GetMethodWebRequest(getServerUrl());
WebResponse response = TestUtils.tryGetResponse(conversation, request );
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 );
assertEquals(2, forms.length);
// 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
assertTrue( forms[1].getParameterValue("url").endsWith("/img/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00"));
assertTrue(forms[1].getParameterValue("url").endsWith("/img/SoWkIImgISaiIKnKuDBIrRLJu798pKi12m00"));
// 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 {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/AqijAixCpmC0");
WebResponse response = conversation.getResource( request);
WebResponse response = conversation.getResource(request);
// 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", 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);
while ((n = responseStream.read(buf)) != -1) {
imageStream.write(buf, 0, n);
}
imageStream.close();
byte[] inMemoryImage = imageStream.toByteArray();
int diagramLen = inMemoryImage.length;
assertTrue( diagramLen > 10000);
assertTrue( diagramLen < 20000);
assertTrue(diagramLen > 10000);
assertTrue(diagramLen < 20000);
responseStream.close();
}
@ -45,21 +45,21 @@ public class TestImage extends WebappTestCase {
WebConversation conversation = new WebConversation();
// Bob -> Alice : hello
WebRequest request = new GetMethodWebRequest(getServerUrl() + "img/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000");
WebResponse response = conversation.getResource( request);
WebResponse response = conversation.getResource(request);
// 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", response.getContentType());
// 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
assertNotNull( response.getHeaderField( "Last-Modified"));
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()));
Date lastModified = format.parse(response.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) {
while (responseStream.read() != -1) {
; // Do nothing
}
}

View File

@ -15,41 +15,39 @@ public class TestProxy extends WebappTestCase {
*/
public void testDefaultProxy() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/"
+ getServerUrl() + "welcome");
WebResponse response = conversation.getResource( request);
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());
// assertEquals( "Response content type is not PNG", "image/png", response.getContentType());
// 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);
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 < 2500);
assertTrue(diagramLen > 1500);
assertTrue(diagramLen < 2500);
}
public void testProxyWithFormat() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/"
+ getServerUrl() + "welcome");
WebResponse response = conversation.getResource( request);
WebRequest request = new GetMethodWebRequest(getServerUrl() + "proxy/svg/" + getServerUrl() + "welcome");
WebResponse response = conversation.getResource(request);
// Analyze response
// Verifies the Content-Type header
// TODO 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 > 1000);
assertTrue( diagramLen < 3000);
assertTrue(diagramLen > 1000);
assertTrue(diagramLen < 3000);
}
/**
@ -59,11 +57,11 @@ public class TestProxy extends WebappTestCase {
WebConversation conversation = new WebConversation();
// Try to proxify an invalid address
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
// 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();
assertEquals( 2, forms.length );
assertEquals(2, forms.length);
}
}

View File

@ -8,33 +8,34 @@ import com.meterware.httpunit.*;
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 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() {
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
/**
* 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
* @throws an
* Exception if getting the response fails
*/
public static WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception {
WebResponse response=null;
public static WebResponse tryGetResponse(WebConversation conversation, WebRequest request) throws Exception {
WebResponse response = null;
try {
response = conversation.getResponse( request );
response = conversation.getResponse(request);
} 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;
}
return response;
}
}