1
0

LOGGING-185: Fix failing tests (#180)

* Fix names of deployed artifacts

We remove the obsolete `finalName` property and fix the Maven Failsafe
executions.

* Fix failing tests

* Reenable integration tests

* Disable SecurityManager tests on Java 21

* Simplify build workflow

* Set `fail-fast` to false

* Fix NPEs caused by `String.trim()`

* Span a different JVM per test case
This commit is contained in:
Piotr P. Karwasz
2023-11-19 01:36:11 +01:00
committed by GitHub
parent 6edbb07301
commit 6ad06e9074
10 changed files with 116 additions and 82 deletions

View File

@@ -0,0 +1,52 @@
/*
* 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.commons.logging;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
* Helper class to retrieve the names of all this project artifacts.
*/
public final class Artifacts {
private static final String ARTIFACT_ID = "commons-logging";
private static final String VERSION;
static {
try (final InputStream pomProperties = Artifacts.class.getResourceAsStream(
"/META-INF/maven/commons-logging/commons-logging/pom.properties")) {
final Properties props = new Properties();
props.load(pomProperties);
VERSION = props.getProperty("version");
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public static String getMainJarName() {
return ARTIFACT_ID + "-" + VERSION + ".jar";
}
public static String getAdaptersJarName() {
return ARTIFACT_ID + "-" + VERSION + "-adapters.jar";
}
private Artifacts() {
}
}

View File

@@ -16,6 +16,7 @@
*/
package org.apache.commons.logging;
import java.lang.reflect.InvocationTargetException;
import junit.framework.TestCase;
/**
@@ -184,8 +185,12 @@ public class LoadTestCase extends TestCase{
setAllowFlawedContext(cls, "false");
execute(cls);
fail("Logging config succeeded when context classloader was null!");
} catch (final LogConfigurationException ex) {
// expected; the boot classloader doesn't *have* JCL available
} catch (final InvocationTargetException ex) {
final Throwable targetException = ex.getTargetException();
// LogConfigurationException is expected; the boot classloader doesn't *have* JCL available
if (!(targetException instanceof LogConfigurationException)) {
throw ex;
}
}
// Context classloader is the system classloader.
@@ -209,8 +214,12 @@ public class LoadTestCase extends TestCase{
execute(cls);
fail("Error: somehow downcast a Logger loaded via system classloader"
+ " to the Log interface loaded via a custom classloader");
} catch (final LogConfigurationException ex) {
// expected
} catch (final InvocationTargetException ex) {
final Throwable targetException = ex.getTargetException();
// LogConfigurationException is expected
if (!(targetException instanceof LogConfigurationException)) {
throw ex;
}
}
}
}

View File

@@ -26,6 +26,7 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Assert;
/**
* A ClassLoader which sees only specified classes, and which can be
@@ -121,7 +122,11 @@ public class PathableClassLoader extends URLClassLoader {
final String fileName = System.getProperty(logicalLib);
if (fileName != null) {
try {
final URL libUrl = new File(fileName).toURL();
final File file = new File(fileName);
if (!file.exists()) {
Assert.fail("Unable to add logical library " + fileName);
}
final URL libUrl = file.toURL();
addURL(libUrl);
return;
} catch (final java.net.MalformedURLException e) {

View File

@@ -26,6 +26,7 @@ import java.util.Set;
import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.commons.logging.Artifacts;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
@@ -42,6 +43,7 @@ import org.apache.commons.logging.PathableTestSuite;
public class ChildFirstTestCase extends TestCase {
/**
* Sets up a custom classloader hierarchy for this test case.
* The hierarchy is:
@@ -66,6 +68,7 @@ public class ChildFirstTestCase extends TestCase {
// this class, so use that as the source for future access to classes
// from the junit package.
parent.useExplicitLoader("junit.", thisClassLoader);
parent.useExplicitLoader("org.junit.", thisClassLoader);
// Make the commons-logging.jar classes visible via the parent
parent.addLogicalLib("commons-logging");
@@ -230,7 +233,7 @@ public class ChildFirstTestCase extends TestCase {
resource = childLoader.getResource("org/apache/commons/logging/impl/Log4JLogger.class");
assertNotNull("Unable to locate Log4JLogger.class resource", resource);
assertTrue("Incorrect source for Log4JLogger class",
resource.toString().indexOf("/commons-logging-adapters-1.") > 0);
resource.toString().indexOf(Artifacts.getAdaptersJarName()) > 0);
}
/**
@@ -310,8 +313,8 @@ public class ChildFirstTestCase extends TestCase {
urlsToStrings[1] = urls[1].toString();
Arrays.sort(urlsToStrings);
assertTrue("Incorrect source for Log4JLogger class",
urlsToStrings[0].indexOf("/commons-logging-1.") > 0);
urlsToStrings[0].indexOf(Artifacts.getAdaptersJarName()) > 0);
assertTrue("Incorrect source for Log4JLogger class",
urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0);
urlsToStrings[1].indexOf(Artifacts.getMainJarName()) > 0);
}
}

View File

@@ -28,6 +28,7 @@ import java.util.Set;
import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.commons.logging.Artifacts;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
@@ -67,6 +68,7 @@ public class ParentFirstTestCase extends TestCase {
// this class, so use that as the source for future access to classes
// from the junit package.
parent.useExplicitLoader("junit.", thisClassLoader);
parent.useExplicitLoader("org.junit.", thisClassLoader);
// make the commons-logging.jar classes visible via the parent
parent.addLogicalLib("commons-logging");
@@ -228,7 +230,7 @@ public class ParentFirstTestCase extends TestCase {
resource = childLoader.getResource("org/apache/commons/logging/impl/Log4JLogger.class");
assertNotNull("Unable to locate Log4JLogger.class resource", resource);
assertTrue("Incorrect source for Log4JLogger class",
resource.toString().indexOf("/commons-logging-1.") > 0);
resource.toString().indexOf(Artifacts.getMainJarName()) > 0);
}
/**
@@ -301,9 +303,9 @@ public class ParentFirstTestCase extends TestCase {
urlsToStrings[1] = urls[1].toString();
Arrays.sort(urlsToStrings);
assertTrue("Incorrect source for Log4JLogger class",
urlsToStrings[0].indexOf("/commons-logging-1.") > 0);
urlsToStrings[0].indexOf(Artifacts.getAdaptersJarName()) > 0);
assertTrue("Incorrect source for Log4JLogger class",
urlsToStrings[1].indexOf("/commons-logging-adapters-1.") > 0);
urlsToStrings[1].indexOf(Artifacts.getMainJarName()) > 0);
}
}

View File

@@ -31,6 +31,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
import org.junit.Assume;
/**
* Tests for logging with a security policy that allows JCL access to everything.
@@ -87,6 +88,10 @@ public class SecurityAllowedTestCase extends TestCase
* overrides should take effect.
*/
public void testAllAllowed() {
// Ignore on Java 21
if (System.getProperty("java.version").startsWith("21.")) {
return;
}
System.setProperty(
LogFactory.HASHTABLE_IMPLEMENTATION_PROPERTY,
CustomHashtable.class.getName());

View File

@@ -32,6 +32,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.PathableClassLoader;
import org.apache.commons.logging.PathableTestSuite;
import org.junit.Assume;
/**
* Tests for logging with a security policy that forbids JCL access to anything.
@@ -63,6 +64,7 @@ public class SecurityForbiddenTestCase extends TestCase
public static Test suite() throws Exception {
final PathableClassLoader parent = new PathableClassLoader(null);
parent.useExplicitLoader("junit.", Test.class.getClassLoader());
parent.useExplicitLoader("org.junit.", Test.class.getClassLoader());
parent.addLogicalLib("commons-logging");
parent.addLogicalLib("testclasses");
@@ -117,6 +119,10 @@ public class SecurityForbiddenTestCase extends TestCase
* should fall back to the built-in defaults.
*/
public void testAllForbidden() {
// Ignore on Java 21
if (System.getProperty("java.version").startsWith("21.")) {
return;
}
System.setProperty(
LogFactory.HASHTABLE_IMPLEMENTATION_PROPERTY,
CustomHashtable.class.getName());
@@ -166,6 +172,10 @@ public class SecurityForbiddenTestCase extends TestCase
* than the context classloader of the current thread tries to log something.
*/
public void testContextClassLoader() {
// Ignore on Java 21
if (System.getProperty("java.version").startsWith("21.")) {
return;
}
System.setProperty(
LogFactory.HASHTABLE_IMPLEMENTATION_PROPERTY,
CustomHashtable.class.getName());