Quantcast

Use a file as input to unit tests

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Use a file as input to unit tests

eitland

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?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a file as input to unit tests

Christian Mueller
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 <[hidden email]> 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.
>



--
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a file as input to unit tests

eitland
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) and I'm be happy to hand out some upvotes : )

Regards,
Erik
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a file as input to unit tests

Babak Vahdat
In reply to this post by eitland
Hi

Do you mean:

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

Babak
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a file as input to unit tests

Babak Vahdat


Am 03.11.12 17:45 schrieb "Babak Vahdat" unter
<[hidden email]>:

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


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Use a file as input to unit tests

Claus Ibsen-2
In reply to this post by Babak Vahdat
On Sat, Nov 3, 2012 at 5:45 PM, Babak Vahdat
<[hidden email]> 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: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
Loading...