Doesn't fail on empty pom; fixes gh-71
This commit is contained in:
@@ -19,6 +19,7 @@ import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.apache.maven.model.Model;
|
||||
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
|
||||
@@ -37,12 +38,19 @@ class PomReader {
|
||||
if (file.isDirectory()) {
|
||||
pom = new File(file,"pom.xml");
|
||||
}
|
||||
String fileText = "";
|
||||
try(Reader reader = new FileReader(pom)) {
|
||||
if (file.isFile()) {
|
||||
fileText = new String(Files.readAllBytes(file.toPath()));
|
||||
}
|
||||
MavenXpp3Reader xpp3Reader = new MavenXpp3Reader();
|
||||
return xpp3Reader.read(reader);
|
||||
}
|
||||
catch (XmlPullParserException | IOException e) {
|
||||
throw new IllegalStateException("Failed to read file: "+pom.getAbsolutePath(), e);
|
||||
if (file.isFile() && fileText.length() == 0) {
|
||||
throw new IllegalStateException("File [" + pom.getAbsolutePath() + "] is empty", e);
|
||||
}
|
||||
throw new IllegalStateException("Failed to read file: " + pom.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.release.internal.pom;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
@@ -38,6 +39,7 @@ public class PomReaderTests {
|
||||
PomReader pomReader = new PomReader();
|
||||
File springCloudReleaseProjectPom;
|
||||
File springCloudReleaseProject;
|
||||
File empty;
|
||||
File licenseFile;
|
||||
|
||||
@Before
|
||||
@@ -45,6 +47,7 @@ public class PomReaderTests {
|
||||
URI scRelease = GitRepoTests.class.getResource("/projects/spring-cloud-release").toURI();
|
||||
this.springCloudReleaseProject = new File(scRelease);
|
||||
this.springCloudReleaseProjectPom = new File(scRelease.getPath(), "pom.xml");
|
||||
this.empty = new File(GitRepoTests.class.getResource("/projects/project/empty.xml").toURI());
|
||||
this.licenseFile = new File(scRelease.getPath(), "LICENSE.txt");
|
||||
}
|
||||
|
||||
@@ -77,4 +80,12 @@ public class PomReaderTests {
|
||||
.hasMessageStartingWith("Failed to read file: ")
|
||||
.hasCauseInstanceOf(XmlPullParserException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_throw_exception_when_file_is_empty() {
|
||||
thenThrownBy(() -> this.pomReader.readPom(this.empty))
|
||||
.hasMessageStartingWith("File [")
|
||||
.hasMessageContaining("] is empty")
|
||||
.hasCauseInstanceOf(EOFException.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user