Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

arquillian add resources from main folder

I have a problem inside the business code of our JAVA EE application server. We access some files inside the src/main/resources folder with

  InputStream inputStream = Thread
                .currentThread()
                .getContextClassLoader()
                .getResourceAsStream(filePath);

In production this works, but I would like to test a part of code which use this functionality inside my arquillian test.

We are using shrinkwrap to generate our test.war.

 WebArchive testArchive = ShrinkWrap.create(WebArchive.class, "test.war")
                .addPackages(true, "ch.microtronic.evending")
                .addAsWebInfResource("wildfly-ds.xml")
                .setWebXML(new File("src/main/webapp/WEB-INF/web.xml"))
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml");

I have some trouble to add the resources from src/main/resources to the test.war.

I can only add files from src/test/resources.

Our directory structure looks like:

src
 |__main
 |   |__java
 |   |__resources
 |   |__webapp
 |   
 |__test
     |__java
     |__resources

What I have to do?

like image 810
Simon Schüpbach Avatar asked Aug 12 '16 12:08

Simon Schüpbach


3 Answers

If you want to add files that are not in src/test/resources you just need to use an overloaded version addAsResource(File, String) instead of addAsResource(String, String). For your example that would look like:

.addAsResource(new File("src/main/resources/test-persistence.xml"),"META-INF/persistence.xml");

I've just solved similar issue in my code, so this approach definitely works.

like image 156
Alex T Avatar answered Nov 11 '22 12:11

Alex T


This one-liner adds recursive all main resources from src:

testArchive.addAsResource(new File("src/main/resources/"), "");

this one for some resources postprocessed by maven:

testArchive.addAsResource(new File("target/classes/META-INF/"), "META-INF/");
like image 3
radzimir Avatar answered Nov 11 '22 11:11

radzimir


To include a subtree I use the following.

Say that you need in your classpath all files under src/main/resources/db/migration as db/migration/FILE_NAME

Path resources_dir = Paths.get("src", "main", "resources");
Path migrations_dir = resources_dir.resolve(Paths.get("db", "migration"));
Files.walk(migrations_dir)
  .forEach((p) -> jar.addAsResource(p.toFile(),
                                    resources_dir.relativize(p).toString()));

relativize

like image 2
Cec Avatar answered Nov 11 '22 11:11

Cec