TIKA-4254 - Fix non-idempotent unit test TestMimeTypes#testJavaRegex
#1754
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.
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.