Skip to content

Commit

Permalink
update junit test classes and there dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian authored and arnaudroques committed Oct 11, 2021
1 parent 221af78 commit 8d5be87
Show file tree
Hide file tree
Showing 20 changed files with 700 additions and 469 deletions.
22 changes: 6 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<skipTests>true</skipTests>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -174,6 +174,7 @@
<maven.build.timestamp.format>yyyyMMdd-HHmm
</maven.build.timestamp.format>
<timestamp>${maven.build.timestamp}</timestamp>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
Expand All @@ -191,11 +192,6 @@
<artifactId>codemirror</artifactId>
<version>3.21</version>
</dependency>
<dependency>
<groupId>HTTPClient</groupId>
<artifactId>HTTPClient</artifactId>
<version>0.3-3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand All @@ -205,19 +201,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>httpunit</groupId>
<artifactId>httpunit</artifactId>
<version>1.7</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>rhino</groupId>
<artifactId>js</artifactId>
<version>1.7R2</version>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.53.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
68 changes: 30 additions & 38 deletions src/main/java/net/sourceforge/plantuml/servlet/OldProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@
*/
package net.sourceforge.plantuml.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import HTTPClient.CookieModule;
import HTTPClient.HTTPConnection;
import HTTPClient.HTTPResponse;
import HTTPClient.ModuleException;
import HTTPClient.ParseException;

import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
Expand All @@ -52,54 +48,50 @@
@SuppressWarnings("serial")
public class OldProxyServlet extends HttpServlet {

private static final Pattern PROXY_PATTERN = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(http://.*)");
private static final Pattern PROXY_PATTERN = Pattern.compile("/\\w+/proxy/((\\d+)/)?((\\w+)/)?(https?://.*)");
private String format;

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

final String uri = request.getRequestURI();

// Check if the src URL is valid
Matcher proxyMatcher = PROXY_PATTERN.matcher(uri);
if (proxyMatcher.matches()) {
String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
} else {
request.setAttribute("net.sourceforge.plantuml.servlet.decoded", "ERROR Invalid proxy syntax : " + uri);
request.removeAttribute("net.sourceforge.plantuml.servlet.encoded");

// forward to index.jsp
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
if (!proxyMatcher.matches()) {
// Bad URI format.
response.setStatus(400);
return;
}

String num = proxyMatcher.group(2); // Optional number of the diagram source
format = proxyMatcher.group(4); // Expected format of the generated diagram
String sourceURL = proxyMatcher.group(5);
handleImageProxy(response, num, sourceURL);
}

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(), false));
FileFormat fileFormat = getOutputFormat();
response.addHeader("Content-Type", fileFormat.getMimeType());
reader.outputImage(response.getOutputStream(), n, new FileFormatOption(fileFormat, false));
}

private String getSource(String uri) throws IOException {
CookieModule.setCookiePolicyHandler(null);

final Pattern p = Pattern.compile("http://[^/]+(/?.*)");
final Matcher m = p.matcher(uri);
if (m.find() == false) {
throw new IOException(uri);
}
private String getSource(final String uri) throws IOException {
final URL url = new URL(uri);
final HTTPConnection httpConnection = new HTTPConnection(url);
try {
final HTTPResponse resp = httpConnection.Get(m.group(1));
return resp.getText();
} catch (ModuleException e) {
throw new IOException(e.toString());
} catch (ParseException e) {
throw new IOException(e.toString());
try (
InputStream responseStream = url.openStream();
InputStreamReader isr = new InputStreamReader(responseStream);
BufferedReader br = new BufferedReader(isr);
) {
String line;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString().trim();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
response.setStatus(400);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<!-- <script src="mode/plantuml.js"></script> -->
<script>
window.onload = function() {
var myCodeMirror = CodeMirror.fromTextArea(
document.myCodeMirror = CodeMirror.fromTextArea(
document.getElementById("text"),
{lineNumbers: true}
);
Expand Down
24 changes: 13 additions & 11 deletions src/test/java/net/sourceforge/plantuml/servlet/TestAsciiArt.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;


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/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/txt/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals("Response character set is not UTF-8", "UTF-8", response.getCharacterSet());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 200);
assertTrue(diagramLen < 250);
Expand Down
43 changes: 25 additions & 18 deletions src/test/java/net/sourceforge/plantuml/servlet/TestCharset.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
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;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;


public class TestCharset extends WebappTestCase {

/**
* Verifies the preservation of unicode characters for the "Bob -> Alice : hell‽" sample
*/
public void testUnicodeSupport() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "txt/SyfFKj2rKt3CoKnELR1Io4ZDoNdKi1S0");
WebResponse response = conversation.getResource(request);
public void testUnicodeSupport() throws IOException {
final URL url = new URL(getServerUrl() + "/txt/SyfFKj2rKt3CoKnELR1Io4ZDoNdKi1S0");
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify that the interrobang unicode character is present
String diagram = response.getText();
String diagram = getContentText(conn);
assertTrue("Interrobang unicode character is not preserved", diagram.contains("‽"));
}

/**
* Verifies the preservation of unicode characters for the
* "participant Bob [[http://www.snow.com/❄]]\nBob -> Alice" sample
*/
public void testUnicodeInCMap() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzShpiilrqlEpzL_DBSbDfOB9Azhf-2OavcS2W00");
WebResponse response = conversation.getResource(request);
public void testUnicodeInCMap() throws IOException {
final URL url = new URL(
getServerUrl() +
"/map/AqWiAibCpYn8p2jHSCfFKeYEpYWfAR3IroylBzShpiilrqlEpzL_DBSbDfOB9Azhf-2OavcS2W00"
);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content and verify that the snow unicode character is present
String map = response.getText();
String map = getContentText(conn);
assertTrue("Snow unicode character is not preserved", map.contains("❄"));
}


}
40 changes: 21 additions & 19 deletions src/test/java/net/sourceforge/plantuml/servlet/TestCheck.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
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;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;


public class TestCheck extends WebappTestCase {

/**
* Verifies the generation of a syntax check for the following sample:
* Bob -> Alice : hello
*/
public void testCorrectSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl()
+ "check/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testCorrectSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/check/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not TEXT PLAIN", "text/plain", response.getContentType());
assertEquals("Response character set is not UTF-8", "UTF-8", response.getCharacterSet());
assertEquals(
"Response content type is not TEXT PLAIN or UTF-8",
"text/plain;charset=utf-8",
conn.getContentType().toLowerCase()
);
// Get the content, check its first characters and verify its size
String checkResult = response.getText();
assertTrue("Response content is not starting with (2 participants)",
checkResult.startsWith("(2 participants)"));
String checkResult = getContentText(conn);
assertTrue(
"Response content is not starting with (2 participants)",
checkResult.startsWith("(2 participants)")
);
int checkLen = checkResult.length();
assertTrue(checkLen > 1);
assertTrue(checkLen < 100);
Expand All @@ -32,12 +36,10 @@ public void testCorrectSequenceDiagram() throws Exception {
* Check the syntax of an invalid sequence diagram :
* Bob -
*/
public void testWrongDiagramSyntax() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "check/SyfFKj050000");
WebResponse response = conversation.getResource(request);
public void testWrongDiagramSyntax() throws IOException {
final URL url = new URL(getServerUrl() + "/check/SyfFKj050000");
// Analyze response
String checkResult = response.getText();
String checkResult = getContentText(url);
assertTrue("Response is not an error", checkResult.startsWith("(Error)"));
}

Expand Down
25 changes: 14 additions & 11 deletions src/test/java/net/sourceforge/plantuml/servlet/TestEPS.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
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;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import java.util.Scanner;

public class TestEPS extends WebappTestCase {

/**
* Verifies the generation of the EPS for the Bob -> Alice sample
*/
public void testSimpleSequenceDiagram() throws Exception {
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(getServerUrl() + "eps/" + TestUtils.SEQBOB);
WebResponse response = conversation.getResource(request);
public void testSimpleSequenceDiagram() throws IOException {
final URL url = new URL(getServerUrl() + "/eps/" + TestUtils.SEQBOB);
final URLConnection conn = url.openConnection();
// Analyze response
// Verifies the Content-Type header
assertEquals("Response content type is not EPS", "application/postscript", response.getContentType());
assertEquals(
"Response content type is not EPS",
"application/postscript",
conn.getContentType().toLowerCase()
);
// Get the content and verify its size
String diagram = response.getText();
String diagram = getContentText(conn);
int diagramLen = diagram.length();
assertTrue(diagramLen > 10000);
assertTrue(diagramLen < 12000);
}

}
Loading

0 comments on commit 8d5be87

Please sign in to comment.