FileDocCategorySizeDatePackage
TestPictures.javaAPI DocApache Poi 3.0.15290Thu May 31 18:45:56 BST 2007org.apache.poi.hwpf.usermodel

TestPictures

public class TestPictures extends TestCase
Test the picture handling
author
Nick Burch (nick at torchbox dot com)

Fields Summary
private String
dirname
Constructors Summary
Methods Summary
private byte[]loadImage(java.lang.String filename)

    	ByteArrayOutputStream b = new ByteArrayOutputStream();
    	FileInputStream fis = new FileInputStream(dirname + "/" + filename);
    	
    	byte[] buf = new byte[4096];
    	int read = 0;
    	while( (read = fis.read(buf)) > -1 ) {
    		b.write(buf, 0, read);
    	}
    	return b.toByteArray();
    
protected voidsetUp()

	
         
    
public voidtestDifferentImages()
pngs and jpegs

    	HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/testPictures.doc"));
    	List pics = doc.getPicturesTable().getAllPictures();
    	
    	assertNotNull(pics);
    	assertEquals(7, pics.size());
    	for(int i=0; i<pics.size(); i++) {
    		Object p = pics.get(i);
    		assertTrue(p instanceof Picture);
    		
    		Picture pic = (Picture)p;
    		assertNotNull(pic.suggestFileExtension());
    		assertNotNull(pic.suggestFullFileName());
    	}
    	
    	assertEquals("jpg", ((Picture)pics.get(0)).suggestFileExtension());
    	assertEquals("jpg", ((Picture)pics.get(1)).suggestFileExtension());
    	assertEquals("png", ((Picture)pics.get(3)).suggestFileExtension());
    	assertEquals("png", ((Picture)pics.get(4)).suggestFileExtension());
    	assertEquals("wmf", ((Picture)pics.get(5)).suggestFileExtension());
    	assertEquals("jpg", ((Picture)pics.get(6)).suggestFileExtension());
    
public voidtestEmfComplexImage()
emf image, with a crazy offset

    	HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/emf_2003_image.doc"));
    	List pics = doc.getPicturesTable().getAllPictures();
    	
    	assertNotNull(pics);
    	assertEquals(1, pics.size());

    	Picture pic = (Picture)pics.get(0);
    	assertNotNull(pic.suggestFileExtension());
    	assertNotNull(pic.suggestFullFileName());
    	
    	// This one's tricky
    	// TODO: Fix once we've sorted bug #41898
    	assertNotNull(pic.getContent());
    	assertNotNull(pic.getRawContent());
    	
    	// These are probably some sort of offset, need to figure them out
    	assertEquals(4, pic.getSize());
    	assertEquals(0x80000000l, LittleEndian.getUInt(pic.getContent()));
    	assertEquals(0x80000000l, LittleEndian.getUInt(pic.getRawContent()));
    
public voidtestEmfImage()
emf image, nice and simple

    	HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/vector_image.doc"));
    	List pics = doc.getPicturesTable().getAllPictures();
    	
    	assertNotNull(pics);
    	assertEquals(1, pics.size());
    	
    	Picture pic = (Picture)pics.get(0);
    	assertNotNull(pic.suggestFileExtension());
    	assertNotNull(pic.suggestFullFileName());
    	assertTrue(pic.getSize() > 128);
    	
    	// Check right contents
    	byte[] emf = loadImage("vector_image.emf");
    	byte[] pemf = pic.getContent();
    	assertEquals(emf.length, pemf.length);
    	for(int i=0; i<emf.length; i++) {
    		assertEquals(emf[i], pemf[i]);
    	}
    
public voidtestTwoImages()
two jpegs

    	HWPFDocument doc = new HWPFDocument(new FileInputStream(dirname + "/two_images.doc"));
    	List pics = doc.getPicturesTable().getAllPictures();
    	
    	assertNotNull(pics);
    	assertEquals(pics.size(), 2);
    	for(int i=0; i<pics.size(); i++) {
    		Object p = pics.get(i);
    		assertTrue(p instanceof Picture);
    		
    		Picture pic = (Picture)p;
    		assertNotNull(pic.suggestFileExtension());
    		assertNotNull(pic.suggestFullFileName());
    	}
    	
    	Picture picA = (Picture)pics.get(0);
    	Picture picB = (Picture)pics.get(1);
    	assertEquals("jpg", picA.suggestFileExtension());
    	assertEquals("jpg", picA.suggestFileExtension());