Skip to content

Commit ee3eddd

Browse files
authored
Merge pull request apache#408 from apache/WW-4043-moves-test-utils
[WW-4043] Moves TestUtils into junit-plugin
2 parents 387c203 + 1e14438 commit ee3eddd

File tree

7 files changed

+37
-122
lines changed

7 files changed

+37
-122
lines changed

core/src/test/java/org/apache/struts2/TestUtils.java

Lines changed: 0 additions & 94 deletions
This file was deleted.

plugins/json/src/test/java/org/apache/struts2/json/DefaultJSONWriterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.apache.struts2.StrutsTestCase;
2222
import org.apache.struts2.json.annotations.JSONFieldBridge;
2323
import org.apache.struts2.json.bridge.StringBridge;
24+
import org.apache.struts2.util.TestUtils;
2425
import org.junit.Test;
2526

2627
import java.net.URL;

plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.apache.struts2.StrutsStatics;
2626
import org.apache.struts2.StrutsTestCase;
27+
import org.apache.struts2.util.TestUtils;
2728
import org.springframework.mock.web.MockHttpServletRequest;
2829
import org.springframework.mock.web.MockHttpServletResponse;
2930
import org.springframework.mock.web.MockServletContext;

plugins/json/src/test/java/org/apache/struts2/json/JSONPopulatorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828

2929
import junit.framework.TestCase;
30+
import org.apache.struts2.util.TestUtils;
3031

3132
public class JSONPopulatorTest extends TestCase {
3233

plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@
3535

3636
import javax.servlet.http.HttpServletResponse;
3737

38-
import org.aopalliance.intercept.MethodInterceptor;
39-
import org.aopalliance.intercept.MethodInvocation;
4038
import org.apache.struts2.StrutsStatics;
4139
import org.apache.struts2.StrutsTestCase;
40+
import org.apache.struts2.util.TestUtils;
4241
import org.springframework.aop.framework.ProxyFactory;
4342
import org.springframework.mock.web.MockHttpServletRequest;
4443
import org.springframework.mock.web.MockHttpServletResponse;

plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.struts2.StrutsTestCase;
3333
import org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor;
3434
import org.apache.struts2.interceptor.validation.SkipValidation;
35+
import org.apache.struts2.util.TestUtils;
3536

3637
import javax.servlet.http.HttpServletResponse;
3738
import java.io.PrintWriter;

plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java renamed to plugins/junit/src/main/java/org/apache/struts2/util/TestUtils.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.struts2.json;
19+
package org.apache.struts2.util;
2020

2121
import org.apache.commons.io.IOUtils;
2222
import org.apache.commons.lang3.StringUtils;
2323
import org.junit.Assert;
2424

2525
import java.net.URL;
26+
import java.nio.charset.Charset;
27+
import java.nio.charset.StandardCharsets;
2628
import java.util.regex.Matcher;
2729
import java.util.regex.Pattern;
2830

2931
/**
3032
* Utility methods for test classes
3133
*/
3234
public class TestUtils {
35+
3336
/**
3437
* A regex pattern for recognizing blocks of whitespace characters.
3538
*/
@@ -39,21 +42,16 @@ public class TestUtils {
3942
* normalizes a string so that strings generated on different platforms can
4043
* be compared. any group of one or more space, tab, \r, and \n characters
4144
* are converted to a single space character
42-
*
43-
* @param obj
44-
* the object to be normalized. normalize will perform its
45-
* operation on obj.toString().trim() ;
46-
* @param appendSpace
45+
*
46+
* @param json the JSON to be normalized. normalize will trim before starting
47+
* @param removeSpaces removes white spaces from the JSON or not
4748
* @return the normalized string
4849
*/
49-
public static String normalize(Object obj, boolean appendSpace) {
50-
Matcher matcher = WHITESPACE_BLOCK.matcher(StringUtils.trim(obj.toString()));
51-
/*
52-
FIXME: appendSpace has been always ignored, uncommenting the following line will cause dozen of test fails
53-
if (appendSpace) {
54-
return matcher.replaceAll(" ");
50+
public static String normalize(String json, boolean removeSpaces) {
51+
Matcher matcher = WHITESPACE_BLOCK.matcher(StringUtils.trim(json));
52+
if (removeSpaces) {
53+
return matcher.replaceAll("").replaceAll(" ", "");
5554
}
56-
*/
5755
return matcher.replaceAll("");
5856
}
5957

@@ -64,17 +62,15 @@ public static String normalize(URL url) throws Exception {
6462
/**
6563
* Attempt to verify the contents of text against the contents of the URL
6664
* specified. Performs a trim on both ends
67-
*
68-
* @param url
69-
* the HTML snippet that we want to validate against
70-
* @throws Exception
71-
* if the validation failed
65+
*
66+
* @param url the HTML snippet that we want to validate against
67+
* @throws Exception if the validation failed
7268
*/
7369
public static boolean compare(URL url, String text) throws Exception {
74-
/**
75-
* compare the trimmed values of each buffer and make sure they're
76-
* equivalent. however, let's make sure to normalize the strings first
77-
* to account for line termination differences between platforms.
70+
/*
71+
compare the trimmed values of each buffer and make sure they're
72+
equivalent. however, let's make sure to normalize the strings first
73+
to account for line termination differences between platforms.
7874
*/
7975
String writerString = TestUtils.normalize(text, true);
8076
String bufferString = TestUtils.normalize(readContent(url), true);
@@ -85,13 +81,23 @@ public static boolean compare(URL url, String text) throws Exception {
8581
public static void assertEquals(URL source, String text) throws Exception {
8682
String writerString = TestUtils.normalize(text, true);
8783
String bufferString = TestUtils.normalize(readContent(source), true);
88-
Assert.assertEquals(bufferString,writerString);
84+
Assert.assertEquals(bufferString, writerString);
8985
}
9086

9187
public static String readContent(URL url) throws Exception {
92-
if (url == null)
93-
throw new Exception("unable to verify a null URL");
88+
return readContent(url, StandardCharsets.UTF_8);
89+
}
9490

95-
return IOUtils.toString(url.openStream());
91+
public static String readContent(URL url, Charset encoding) throws Exception {
92+
if (url == null) {
93+
throw new IllegalArgumentException("Unable to verify a null URL");
94+
}
95+
96+
if (encoding == null) {
97+
throw new IllegalArgumentException("Unable to verify the URL using a null Charset");
98+
}
99+
100+
return IOUtils.toString(url.openStream(), encoding);
96101
}
102+
97103
}

0 commit comments

Comments
 (0)