You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/10/08 23:02:57 UTC

git commit: Add test for isolated ClassLoaders and LoggerContexts.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master c23fe1c13 -> c06375711


Add test for isolated ClassLoaders and LoggerContexts.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/c0637571
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/c0637571
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/c0637571

Branch: refs/heads/master
Commit: c0637571169d3c890ecb8cebcc3a3b00907f88d1
Parents: c23fe1c
Author: Matt Sicker <ma...@apache.org>
Authored: Wed Oct 8 15:56:38 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Wed Oct 8 15:56:38 2014 -0500

----------------------------------------------------------------------
 .../ClassLoaderContextSelectorTest.java         | 62 +++++++++++++++++
 .../log4j/core/selector/TestClassLoader.java    | 73 ++++++++++++++++++++
 .../logging/log4j/core/selector/a/Logging1.java | 26 +++++++
 .../logging/log4j/core/selector/b/Logging2.java | 26 +++++++
 .../logging/log4j/core/selector/c/Logging3.java | 26 +++++++
 5 files changed, 213 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c0637571/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelectorTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelectorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelectorTest.java
new file mode 100644
index 0000000..5b7e6ad
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/ClassLoaderContextSelectorTest.java
@@ -0,0 +1,62 @@
+/*
+ * 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.core.selector;
+
+import java.lang.reflect.Field;
+
+import org.apache.logging.log4j.core.Logger;
+import org.apache.logging.log4j.core.util.ReflectionUtil;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class ClassLoaderContextSelectorTest {
+
+    private static final String PKG = ClassLoaderContextSelectorTest.class.getPackage().getName();
+
+    private ClassLoader loader1, loader2, loader3;
+
+    @Before
+    public void setUp() throws Exception {
+        loader1 = new TestClassLoader();
+        loader2 = new TestClassLoader();
+        loader3 = new TestClassLoader();
+        assertNotSame(loader1, loader2);
+        assertNotSame(loader1, loader3);
+        assertNotSame(loader2, loader3);
+    }
+
+    @Test
+    public void testMultipleClassLoaders() throws Exception {
+        final Class<?> logging1 = loader1.loadClass(PKG + ".a.Logging1");
+        final Field field1 = logging1.getDeclaredField("logger");
+        final Logger logger1 = (Logger) ReflectionUtil.getStaticFieldValue(field1);
+        assertNotNull(logger1);
+        final Class<?> logging2 = loader2.loadClass(PKG + ".b.Logging2");
+        final Field field2 = logging2.getDeclaredField("logger");
+        final Logger logger2 = (Logger) ReflectionUtil.getStaticFieldValue(field2);
+        assertNotNull(logger2);
+        final Class<?> logging3 = loader3.loadClass(PKG + ".c.Logging3");
+        final Field field3 = logging3.getDeclaredField("logger");
+        final Logger logger3 = (Logger) ReflectionUtil.getStaticFieldValue(field3);
+        assertNotNull(logger3);
+        assertNotSame(logger1.getContext(), logger2.getContext());
+        assertNotSame(logger1.getContext(), logger3.getContext());
+        assertNotSame(logger2.getContext(), logger3.getContext());
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c0637571/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/TestClassLoader.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/TestClassLoader.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/TestClassLoader.java
new file mode 100644
index 0000000..38f93b4
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/TestClassLoader.java
@@ -0,0 +1,73 @@
+/*
+ * 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.core.selector;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.logging.log4j.core.util.Closer;
+import org.apache.logging.log4j.core.util.Throwables;
+import org.apache.logging.log4j.util.LoaderUtil;
+import sun.misc.IOUtils;
+
+/**
+ * ClassLoader that loads class in this package (or sub-package) by hand, otherwise delegating to the TCCL.
+ *
+ * @since 2.1
+ */
+public class TestClassLoader extends ClassLoader {
+
+    public TestClassLoader() {
+        super(LoaderUtil.getThreadContextClassLoader());
+    }
+
+    @Override
+    public Class<?> loadClass(final String name) throws ClassNotFoundException {
+        if (name.startsWith(getClass().getPackage().getName())) {
+            return findClass(name);
+        }
+        return super.loadClass(name);
+    }
+
+    @Override
+    protected Class<?> findClass(final String name) throws ClassNotFoundException {
+        final String path = name.replace('.', '/').concat(".class");
+        final URL resource = super.getResource(path);
+        if (resource == null) {
+            throw new ClassNotFoundException(name);
+        }
+        try {
+            final URLConnection uc = resource.openConnection();
+            final int len = uc.getContentLength();
+            final InputStream in = new BufferedInputStream(uc.getInputStream());
+            byte[] bytecode;
+            try {
+                // laziness means using sun.misc
+                bytecode = IOUtils.readFully(in, len, true);
+            } finally {
+                Closer.closeSilently(in);
+            }
+            return defineClass(name, bytecode, 0, bytecode.length);
+        } catch (IOException e) {
+            Throwables.rethrow(e);
+            return null; // unreachable
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c0637571/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/a/Logging1.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/a/Logging1.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/a/Logging1.java
new file mode 100644
index 0000000..44ee84f
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/a/Logging1.java
@@ -0,0 +1,26 @@
+/*
+ * 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.core.selector.a;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+// loaded dynamically
+@SuppressWarnings("UnusedDeclaration")
+public class Logging1 {
+    static Logger logger = LogManager.getLogger();
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c0637571/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/b/Logging2.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/b/Logging2.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/b/Logging2.java
new file mode 100644
index 0000000..6279783
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/b/Logging2.java
@@ -0,0 +1,26 @@
+/*
+ * 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.core.selector.b;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+// loaded dynamically
+@SuppressWarnings("UnusedDeclaration")
+public class Logging2 {
+    static Logger logger = LogManager.getLogger();
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c0637571/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/c/Logging3.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/c/Logging3.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/c/Logging3.java
new file mode 100644
index 0000000..52708b7
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/selector/c/Logging3.java
@@ -0,0 +1,26 @@
+/*
+ * 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.core.selector.c;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+// loaded dynamically
+@SuppressWarnings("UnusedDeclaration")
+public class Logging3 {
+    static Logger logger = LogManager.getLogger();
+}