You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/01/30 19:54:36 UTC

[logging-log4j2] 05/06: Adding new unit and integration (for slow path in java8 StackLocator) tests for stack elements order

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

pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit ccbd99ebf6a9a3165dfe5f6215137ec7b7a933a7
Author: Aliaksei Dubrouski <ad...@pandora.com>
AuthorDate: Thu Jan 19 16:32:00 2023 -0800

    Adding new unit and integration (for slow path in java8 StackLocator) tests for stack elements order
---
 .../logging/log4j/util/StackLocatorTestIT.java     | 64 ++++++++++++++++++++++
 .../logging/log4j/util/StackLocatorUtilTest.java   | 11 +++-
 .../log4j/core/impl/ThrowableProxyHelperTest.java  |  3 +-
 3 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorTestIT.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorTestIT.java
new file mode 100644
index 0000000000..8219b674bd
--- /dev/null
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorTestIT.java
@@ -0,0 +1,64 @@
+/*
+ * 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.logging.log4j.util;
+
+import java.security.Permission;
+import java.util.Deque;
+
+import org.apache.logging.log4j.test.junit.SecurityManagerTestRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.parallel.ResourceLock;
+
+
+/**
+ * Tests https://github.com/apache/logging-log4j2/pull/1214.
+ * <p>
+ * Using a security manager can mess up other tests so this is best used from
+ * integration tests (classes that end in "IT" instead of "Test" and
+ * "TestCase".)
+ * </p>
+ *
+ * @see StackLocator
+ * @see SecurityManager
+ * @see System#setSecurityManager(SecurityManager)
+ */
+@ResourceLock("java.lang.SecurityManager")
+public class StackLocatorTestIT {
+  @Rule
+  public final SecurityManagerTestRule rule = new SecurityManagerTestRule(new TestSecurityManager());
+
+  /**
+   * Always throws a SecurityException for any reques to create a new SecurityManager
+   */
+  private static class TestSecurityManager extends SecurityManager {
+    @Override
+    public void checkPermission(final Permission permission) {
+      if ("createSecurityManager".equals(permission.getName())) {
+        throw new SecurityException();
+      }
+    }
+  }
+
+  @Test
+  public void testGetCurrentStacktraceSlowPath() {
+    final StackLocator stackLocator = StackLocator.getInstance();
+    final Deque<Class<?>> classes = stackLocator.getCurrentStackTrace();
+    Assertions.assertSame(StackLocator.class, classes.getFirst());
+  }
+}
diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
index 15ff9b0735..455ccc20f2 100644
--- a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/StackLocatorUtilTest.java
@@ -84,6 +84,15 @@ public class StackLocatorUtilTest {
         assertSame(StackLocatorUtilTest.class, classes.removeFirst());
     }
 
+    @Test
+    public void testTopElementInStackTrace() {
+        final StackLocator stackLocator = StackLocator.getInstance();
+        final Deque<Class<?>> classes = stackLocator.getCurrentStackTrace();
+        //Removing private class in "PrivateSecurityManagerStackTraceUtil"
+        classes.removeFirst();
+        assertSame(PrivateSecurityManagerStackTraceUtil.class, classes.getFirst());
+    }
+
     @Test
     public void testGetCallerClassViaName() throws Exception {
         final Class<?> expected = InterceptingExecutableInvoker.class;
@@ -150,7 +159,7 @@ public class StackLocatorUtilTest {
         final StackTraceElement element = new Foo().foo();
         assertEquals("org.apache.logging.log4j.util.StackLocatorUtilTest$Foo", element.getClassName());
         // The line number below may need adjustment if this file is changed.
-        assertEquals(116, element.getLineNumber());
+        assertEquals(125, element.getLineNumber());
     }
 
     @Test
diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelperTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelperTest.java
index 5575fb5473..628650261b 100644
--- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelperTest.java
+++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/ThrowableProxyHelperTest.java
@@ -20,10 +20,11 @@ import java.util.ArrayDeque;
 import java.util.Deque;
 import java.util.HashMap;
 import java.util.Map;
+
 import org.junit.Test;
 
-import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * Tests ThrowableProxyHelper.