-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
1f6758f
commit 1639b66
Showing
4 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
72 changes: 72 additions & 0 deletions
72
apps/bfd-shared-test-utils/src/main/java/gov/cms/bfd/MavenUtils.java
This file contains 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,72 @@ | ||
package gov.cms.bfd; | ||
|
||
import java.io.File; | ||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.xpath.XPath; | ||
import javax.xml.xpath.XPathConstants; | ||
import javax.xml.xpath.XPathFactory; | ||
import org.testcontainers.shaded.com.google.common.annotations.VisibleForTesting; | ||
import org.testcontainers.shaded.com.google.common.base.Strings; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.NodeList; | ||
|
||
/** Extract maven project meta data for use in tests. */ | ||
public class MavenUtils { | ||
/** Name of maven pom file to search for. */ | ||
public static final String POM_FILE_NAME = "pom.xml"; | ||
|
||
/** | ||
* Searches for a {@code pom.xml} file starting from the current working directory and then | ||
* proceeding through parent directories until root is reached. Looks for parent version string | ||
* within the pom file and returns it if one is found that is not empty. | ||
* | ||
* @return project version string from pom file | ||
* @throws RuntimeException if any error happens or no suitable pom file could be found | ||
*/ | ||
public static String findProjectVersion() { | ||
return findProjectVersion(POM_FILE_NAME); | ||
} | ||
|
||
/** | ||
* Searches for a file with the given name starting from the current working directory and then | ||
* proceeding through parent directories until root is reached. Looks for parent version string | ||
* within the pom file and returns it if one is found that is not empty. | ||
* | ||
* @param pomFileName name of file to search for | ||
* @return project version string from pom file | ||
* @throws RuntimeException if any error happens or no suitable pom file could be found | ||
*/ | ||
@VisibleForTesting | ||
static String findProjectVersion(String pomFileName) { | ||
File previousDirectory; | ||
File directory = new File(".").getAbsoluteFile(); | ||
do { | ||
File pomFile = new File(directory, pomFileName); | ||
if (pomFile.exists()) { | ||
try { | ||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
Document document = builder.parse(new File(pomFileName)); | ||
XPath xPath = XPathFactory.newInstance().newXPath(); | ||
NodeList nodes = | ||
(NodeList) | ||
xPath.evaluate("/project/parent/version", document, XPathConstants.NODESET); | ||
for (int i = 0; i < nodes.getLength(); ++i) { | ||
Element e = (Element) nodes.item(i); | ||
String version = e.getTextContent(); | ||
if (!Strings.isNullOrEmpty(version)) { | ||
return version; | ||
} | ||
} | ||
} catch (Exception ex) { | ||
throw new RuntimeException("encountered error while parsing pom.xml file", ex); | ||
} | ||
} | ||
previousDirectory = directory; | ||
directory = previousDirectory.getParentFile(); | ||
} while (directory != null && !directory.equals(previousDirectory)); | ||
throw new RuntimeException("failed to find a pom.xml file containing a version!"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
apps/bfd-shared-test-utils/src/test/java/gov/cms/bfd/MavenUtilsTest.java
This file contains 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,24 @@ | ||
package gov.cms.bfd; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.shaded.com.google.common.base.Strings; | ||
|
||
/** Unit tests for {@link MavenUtils}. */ | ||
public class MavenUtilsTest { | ||
/** Verifies that pom file can be found and parsed successfully. */ | ||
@Test | ||
void findsProjectVersionSuccessfully() { | ||
String version = MavenUtils.findProjectVersion(); | ||
assertFalse(Strings.isNullOrEmpty(version), "version string should be non-empty"); | ||
} | ||
|
||
/** Verifies that method throws an exception if it cannot find a pom file. */ | ||
@Test | ||
void failsToFindPomFile() { | ||
assertThrows( | ||
RuntimeException.class, () -> MavenUtils.findProjectVersion("not-a-real-pom-file")); | ||
} | ||
} |