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

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.

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".

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
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> wrote:
Hi Dirk,

I've used the way suggested in 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
E-mail:  mikhail.kalkov@xxxxxxxxxxxxxx
Web:     www.purplescout.se



Från: "Dirk Fauth" <dirk.fauth@xxxxxxxxx>
Till: 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 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
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