This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-4043-moves-test-utils in repository https://gitbox.apache.org/repos/asf/struts.git
commit 592c942c764c3eeca69ca04a4e93f824dcbd6a53 Author: Lukasz Lenart <lukaszlen...@apache.org> AuthorDate: Sun Apr 26 09:31:18 2020 +0200 WW-4043 Moves TestUtils into junit-plugin --- .../test/java/org/apache/struts2/TestUtils.java | 94 ---------------------- .../apache/struts2/json/DefaultJSONWriterTest.java | 1 + .../apache/struts2/json/JSONInterceptorTest.java | 1 + .../org/apache/struts2/json/JSONPopulatorTest.java | 1 + .../org/apache/struts2/json/JSONResultTest.java | 3 +- .../json/JSONValidationInterceptorTest.java | 1 + .../java/org/apache/struts2/util}/TestUtils.java | 42 +++++----- 7 files changed, 23 insertions(+), 120 deletions(-) diff --git a/core/src/test/java/org/apache/struts2/TestUtils.java b/core/src/test/java/org/apache/struts2/TestUtils.java deleted file mode 100644 index 32f5630..0000000 --- a/core/src/test/java/org/apache/struts2/TestUtils.java +++ /dev/null @@ -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(); - } -} diff --git a/plugins/json/src/test/java/org/apache/struts2/json/DefaultJSONWriterTest.java b/plugins/json/src/test/java/org/apache/struts2/json/DefaultJSONWriterTest.java index 1f34921..05e3280 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/DefaultJSONWriterTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/DefaultJSONWriterTest.java @@ -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; diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java index 9f6b76f..5b9ad8b 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java @@ -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; diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONPopulatorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONPopulatorTest.java index b2101c6..dd6fa72 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONPopulatorTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONPopulatorTest.java @@ -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 { diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java index 5062103..5f6aef7 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONResultTest.java @@ -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; diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java index a4fd58d..d0bd903 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONValidationInterceptorTest.java @@ -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; diff --git a/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java b/plugins/junit/src/main/java/org/apache/struts2/util/TestUtils.java similarity index 71% rename from plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java rename to plugins/junit/src/main/java/org/apache/struts2/util/TestUtils.java index f48e07b..131f009 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/TestUtils.java +++ b/plugins/junit/src/main/java/org/apache/struts2/util/TestUtils.java @@ -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); } }