/*
* Copyright (C) 2010 Dedicon <http://dedicon.nl>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
Linking altText statically or dynamically with other modules
is making a combined work based on altText. Thus, the terms and
conditions of the GNU General Public License cover
the whole combination. In addition, as a special exception,
the copyright holders of altText give you permission to
combine altText program with free software programs or libraries
that are released under the GNU LGPL or the Common Public License version 1.0.
You may copy and distribute such a system following the terms
of the GNU GPL for altText and the licenses of the other code
concerned, provided that you include the source code of that other
code when and as the GNU GPL requires distribution of source code.
Note that people who make modified versions of altText are not obligated to
grant this special exception for their modified versions; it is their
choice whether to do so. The GNU General Public License gives permission
to release a modified version without this exception; this exception
also makes it possible to release a modified version which carries
forward this exception.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author : Javier Asensio Cubero capitan{.}cambio{@}gmail{.}com
*/
package nl.dedicon.converter.tests.odt.processors;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import junit.framework.Assert;
import nl.dedicon.converter.core.Processor;
import nl.dedicon.converter.core.io.InputFile;
import nl.dedicon.converter.core.util.ConfigurationManager;
import nl.dedicon.converter.odt.io.OdtPackage;
import nl.dedicon.converter.odt.processors.ImgConverterProcessor;
import nl.dedicon.converter.odt.processors.OdtPreprocessor;
import org.junit.BeforeClass;
import org.junit.Test;
public class ImgConverterProcessorTest extends ImgConverterProcessor {
static OdtPackage mOPack;
private static String CONF_PATH = "." + File.separator + "tests"
+ File.separator + "configFiles" + File.separator + "okConf.xml";
private static final String INPUT_FILE = "." + File.separator + "tests"
+ File.separator + "documents" + File.separator + "odtImgConverter"
+ File.separator + "test.odt";
@BeforeClass
public static void setUp() throws Exception {
ConfigurationManager.getInstance().loadConfiguration(CONF_PATH);
InputFile inputFile = new InputFile(INPUT_FILE);
mOPack = new OdtPackage(inputFile);
Processor proc = new OdtPreprocessor();
proc.perform(mOPack);
}
@Test
public void testGetUnsupportedImgs() {
HashMap<String, Boolean> res = new HashMap<String, Boolean>();
res.put("Pictures/2000005700009F7900004648BF5B85FE.wmf", false);
res.put("Pictures/2000012700003F410000128071205ACC.wmf", false);
ImgConverterProcessor imgConverter = new ImgConverterProcessor();
Collection<String> mUnsupportedImgs = this.getUnsupportedImgs(mOPack);
Assert.assertTrue(mUnsupportedImgs.size() == 2);
for (String key : mUnsupportedImgs) {
res.put(key, true);
}
for (String key : res.keySet()) {
Assert.assertTrue(res.get(key));
}
}
@Test
public void testInputFile() throws Exception {
ImgConverterProcessor imgConverter = new ImgConverterProcessor();
Collection<String> mUnsupportedImgs = this.getUnsupportedImgs(mOPack);
ArrayList<String> ar = new ArrayList<String>(mUnsupportedImgs);
String originalFile = ar.get(0);
File f = this.getInputFile(originalFile, mOPack);
String resultSum = this.getMD5Checksum(new FileInputStream(f));
String originalSum = this.getMD5Checksum(mOPack.getZipFile().getInputStream(mOPack.getZipFile().getEntry(originalFile)));
Assert.assertEquals(originalSum, resultSum);
}
@Test
public void testConvertToPng() throws Exception {
ImgConverterProcessor imgConverter = new ImgConverterProcessor();
Collection<String> mUnsupportedImgs = this.getUnsupportedImgs(mOPack);
ArrayList<String> ar = new ArrayList<String>(mUnsupportedImgs);
String originalFile = ar.get(0);
File f = this.getInputFile(originalFile, mOPack);
String newEntry=this.convertToPng(originalFile, mOPack);
Assert.assertTrue(mOPack.getZipFile().getEntry(newEntry)!=null);
}
@Test
public void testFull() throws Exception {
ImgConverterProcessor imgConverter = new ImgConverterProcessor();
imgConverter.perform(mOPack);
Collection<String> mUnsupportedImgs = this.getUnsupportedImgs(mOPack);
Assert.assertEquals(0, mUnsupportedImgs.size());
ZipFile zf = mOPack.getZipFile();
Enumeration<? extends ZipEntry> e=zf.entries();
while(e.hasMoreElements()){
ZipEntry entry = e.nextElement();
Assert.assertFalse(entry.getName().contains("wmf")&&!entry.getName().contains("png"));
}
}
public static byte[] createChecksum(InputStream fis) throws Exception {
byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();
return complete.digest();
}
// see this How-to for a faster way to convert
// a byte array to a HEX string
public static String getMD5Checksum(InputStream fis) throws Exception {
byte[] b = createChecksum( fis);
String result = "";
for (int i = 0; i < b.length; i++) {
result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
}
return result;
}
}
|