Pochatkin commented on code in PR #2429: URL: https://github.com/apache/ignite-3/pull/2429#discussion_r1296828017
########## modules/core/src/testFixtures/java/org/apache/ignite/internal/testframework/log4j2/TestLogChecker.java: ########## @@ -0,0 +1,321 @@ +/* + * 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.ignite.internal.testframework.log4j2; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Predicate; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.LoggerConfig; +import org.apache.logging.log4j.core.config.Property; + +/** + * Test log checker. + */ +public class TestLogChecker { + /** Logger name. */ + private final String loggerName; + + /** List of handlers. */ + private final List<Handler> handlers = new ArrayList<>(1); + + /** Lock that is used to synchronize the collection of handlers. */ + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** Test log appender that is used to check log events/messages. */ + private final TestLogAppender appender; + + /** Logger configuration. */ + private Configuration config; + + /** + * Creates a new instance of {@link TestLogChecker} for the given {@code loggerName}. + * + * @return New instance of {@link TestLogChecker}. + **/ + public static TestLogChecker create(String loggerName) { + return create(loggerName, false); + } + + /** + * Creates a new instance of {@link TestLogChecker} for the given {@code loggerName}. + * + * @param started Whether the checker should be started. + * @return New instance of {@link TestLogChecker}. + **/ + public static TestLogChecker create(String loggerName, boolean started) { + TestLogChecker checker = new TestLogChecker(loggerName); + + if (started) { + checker.start(); + } + + return checker; + } + + /** + * Creates a new instance of {@link TestLogChecker} for the given {@code clazz}. + * + * @return New instance of {@link TestLogChecker}. + **/ + public static TestLogChecker create(Class<?> clazz) { + return create(clazz, false); + } + + /** + * Creates a new instance of {@link TestLogChecker} for the given {@code clazz}. + * + * @param started Whether the checker should be started. + * @return New instance of {@link TestLogChecker}. + **/ + public static TestLogChecker create(Class<?> clazz, boolean started) { + return create(clazz.getName(), started); + } + + /** + * Creates a new instance of {@link TestLogChecker}. + * + * @param loggerName Logger name. + */ + public TestLogChecker(String loggerName) { + Objects.requireNonNull(loggerName); + + this.loggerName = loggerName; + this.appender = new TestLogAppender(loggerName); + } + + /** + * Creates a new instance of {@link TestLogChecker} with the given {@code predicate}. + * + * @param loggerName Logger name. + * @param predicate Predicate to check log messages. + */ + public TestLogChecker(String loggerName, Predicate<String> predicate) { + this(loggerName, predicate, () -> {}); + } + + /** + * Creates a new instance of {@link TestLogChecker} with the given {@code predicate} and {@code action}. + * + * @param loggerName Logger name. + * @param predicate Predicate to check log messages. + * @param action Action to be executed when the {@code predicate} is matched. + */ + public TestLogChecker(String loggerName, Predicate<String> predicate, Runnable action) { + this(loggerName, new Handler(predicate, action)); + } + + /** + * Creates a new instance of {@link TestLogChecker} with the given {@code predicate} and {@code action}. + * + * @param loggerName Logger name. + * @param handlers List of handlers to be added. + */ + public TestLogChecker(String loggerName, Handler... handlers) { + this(loggerName); + + lock.writeLock().lock(); + try { + for (Handler handler : handlers) { Review Comment: Collections.addAll(this.handlers, handlers); -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
