-
Couldn't load subscription status.
- Fork 2.8k
test cover DefaultModelProcessor#read
#2304
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
test cover DefaultModelProcessor#read
#2304
Conversation
25dbfc7 to
0482492
Compare
Modernize codebase with Java improvements - test DefaultModelProcessor#readModernize codebase with Java improvements - test DefaultModelProcessor#read
…faultModelProcessor#read
0482492 to
542101a
Compare
…faultModelProcessor#read
542101a to
7ed0ace
Compare
…faultModelProcessor#read
7ed0ace to
e46da18
Compare
…faultModelProcessor#read
e46da18 to
02b5deb
Compare
|
this one important to give liberty on code back to us; making code liquide while logic remains tight and dusty. |
|
ideally when merging this, the test will be obsolet in follow up, allowing any adjustment: then we have awareness that coverage is a real deal and not a nice gimmick. |
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java
Show resolved
Hide resolved
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java
Show resolved
Hide resolved
| return project; | ||
| } | ||
|
|
||
| private Model doRead(XmlReaderRequest request) throws IOException { |
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 wrong/counterfeit because:
Currently, the API is designed to throw XmlReaderException. Check the implemented interface, there's no IOException thrown here.
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java
Outdated
Show resolved
Hide resolved
| } catch (IOException e) { | ||
| exceptions.forEach(e::addSuppressed); | ||
| throw e; | ||
| } catch (RuntimeException ex) { |
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.
Per Effective Java don't catch a raw runtime exception, only the specific subclass you expect to be thrown
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.
manage to change to } catch (ModelParserException e) {
as IOError is a sitting duck ready, for refactoring into void:
It's not thrown, that's right. So do not add a throw clause.
Modernize codebase with Java improvements - test DefaultModelProcessor#readDefaultModelProcessor#read
…faultModelProcessor#read
02b5deb to
b6809cd
Compare
b6809cd to
0b043ae
Compare
| return parent.get().withPomFile(pomFile); | ||
| } | ||
| } catch (ModelParserException e) { | ||
| exceptions.add(e); |
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.
is it mandatory to continue if an error occurs to give full error report? Or can we have here early exit if the branch kicks in like above in its counterpart?
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 suppose it depends on whether one parser throwing an exception invalidates answers from subsequent parsers in the loop. I suspect it doesn't since you skip subsequent parsers once one returns.
0b043ae to
92b5e79
Compare
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java
Outdated
Show resolved
Hide resolved
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java
Show resolved
Hide resolved
| return parent.get().withPomFile(pomFile); | ||
| } | ||
| } catch (ModelParserException e) { | ||
| exceptions.add(e); |
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 suppose it depends on whether one parser throwing an exception invalidates answers from subsequent parsers in the loop. I suspect it doesn't since you skip subsequent parsers once one returns.
| void readWithParserThrowingExceptionShouldCollectException() { | ||
| ModelXmlFactory factory = mock(ModelXmlFactory.class); | ||
| ModelParser parser = mock(ModelParser.class); | ||
| XmlReaderRequest request = mock(XmlReaderRequest.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.
does this have to be mocked? can you just use the class?
| when(mockSource.getPath()).thenReturn(Path.of("other/pom.xml")); | ||
| ModelParser parser = mock(ModelParser.class); | ||
| when(parser.locate(any())).thenReturn(Optional.of(mockSource)); | ||
| assertTrue(assertThrows(IllegalArgumentException.class, () -> new DefaultModelProcessor( |
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.
nesting asserts is strange. Maybe don't do this, or split it into two separate statements
| } | ||
|
|
||
| @Nested | ||
| class WithTmpFiles { |
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 strange. I haven't seen nested test classes before. Might be fine, but is there a strong reason for this?
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.
SOC is kind of crazy at first, but it can be handy—and necessary for special test patterns. It simplifies complex scenarios without forcing to massively copy or abstract test code, as logic can now be shared and layered in one place.
It’s similar to a dedicated class but with inheritance, allowing special sub-conditions. That said, it’s flexible and therefore has significant trade-offs (both good and bad side effects).
JUnit Nested Classes: Quick Pros & Cons
Pros:
✅ Better organization
✅ Clearer test grouping
✅ Flexible setup per group
✅ Reduces duplication
✅ Good for stateful testing
Cons:
❌ Adds complexity
❌ IDE navigation may suffer
❌ Risk of over-nesting
❌ Must be non-static
❌ Learning curve for new devs
Best for: Complex tests with shared setups or multiple states.
|
|
||
| @Test | ||
| void locateExistingPomWithDirectoryContainingPom() throws IOException { | ||
| testProjectDir = createTempDirectory(tempDir, "project"); |
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 test might belong elsewhere if it's not using the same fixture.
|
|
||
| @Test | ||
| void locateExistingPomWithDirectoryWithoutPom() throws IOException { | ||
| testProjectDir = createTempDirectory(tempDir, "project"); |
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 test might belong elsewhere if it's not using the same fixture.
|
|
||
| @Test | ||
| void locateExistingPomWithPomFile() throws IOException { | ||
| testPomFile = Files.createTempFile(tempDir, "pom", ".xml"); |
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 test might belong elsewhere if it's not using the same fixture.
| } else { | ||
| return null; | ||
| } | ||
| private static Optional<Model> parent(XmlReaderRequest request, ModelParser parser, Path projectDirectory) { |
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.
A parent usually refers to the parent model, so I'd rather use parentDirectory to make it clear what we're looking for.
|
|
||
| private Model doRead(XmlReaderRequest request) throws IOException { | ||
| return modelXmlFactory.read(request); | ||
| private static void throwErrorsSuppressed(List<ModelParserException> exceptions) throws IOException { |
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.
We're not using the IOException in the API, so not sure why we'd want to build one here. Model parsing exception aren't necessarily IoExceptions.
…faultModelProcessor#read
92b5e79 to
49d13d9
Compare
| .orElse(null)) | ||
| .filter(Objects::nonNull) | ||
| .findFirst() | ||
| .orElseGet(() -> doLocateExistingPom(projectDirectory)); |
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.
unpleasant do prefix is fix for naming collision @elharo
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.
finally covered, small polish.
| .orElseGet(() -> locateExistingPomWithFallback(projectDirectory))); | ||
| } | ||
|
|
||
| private static Path throwIfWrongProjectDirLocation(Path projectDirectory, Path pom) { |
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.
SOC
| return pom; | ||
| } | ||
|
|
||
| private Path locateExistingPomWithFallback(Path project) { |
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.
SRP
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java
Show resolved
Hide resolved
|
|
||
| @Test | ||
| void locateExistingPomShouldHandleRegularFileInput() throws IOException { | ||
| // Create a temporary file |
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.
meldown
DefaultModelProcessor#readDefaultModelProcessor#read
|
wip reopen |
Pull #2304: Modernize codebase with Java improvements - test
DefaultModelProcessor#readenable for:
DefaultModelProcessor#read#2292Unnecessary throws#2291100 as everything else is enough for a tremendous project like maven: