You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by za...@apache.org on 2022/06/15 13:31:55 UTC

[hive] branch master updated: HIVE-26309: Remove Log4jConfig junit extension in favor of LoggerContextSource (Stamatis Zampetakis, reviewed by Alessandro Solimando, Laszlo Bodor)

This is an automated email from the ASF dual-hosted git repository.

zabetak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new da96760e83d HIVE-26309: Remove Log4jConfig junit extension in favor of LoggerContextSource (Stamatis Zampetakis, reviewed by Alessandro Solimando, Laszlo Bodor)
da96760e83d is described below

commit da96760e83d8a87e1dc5f9d30f7a2ea29307db9d
Author: Stamatis Zampetakis <za...@gmail.com>
AuthorDate: Thu Jun 9 17:19:32 2022 +0200

    HIVE-26309: Remove Log4jConfig junit extension in favor of LoggerContextSource (Stamatis Zampetakis, reviewed by Alessandro Solimando, Laszlo Bodor)
    
    Closes #3356
---
 llap-server/pom.xml                                |  7 ++
 .../llap/daemon/impl/TestLlapDaemonLogging.java    |  7 +-
 testutils/pom.xml                                  |  5 --
 .../testutils/junit/extensions/Log4jConfig.java    | 37 ----------
 .../junit/extensions/Log4jConfigExtension.java     | 83 ----------------------
 .../junit/extensions/TestLog4jConfigExtension.java | 65 -----------------
 .../src/test/resources/test0-log4j2.properties     | 32 ---------
 .../src/test/resources/test1-log4j2.properties     | 32 ---------
 8 files changed, 10 insertions(+), 258 deletions(-)

diff --git a/llap-server/pom.xml b/llap-server/pom.xml
index 7d1f93cb46b..42251033679 100644
--- a/llap-server/pom.xml
+++ b/llap-server/pom.xml
@@ -334,6 +334,13 @@
         </exclusion>
       </exclusions>
     </dependency>
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-core</artifactId>
+      <version>${log4j2.version}</version>
+      <classifier>tests</classifier>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
diff --git a/llap-server/src/test/org/apache/hadoop/hive/llap/daemon/impl/TestLlapDaemonLogging.java b/llap-server/src/test/org/apache/hadoop/hive/llap/daemon/impl/TestLlapDaemonLogging.java
index 467961a2c43..9b03f0fb162 100644
--- a/llap-server/src/test/org/apache/hadoop/hive/llap/daemon/impl/TestLlapDaemonLogging.java
+++ b/llap-server/src/test/org/apache/hadoop/hive/llap/daemon/impl/TestLlapDaemonLogging.java
@@ -25,8 +25,9 @@ import org.apache.hadoop.security.Credentials;
 import org.apache.hadoop.security.token.Token;
 import org.apache.hive.testutils.junit.extensions.DoNothingTCPServer;
 import org.apache.hive.testutils.junit.extensions.DoNothingTCPServerExtension;
-import org.apache.hive.testutils.junit.extensions.Log4jConfig;
+import org.apache.logging.log4j.junit.LoggerContextSource;
 import org.apache.tez.common.security.TokenCache;
+
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 
@@ -44,10 +45,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 /**
  * Tests for the log4j configuration of the LLAP daemons.
  */
+@LoggerContextSource("llap-daemon-routing-log4j2.properties")
 public class TestLlapDaemonLogging {
 
   @Test
-  @Log4jConfig("llap-daemon-routing-log4j2.properties")
   @ExtendWith(LlapDaemonExtension.class)
   @ExtendWith(DoNothingTCPServerExtension.class)
   void testQueryRoutingNoLeakFileDescriptors(LlapDaemon daemon, DoNothingTCPServer amMockServer)
@@ -74,7 +75,6 @@ public class TestLlapDaemonLogging {
   }
 
   @Test
-  @Log4jConfig("llap-daemon-routing-log4j2.properties")
   @ExtendWith(LlapDaemonExtension.class)
   @ExtendWith(DoNothingTCPServerExtension.class)
   void testQueryRoutingLogFileNameOnIncompleteQuery(LlapDaemon daemon, DoNothingTCPServer amMockServer)
@@ -97,7 +97,6 @@ public class TestLlapDaemonLogging {
   }
 
   @Test
-  @Log4jConfig("llap-daemon-routing-log4j2.properties")
   @ExtendWith(LlapDaemonExtension.class)
   @ExtendWith(DoNothingTCPServerExtension.class)
   void testQueryRoutingLogFileNameOnCompleteQuery(LlapDaemon daemon, DoNothingTCPServer amMockServer)
diff --git a/testutils/pom.xml b/testutils/pom.xml
index d2d4fe27061..431c4e32601 100644
--- a/testutils/pom.xml
+++ b/testutils/pom.xml
@@ -37,11 +37,6 @@
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.apache.logging.log4j</groupId>
-      <artifactId>log4j-core</artifactId>
-      <version>${log4j2.version}</version>
-    </dependency>
     <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter-api</artifactId>
diff --git a/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfig.java b/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfig.java
deleted file mode 100644
index dd96941ff42..00000000000
--- a/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfig.java
+++ /dev/null
@@ -1,37 +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.hive.testutils.junit.extensions;
-
-import org.junit.jupiter.api.extension.ExtendWith;
-
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-
-/**
- * Indicates the Log4j2 configuration to be used for the duration of a test.
- */
-@Retention(RetentionPolicy.RUNTIME)
-@ExtendWith(Log4jConfigExtension.class)
-public @interface Log4jConfig {
-  /**
-   * Returns the name of the log4j2 configuration file to use.
-   *
-   * The file should be present in the resources.
-   */
-  String value();
-}
diff --git a/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfigExtension.java b/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfigExtension.java
deleted file mode 100644
index 774b9287b11..00000000000
--- a/testutils/src/java/org/apache/hive/testutils/junit/extensions/Log4jConfigExtension.java
+++ /dev/null
@@ -1,83 +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.hive.testutils.junit.extensions;
-
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
-import org.junit.jupiter.api.extension.ExtensionContext;
-
-import java.lang.reflect.AnnotatedElement;
-import java.net.URL;
-import java.util.Optional;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.locks.ReentrantLock;
-
-/**
- * A JUnit Jupiter extension that loads a log4j2 configuration from a file present in the resources of the project
- * when the test uses the {@link Log4jConfig} annotation.
- *
- * The configuration that was in use before starting the annotated test is restored when the test completes
- * (success, or failure).
- *
- * The class is thread-safe but tests with {@link Log4jConfig} cannot run in parallel due to the nature of the log4j
- * configuration.
- *
- * @see Log4jConfig
- */
-public class Log4jConfigExtension implements BeforeEachCallback, AfterEachCallback {
-  private static final ReentrantLock LOCK = new ReentrantLock();
-  private static Configuration oldConfig = null;
-
-  @Override
-  public void beforeEach(ExtensionContext context) throws Exception {
-    if (!LOCK.tryLock(1, TimeUnit.MINUTES)) {
-      throw new IllegalStateException(
-          "Lock acquisition failed cause another test is using a custom Log4j configuration.");
-    }
-    assert oldConfig == null;
-    LoggerContext ctx = LoggerContext.getContext(false);
-    oldConfig = ctx.getConfiguration();
-    Optional<AnnotatedElement> element = context.getElement();
-    if (!element.isPresent()) {
-      throw new IllegalStateException("The extension needs an annotated method");
-    }
-    Log4jConfig annotation = element.get().getAnnotation(Log4jConfig.class);
-    if (annotation == null) {
-      throw new IllegalStateException(Log4jConfig.class.getSimpleName() + " annotation is missing.");
-    }
-    String configFileName = annotation.value();
-    URL config = Log4jConfig.class.getClassLoader().getResource(configFileName);
-    if (config == null) {
-      throw new IllegalStateException("File " + configFileName + " was not found in resources.");
-    }
-    ctx.setConfigLocation(config.toURI());
-  }
-
-  @Override
-  public void afterEach(ExtensionContext context) {
-    try {
-      LoggerContext ctx = LoggerContext.getContext(false);
-      ctx.setConfiguration(oldConfig);
-      oldConfig = null;
-    } finally {
-      LOCK.unlock();
-    }
-  }
-}
diff --git a/testutils/src/test/java/org/apache/hive/testutils/junit/extensions/TestLog4jConfigExtension.java b/testutils/src/test/java/org/apache/hive/testutils/junit/extensions/TestLog4jConfigExtension.java
deleted file mode 100644
index c5f92522cc3..00000000000
--- a/testutils/src/test/java/org/apache/hive/testutils/junit/extensions/TestLog4jConfigExtension.java
+++ /dev/null
@@ -1,65 +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.hive.testutils.junit.extensions;
-
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-
-/**
- * Tests for {@link Log4jConfigExtension}.
- */
-public class TestLog4jConfigExtension {
-
-  @Test
-  @Log4jConfig("test0-log4j2.properties")
-  void testUseExplicitConfig0() {
-    LoggerContext ctx = LoggerContext.getContext(false);
-    Configuration c = ctx.getConfiguration();
-    assertEquals("TestConfig0Log4j2", c.getName());
-    assertNotNull(c.getAppender("appender_zero"));
-    assertEquals(1, c.getAppenders().size());
-    assertEquals(2, c.getLoggers().size());
-    assertNotNull(c.getLoggerConfig("logger_zero"));
-    assertNotNull(c.getLoggerConfig("root"));
-  }
-
-  @Test
-  void testNoExplicitConfig() {
-    LoggerContext ctx = LoggerContext.getContext(false);
-    Configuration c = ctx.getConfiguration();
-    assertEquals("HiveLog4j2Test", c.getName());
-  }
-
-  @Test
-  @Log4jConfig("test1-log4j2.properties")
-  void testUseExplicitConfig1() {
-    LoggerContext ctx = LoggerContext.getContext(false);
-    Configuration c = ctx.getConfiguration();
-    assertEquals("TestConfig1Log4j2", c.getName());
-    assertNotNull(c.getAppender("appender_one"));
-    assertEquals(1, c.getAppenders().size());
-    assertEquals(2, c.getLoggers().size());
-    assertNotNull(c.getLoggerConfig("logger_one"));
-    assertNotNull(c.getLoggerConfig("root"));
-  }
-
-}
diff --git a/testutils/src/test/resources/test0-log4j2.properties b/testutils/src/test/resources/test0-log4j2.properties
deleted file mode 100644
index f1598b958ac..00000000000
--- a/testutils/src/test/resources/test0-log4j2.properties
+++ /dev/null
@@ -1,32 +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.
-
-# Log4j2 configuration only for testing purposes
-
-status = INFO
-name = TestConfig0Log4j2
-
-appenders = ap0
-
-appender.ap0.type = Console
-appender.ap0.name = appender_zero
-
-loggers = log0
-logger.log0.name = logger_zero
-
-rootLogger.level = INFO
-rootLogger.appenderRefs = root
-rootLogger.appenderRef.root.ref = ap0
diff --git a/testutils/src/test/resources/test1-log4j2.properties b/testutils/src/test/resources/test1-log4j2.properties
deleted file mode 100644
index 4307006c0de..00000000000
--- a/testutils/src/test/resources/test1-log4j2.properties
+++ /dev/null
@@ -1,32 +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.
-
-# Log4j2 configuration only for testing purposes
-
-status = INFO
-name = TestConfig1Log4j2
-
-appenders = ap1
-
-appender.ap1.type = Console
-appender.ap1.name = appender_one
-
-loggers = log1
-logger.log1.name = logger_one
-
-rootLogger.level = INFO
-rootLogger.appenderRefs = root
-rootLogger.appenderRef.root.ref = ap1