Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

JUnit question: testing file generators

Thread view: 
Rhino - 12 Jan 2006 14:36 GMT
I have some classes that generate flat files and I'm trying to think of some
simple way to test to see that they are working correctly.

The classes in question read a ResourceBundle for data, then format that
data and write them out to a flat file with a PrintWriter. The only obvious
method of testing that comes to mind is to create a file that contains what
_should_ be in the generated file, then see if the file which is actually
generated is identical to the expected result. That would fall within the
basic design of JUnit. But I'm not sure how to compare two files within
JUnit even if that is the best way of testing the file generators.

Is there a way to compare two files within JUnit? If not, is there some
other way to test my file generating classes?

Signature

Rhino

Roedy Green - 12 Jan 2006 23:19 GMT
On Thu, 12 Jan 2006 09:36:33 -0500, "Rhino"
<no.offline.contact.please@nospam.com> wrote, quoted or indirectly
quoted someone who said :

>Is there a way to compare two files within JUnit? If not, is there some
>other way to test my file generating classes?

If the files are not too long, you read both in a byte array or string
and compare.  If they are very long, compute an Adler checksum on
each. Compare the lengths first. If they differ, you don't even need
to compute the checksum. If the checksums differ, you have a problem.

see http://mindprod.com/jgloss/adler.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Luc The Perverse - 13 Jan 2006 00:47 GMT
> On Thu, 12 Jan 2006 09:36:33 -0500, "Rhino"
> <no.offline.contact.please@nospam.com> wrote, quoted or indirectly
[quoted text clipped - 9 lines]
>
> see http://mindprod.com/jgloss/adler.html

Whoa!  I didn't know Java had native Adler support (in fact I was quite sure
it didn't)

I personally prefer SHA-256 for my checksumming needs.  If you are only
doing one file you won't notice a speed difference, and Adler is known to be
weak for small file types.  (I realize Roedy specific you should only do
this if you have a long file, but I wanted to make this warning.)

--
LTP

:)
Roedy Green - 13 Jan 2006 02:15 GMT
On Thu, 12 Jan 2006 17:47:27 -0700, "Luc The Perverse"
<sll_noSpamlicious_z_XXX_m@cc.usu.edu> wrote, quoted or indirectly
quoted someone who said :

>> see http://mindprod.com/jgloss/adler.html
>
>Whoa!  I didn't know Java had native Adler support (in fact I was quite sure
>it didn't)
Yes it does. I use it in my "untouch" utility that puts files back to
what they used to be if they have not really changed.
It is very quick.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Andrew McDonagh - 13 Jan 2006 01:23 GMT
> On Thu, 12 Jan 2006 09:36:33 -0500, "Rhino"
> <no.offline.contact.please@nospam.com> wrote, quoted or indirectly
[quoted text clipped - 9 lines]
>
> see http://mindprod.com/jgloss/adler.html

finding out if the files is different is only the beginning. JUnit tests
tell you why they are different as well.

Rhino, there's actually a few add-ons available for JUnit that do
exactly this.

Most provide a new assertfile method.

Junit-addons is probably the dominant player in this field...

http://junit-addons.sourceforge.net/

For its File asserts see...
http://junit-addons.sourceforge.net/junitx/framework/FileAssert.html

assertBinaryEquals(java.io.File expected, java.io.File actual)
//Asserts that two binary files are equal.

assertBinaryEquals(java.lang.String message, java.io.File expected,
java.io.File actual)
// Asserts that two binary files are equal.

assertEquals(java.io.File expected, java.io.File actual)
// Asserts that two files are equal.

assertEquals(java.lang.String message, java.io.File expected,
java.io.File actual)
//Asserts that two files are equal.

assertEquals(java.lang.String message, java.io.Reader expected,
java.io.Reader actual)
//Testing only Asserts that two readers are equal.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.