Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,6 @@ public void deleteTestReports() {
@Override
public void validateTestReportsExist() {
//TODO: rewrite validateTestReportExists() to accept one argument or to accept a null as the second argument
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this TODO statement still needed?

TestUtils.validateTestReportExists(TEST_REPORT_PATH, TEST_REPORT_PATH);
TestUtils.validateTestReportExists(TEST_REPORT_PATH.toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,6 @@ public void deleteTestReports() {
*/
@Override
public void validateTestReportsExist() {
TestUtils.validateTestReportExists(TEST_REPORT_PATH, TEST_REPORT_PATH);
TestUtils.validateTestReportExists(TEST_REPORT_PATH.toFile());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public class MavenSingleModMPProjectTest extends SingleModMPProjectTestCommon {
@BeforeAll
public static void setup() {
StepWorker.registerProcessor(new StepLogger());
// Clear the cache to force the download of the Maven report plugin. It is not specified in the test case so the latest will be used (3.5.0 or later)
TestUtils.clearMavenPluginCache();
prepareEnv(PROJECTS_PATH, SM_MP_PROJECT_NAME);
}

Expand Down Expand Up @@ -213,10 +215,17 @@ public void deleteTestReports() {

/**
* Validates that test reports were generated.
* Since no specific report generator was chosen in the pom the
* latest report generator should be used (3.5 or later, see setup()).
*
*/
@Override
public void validateTestReportsExist() {
TestUtils.validateTestReportExists(pathToITReport34, pathToITReport35);
TestUtils.validateTestReportExists(pathToUTReport34, pathToUTReport35);
TestUtils.validateTestReportExists(pathToITReport34.toFile(), pathToITReport35.toFile());
TestUtils.validateTestReportExists(pathToUTReport34.toFile(), pathToUTReport35.toFile());
Assertions.assertTrue(pathToITReport35.toFile().exists(), "Integration test report missing: " + pathToITReport35);
Assertions.assertTrue(pathToUTReport35.toFile().exists(), "Unit test report missing: " + pathToUTReport35);
Assertions.assertFalse(pathToITReport34.toFile().exists(), "Integration test report should not be generated: " + pathToITReport34);
Assertions.assertFalse(pathToUTReport34.toFile().exists(), "Unit test report should not be generated: " + pathToUTReport34);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -97,8 +98,12 @@ public class MavenSingleModMPSIDProjectTest extends SingleModMPProjectTestCommon
public static void setup() {
try {
StepWorker.registerProcessor(new StepLogger());
// Copy the directory from PROJECTS_PATH to PROJECTS_PATH_NEW
// Copy the test case to a different path so I can test the directory name and modify the test case
TestUtils.copyDirectory(PROJECTS_PATH, PROJECTS_PATH_NEW);
// Clear the cache to force the download of the Maven report plugin specified in the test case
TestUtils.clearMavenPluginCache();
// Force Maven report generator 3.4 to generate the report in the old location
setupReportGenerator();
prepareEnv(PROJECTS_PATH_NEW, SM_MP_PROJECT_NAME);
} catch (IOException e) {
System.err.println("Setup failed: " + e.getMessage());
Expand All @@ -107,6 +112,22 @@ public static void setup() {
}
}

/**
* Modify the pom.xml file of the project to specify the Maven Surefire report generator version 3.4.0
*
* @throws IOException
*/
private static void setupReportGenerator() throws IOException {
File pomFile = Paths.get(PROJECTS_PATH_NEW, SM_MP_PROJECT_NAME, "pom.xml").toFile();
String oldStr = "<!-- Test report insertion point, do not remove -->";
String newStr = " <plugin>\n" +
" <groupId>org.apache.maven.plugins</groupId>\n" +
" <artifactId>maven-surefire-report-plugin</artifactId>\n" +
" <version>3.4.0</version>\n" +
" </plugin>";
replaceString(oldStr, newStr, pomFile);
}

/**
* Cleanup includes deleting the created project path.
*/
Expand Down Expand Up @@ -239,10 +260,17 @@ public void deleteTestReports() {

/**
* Validates that test reports were generated.
* Since a specific report generator was chosen in the pom
* the specified report generator should be used (3.4.0).
*
*/
@Override
public void validateTestReportsExist() {
TestUtils.validateTestReportExists(pathToITReport34, pathToITReport35);
TestUtils.validateTestReportExists(pathToUTReport34, pathToUTReport35);
TestUtils.validateTestReportExists(pathToITReport34.toFile(), pathToITReport35.toFile());
TestUtils.validateTestReportExists(pathToUTReport34.toFile(), pathToUTReport35.toFile());
Assertions.assertTrue(pathToITReport34.toFile().exists(), "Integration test report missing: " + pathToITReport34);
Assertions.assertTrue(pathToUTReport34.toFile().exists(), "Unit test report missing: " + pathToUTReport34);
Assertions.assertFalse(pathToITReport35.toFile().exists(), "Integration test report should not be generated: " + pathToITReport35);
Assertions.assertFalse(pathToUTReport35.toFile().exists(), "Unit test report should not be generated: " + pathToUTReport35);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Map;
Expand Down Expand Up @@ -1006,6 +1010,15 @@ public static void deleteDirectoryIfExists(String dirPath) {
}
}

protected static void replaceString(String str, String replacement, File file) throws IOException {
Path path = file.toPath();
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path), charset);

content = content.replaceAll(str, replacement);
Files.write(path, content.getBytes(charset));
}

/**
* Returns the projects directory path.
*
Expand Down
34 changes: 26 additions & 8 deletions src/test/java/io/openliberty/tools/intellij/it/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;


/**
Expand Down Expand Up @@ -370,31 +372,35 @@ public static void printLibertyMessagesLogFile(String msgHeader, String wlpMsgLo
}

/**
* Validates that one of the test reports represented by the input paths exists.
* Validates that one of the test reports represented by the input directories exists.
* Wait 100s for one of the directories to be created.
*
* @param pathToTestReport34 The path to the report for maven-surefire-report-plugin 3.4 or earlier
* @param pathToTestReport35 The path to the report for maven-surefire-report-plugin 3.5 or later
* @param dirs directories where test reports may be found
* @return the first directory created or throw an exception if none are created
*/
public static void validateTestReportExists(Path pathToTestReport34, Path pathToTestReport35) {
public static File validateTestReportExists(File... dirs) {
if (dirs.length <= 0) {
return null;
}
int retryCountLimit = 100;
int retryIntervalSecs = 1;
int retryCount = 0;

while (retryCount < retryCountLimit) {
retryCount++;
Optional<File> found = Arrays.stream(dirs).filter(File::exists).findFirst();

boolean fileExists = fileExists(pathToTestReport34.toAbsolutePath()) || fileExists(pathToTestReport35.toAbsolutePath());
if (!fileExists) {
if (!found.isPresent()) {
try {
Thread.sleep(retryIntervalSecs * 1000);
} catch (Exception e) {
e.printStackTrace(System.out);
}
} else {
return;
return found.get();
}
}
throw new IllegalStateException("Timed out waiting for test report: " + pathToTestReport34 + " or " + pathToTestReport35 + " file to be created.");
throw new IllegalStateException("Timed out waiting for test report: " + dirs[0] + " file to be created.");
}

/**
Expand Down Expand Up @@ -472,6 +478,18 @@ public static boolean deleteDirectory(File file) {
return file.delete();
}

/**
* Delete the directory that contains the resources for Maven plugins.
* If you try to use Maven the resources will be downloaded.
*
* @return true if the operation is successful
* @throws IOException
*/
public static boolean clearMavenPluginCache() {
Path pathToBeDeleted = Paths.get(System.getProperty("user.home"), ".m2/repository/org/apache/maven/plugins");
return TestUtils.deleteDirectory(pathToBeDeleted.toFile());
}

/**
* Prints a formatted message to STDOUT.
*
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/projects/maven/singleModMavenMP/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<!-- Test report insertion point, do not remove -->
<!-- Plugin to run functional tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
Loading