Discussion:
getResource() in test cases
(too old to reply)
Dirk Fauth
2013-09-05 14:48:58 UTC
Permalink
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
Mickael Istria
2013-09-05 15:25:21 UTC
Permalink
Can you see your file in the output jar
target/<artifactid>-<version>.jar ? This is the file used during test,
so you should check it contains your resource.
--
Mickael Istria
Eclipse developer at JBoss, by Red Hat <http://www.jboss.org/tools>
My blog <http://mickaelistria.wordpress.com> - My Tweets
<http://twitter.com/mickaelistria>
Dirk Fauth
2013-09-05 15:30:37 UTC
Permalink
Looking in the created jar I can see that there is the top level package
folder, the META-INF folder and the test folder. The test folder contains
the resources folder with the XML file to load.

So that seems to be correct.
Can you see your file in the output jar target/<artifactid>-<version>.jar
? This is the file used during test, so you should check it contains your
resource.
--
Mickael Istria
Eclipse developer at JBoss, by Red Hat <http://www.jboss.org/tools>
My blog <http://mickaelistria.wordpress.com> - My Tweets<http://twitter.com/mickaelistria>
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Mikhail Kalkov
2013-09-05 15:35:56 UTC
Permalink
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: ***@purplescout.se
Web: www.purplescout.se


----- Ursprungligt meddelande -----

Från: "Dirk Fauth" <***@gmail.com>
Till: tycho-***@eclipse.org
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-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 11:10:40 UTC
Permalink
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 <
Post by Mikhail Kalkov
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
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Tony HEDOUX
2013-09-06 12:07:43 UTC
Permalink
Hi,

I think Surefire tests are executed in integration-test phase no?

Tony
Post by Dirk Fauth
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
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
- 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
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
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 <tel:%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se <http://www.purplescout.se>
------------------------------------------------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
--
*Tony HEDOUX*
/Software Engineer/

Phone : +33 (0)2 43 49 14 14
Mail : ***@all4tec.net
------------------------------------------------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net <http://www.all4tec.net>
Dirk Fauth
2013-09-06 12:15:03 UTC
Permalink
I created a simple scenario that only consists of a plugin, a test fragment
for that plugin and a parent project. The tests are only executed for "mvn
clean install". mvn test and mvn package have no effect on the test
executions.
Post by Tony HEDOUX
Hi,
I think Surefire tests are executed in integration-test phase no?
Tony
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
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
- 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
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 <
Post by Mikhail Kalkov
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 <%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
--
*Tony HEDOUX*
*Software Engineer*
Phone : +33 (0)2 43 49 14 14
------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Robert Munteanu
2013-09-06 12:17:24 UTC
Permalink
Try mvn verify, that invokes the integration-test phase.
Post by Dirk Fauth
I created a simple scenario that only consists of a plugin, a test
fragment for that plugin and a parent project. The tests are only executed
for "mvn clean install". mvn test and mvn package have no effect on the
test executions.
Post by Tony HEDOUX
Hi,
I think Surefire tests are executed in integration-test phase no?
Tony
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
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
- 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
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 <
Post by Mikhail Kalkov
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 <%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
--
*Tony HEDOUX*
*Software Engineer*
Phone : +33 (0)2 43 49 14 14
------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
--
http://robert.muntea.nu/
Tony HEDOUX
2013-09-06 12:46:57 UTC
Permalink
Just make the same here, and 'mvn integration-test' works, my tests are
executed.

http://www.eclipse.org/tycho/sitedocs/tycho-surefire/tycho-surefire-plugin/test-mojo.html
Post by Dirk Fauth
I created a simple scenario that only consists of a plugin, a test
fragment for that plugin and a parent project. The tests are only
executed for "mvn clean install". mvn test and mvn package have no
effect on the test executions.
Hi,
I think Surefire tests are executed in integration-test phase no?
Tony
Post by Dirk Fauth
Hi,
thanks for the replies. I was digging into this a bit deeper
regarding your answers and found out several facts. So to help
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
- 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
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
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
<tel:%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se <http://www.purplescout.se>
------------------------------------------------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
--
*Tony HEDOUX*
/Software Engineer/
Phone : +33 (0)2 43 49 14 14
<tel:%2B33%20%280%292%2043%2049%2014%2014>
------------------------------------------------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net <http://www.all4tec.net>
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
--
*Tony HEDOUX*
/Software Engineer/

Mail : ***@all4tec.net
------------------------------------------------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net <http://www.all4tec.net>
Dirk Fauth
2013-09-06 12:50:08 UTC
Permalink
Yes you are right! My fault! I forgot about the integration-test lifecycle!
:(

Thanks for clearing up my wrong interpretion!
Post by Tony HEDOUX
Just make the same here, and 'mvn integration-test' works, my tests are
executed.
http://www.eclipse.org/tycho/sitedocs/tycho-surefire/tycho-surefire-plugin/test-mojo.html
I created a simple scenario that only consists of a plugin, a test
fragment for that plugin and a parent project. The tests are only executed
for "mvn clean install". mvn test and mvn package have no effect on the
test executions.
Post by Tony HEDOUX
Hi,
I think Surefire tests are executed in integration-test phase no?
Tony
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
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
- 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
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 <
Post by Mikhail Kalkov
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 <%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
--
*Tony HEDOUX*
*Software Engineer*
Phone : +33 (0)2 43 49 14 14 <%2B33%20%280%292%2043%2049%2014%2014>
------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
--
*Tony HEDOUX*
*Software Engineer*
------------------------------
6 rue Léonard De Vinci - BP 0119 - 53001 LAVAL Cedex - FRANCE
www.all4tec.net
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Igor Fedorenko
2013-09-06 13:21:01 UTC
Permalink
Post by Dirk Fauth
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
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.
Post by Dirk Fauth
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
Post by Dirk Fauth
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
- 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
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
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 <tel:%2B46%20%280%29%20732%20-%20051405>
Web: www.purplescout.se <http://www.purplescout.se>
------------------------------------------------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 13:41:47 UTC
Permalink
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?
Post by Igor Fedorenko
Post by Dirk Fauth
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
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
Post by Dirk Fauth
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?
Post by Dirk Fauth
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
- 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
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
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/>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>
Web: www.purplescout.se <http://www.purplescout.se>
------------------------------**------------------------------**
------------
*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.
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>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
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
Igor Fedorenko
2013-09-06 13:51:26 UTC
Permalink
Not sure. I need a small standalone example that demonstrates the
problem to tell if this is a bug or not.

--
Regards,
Igor
Post by Igor Fedorenko
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?
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
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
- 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
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
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/>
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>
Web: www.purplescout.se <http://www.purplescout.se>
<http://www.purplescout.se>
------------------------------__------------------------------__------------
*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.
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>
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
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_________________________________________________
tycho-user mailing list
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_________________________________________________
tycho-user mailing list
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_________________________________________________
tycho-user mailing list
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 14:06:11 UTC
Permalink
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.
Post by Igor Fedorenko
Not sure. I need a small standalone example that demonstrates the
problem to tell if this is a bug or not.
--
Regards,
Igor
Post by Igor Fedorenko
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?
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
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
- 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
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
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>
Web: www.purplescout.se <http://www.purplescout.se>
<http://www.purplescout.se>
------------------------------**__----------------------------**
--__------------
*>
*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.
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
__>
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
__>
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
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
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
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
Igor Fedorenko
2013-09-07 15:36:41 UTC
Permalink
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
Post by Dirk Fauth
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.
Not sure. I need a small standalone example that demonstrates the
problem to tell if this is a bug or not.
--
Regards,
Igor
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
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
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
- 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
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
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>
Web: www.purplescout.se
<http://www.purplescout.se> <http://www.purplescout.se>
<http://www.purplescout.se>
------------------------------____----------------------------__--__------------
*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.
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
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
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
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
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
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_________________________________________________
tycho-user mailing list
https://dev.eclipse.org/__mailman/listinfo/tycho-user
<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Mikhail Kalkov
2013-09-06 12:21:17 UTC
Permalink
I believe the surefire tests are executed in the verify phase of the default lifecycle. Please, check http://developer-blog.cloudbees.com/2012/12/maven-and-hack.html to make sure you're not making a mistake by using mvn install in this context.

Also, I don't see any problem if an Eclipse plug-in depends on Eclipse Platform, but I may miss something. Anyway, getResourceAsStream() sounds like a good alternative to me. Why did you have to use manual file-copying instead of it?


/Mikhail

----- Ursprungligt meddelande -----

Från: "Dirk Fauth" <***@gmail.com>
Till: "Tycho user list" <tycho-***@eclipse.org>
Skickat: fredag, 6 sep 2013 13:10:40
Ämne: 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 < ***@purplescout.se > 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: ***@purplescout.se
Web: www.purplescout.se




Från: "Dirk Fauth" < ***@gmail.com >
Till: tycho-***@eclipse.org
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-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user


_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user





_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 12:28:34 UTC
Permalink
"I don't see any problem if an Eclipse plug-in depends on Eclipse Platform"

Well, there is no problem. It is more my personal opinion that I don't like
to implement testcases that need to load resources by creating URIs that
are platform dependent. I like more the general approaches, for example if
my code will move to some general packages that are not tied to the Eclipse
Platform. :)

"Why did you have to use manual file-copying instead of it?"

Because I have some API that works on File objects. It reads a file,
transforms the contents and create the output file in relation to the input
file. It is some file interface code. So I need to be able to read the
files from the file system and write new files at the same place.


On Fri, Sep 6, 2013 at 2:21 PM, Mikhail Kalkov <
I believe the surefire tests are executed in the *verify *phase of the
default lifecycle. Please, check
http://developer-blog.cloudbees.com/2012/12/maven-and-hack.html to make
sure you're not making a mistake by using mvn install in this context.
Also, I don't see any problem if an Eclipse plug-in depends on Eclipse
Platform, but I may miss something. Anyway, getResourceAsStream() sounds
like a good alternative to me. Why did you have to use manual file-copying
instead of it?
/Mikhail
------------------------------
*Skickat: *fredag, 6 sep 2013 13:10:40
*Ämne: *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
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
- 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
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 <
Post by Mikhail Kalkov
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
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Mikhail Kalkov
2013-09-06 12:50:00 UTC
Permalink
Thanks for the explanation!

As for manual file-copying... Maybe, this article would help you marry getResource() with the code relying on the File API: https://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html


/Mikhail

----- Ursprungligt meddelande -----

Från: "Dirk Fauth" <***@gmail.com>
Till: "Tycho user list" <tycho-***@eclipse.org>
Skickat: fredag, 6 sep 2013 14:28:34
Ämne: Re: [tycho-user] getResource() in test cases





"I don't see any problem if an Eclipse plug-in depends on Eclipse Platform"

Well, there is no problem. It is more my personal opinion that I don't like to implement testcases that need to load resources by creating URIs that are platform dependent. I like more the general approaches, for example if my code will move to some general packages that are not tied to the Eclipse Platform. :)

"Why did you have to use manual file-copying instead of it?"

Because I have some API that works on File objects. It reads a file, transforms the contents and create the output file in relation to the input file. It is some file interface code. So I need to be able to read the files from the file system and write new files at the same place.




On Fri, Sep 6, 2013 at 2:21 PM, Mikhail Kalkov < ***@purplescout.se > wrote:




I believe the surefire tests are executed in the verify phase of the default lifecycle. Please, check http://developer-blog.cloudbees.com/2012/12/maven-and-hack.html to make sure you're not making a mistake by using mvn install in this context.

Also, I don't see any problem if an Eclipse plug-in depends on Eclipse Platform, but I may miss something. Anyway, getResourceAsStream() sounds like a good alternative to me. Why did you have to use manual file-copying instead of it?


/Mikhail



Från: "Dirk Fauth" < ***@gmail.com >
Till: "Tycho user list" < tycho-***@eclipse.org >
Skickat: fredag, 6 sep 2013 13:10:40
Ämne: 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 < ***@purplescout.se > wrote:

<blockquote>


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: ***@purplescout.se
Web: www.purplescout.se




Från: "Dirk Fauth" < ***@gmail.com >
Till: tycho-***@eclipse.org
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-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user


_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user





_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user


_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user


</blockquote>


_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 12:59:02 UTC
Permalink
Nope, it doesn't help, because of the "bundleclass" in the URI. And it is
from 2007, today you would more like to work with NIO ;)


On Fri, Sep 6, 2013 at 2:50 PM, Mikhail Kalkov <
Post by Mikhail Kalkov
Thanks for the explanation!
As for manual file-copying... Maybe, this article would help you marry
https://weblogs.java.net/blog/kohsuke/archive/2007/04/how_to_convert.html
/Mikhail
------------------------------
*Skickat: *fredag, 6 sep 2013 14:28:34
*Ämne: *Re: [tycho-user] getResource() in test cases
"I don't see any problem if an Eclipse plug-in depends on Eclipse Platform"
Well, there is no problem. It is more my personal opinion that I don't
like to implement testcases that need to load resources by creating URIs
that are platform dependent. I like more the general approaches, for
example if my code will move to some general packages that are not tied to
the Eclipse Platform. :)
"Why did you have to use manual file-copying instead of it?"
Because I have some API that works on File objects. It reads a file,
transforms the contents and create the output file in relation to the input
file. It is some file interface code. So I need to be able to read the
files from the file system and write new files at the same place.
On Fri, Sep 6, 2013 at 2:21 PM, Mikhail Kalkov <
I believe the surefire tests are executed in the *verify *phase of the
default lifecycle. Please, check
http://developer-blog.cloudbees.com/2012/12/maven-and-hack.html to make
sure you're not making a mistake by using mvn install in this context.
Also, I don't see any problem if an Eclipse plug-in depends on Eclipse
Platform, but I may miss something. Anyway, getResourceAsStream() sounds
like a good alternative to me. Why did you have to use manual file-copying
instead of it?
/Mikhail
------------------------------
*Skickat: *fredag, 6 sep 2013 13:10:40
*Ämne: *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
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
- 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
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 <
Post by Mikhail Kalkov
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
Web: www.purplescout.se
------------------------------
*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.
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
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Mikhail Kalkov
2013-09-06 13:46:08 UTC
Permalink
Wouldn't something like this work? Or did getResource() return you a bundleclass:// URL?
ClassLoader loader = Thread . currentThread (). getContextClassLoader ();
URL url = loader.getResource("xml/ filename with whitespaces .xml");
File f = new File ( url . toURI ()); Beware, I haven't run this code.


/Mikhail

----- Ursprungligt meddelande -----

Från: "Dirk Fauth" <***@gmail.com>
Till: "Tycho user list" <tycho-***@eclipse.org>
Skickat: fredag, 6 sep 2013 15:41:47
Ämne: Re: [tycho-user] getResource() in test cases



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 < ***@ifedorenko.com > wrote:





On 2013-09-06 7:10 AM, Dirk Fauth wrote:

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




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


</blockquote>

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



<blockquote>


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
< ***@purplescout.se <mailto: mikhail.kalkov@ purplescout.se >>


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 <tel:%2B46%20%280%29%20732%20- %20051405>
E-mail: ***@purplescout.se
<mailto: mikhail.kalkov@ purplescout.se >
Web: www.purplescout.se < http://www.purplescout.se >


------------------------------ ------------------------------ ------------
*Från: *"Dirk Fauth" < ***@gmail.com
<mailto: ***@gmail.com >>
*Till: * tycho-***@eclipse.org <mailto: tycho-***@eclipse.org >
*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-***@eclipse.org <mailto: tycho-***@eclipse.org >

https://dev.eclipse.org/ mailman/listinfo/tycho-user


______________________________ _________________
tycho-user mailing list
tycho-***@eclipse.org <mailto: tycho-***@eclipse.org >

https://dev.eclipse.org/ mailman/listinfo/tycho-user




______________________________ _________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/ mailman/listinfo/tycho-user


</blockquote>


______________________________ _________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/ mailman/listinfo/tycho-user

</blockquote>


_______________________________________________
tycho-user mailing list
tycho-***@eclipse.org
https://dev.eclipse.org/mailman/listinfo/tycho-user
Dirk Fauth
2013-09-06 13:49:14 UTC
Permalink
That's what I'm trying to say - getResource() returns a bundleclass:// URL
when running the test plugin with tycho surefire


On Fri, Sep 6, 2013 at 3:46 PM, Mikhail Kalkov <
Post by Mikhail Kalkov
Wouldn't something like this work? Or did getResource() return you a bundleclass:// URL?
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource("xml/filename with whitespaces.xml");
File f = new File(url.toURI());
Beware, I haven't run this code.
/Mikhail
------------------------------
*Skickat: *fredag, 6 sep 2013 15:41:47
*Ämne: *Re: [tycho-user] getResource() in test cases
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?
Post by Igor Fedorenko
Post by Dirk Fauth
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
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
Post by Dirk Fauth
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?
Post by Dirk Fauth
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
- 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
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
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/>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>
Web: www.purplescout.se <http://www.purplescout.se>
------------------------------**------------------------------**
------------
*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.
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>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
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
______________________________**_________________
tycho-user mailing list
https://dev.eclipse.org/**mailman/listinfo/tycho-user<https://dev.eclipse.org/mailman/listinfo/tycho-user>
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
_______________________________________________
tycho-user mailing list
https://dev.eclipse.org/mailman/listinfo/tycho-user
Mikhail Kalkov
2013-09-09 11:56:18 UTC
Permalink
Igor Fedorenko
2013-09-09 12:08:44 UTC
Permalink
Tycho runs tests with current working directory set to project basedir.
Same as JDT and PDE default config.

--
Regards,
Igor

On 2013-09-09 7:56 AM, Mikhail Kalkov wrote:
Mikhail Kalkov
2013-09-09 12:37:17 UTC
Permalink
Thanks for the tip, it simplifies things a lot!

/Mikhail

----- Ursprungligt meddelande -----
Från: "Igor Fedorenko" <***@ifedorenko.com>
Till: tycho-***@eclipse.org
Skickat: måndag, 9 sep 2013 14:08:44
Ämne: Re: [tycho-user] getResource() in test cases

Tycho runs tests with current working directory set to project basedir.
Same as JDT and PDE default config.

--
Regards,
Igor

On 2013-09-09 7:56 AM, Mikhail Kalkov wrote:
Continue reading on narkive:
Loading...