-
Notifications
You must be signed in to change notification settings - Fork 4
refactor(annotations): Extract InjectedTestCaseTesterBinding from Tes… #983
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/main/java/org/dbunit/annotation/runtime/InjectedTestCaseTesterBinding.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /* | ||
| * | ||
| * The DbUnit Database Testing Framework | ||
| * Copyright (C)2002-2026, DbUnit.org | ||
| * | ||
| * This library is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 2.1 of the License, or (at your option) any later version. | ||
| * | ||
| * This library is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public | ||
| * License along with this library; if not, write to the Free Software | ||
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| * | ||
| */ | ||
| package org.dbunit.annotation.runtime; | ||
|
|
||
| import java.util.Objects; | ||
| import java.util.concurrent.Callable; | ||
|
|
||
| import org.dbunit.IDatabaseTester; | ||
| import org.dbunit.PrepAndExpectedTestCase; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Resolves the {@link IDatabaseTester} for an already-injected {@code @DbUnitTestCase} instance: | ||
| * {@link PrepAndExpectedTestCase#getDatabaseTester()} if it already has one, otherwise a | ||
| * caller-supplied fallback, wired back onto the test case and round-trip-verified. | ||
| * | ||
| * <p>Public so a binding outside {@code org.dbunit.annotation.runtime} - such as | ||
| * {@code DbUnitExtension} or a Spring {@code TestExecutionListener} - can reach it too, sharing | ||
| * the one round-trip-verification rule rather than each re-implementing it: a test case whose | ||
| * type overrides {@code setDatabaseTester()} but does not round-trip | ||
| * {@code getDatabaseTester()} back to the same instance is a bug (an operation applied to the | ||
| * resolved tester would silently never reach the tester the test case actually uses) and fails | ||
| * fast, while one that never overrides {@code setDatabaseTester()} at all is the documented | ||
| * self-managed-connection pattern and only logged. | ||
| * | ||
| * @author Jeff Jensen | ||
| * @since 3.6.0 | ||
| */ | ||
| public class InjectedTestCaseTesterBinding | ||
| { | ||
| private static final Logger log = LoggerFactory.getLogger(InjectedTestCaseTesterBinding.class); | ||
|
|
||
| private InjectedTestCaseTesterBinding() | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * Resolves and wires the tester for {@code testCase}. | ||
| * | ||
| * @param testCase The already-injected test case, resolved and type-checked by the caller. | ||
| * @param fieldDescription Where {@code testCase} came from, for an exception or log message | ||
| * - e.g. {@code "field 'testCase' in com.example.MyTest"}. | ||
| * @param fallbackTester Supplies a tester when {@code testCase.getDatabaseTester()} is | ||
| * {@code null}; not called otherwise. | ||
| * @return The resolved tester: {@code testCase.getDatabaseTester()} if already set, otherwise | ||
| * {@code fallbackTester}'s value. | ||
| * @throws Exception If {@code fallbackTester} fails. | ||
| * @throws IllegalStateException If {@code testCase}'s type overrides | ||
| * {@code setDatabaseTester()} but does not round-trip {@code getDatabaseTester()} | ||
| * back to the same instance. | ||
| */ | ||
| public static IDatabaseTester resolveTester(final PrepAndExpectedTestCase testCase, | ||
| final String fieldDescription, final Callable<IDatabaseTester> fallbackTester) | ||
| throws Exception | ||
| { | ||
| final IDatabaseTester existing = testCase.getDatabaseTester(); | ||
| if (existing != null) | ||
| { | ||
| return existing; | ||
| } | ||
|
|
||
| final IDatabaseTester tester = Objects.requireNonNull(fallbackTester.call(), | ||
| "fallbackTester must supply an IDatabaseTester."); | ||
|
|
||
| // The executor drives this exact, already-injected instance directly rather than | ||
| // constructing a fresh one, so the fallback tester must be wired onto it here - | ||
| // otherwise it keeps whatever databaseTester it reports for an implementation that does | ||
| // not override setDatabaseTester(). | ||
| testCase.setDatabaseTester(tester); | ||
| if (testCase.getDatabaseTester() == tester) | ||
| { | ||
| return tester; | ||
| } | ||
|
|
||
| if (overridesSetDatabaseTester(testCase)) | ||
| { | ||
| // Opted into automatic wiring by overriding setDatabaseTester(), so a round-trip | ||
| // failure means the override itself is broken - fail fast the same way every other | ||
| // @DbUnitConfig-driven setter does for a silent no-op, rather than let | ||
| // @DbUnitSetup/@DbUnitTearDown operations quietly target a tester this test case | ||
| // never actually uses. | ||
| throw new IllegalStateException(fieldDescription | ||
| + " is annotated @DbUnitTestCase; its value's type (" | ||
| + testCase.getClass().getName() + ") overrides setDatabaseTester(), but" | ||
| + " getDatabaseTester() does not return the same instance right after being" | ||
| + " given it. @DbUnitSetup/@DbUnitTearDown operations set on the resolved" | ||
| + " IDatabaseTester would silently never reach the operations this test case" | ||
| + " actually runs. Fix setDatabaseTester()/getDatabaseTester() to round-trip" | ||
| + " the same instance."); | ||
| } | ||
| // A test case that never overrides setDatabaseTester() at all is the documented | ||
| // self-managed-connection pattern, not a bug - see annotations.adoc - so this stays a | ||
| // diagnostic log, not a failure. | ||
| log.debug("PrepAndExpectedTestCase {} does not round-trip getDatabaseTester()/" | ||
| + "setDatabaseTester(); @DbUnitSetup/@DbUnitTearDown operations set on the" | ||
| + " resolved IDatabaseTester may not reach the operations this test case actually" | ||
| + " runs unless it independently uses the same tester instance.", | ||
| testCase.getClass().getName()); | ||
| return tester; | ||
| } | ||
|
|
||
| /** | ||
| * Returns whether {@code testCase}'s runtime type overrides | ||
| * {@link PrepAndExpectedTestCase#setDatabaseTester(IDatabaseTester)}, rather than inheriting | ||
| * the interface's own no-op default body. See | ||
| * {@link DefaultMethodOverrideCheck#overridesDefaultMethod} for this check's own known | ||
| * limitation. | ||
| */ | ||
| private static boolean overridesSetDatabaseTester(final PrepAndExpectedTestCase testCase) | ||
| { | ||
| return DefaultMethodOverrideCheck.overridesDefaultMethod(testCase.getClass(), | ||
| PrepAndExpectedTestCase.class, "setDatabaseTester", IDatabaseTester.class); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.