1 2 Previous Next 16 Replies Latest reply on Jan 16, 2013 7:44 AM by greglevilain Go to original post
      • 15. Re: How to add a folder and its files to the test file in Arquillian?
        alrubinger

        Super!  And note this forum Thread?

         

        S,

        ALR

        • 16. Re: How to add a folder and its files to the test file in Arquillian?
          greglevilain

          Thanks u j. I've been using your code. Here's my way to do it now, using ArchivePath facilities :

          PS: I'm not sure about the necessity to differentiate RESOURCES (addAsResource) and WEB_RESOURCES (addAsWebResource)...

           

              @Deployment
              public static WebArchive createDeployment() {

           

                  File[] libs = Maven.resolver().loadPomFromFile("pom.xml").importRuntimeDependencies().asFile();
                  WebArchive war = ShrinkWrap.create(WebArchive.class, "MyApp");
                  war.addPackages(true, "org.demo.myapp");
                  war.addAsLibraries(libs);

           

                  File SRC_MAIN_WEBAPP = new File("src/main/webapp");
                  File SRC_MAIN_RESOURCES = new File("src/main/resources");

           

                  addFiles(war, SRC_MAIN_WEBAPP, ArquillianResourceType.WEB_RESOURCES);
                  addFiles(war, SRC_MAIN_RESOURCES, "WEB-INF/classes", ArquillianResourceType.RESOURCES);

           

                  System.out.println(war.toString(true));
                  return war;
              }

           

              private enum ArquillianResourceType {
                  RESOURCES, WEB_RESOURCES;
              }

           

              private static void addFiles(WebArchive war, File dir, ArquillianResourceType resourceType) {
                  addFiles(war, dir, "", resourceType);
              }

           

              private static void addFiles(WebArchive war, File dir, String context, ArquillianResourceType resourceType) {
                  addFiles(war, dir, ArchivePaths.create(context), resourceType);
              }

           

              private static void addFiles(WebArchive war, File dir, ArchivePath context, ArquillianResourceType resourceType) {
                  if (!dir.isDirectory()) {
                      throw new RuntimeException(dir.getAbsolutePath() + " is not a directory");
                  }

           

                  for (File f : dir.listFiles()) {
                      if (f.isFile()) {
                          ArchivePath fileContext = ArchivePaths.create(context, f.getName());
                          switch (resourceType) {
                          case RESOURCES:
                              war.addAsResource(f, fileContext);
                              break;
                          case WEB_RESOURCES:
                              war.addAsWebResource(f, fileContext);
                              break;
                          }
                      } else {
                          addFiles(war, f, ArchivePaths.create(context, f.getName()), resourceType);
                      }
                  }
              }

           

           

          1 of 1 people found this helpful
          1 2 Previous Next