-
1. Re: the context path of a deployed resource
aslak May 31, 2011 6:35 AM (in response to samjaya)put the xxxxx.docx file on classpath, e.g. in src/test/resources/xxxx.docx then add it to the Archive using addAsReosurce("xxxx.docx")
-
2. Re: the context path of a deployed resource
samjaya May 31, 2011 7:16 AM (in response to aslak)thanks for the super quick reply.
I've already tried what you have told. My problem is not attaching the resource to the archive. But accessing it from within the test itself.
Say i did what you have toldthen what should come for the parameter of :
foo.findFile(String filepath); method.
i've tried using foo.findfile("xxxx.docx");
and foo.findfile("/src/test/resources/xxxx.docx");
but both did not work. They gave file not found errors in the server log.
It is super if i can use a file path like ""/src/test/resources/xxxx.docx"". So it would work on all machines.
here is the deployment code:
@Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addClass(Foo.class)
.addAsResource("persistence.xml", "META-INF/persistence.xml")
.addAsResource("Contract.docx")
.addAsResource("contract.xml")
.addAsResource(EmptyAsset.INSTANCE, "META-INF/beans.xml");
}
Help!
-
3. Re: the context path of a deployed resource
aslak May 31, 2011 8:10 AM (in response to samjaya)aha, I missunderstood..
hmm, depends a bit on what you are testing i guess...
A couple of options on top of my head..
- Read the file from the deployment, e.g. Thread.currentThread().getContextClassLoader().getResource(name) and write it to e.g. File.createTempFile() and pass that Path to the find method.
- Open Foo.findFile for URL instead of String path, and use food.findFile(Thread.currentThread().getContextClassLoader().getResource(name))
-
4. Re: the context path of a deployed resource
samjaya Jun 1, 2011 1:47 AM (in response to aslak)hmm. I thought there was easy way to access the file in the deployed .war itself. Anyway i tried the long way around and got the test passing.
What i did:
add the resource as follows:
.....addAsResource("XXXX.docx")
write it to a file as follows:
InputStream in1=Thread.currentThread().getContextClassLoader().getResourceAsStream("XXXX.docx");
assertNotNull(in1);
if( new File("F:/tempFiles/contentRepoFacade").mkdirs()){
IOUtils.copy(in1,new FileOutputStream(new File("F:/tempFiles/contentRepoFacade/XXXX.docx")));
then give the path to find() as follows:
foo.find("F:/tempFiles/contentRepoFacade/XXXX.docx");
Thanks Aslak!