package no.dusken.aranea.util;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.service.StoreImageService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
public class TestImageUtils {
private ImageUtils imageUtils;
private StoreImageService storeImageService;
@Before
public void setUp(){
imageUtils = new ImageUtils();
imageUtils.setImageDirectory(new File("/tmp"));
storeImageService = mock(StoreImageService.class);
imageUtils.setStoreImageService(storeImageService);
imageUtils.setPDF_HEIGHT(800);
}
@Test
public void testResize() throws Exception {
// get the file locally
File originalFile = new File("src/test/resources/testImage.jpg");
assertTrue("Could not read test image", originalFile.exists() && originalFile.canRead());
File newFile = imageUtils.resizeImage(originalFile, 20, 30);
// read the new file with
BufferedImage newImage = ImageIO.read(newFile);
assertEquals("Width is wrong", 20, newImage.getWidth());
assertEquals("Height is wrong", 20, newImage.getHeight());
newFile = imageUtils.resizeImage(originalFile, 30, 25);
// read the new file with
newImage = ImageIO.read(newFile);
assertEquals("Width is wrong", 25, newImage.getWidth());
assertEquals("Height is wrong", 25, newImage.getHeight());
newFile.delete();
}
@Test
public void testMakePdfThumbnail() throws IOException {
File originalFile = new File("src/test/resources/testPdf.pdf");
assertTrue("Could not read test pdf", originalFile.exists() && originalFile.canRead());
File ifile = new File("/tmp/tmp/testPdf.pdf.png");
Image mockimage = mock(Image.class);
Mockito.when(mockimage.getWidth()).thenReturn(618);
Mockito.when(mockimage.getHeight()).thenReturn(800);
Mockito.when(mockimage.getUrl()).thenReturn("/tmp/testPdf.pdf.png");
Mockito.when(storeImageService.createImage(ifile)).thenReturn(mockimage);
Image image = imageUtils.makeThumbnail(originalFile);
assertEquals("Image object has wrong height", 800, image.getHeight().intValue());
assertEquals("Image object has wrong width", 618, image.getWidth().intValue());
File file = imageUtils.getImageFile(image);
BufferedImage newImage = ImageIO.read(file);
assertEquals("Image file has wrong height", 800, newImage.getHeight());
assertEquals("Image file has wrong width", 618, newImage.getWidth());
file.delete();
ifile.delete();
}
}