From acb20f2f17cd2fde70b045d5e117228178ea8a99 Mon Sep 17 00:00:00 2001 From: Simon Urli Date: Wed, 15 Jun 2022 15:27:06 +0200 Subject: [PATCH] XWIKI-19840: Use the LogCaptureValidator in webstandard validation tests (cherry picked from commit 369237122454a7216d543a34f66279cfb04caef1) --- .../framework/DefaultValidationTest.java | 82 +++++++++---------- .../framework/DocumentReferenceTarget.java | 24 ++++++ 2 files changed, 65 insertions(+), 41 deletions(-) diff --git a/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DefaultValidationTest.java b/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DefaultValidationTest.java index e31dda4af0b..5a7402295dd 100644 --- a/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DefaultValidationTest.java +++ b/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DefaultValidationTest.java @@ -24,16 +24,16 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.io.StringReader; -import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Locale; import java.util.Set; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.lang3.StringUtils; import org.xwiki.model.reference.DocumentReference; +import org.xwiki.test.integration.junit.LogCaptureConfiguration; +import org.xwiki.test.integration.junit.LogCaptureValidator; import org.xwiki.validator.ValidationError; import org.xwiki.validator.Validator; @@ -63,50 +63,29 @@ public class DefaultValidationTest extends AbstractValidationTest */ protected PrintStream stderr; - /** - * This map contains the warning logs that are expected during the test. - */ - protected Map> expectedWarningLogs; - /** * The new stderr stream we're using to replace the default console output. */ protected ByteArrayOutputStream err; + private LogCaptureValidator logCaptureValidator; + private LogCaptureConfiguration logCaptureConfiguration; + public DefaultValidationTest(Target target, HttpClient client, Validator validator, String credentials) throws Exception { super("testDocumentValidity", target, client, credentials); + this.logCaptureValidator = new LogCaptureValidator(); this.validator = validator; - this.expectedWarningLogs = new HashMap<>(); - this.initializeExpectedWarningLogs(); - } - - private void initializeExpectedWarningLogs() - { - Target resetPassword = new DocumentReferenceTarget(new DocumentReference("xwiki", "XWiki", "ResetPassword")); - this.expectedWarningLogs.put(resetPassword, - Collections.singletonList("[DEPRECATED] The page [XWiki.ResetPassword] should not be used anymore in favor " - + "of the new 'authenticate/reset' URL.")); - - Target resetPasswordComplete = - new DocumentReferenceTarget(new DocumentReference("xwiki", "XWiki", "ResetPasswordComplete")); - this.expectedWarningLogs.put(resetPasswordComplete, - Collections.singletonList("The page [XWiki.ResetPasswordComplete] should not be used anymore in favor of " - + "the new 'authenticate/reset' URL.")); - - Target forgotUsername = - new DocumentReferenceTarget(new DocumentReference("xwiki", "XWiki", "ForgotUsername")); - this.expectedWarningLogs.put(forgotUsername, - Collections.singletonList("The page [XWiki.ForgotUsername] should not be used anymore in favor of the " - + "new 'authenticate/forgot' URL.")); } @Override protected void setUp() throws Exception { super.setUp(); + this.logCaptureConfiguration = new LogCaptureConfiguration(); + this.registerExpectedLogs(); // TODO Until we find a way to incrementally display the result of tests this stays System.out.println(getName()); @@ -121,6 +100,30 @@ protected void setUp() throws Exception System.setErr(new PrintStream(this.err)); } + private void registerExpectedLogs() + { + Target resetPassword = new DocumentReferenceTarget(new DocumentReference("xwiki", "XWiki", "ResetPassword", + Locale.ROOT)); + if (this.target.equals(resetPassword)) { + this.logCaptureConfiguration.registerExpected("[DEPRECATED] The page [XWiki.ResetPassword] " + + "should not be used anymore in favor of the new 'authenticate/reset' URL."); + } + + Target resetPasswordComplete = new DocumentReferenceTarget( + new DocumentReference("xwiki", "XWiki", "ResetPasswordComplete", Locale.ROOT)); + if (this.target.equals(resetPasswordComplete)) { + this.logCaptureConfiguration.registerExpected("[DEPRECATED] The page [XWiki.ResetPasswordComplete] " + + "should not be used anymore in favor of the new 'authenticate/reset' URL."); + } + + Target forgotUsername = new DocumentReferenceTarget( + new DocumentReference("xwiki", "XWiki", "ForgotUsername", Locale.ROOT)); + if (this.target.equals(forgotUsername)) { + this.logCaptureConfiguration.registerExpected("[DEPRECATED] The page [XWiki.ForgotUsername] " + + "should not be used anymore in favor of the new 'authenticate/forgot' URL."); + } + } + @Override protected void tearDown() throws Exception { @@ -135,11 +138,11 @@ protected void tearDown() throws Exception // Detect server-side error/warning messages from the stdout assertFalse(String.format("Errors found in the stdout output: [%s]", output), hasLogErrors(output)); - assertFalse(String.format("Warnings found in the stdout output: [%s]", output), hasLogWarnings(output)); + this.logCaptureValidator.validate(output, this.logCaptureConfiguration, false); // Detect server-side error/warning messages from the stderr assertFalse(String.format("Errors found in the stderr output: [%s]", errput), hasLogErrors(errput)); - assertFalse(String.format("Warnings found in the stderr output: [%s]", errput), hasLogWarnings(errput)); + this.logCaptureValidator.validate(errput, this.logCaptureConfiguration); super.tearDown(); } @@ -166,15 +169,12 @@ public void testDocumentValidity() throws Exception boolean hasError = false; for (ValidationError error : errors) { if (error.getType() == ValidationError.Type.WARNING) { - List expectedLogs = this.expectedWarningLogs.getOrDefault(this.target, Collections.emptyList()); - if (!expectedLogs.contains(error.getMessage())) { - if (error.getLine() >= 0) { - System.out - .println( - "Warning at " + error.getLine() + ":" + error.getColumn() + " " + error.getMessage()); - } else { - System.out.println("Warning " + error.getMessage()); - } + if (error.getLine() >= 0) { + System.out + .println( + "Warning at " + error.getLine() + ":" + error.getColumn() + " " + error.getMessage()); + } else { + System.out.println("Warning " + error.getMessage()); } } else { if (error.getLine() >= 0) { diff --git a/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DocumentReferenceTarget.java b/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DocumentReferenceTarget.java index baf24b1f47f..771ab037b86 100644 --- a/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DocumentReferenceTarget.java +++ b/xwiki-platform-distribution/xwiki-platform-distribution-flavor/xwiki-platform-distribution-flavor-test/xwiki-platform-distribution-flavor-test-webstandards/src/test/it/org/xwiki/test/webstandards/framework/DocumentReferenceTarget.java @@ -19,6 +19,8 @@ */ package org.xwiki.test.webstandards.framework; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.xwiki.model.reference.DocumentReference; public class DocumentReferenceTarget implements Target @@ -40,4 +42,26 @@ public String getName() { return documentReference.toString(); } + + @Override + public boolean equals(Object o) + { + if (this == o) { + return true; + } + + if (o == null || getClass() != o.getClass()) { + return false; + } + + DocumentReferenceTarget that = (DocumentReferenceTarget) o; + + return new EqualsBuilder().append(documentReference, that.documentReference) + .isEquals(); + } + + @Override public int hashCode() + { + return new HashCodeBuilder().append(documentReference).toHashCode(); + } }