mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
WW-4043 Moves TestUtils into junit-plugin
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* Utility methods for test classes
|
||||
*
|
||||
*/
|
||||
public class TestUtils {
|
||||
/**
|
||||
* normalizes a string so that strings generated on different platforms can be compared. any group of one or more
|
||||
* space, tab, \r, and \n characters are converted to a single space character
|
||||
*
|
||||
* @param obj the object to be normalized. normalize will perform its operation on obj.toString().trim() ;
|
||||
* @param appendSpace
|
||||
* @return the normalized string
|
||||
*/
|
||||
public static String normalize(Object obj, boolean appendSpace) {
|
||||
StringTokenizer st =
|
||||
new StringTokenizer(obj.toString().trim(), " \t\r\n");
|
||||
StringBuilder buffer = new StringBuilder(128);
|
||||
|
||||
while(st.hasMoreTokens()) {
|
||||
buffer.append(st.nextToken());
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String normalize(URL url) throws Exception {
|
||||
return normalize(readContent(url), true);
|
||||
}
|
||||
/**
|
||||
* Attempt to verify the contents of text against the contents of the URL specified. Performs a
|
||||
* trim on both ends
|
||||
*
|
||||
* @param url the HTML snippet that we want to validate against
|
||||
* @throws Exception if the validation failed
|
||||
*/
|
||||
public static boolean compare(URL url, String text)
|
||||
throws Exception {
|
||||
/**
|
||||
* compare the trimmed values of each buffer and make sure they're equivalent. however, let's make sure to
|
||||
* normalize the strings first to account for line termination differences between platforms.
|
||||
*/
|
||||
String writerString = TestUtils.normalize(text, true);
|
||||
String bufferString = TestUtils.normalize(readContent(url), true);
|
||||
|
||||
return bufferString.equals(writerString);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String readContent(URL url)
|
||||
throws Exception {
|
||||
if(url == null) {
|
||||
throw new Exception("unable to verify a null URL");
|
||||
}
|
||||
|
||||
StringBuilder buffer = new StringBuilder(128);
|
||||
try (InputStream in = url.openStream()) {
|
||||
byte[] buf = new byte[4096];
|
||||
int nbytes;
|
||||
|
||||
while ((nbytes = in.read(buf)) > 0) {
|
||||
buffer.append(new String(buf, 0, nbytes));
|
||||
}
|
||||
}
|
||||
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ package org.apache.struts2.json;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
import org.apache.struts2.json.annotations.JSONFieldBridge;
|
||||
import org.apache.struts2.json.bridge.StringBridge;
|
||||
import org.apache.struts2.util.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.net.URL;
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.Map;
|
||||
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
import org.apache.struts2.util.TestUtils;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.apache.struts2.util.TestUtils;
|
||||
|
||||
public class JSONPopulatorTest extends TestCase {
|
||||
|
||||
|
||||
@@ -35,10 +35,9 @@ import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
import org.apache.struts2.util.TestUtils;
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.StrutsTestCase;
|
||||
import org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor;
|
||||
import org.apache.struts2.interceptor.validation.SkipValidation;
|
||||
import org.apache.struts2.util.TestUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
+18
-24
@@ -16,13 +16,14 @@
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.json;
|
||||
package org.apache.struts2.util;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -39,21 +40,16 @@ public class TestUtils {
|
||||
* normalizes a string so that strings generated on different platforms can
|
||||
* be compared. any group of one or more space, tab, \r, and \n characters
|
||||
* are converted to a single space character
|
||||
*
|
||||
* @param obj
|
||||
* the object to be normalized. normalize will perform its
|
||||
* operation on obj.toString().trim() ;
|
||||
* @param appendSpace
|
||||
*
|
||||
* @param json the JSON to be normalized. normalize will trim before starting
|
||||
* @param removeSpaces removes white spaces from the JSON or not
|
||||
* @return the normalized string
|
||||
*/
|
||||
public static String normalize(Object obj, boolean appendSpace) {
|
||||
Matcher matcher = WHITESPACE_BLOCK.matcher(StringUtils.trim(obj.toString()));
|
||||
/*
|
||||
FIXME: appendSpace has been always ignored, uncommenting the following line will cause dozen of test fails
|
||||
if (appendSpace) {
|
||||
return matcher.replaceAll(" ");
|
||||
public static String normalize(String json, boolean removeSpaces) {
|
||||
Matcher matcher = WHITESPACE_BLOCK.matcher(StringUtils.trim(json));
|
||||
if (removeSpaces) {
|
||||
return matcher.replaceAll("").replaceAll(" ", "");
|
||||
}
|
||||
*/
|
||||
return matcher.replaceAll("");
|
||||
}
|
||||
|
||||
@@ -64,17 +60,15 @@ public class TestUtils {
|
||||
/**
|
||||
* Attempt to verify the contents of text against the contents of the URL
|
||||
* specified. Performs a trim on both ends
|
||||
*
|
||||
* @param url
|
||||
* the HTML snippet that we want to validate against
|
||||
* @throws Exception
|
||||
* if the validation failed
|
||||
*
|
||||
* @param url the HTML snippet that we want to validate against
|
||||
* @throws Exception if the validation failed
|
||||
*/
|
||||
public static boolean compare(URL url, String text) throws Exception {
|
||||
/**
|
||||
* compare the trimmed values of each buffer and make sure they're
|
||||
* equivalent. however, let's make sure to normalize the strings first
|
||||
* to account for line termination differences between platforms.
|
||||
/*
|
||||
compare the trimmed values of each buffer and make sure they're
|
||||
equivalent. however, let's make sure to normalize the strings first
|
||||
to account for line termination differences between platforms.
|
||||
*/
|
||||
String writerString = TestUtils.normalize(text, true);
|
||||
String bufferString = TestUtils.normalize(readContent(url), true);
|
||||
@@ -85,13 +79,13 @@ public class TestUtils {
|
||||
public static void assertEquals(URL source, String text) throws Exception {
|
||||
String writerString = TestUtils.normalize(text, true);
|
||||
String bufferString = TestUtils.normalize(readContent(source), true);
|
||||
Assert.assertEquals(bufferString,writerString);
|
||||
Assert.assertEquals(bufferString, writerString);
|
||||
}
|
||||
|
||||
public static String readContent(URL url) throws Exception {
|
||||
if (url == null)
|
||||
throw new Exception("unable to verify a null URL");
|
||||
|
||||
return IOUtils.toString(url.openStream());
|
||||
return IOUtils.toString(url.openStream(), StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user