FileDocCategorySizeDatePackage
TestHWPFPictures.javaAPI DocApache Poi 3.0.14556Sun Mar 11 12:59:30 GMT 2007org.apache.poi.hwpf

TestHWPFPictures

public class TestHWPFPictures extends TestCase
Test picture support in HWPF
author
nick

Fields Summary
private String
docAFile
private String
docBFile
private String
docCFile
private String
imgAFile
private String
imgBFile
private String
imgCFile
Constructors Summary
Methods Summary
private voidassertBytesSame(byte[] a, byte[] b)

		assertEquals(a.length, b.length);
		for(int i=0; i<a.length; i++) {
			assertEquals(a[i],b[i]);
		}
	
private byte[]readFile(java.lang.String file)

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		FileInputStream fis = new FileInputStream(file);
		byte[] buffer = new byte[1024];
		
		int read = 0;
		while(read > -1) {
			read = fis.read(buffer);
			if(read > 0) {
				baos.write(buffer,0,read);
			}
		}
		
		return baos.toByteArray();
	
protected voidsetUp()

		String dirname = System.getProperty("HWPF.testdata.path");
		
		docAFile = dirname + "/testPictures.doc";
		docBFile = dirname + "/two_images.doc";
		docCFile = dirname + "/vector_image.doc";
		
		imgAFile = dirname + "/simple_image.jpg";
		imgBFile = dirname + "/simple_image.png";
		imgCFile = dirname + "/vector_image.emf";
	
public voidtestCompressedImageData()
Test that compressed image data is correctly returned.

		HWPFDocument docC = new HWPFDocument(new FileInputStream(docCFile));
		PicturesTable picC = docC.getPicturesTable();
		List picturesC = picC.getAllPictures();
		
		assertEquals(1, picturesC.size());
		
		Picture pic = (Picture)picturesC.get(0);
		assertNotNull(pic);
		
		// Check the same
		byte[] picBytes = readFile(imgCFile);
		
		assertEquals(picBytes.length, pic.getContent().length);
		assertBytesSame(picBytes, pic.getContent());
	
public voidtestImageCount()
Test that we have the right numbers of images in each file

		HWPFDocument docA = new HWPFDocument(new FileInputStream(docAFile));
		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));
		
		assertNotNull(docA.getPicturesTable());
		assertNotNull(docB.getPicturesTable());
		
		PicturesTable picA = docA.getPicturesTable();
		PicturesTable picB = docB.getPicturesTable();
		
		List picturesA = picA.getAllPictures();
		List picturesB = picB.getAllPictures();
		
		assertEquals(7, picturesA.size());
		assertEquals(2, picturesB.size());
	
public voidtestImageData()
Test that we have the right images in at least one file

		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));
		PicturesTable picB = docB.getPicturesTable();
		List picturesB = picB.getAllPictures();
		
		assertEquals(2, picturesB.size());
		
		Picture pic1 = (Picture)picturesB.get(0);
		Picture pic2 = (Picture)picturesB.get(1);
		
		assertNotNull(pic1);
		assertNotNull(pic2);
		
		// Check the same
		byte[] pic1B = readFile(imgAFile);
		byte[] pic2B = readFile(imgBFile);
		
		assertEquals(pic1B.length, pic1.getContent().length);
		assertEquals(pic2B.length, pic2.getContent().length);

		assertBytesSame(pic1B, pic1.getContent());
		assertBytesSame(pic2B, pic2.getContent());
	
public voidtestOpen()
Test just opening the files

		HWPFDocument docA = new HWPFDocument(new FileInputStream(docAFile));
		HWPFDocument docB = new HWPFDocument(new FileInputStream(docBFile));