Skip to content

Let engine utContext.classLoader fallback to UtBot own class path #2556

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

Merged
merged 2 commits into from
Aug 29, 2023

Conversation

IlyaMuravjov
Copy link
Collaborator

Description

Fixes #2523 by utilizing child first class loader.

Additional context

#2523 is a regression caused by #2479, however we can't revert #2479, because otherwise UtBot classpath would be prioritized over user's classpath, causing issues when user and UtBot use different versions of same library. This PR makes it so UtBot classpath is still used, but only if the class is not found on the user classpath.

How to test

Manual tests

Scenario from issue should no longer reproduce

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov added comp-infrastructure Infrastructure issues ctg-bug-fix PR is fixing a bug labels Aug 25, 2023

override fun getResource(name: String): URL? {
var res: URL? = commonParent.getResource(name)
if (res === null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:
Why do you use === here? Previously, you used ==.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intellij IDEA reports a warning when equals (==) is used for URL saying it may perform a DNS lookup, using reference equality helps to avoid the warning.

// - Finally, if all else fails we try to load class/resource from UtBot classpath
// - at this step we load classes from UtBot project and its dependencies (e.g. Mockito if user doesn't have it, see #2545)
UtContext.setUtContext(UtContext(
FallbackClassLoader(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:
Extract it to variable

UtContext.setUtContext(UtContext(
FallbackClassLoader(
urls = params.classpathForUrlsClassloader.map { File(it).toURI().toURL() }.toTypedArray(),
fallback = ClassLoader.getSystemClassLoader(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor:
Extract it to variable

Copy link
Member

@sergeypospelov sergeypospelov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though there are some minors

@IlyaMuravjov IlyaMuravjov merged commit 48264f3 into main Aug 29, 2023
@IlyaMuravjov IlyaMuravjov deleted the ilya_m/fallback_class_loader branch August 29, 2023 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-infrastructure Infrastructure issues ctg-bug-fix PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ClassNotFoundException: org.mockito.MockedConstruction$Context in some Gradle projects when generate tests with Fuzzing
3 participants