4 Replies Latest reply on Jun 1, 2011 1:47 AM by samjaya

    the context path of a deployed resource

    samjaya

      I am using Arquillian to do some unit tests that involve passing a .docx file to one of the methods. currently i am using an absolute path , for ex: F:/somefolder/somefile.docx.

      in the test i have

      @Test

      public void testfoo(){

           foo.findFile("F:/somefolder/somefile.docx.");

      }

       

      This is working fine on my machine because i have that file. But since i work on a team project this wont work on a another machine resulting in a testfailure and a lot of trouble for me.

      i have tried to add the resource like this:

       

      ..addAsResource("F:/somefolder/somefile.docx:", "META-INF/somefile.docx")

       

      but then what is the file path i need to give as

      foo.findFile("????????????????????");

       

      i've tried different things.I know this can be a shrinkwrap problem. But if you can help me here it's much appreciated.

      Thanks in advance.

        • 1. Re: the context path of a deployed resource
          aslak

          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

            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 told

            then 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

              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

                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!