001: /**
002: * Copyright (c) 2003-2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package test.pdfbox.filter;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.IOException;
035: import java.io.File;
036: import java.io.FileInputStream;
037:
038: import java.util.Collection;
039: import java.util.Iterator;
040:
041: import junit.framework.Test;
042: import junit.framework.TestCase;
043: import junit.framework.TestSuite;
044:
045: import org.pdfbox.cos.COSDictionary;
046:
047: import org.pdfbox.filter.DCTFilter;
048: import org.pdfbox.filter.CCITTFaxDecodeFilter;
049: import org.pdfbox.filter.Filter;
050: import org.pdfbox.filter.FilterManager;
051: import org.pdfbox.filter.RunLengthDecodeFilter;
052:
053: /**
054: * This will test all of the filters in the PDFBox system.
055: *
056: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
057: * @version $Revision: 1.7 $
058: */
059: public class TestFilters extends TestCase {
060: private static final int BUFFER_SIZE = 2048;
061: private static final COSDictionary EMPTY_DICTIONARY = new COSDictionary();
062:
063: /**
064: * Constructor.
065: *
066: * @param name The name of the test to run.
067: */
068: public TestFilters(String name) {
069: super (name);
070: }
071:
072: /**
073: * This will get the suite of test that this class holds.
074: *
075: * @return All of the tests that this class holds.
076: */
077: public static Test suite() {
078: return new TestSuite(TestFilters.class);
079: }
080:
081: /**
082: * This will test all of the filters in the system.
083: *
084: * @throws IOException If there is an exception while encoding.
085: */
086: public void testFilters() throws IOException {
087: FilterManager manager = new FilterManager();
088: Collection filters = manager.getFilters();
089:
090: Iterator filterIter = filters.iterator();
091: while (filterIter.hasNext()) {
092: long start = System.currentTimeMillis();
093: Filter filter = (Filter) filterIter.next();
094: if (!(filter instanceof DCTFilter
095: || filter instanceof CCITTFaxDecodeFilter || filter instanceof RunLengthDecodeFilter)) {
096: checkFilter(new File("classes"), filter);
097: long stop = System.currentTimeMillis();
098: System.out.println("Time for filter "
099: + filter.getClass().getName() + "="
100: + (stop - start));
101: }
102: }
103: }
104:
105: /**
106: * This will check the filter.
107: *
108: * @param file The file or directory to test.
109: * @param filter The filter to check.
110: *
111: * @throws IOException If there is an exception while encoding.
112: */
113: private void checkFilter(File file, Filter filter)
114: throws IOException {
115: if (file.isDirectory()) {
116: File[] subFiles = file.listFiles();
117: for (int i = 0; i < subFiles.length; i++) {
118: checkFilter(subFiles[i], filter);
119: }
120: } else {
121: ByteArrayOutputStream output = new ByteArrayOutputStream();
122:
123: ByteArrayOutputStream encoded = new ByteArrayOutputStream();
124: ByteArrayOutputStream decoded = new ByteArrayOutputStream();
125: FileInputStream fin = new FileInputStream(file);
126: int amountRead = 0;
127: byte[] buffer = new byte[BUFFER_SIZE];
128: while ((amountRead = fin.read(buffer, 0, BUFFER_SIZE)) != -1) {
129: output.write(buffer, 0, amountRead);
130: }
131: fin.close();
132: byte[] original = output.toByteArray();
133: filter.encode(new ByteArrayInputStream(original), encoded,
134: EMPTY_DICTIONARY);
135: filter.decode(new ByteArrayInputStream(encoded
136: .toByteArray()), decoded, EMPTY_DICTIONARY);
137:
138: cmpArray(original, decoded.toByteArray(), filter, file);
139: }
140: }
141:
142: /**
143: * This will compare a couple of arrays and fail if they do not match.
144: *
145: * @param firstArray The first array.
146: * @param secondArray The second array.
147: * @param filter The filter that did the encoding.
148: * @param file The file that was encoded.
149: */
150: private void cmpArray(byte[] firstArray, byte[] secondArray,
151: Filter filter, File file) {
152: String fileMsg = filter.getClass().getName() + " "
153: + file.getAbsolutePath();
154: if (firstArray.length != secondArray.length) {
155: fail("The array lengths do not match for " + fileMsg
156: + ", firstArray length was: " + firstArray.length
157: + ", secondArray length was: " + secondArray.length);
158: }
159:
160: for (int i = 0; i < firstArray.length; i++) {
161: if (firstArray[i] != secondArray[i]) {
162: fail("Array data does not match " + fileMsg);
163: }
164: }
165: }
166: }
|