-
15. Re: How to add a folder and its files to the test file in Arquillian?
alrubinger Apr 5, 2012 3:47 PM (in response to bcn)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 Jan 16, 2013 7:44 AM (in response to bcn)1 of 1 people found this helpfulThanks 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);
}
}
}@Deployment
public static WebArchive createDeployment() {
File SRC_MAIN_WEBAPP = new File("src/main/webapp");
File SRC_MAIN_RESOURCES = new File("src/main/resources");
war.addPackages(true, "org.demo.packager");
war.addAsLibraries(libs);
addFiles(war, SRC_MAIN_WEBAPP, ArquillianResourceType.WEB_RESOURCES);
addFiles(war, SRC_MAIN_RESOURCES, "WEB-INF/classes", ArquillianResourceType.RESOURCES);
return war;
}
RESOURCES, WEB_RESOURCES;
}
addFiles(war, dir, "", resourceType);
}
addFiles(war, dir, ArchivePaths.create(context), resourceType);
}
if (!dir.isDirectory()) {
throw new RuntimeException(dir.getAbsolutePath() + " is not a directory");
}
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);
}
}
}