You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by eitland <er...@gmail.com> on 2012/11/03 15:31:46 UTC

Use a file as input to unit tests

I think I have seen an elegant way to use a file as input for a unit test in
apache camel but my google skills are failing me.

What I want is instead of:

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
    <snip>...long real life xml that quickly fills up test files.</snip>";
template.sendBody("direct:create", xml);

What I think I have seen is something like:

template.sendBody("direct:create",
someCamelMetod("/src/data/someXmlFile.xml"));

Anybody knows where/if this is documented? 




--
View this message in context: http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722069.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Use a file as input to unit tests

Posted by Claus Ibsen <cl...@gmail.com>.
On Sat, Nov 3, 2012 at 5:45 PM, Babak Vahdat
<ba...@swissonline.ch> wrote:
> Hi
>
> Do you mean:
>
> template.sendBody("direct:create",
> context.getTypeConverter().convertTo(String.class, new
> File("src/data/someXmlFile.xml")));
>
> Babak
>

You may also just send the java.io.File as body. As often the type
conversion will kick in and load the content using a FileInputStream
out of the box, when needed. Though you can also as Babak has shown
cover the file to a String beforehand.


>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722069p5722079.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
FuseSource is now part of Red Hat
Email: cibsen@redhat.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen

Re: Use a file as input to unit tests

Posted by Babak Vahdat <ba...@swissonline.ch>.

Am 03.11.12 17:45 schrieb "Babak Vahdat" unter
<ba...@swissonline.ch>:

>Hi
>
>Do you mean:
>
>template.sendBody("direct:create",
>context.getTypeConverter().convertTo(String.class, new
>File("src/data/someXmlFile.xml")));
>
>Babak
>

Ah just spotted that Christian has already suggested the same approach so
please ignore my answer.

Babak

>
>
>--
>View this message in context:
>http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722
>069p5722079.html
>Sent from the Camel - Users mailing list archive at Nabble.com.



Re: Use a file as input to unit tests

Posted by Babak Vahdat <ba...@swissonline.ch>.
Hi

Do you mean:

template.sendBody("direct:create",
context.getTypeConverter().convertTo(String.class, new
File("src/data/someXmlFile.xml"))); 

Babak



--
View this message in context: http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722069p5722079.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Use a file as input to unit tests

Posted by eitland <er...@gmail.com>.
Hi Christian and thanks!

I ended up creating a simple helper function: private void readFile(String
fileName) throws ... 

Still I think I have seen an even more elegant solution somewhere. 

The question is also cross posted to StackOverflow (
http://stackoverflow.com/questions/13209889/use-a-file-as-input-to-unit-tests
<http://stackoverflow.com/questions/13209889/use-a-file-as-input-to-unit-tests> 
) and I'm be happy to hand out some upvotes : )

Regards,
Erik



--
View this message in context: http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722069p5722078.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Use a file as input to unit tests

Posted by Christian Müller <ch...@gmail.com>.
May be context.getTypeConverter().convertTo(String.class,
this.getClass().getResourceAsStream("/src/data/someXmlFile.xml"));

Apache commons-io provides also some useful methods for this [1].

[1]
http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?view=markup

Best,
Christian

On Sat, Nov 3, 2012 at 3:31 PM, eitland <er...@gmail.com> wrote:

>
> I think I have seen an elegant way to use a file as input for a unit test
> in
> apache camel but my google skills are failing me.
>
> What I want is instead of:
>
> String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
>     <snip>...long real life xml that quickly fills up test files.</snip>";
> template.sendBody("direct:create", xml);
>
> What I think I have seen is something like:
>
> template.sendBody("direct:create",
> someCamelMetod("/src/data/someXmlFile.xml"));
>
> Anybody knows where/if this is documented?
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/Use-a-file-as-input-to-unit-tests-tp5722069.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--