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
2121import org .apache .commons .io .IOUtils ;
2222import org .apache .commons .lang3 .StringUtils ;
2323import org .junit .Assert ;
2424
2525import java .net .URL ;
26+ import java .nio .charset .Charset ;
27+ import java .nio .charset .StandardCharsets ;
2628import java .util .regex .Matcher ;
2729import java .util .regex .Pattern ;
2830
2931/**
3032 * Utility methods for test classes
3133 */
3234public 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