java - Testing image files with JUnit -


i'm looking direction on how test dynamically generated image expected image junit. bit of background:

i have code generates thumbnail images java.awt.image.bufferedimage's , stores bufferedimages java.io.file object.

i wrote simple unit test check few key items in order gain confidence generated image correct:

@test public void testgeneratedefaultthumbnail() {     file file = // somegeneratedfile.jpg      assertnotnull(file);     assertequals(10211l, file.length()); } 

basically, i'm confirming file size expect.

this works fine locally on machine (mac osx), our tests run on our build server (ubuntu), 1 of tests failing following:

java.lang.assertionerror: expected:<10211> was:<10158>   @ org.junit.assert.fail(assert.java:88)   @ org.junit.assert.failnotequals(assert.java:743)   @ org.junit.assert.assertequals(assert.java:118)   @ org.junit.assert.assertequals(assert.java:555)   @ org.junit.assert.assertequals(assert.java:542)   @ c.v.t.thumbnailtest.testgeneratedefaultthumbnail(thumbnailtest.java:53) 

three part question:

  • why may file size different image across os's?
  • is there standard approach unit testing image generation?
  • should change approach compare 2 file objects (where pre-generated file exists resource in project)?

thanks!

i guess there's difference in metadata being written out? image data same. if care image data , not metadata image file can pull out pixel data byte array.

this idea anyway since comparing entire file problematic since metadata may have timestamps of file generation break tests.

if have image bufferedimage can on expected , actual variables.

//this gets actual raster data byte array byte[] bytearray = ((databufferbyte) bufferedimage.getdata().getdatabuffer()).getdata(); 

then compare byte arrays equality

assertarrayequal(expectedarray, actualarray); 

if don't have data bufferedimage yet it's easy file

bufferedimage originalimage =                            imageio.read(new file("path/to/my.jpg")); 

or inputstream

  try{   bufferedimage originalimage =                            imageio.read(inputstream));   }finally{       //close inputstream       inputstream.close();   } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -