-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add ClassLoaderUtils and log location from which an engine was loaded #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -47,7 +47,9 @@ on GitHub. | |||
|
|||
===== New Features and Improvements | |||
|
|||
* ❓ | |||
* Added `getSourceLocation()` method in `TestEngine` interface to provide information | |||
about the location from where the engine class was loaded from. When location URL is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the location from which the engine class was loaded."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"When the location URL is available..."
///CLOVER:ON | ||
|
||
/** | ||
* Get the location from where this objects underlying class was loaded from. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Get the location from which the supplied object's class was loaded."
/** | ||
* Get the location from where this objects underlying class was loaded from. | ||
* | ||
* @param object to find the location its class was loaded from for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a tough one, grammatically speaking, but something like the following is more suitable:
@param object the object for whose class the location should be retrieved
In general, we always follow the pattern @param object the object...
.
} | ||
if (loader != null) { | ||
String name = object.getClass().getCanonicalName(); | ||
name = name.replace(".", "/") + ".class"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we have this kind of "replace" call in multiple places in the code base now?
If so, I think that would warrant the introduction of a new ClassUtils
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Find in path..." revealed only this occurance of replace(".", "/")
-- so no need for a ClassUtils
collection for this specific use-case.
On the other hand, what happens when the class is a nested one, that is separated with a $
-sign? Going to write a test for that.
*/ | ||
@API(Internal) | ||
public final class ClassLoaderUtils { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please relocate org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()
to this class as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relocating in a separate commit to gather all 14 related file changes in a single place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done via 89c9834
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
return Optional.empty(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we shouldn't wrap the meat of getLocation()
in a try-catch block just in case....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense.
We could also try the .getProtectionDomain().getCodeSource().getLocation()
as a last resort here.
@@ -175,4 +177,16 @@ | |||
PackageUtils.getAttribute(getClass(), Package::getImplementationVersion).orElse("DEVELOPMENT")); | |||
} | |||
|
|||
/** | |||
* Get the location from where this engine was loaded from. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Get the location from which this engine was loaded."
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit tests for {@link PackageUtils}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PackageUtils
? 😜
b94af6e
to
b75b5bb
Compare
This commit enhances the configuration-level log statement with the location URL from where the classloader loaded the engine class. Also introduce dedicated `ClassLoaderUtils` class that encapsulates static class-related helper methods. As a first action, this commit moves `getDefaultClassLoader()` to `ClassLoaderUtils` helper.
b75b5bb
to
455f911
Compare
getSourceLocation()
in TestEngine
interface
Failing "pr" build: https://travis-ci.org/junit-team/junit5/builds/221362686 ? Re-creating this PR. |
Overview
This commit enhances the configuration-level log statement with the location URL from where the classloader loaded the engine class. This commit also introduces dedicated
ClassLoaderUtils
class that encapsulates static class-related helper methods.Addresses part of #775
I hereby agree to the terms of the JUnit Contributor License Agreement.