3

I want to read a test CSV file (xxx.csv) in my integration test using arquillian. I am packing my archive as follows:

@Deployment
public static WebArchive createDeployment() {
    WebArchive archive = ShrinkWrap.create(WebArchive.class);
    archive.addClasses(FileReader.class, FileWriteService.class, MockDataFactory.class, CSVFileReader.class, ExcelFileReader.class);
    archive.addAsResource("config.properties").addAsResource("xxx.csv");
    System.out.println(archive.toString(true));
    return archive;
}

Where the print shows me:

> 7fadc596-9353-4cac-aacc-cf5ec5d94c16.war: /WEB-INF/
> /WEB-INF/classes/ /WEB-INF/classes/config.properties
> /WEB-INF/classes/com/ /WEB-INF/classes/com/goodgamestudios/
> /WEB-INF/classes/com/goodgamestudios/icosphere/
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/CSVFileReader.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/ExcelFileReader$SheetHandler.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/ExcelFileReader.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/ExcelFileReader$xssfDataType.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/ExcelFileReader$1.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileReader/FileReader.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileWriter/
> /WEB-INF/classes/com/goodgamestudios/icosphere/service/fileWriter/FileWriteService.class
> /WEB-INF/classes/com/goodgamestudios/icosphere/datamodel/
> /WEB-INF/classes/com/goodgamestudios/icosphere/datamodel/MockData/
> /WEB-INF/classes/com/goodgamestudios/icosphere/datamodel/MockData/MockDataFactory.class
> /WEB-INF/classes/xxx.csv

As you can see, the file is clearly in the archive (last row).

Now I am trying to open it. I have tried:

private File getFile(String filename) throws IOException {
    return convertInputStreamToFileHelper(getClass().getResourceAsStream(filename), filename);
}

new File(this.getClass().getResource("/xxx.csv").getFile()

new File("/xxx.csv")

new File("xxx.csv")

but none would work.

olkoza
  • 715
  • 2
  • 17
  • 35
  • what app server are you running? Where are you trying to open the file, in your test, or within a class also packaged in the .war file? – Tea Curran Sep 22 '15 at 15:54

3 Answers3

4

You can get InputStream: this.getClass().getResourceAsStream("/xxx.csv"), but it is not possible to get File object for packed resources inside war. If you need File object you can copy InputStream content to tmp file.

sibnick
  • 3,995
  • 20
  • 20
0

You have to use the full path to the file. In order to try check out the following code:

URL fileURL = this.getClass().getResource("/WEB-INF/classes/xxx.csv");
System.out.println(fileURL);

You should get a URL different that null.

Be careful with getResource because I think it is case sensitive. At least I had issues with that.

narko
  • 3,645
  • 1
  • 28
  • 33
  • Url comes out to null. – olkoza Sep 18 '15 at 11:48
  • I made a test on my end and worked with the path from the previous comment... Maybe you try opening the war manually and checking if the file is really there? I know you already checked that in the trace, but just to make sure. – narko Sep 18 '15 at 12:12
0

I think it always depends on the application server you use and its classpath settings!

F.ex. here I have a file in test/resources, and I set the destination as WEB-INF/classes/file, where Wildfly (JBoss) will find it:

.addAsWebInfResource("file.txt",
                     "classes/file.txt")

using:

final InputStream inStream = this.getClass().getClassLoader()
                                .getResourceAsStream(fileName);
cslotty
  • 1,696
  • 20
  • 28