JPlagTestSuiteHelper.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package de.jplag.endToEndTesting.helper;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.StandardCopyOption;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import javax.annotation.processing.FilerException;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import de.jplag.endToEndTesting.constants.Constant;
  14. import de.jplag.options.LanguageOption;
  15. import model.ResultJsonModel;
  16. import model.TestCaseModel;
  17. public class JPlagTestSuiteHelper {
  18. private static final Logger logger = LoggerFactory.getLogger("EndToEndTesting");
  19. private String[] resourceNames;
  20. private String tempFolderPath;
  21. private List<ResultJsonModel> resultModel;
  22. private LanguageOption languageOption;
  23. /**
  24. * Helper class for the endToEnd tests. In this class the necessary resources
  25. * are loaded, prepared and copied for the tests based on the passed parameters.
  26. *
  27. * @param classNames of the resources that are required for the tests
  28. * @throws Exception
  29. */
  30. public JPlagTestSuiteHelper(LanguageOption languageOption) throws Exception {
  31. this.languageOption = languageOption;
  32. this.resourceNames = loadResource();
  33. this.tempFolderPath = getTempFolderPath();
  34. this.resultModel = JsonHelper
  35. .getResultModelFromPath(Constant.BASE_PATH_TO_JAVA_RESULT_JSON.toAbsolutePath().toString());
  36. logger.info(String.format("temp path at [%s]", this.temporaryFolderPath));
  37. }
  38. public TestCaseModel createNewTestCase(String[] classNames) throws Exception {
  39. createNewTestCaseDirectory(classNames);
  40. var functionName = StackWalker.getInstance().walk(stream -> stream.skip(1).findFirst().get()).getMethodName();
  41. ResultJsonModel resultJsonModel = resultModel.stream()
  42. .filter(jsonModel -> functionName.equals(jsonModel.getFunctionName())).findAny().orElse(null);
  43. return new TestCaseModel(tempFolderPath, resultJsonModel, languageOption);
  44. public void clear() throws Exception {
  45. logger.info("Class instance was cleaned!");
  46. deleteCopiedFiles(new File(temporaryFolderPath));
  47. }
  48. /**
  49. * Copies the passed filenames to a temporary path to use them in the tests
  50. *
  51. * @throws Exception
  52. */
  53. private void createNewTestCaseDirectory(String[] classNames) throws Exception {
  54. // before copying files to the test path, check if all files are in the resource
  55. // directory
  56. for (String className : classNames) {
  57. if (!Arrays.asList(resourceNames).contains(className)) {
  58. throw new FileNotFoundException(
  59. String.format("The specified class could not be found! [%s]", className));
  60. }
  61. }
  62. // Copy the resources data to the temporary path
  63. for (int counter = 0; counter < classNames.length; counter++) {
  64. Path originalPath = Path.of(Constant.BASE_PATH_TO_JAVA_RESOURCES_SORTALGO.toString(), classNames[counter]);
  65. Path copiePath = Path.of(tempFolderPath, Constant.TEMP_DIRECTORY_NAME + (counter + 1), classNames[counter]);
  66. try {
  67. File directory = new File(copiePath.toString());
  68. if (!directory.exists()) {
  69. directory.mkdirs();
  70. }
  71. logger.info(String.format("Copy file from [%s] to [%s]", originalPath, copiePath));
  72. Files.copy(originalPath, copiePath, StandardCopyOption.REPLACE_EXISTING);
  73. } catch (IOException ioException) {
  74. logger.error(String.format("The specified file could not be copied! [%s] \n Exception [%s] ",
  75. classNames[counter], ioException.getMessage()));
  76. throw ioException;
  77. }
  78. }
  79. }
  80. /**
  81. * loads the filenames in the specified resources
  82. *
  83. * @return
  84. */
  85. private String[] loadResource() {
  86. String[] pathnames;
  87. File f = new File(Constant.BASE_PATH_TO_JAVA_RESOURCES_SORTALGO.toString());
  88. pathnames = f.list();
  89. return pathnames;
  90. }
  91. /**
  92. * Loads a suitable system path to temporarily store the test cases.
  93. *
  94. * @return The temporary system folder, if any, or the path of the current
  95. * runtime environment
  96. * @throws IOException
  97. */
  98. private String getTempFolderPath() throws IOException {
  99. return !System.getProperty(Constant.TEMP_SYSTEM_DIRECTORY).isBlank()
  100. ? Path.of(System.getProperty(Constant.TEMP_SYSTEM_DIRECTORY), Constant.TEMP_DIRECTORY_NAME).toString()
  101. : Path.of(new File(".").getCanonicalPath(), Constant.TEMP_DIRECTORY_NAME).toString();
  102. }
  103. /**
  104. * Delete directory with including files
  105. *
  106. * @param file
  107. */
  108. private void deleteCopiedFiles(File folder) {
  109. File[] files = folder.listFiles();
  110. if (files != null) { // some JVMs return null for empty dirs
  111. for (File f : files) {
  112. if (f.isDirectory()) {
  113. deleteCopiedFiles(f);
  114. } else {
  115. logger.info(String.format("Delete file in folder: [%s]", f.toString()));
  116. f.delete();
  117. }
  118. }
  119. }
  120. logger.info(String.format("Delete folder: [%s]", folder.toString()));
  121. folder.delete();
  122. }
  123. }