-
Notifications
You must be signed in to change notification settings - Fork 785
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
TIKA-4254 - Fix non-idempotent unit test TestMimeTypes#testJavaRegex
#1754
base: main
Are you sure you want to change the base?
Conversation
The |
@tballison Yes, NIOInspector uses the JUnit Jupiter engine and takes into account of all setup and teardown methods. It seems that the patterns matcher of
|
Maybe I get it: |
I think it might the case. I wrote this dummy test, and it fails under surefire:
Error Message:
|
@THausherr @tballison I confirmed that the two lines in
When the system property
Now take a look at
CLASSLOADER_SPECIFIC_DEFAULT_TYPES is not an instance variable, but a static HashMap .
So in the first test execution, the
Now in the second test execution, the I think the essential idea of |
Fixes https://issues.apache.org/jira/projects/TIKA/issues/TIKA-4254
Brief Description of the Bug
The test
TestMimeTypes#testJavaRegex
is non-idempotent, as it passes in the first run but fails in the second run in the same environment. The source of the problem is that each test execution initializes a new media type (MimeType
) instancetestType
(same problem fortestType2
), and all media types across different test executions attempt to use the same name pattern"rtg_sst_grb_0\\.5\\.\\d{8}"
. Therefore, in the second execution of the test, the linethis.repo.addPattern(testType, pattern, true);
will throw an error, since the name pattern is already used by thetestType
instance initiated from the first test execution. Specifically, in the second run, theaddGlob()
method of thePattern
class will assert conflict patterns and throw aMimeTypeException
(line 123 inPatterns.java
).Failure Message in the 2nd Test Run:
Reproduce
Use the
NIOInspector
plugin that supports rerunning individual tests in the same environment:Proposed Fix
Declare
testType
andtestType2
as static variables and initialize them at class loading time. Therefore, repeated runs oftestJavaRegex()
will not conflict each other. All tests pass and are idempotent after the fix.Necessity of Fix
A fix is recommended as unit tests shall be idempotent, and state pollution shall be mitigated so that newly introduced tests do not fail in the future due to polluted shared states.