Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [tycho-user] getResource() in test cases

>From http://docs.oracle.com/javase/6/docs/api/java/io/File.html:
  A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. ***By default the classes in the java.io package always resolve relative pathnames against the current user directory.*** This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked. 

Igor, does tycho-surefire-plugin always run tests with user.dir set to the path to the unjarred test bundle or how else does the "new File(project-relative-path)" trick work?

/Mikhail

----- Ursprungligt meddelande -----
Från: "Igor Fedorenko" <igor@xxxxxxxxxxxxxx>
Till: tycho-user@xxxxxxxxxxx
Skickat: lördag, 7 sep 2013 17:36:41
Ämne: Re: [tycho-user] getResource() in test cases

Your test bundle is a fragment, this means it does not have its own
classloader. I don't know enough about Equinox, but I think it will
always return bundleresource:// URLs when loaded resources from fragment
bundles.

So you either need to use non-fragment test bundle or don't assume
file:// test resource URLs.

Another possibility is to use "new File(project-relative-path)" instead
of ClassLoader.getResource. This is what I usually do in my tests.

--
Regards,
Igor

On 2013-09-06 10:06 AM, Dirk Fauth wrote:
> Here are the projects I used to play around with the resources issue.
> Please tell me if you received them this way or if I should use another
> way of sharing the example with you.
>
>
>
> On Fri, Sep 6, 2013 at 3:51 PM, Igor Fedorenko <igor@xxxxxxxxxxxxxx
> <mailto:igor@xxxxxxxxxxxxxx>> wrote:
>
>     Not sure. I need a small standalone example that demonstrates the
>     problem to tell if this is a bug or not.
>
>     --
>     Regards,
>     Igor
>
>
>     On 2013-09-06 9:41 AM, Dirk Fauth wrote:
>
>         This is not entirely true, at least this is not how it's supposed to
>         work. More specifically, the test bundle itself is supposed to be
>         directory-based, i.e. not a packaged jar, so resources coming
>         from the
>         test bundle are expected to have file:// urls.
>
>         So this means I ran across some bug?
>
>
>         On Fri, Sep 6, 2013 at 3:21 PM, Igor Fedorenko
>         <igor@xxxxxxxxxxxxxx <mailto:igor@xxxxxxxxxxxxxx>
>         <mailto:igor@xxxxxxxxxxxxxx <mailto:igor@xxxxxxxxxxxxxx>>> wrote:
>
>
>
>              On 2013-09-06 7:10 AM, Dirk Fauth wrote:
>
>                  Hi,
>
>                  thanks for the replies. I was digging into this a bit
>         deeper
>                  regarding
>                  your answers and found out several facts. So to help
>         others who
>                  face the
>                  same issues, here are my results:
>
>                  1. The surefire tests are only executed in the install
>         phase.
>                  Personally
>                  this is confusing, as I tried to build my shortened
>         examples
>                  using "mvn
>                  test" and "mvn package" as I didn't wanted to install
>         it. And no
>                  tests
>                  where executed. They are only executed when executing
>         "mvn install".
>                  Maybe not a big deal and necessary to ensure correct test
>                  execution, but
>                  IMHO confusing. So you need to be aware of that when
>         using Tycho.
>
>
>              Tycho tests are executed during integration-test phase.
>
>
>
>                  2. With the answers of Mickael Istria and Mikhail
>         Karkov I was
>                  able to
>                  find out what's going on. It is very important to know
>         that the
>                  tests
>                  are executed against the packaged plugin jar. Because
>         of this,
>                  the URL
>                  changes in test execution. While executing the test in
>         the IDE
>                  the URL
>                  is a "file:/" URL, executing the test with Tycho it
>         will be a
>                  "bundleclass:/" URL. Using Java File or NIO API, the
>         file can not be
>                  loaded with conventional methods if the URL starts with
>                  "bundleclass".
>
>
>              This is not entirely true, at least this is not how it's
>         supposed to
>              work. More specifically, the test bundle itself is supposed
>         to be
>              directory-based, i.e. not a packaged jar, so resources
>         coming from the
>              test bundle are expected to have file:// urls. It is also
>         possible to
>              control what other plugins should be directory-based during
>         the test,
>              either using Eclipse-BundleShape:dir directory in bundle
>         manifest or
>              using <explodedBundles> tycho-surefire-plugin configuration.
>
>              --
>              Regards,
>              Igor
>
>
>                  So how to solve this?
>
>                  The solution mentioned by Mikhail Karkov is a possible
>                  workaround, but
>                  IMHO it is not a very elegant one. Because in that case
>         I am
>                  forced to
>                  implement my test cases with a quite hard dependency to the
>                  environment.
>                  If I simply want to test some file util classes, I
>         don't want to
>                  deal
>                  with that.
>
>                  I tested and verified two possible workarounds that
>         look a bit more
>                  environment independent.
>
>                  1. Use getResourceAsStream() instead of getResource()
>                  If you only need to read from the file, you don't need
>         the File
>                  reference. The returning InputStream can be used to
>         read from
>                  the file
>                  perfectly. There is no need to deal with the
>         environment in that
>                  case.
>
>                  2. Copy the resources out of the jar into a file system
>         location and
>                  operate on that resource instead of a plugin resource
>                  This is the only solution I've found so far to make file
>                  operation tests
>                  work. So the process for testing need to be:
>                  - copy the necessary resources to the file system
>                  - operate on those resources instead of the ones in the
>         plugin jar
>                  - after execution cleanup the created resources
>
>                  Possibly in this case a TestSuite wrapper can be used
>         to ensure
>                  the copy
>                  and cleanup code in @BeforeClass and @AfterClass as
>         explained here:
>         http://stackoverflow.com/____questions/6580670/testsuite-____setup-in-junit-4
>         <http://stackoverflow.com/__questions/6580670/testsuite-__setup-in-junit-4>
>
>
>         <http://stackoverflow.com/__questions/6580670/testsuite-__setup-in-junit-4
>         <http://stackoverflow.com/questions/6580670/testsuite-setup-in-junit-4>>
>                  But I haven't tested that yet and will modify my test
>         cases now
>                  to see
>                  if that works too.
>
>                  Hopefully my investigation helps others with the same
>         issues. If my
>                  results are wrong, please let me know!
>
>                  Greez,
>                  Dirk
>
>
>                  On Thu, Sep 5, 2013 at 5:35 PM, Mikhail Kalkov
>                  <mikhail.kalkov@xxxxxxxxxxxxxx
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>
>                  <mailto:mikhail.kalkov@__purplescout.se
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>>
>                  <mailto:mikhail.kalkov@
>         <mailto:mikhail.kalkov@>__purpl__escout.se <http://purplescout.se>
>
>                  <mailto:mikhail.kalkov@__purplescout.se
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>>>>
>
>                  wrote:
>
>                       Hi Dirk,
>
>                       I've used the way suggested in
>         http://blog.vogella.com/2010/____07/06/reading-resources-from-____plugin/
>         <http://blog.vogella.com/2010/__07/06/reading-resources-from-__plugin/>
>
>
>         <http://blog.vogella.com/2010/__07/06/reading-resources-from-__plugin/
>         <http://blog.vogella.com/2010/07/06/reading-resources-from-plugin/>>
>                  to
>                       read in XML files and also made sure that the xml/
>         directory is
>                       present in build.properties/bin.includes list.
>
>                       1. Loading XML
>                       +    private MenuModel loadMenuModelFrom(String
>                  testFileLocation) {
>                       +        String fileUrlString = "platform:/plugin/" +
>                  PLUGIN_ID +
>                       testFileLocation;
>                       +        InputStream inputStream = null;
>                       +        try {
>                       +            URL fileUrl = new URL(fileUrlString);
>                       +            inputStream =
>                  fileUrl.openConnection().____getInputStream();
>
>                       +        } catch (MalformedURLException e) {
>                       +            fail("The URL is invalid: " +
>         fileUrlString);
>                       +        } catch (IOException e) {
>                       +            // inputStream = null;
>                       +        }
>                       +        return new
>         XMLParser().readMenuModel(____inputStream);
>
>                             }
>
>                       2. Packaging XML with the binary
>                       -> cat build.properties
>                       source.. = src/
>                       output.. = bin/
>                       bin.includes = META-INF/,\
>                                       .,\
>                                       xml/
>
>                       It works both from Tycho and from Eclipse.
>
>                       Kind regards,
>                       Mikhail Kalkov
>
>                       Purple Scout AB
>                       Software Developer
>
>                       Address: Kyrkogatan 20-22, SE-41110 Gothenburg, Sweden
>                       Phone: +46 (0) 732 - 051405
>         <tel:%2B46%20%280%29%20732%20-%20051405>
>                  <tel:%2B46%20%280%29%20732%20-__%20051405>
>                  <tel:%2B46%20%280%29%20732%20-____%20051405>
>                       E-mail: mikhail.kalkov@xxxxxxxxxxxxxx
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>
>                  <mailto:mikhail.kalkov@__purplescout.se
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>>
>                       <mailto:mikhail.kalkov@
>         <mailto:mikhail.kalkov@>__purpl__escout.se <http://purplescout.se>
>
>                  <mailto:mikhail.kalkov@__purplescout.se
>         <mailto:mikhail.kalkov@xxxxxxxxxxxxxx>>>
>                       Web: www.purplescout.se
>         <http://www.purplescout.se> <http://www.purplescout.se>
>                  <http://www.purplescout.se>
>
>
>
>
>         ------------------------------____----------------------------__--__------------
>
>                       *Från: *"Dirk Fauth" <dirk.fauth@xxxxxxxxx
>         <mailto:dirk.fauth@xxxxxxxxx>
>                  <mailto:dirk.fauth@xxxxxxxxx <mailto:dirk.fauth@xxxxxxxxx>>
>                       <mailto:dirk.fauth@xxxxxxxxx
>         <mailto:dirk.fauth@xxxxxxxxx> <mailto:dirk.fauth@xxxxxxxxx
>         <mailto:dirk.fauth@xxxxxxxxx>>>__>
>                       *Till: *tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx>
>                  <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx>__>
>         <mailto:tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>                  <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx>__>__>
>
>                       *Skickat: *torsdag, 5 sep 2013 16:48:58
>                       *Ämne: *[tycho-user] getResource() in test cases
>
>
>
>                       Hi,
>
>                       I came across some strange behaviour when running
>         my test
>                  cases with
>                       tycho.
>
>                       I have created an eclipse-test-plugin with using the
>                       tycho-surefire-plugin
>
>                       The tycho-surefire-plugin is configured to run a
>         test suite by
>                       applying the arguments for testSuite and testClass.
>
>                       My test cases need to load and process XML files
>         that are
>                  provided
>                       in the sources.
>
>                       I tried two scenarios:
>                       a) put the XML file relative to the test class and
>         load the
>                  file via
>                       <classname>.class.getResource(____)
>
>                       b) put the XML file to src/test/resources and load via
>                       this.getClass().getResource()
>
>                       The first approach is some generic one, the second
>         is Maven
>                  specific.
>
>                       For a) I found the XML file in the compiled
>         target/classes
>                  folder
>                       respectively to the package it should be in.
>
>                       For b) the XML file is located in target/test-classes.
>
>                       So far everything is working as intended. If I run
>         my JUnit
>                  test
>                       from within Eclipse, everything works as expected
>         for both
>                       scenarios. But when running the tests with Tycho,
>         in both
>                  cases I
>                       get errors saying "The given file does not exist!"
>
>                       I've found the mailing list entry here
>         http://dev.eclipse.org/____mhonarc/lists/tycho-user/____msg04534.html
>         <http://dev.eclipse.org/__mhonarc/lists/tycho-user/__msg04534.html>
>         <http://dev.eclipse.org/__mhonarc/lists/tycho-user/__msg04534.html
>         <http://dev.eclipse.org/mhonarc/lists/tycho-user/msg04534.html>>
>
>                  but it
>                       doesn't contain a hint on a solution.
>
>                       Is there any progress on that? Am I doing things
>         wrong to load
>                       resources for my test cases? Is there a solution I
>         haven't
>                  found
>                       anywhere?
>
>                       It would be great if you could give me some
>         feedback on that.
>
>                       Greez,
>                       Dirk
>
>                       ___________________________________________________
>
>                       tycho-user mailing list
>         tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>         <mailto:tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>__>
>                  <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx> <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx>__>__>
>
>         https://dev.eclipse.org/____mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/__mailman/listinfo/tycho-user>
>                  <https://dev.eclipse.org/__mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/mailman/listinfo/tycho-user>>
>
>
>                       ___________________________________________________
>
>                       tycho-user mailing list
>         tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>         <mailto:tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>__>
>                  <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx> <mailto:tycho-user@xxxxxxxxxxx
>         <mailto:tycho-user@xxxxxxxxxxx>__>__>
>
>         https://dev.eclipse.org/____mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/__mailman/listinfo/tycho-user>
>                  <https://dev.eclipse.org/__mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/mailman/listinfo/tycho-user>>
>
>
>
>
>                  ___________________________________________________
>
>                  tycho-user mailing list
>         tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>         <mailto:tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>__>
>         https://dev.eclipse.org/____mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/__mailman/listinfo/tycho-user>
>                  <https://dev.eclipse.org/__mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/mailman/listinfo/tycho-user>>
>
>              ___________________________________________________
>
>              tycho-user mailing list
>         tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>         <mailto:tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>__>
>         https://dev.eclipse.org/____mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/__mailman/listinfo/tycho-user>
>
>              <https://dev.eclipse.org/__mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/mailman/listinfo/tycho-user>>
>
>
>
>
>         _________________________________________________
>         tycho-user mailing list
>         tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>         https://dev.eclipse.org/__mailman/listinfo/tycho-user
>         <https://dev.eclipse.org/mailman/listinfo/tycho-user>
>
>     _________________________________________________
>     tycho-user mailing list
>     tycho-user@xxxxxxxxxxx <mailto:tycho-user@xxxxxxxxxxx>
>     https://dev.eclipse.org/__mailman/listinfo/tycho-user
>     <https://dev.eclipse.org/mailman/listinfo/tycho-user>
>
>
>
>
> _______________________________________________
> tycho-user mailing list
> tycho-user@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/tycho-user
>
_______________________________________________
tycho-user mailing list
tycho-user@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/tycho-user


Back to the top