Search code examples
javamavenjarresourcesembedded-resource

How to test a function that copies a folder out of its own JAR file?


I have a function (see below) that copies all files of a specific folder from its own JAR.
For this function (see below too), I want to write tests, but the tests will be executed before the JAR is builded (it's a maven project) .

One important part of this function is that it only knows the folder name, but not the files in this folder so it must "query" for them.

The problem is, that the APIs to "query" for all files of one folder differ whether they work on a JAR (production) or on the file system (test).

My question is:

  • How to test the function that relay on the not yet existing JAR,
  • or how to "combine" the functions (how to decide whether it must the JAR or non JAR way),
  • or (that would be the best) is there any (small lib) that handle this subject?

The first method used for the jar. (production)
This is a little bit modified version of an answer to Copy directory from a jar file:

public class RessourcesUtil {
  ...

  public static void copyResourcesFromJar((String nameOfResourceDirectory, 
                  File destinationDirectory) throws IOException {

      String jarPath = RessourcesUtil.class.getProtectionDomain().getCodeSource().
                     getLocation().getFile();

      Enumeration<JarEntry> jarEntries = new JarFile(jarPath).entries();
      while (jarEntries.hasMoreElements()) {
          JarEntry fileFromJar = (JarEntry) jarEntries.nextElement();

          if (fileFromJar.getName().startsWith(nameOfJarDirectory)) {
              String plainFileName = fileFromJar.getName().
                 replaceAll(nameOfJarDirectory, "");
            
              File fileToCreate = 
                 new File(destinationDirectory + File.separator + plainFileName);

              if (fileFromJar.isDirectory()) {
                  fileToCreate.mkdir();
                  continue;
              }

              createFile(jarFile.getInputStream(fileFromJar), fileToCreate);
          }
      }
  }

  private static void createFile(InputStream inputStream, 
                   File fileToWrite) throws IOException {

        try (FileOutputStream fos = new FileOutputStream(fileToWrite)) {
          IOUtils.copy(inputStream, fos);
      }
   }
}

Second if the resources are not packed in a jar (for testing):

public class RessourcesUtil {
  public static void copyResourcesFromFileSystem(String nameOfResourceDirectory, 
                  File destinationDirectory) throws IOException {

      String classDir = RessourcesUtil.class.getProtectionDomain().getCodeSource().
                     getLocation().getPath();

      File srcDir = new File(classDir + File.separator + nameOfResourceDirectory);
      FileUtils.copyDirectory(new File(srcDir.getPath()), destinationDirectory);
  }
  ...
}

Both are working, but only under the circumstance of packed or unpacked.

Background:

For a project I wrote a multimodul Maven-plugin in Eclipse that can also be used as a standalone.

My resources are in a different modul than my main or my mojo.
Now I need to copy the a specific folder in resources, from my project into a different folder in every run.

P.S.
Copying every single file from resources via getResourcesAsStream is not an option.


Solution

  • Try this approach,

    Have your maven build as two parts first part being exclude the folder which you want to copy the files, so once this part is completed you will have your jars ready

    second part execute your copying code which takes the input from the jar and moves it to the specified different folder.