KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > pdfbox > filter > TestFilters


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package test.pdfbox.filter;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.ByteArrayOutputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41
42 import junit.framework.Test;
43 import junit.framework.TestCase;
44 import junit.framework.TestSuite;
45
46 import org.pdfbox.cos.COSDictionary;
47
48 import org.pdfbox.filter.DCTFilter;
49 import org.pdfbox.filter.CCITTFaxDecodeFilter;
50 import org.pdfbox.filter.Filter;
51 import org.pdfbox.filter.FilterManager;
52 import org.pdfbox.filter.RunLengthDecodeFilter;
53
54 /**
55  * This will test all of the filters in the PDFBox system.
56  *
57  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
58  * @version $Revision: 1.7 $
59  */

60 public class TestFilters extends TestCase
61 {
62     private static final int BUFFER_SIZE = 2048;
63     private static final COSDictionary EMPTY_DICTIONARY = new COSDictionary();
64
65     /**
66      * Constructor.
67      *
68      * @param name The name of the test to run.
69      */

70     public TestFilters( String JavaDoc name )
71     {
72         super( name );
73     }
74
75     /**
76      * This will get the suite of test that this class holds.
77      *
78      * @return All of the tests that this class holds.
79      */

80     public static Test suite()
81     {
82         return new TestSuite( TestFilters.class );
83     }
84
85     /**
86      * This will test all of the filters in the system.
87      *
88      * @throws IOException If there is an exception while encoding.
89      */

90     public void testFilters() throws IOException JavaDoc
91     {
92         FilterManager manager = new FilterManager();
93         Collection JavaDoc filters = manager.getFilters();
94
95         Iterator JavaDoc filterIter = filters.iterator();
96         while( filterIter.hasNext() )
97         {
98             long start = System.currentTimeMillis();
99             Filter filter = (Filter)filterIter.next();
100             if( !(filter instanceof DCTFilter ||
101                   filter instanceof CCITTFaxDecodeFilter ||
102                   filter instanceof RunLengthDecodeFilter))
103             {
104                 checkFilter( new File JavaDoc( "classes" ), filter );
105                 long stop = System.currentTimeMillis();
106                 System.out.println( "Time for filter " + filter.getClass().getName() + "=" + (stop-start) );
107             }
108         }
109     }
110
111     /**
112      * This will check the filter.
113      *
114      * @param file The file or directory to test.
115      * @param filter The filter to check.
116      *
117      * @throws IOException If there is an exception while encoding.
118      */

119     private void checkFilter( File JavaDoc file, Filter filter ) throws IOException JavaDoc
120     {
121         if( file.isDirectory() )
122         {
123             File JavaDoc[] subFiles = file.listFiles();
124             for( int i=0; i<subFiles.length; i++ )
125             {
126                 checkFilter( subFiles[i], filter );
127             }
128         }
129         else
130         {
131             ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
132
133             ByteArrayOutputStream JavaDoc encoded = new ByteArrayOutputStream JavaDoc();
134             ByteArrayOutputStream JavaDoc decoded = new ByteArrayOutputStream JavaDoc();
135             FileInputStream JavaDoc fin = new FileInputStream JavaDoc( file );
136             int amountRead = 0;
137             byte[] buffer = new byte[ BUFFER_SIZE ];
138             while( (amountRead = fin.read( buffer, 0, BUFFER_SIZE )) != -1 )
139             {
140                 output.write( buffer, 0, amountRead );
141             }
142             fin.close();
143             byte[] original = output.toByteArray();
144             filter.encode( new ByteArrayInputStream JavaDoc( original ), encoded, EMPTY_DICTIONARY );
145             filter.decode( new ByteArrayInputStream JavaDoc( encoded.toByteArray() ), decoded, EMPTY_DICTIONARY );
146
147             cmpArray( original, decoded.toByteArray(), filter, file );
148         }
149     }
150
151     /**
152      * This will compare a couple of arrays and fail if they do not match.
153      *
154      * @param firstArray The first array.
155      * @param secondArray The second array.
156      * @param filter The filter that did the encoding.
157      * @param file The file that was encoded.
158      */

159     private void cmpArray( byte[] firstArray, byte[] secondArray, Filter filter, File JavaDoc file )
160     {
161         String JavaDoc fileMsg = filter.getClass().getName() + " " + file.getAbsolutePath();
162         if( firstArray.length != secondArray.length )
163         {
164             fail( "The array lengths do not match for " + fileMsg +
165                   ", firstArray length was: " + firstArray.length +
166                   ", secondArray length was: " + secondArray.length);
167         }
168
169         for( int i=0; i<firstArray.length; i++ )
170         {
171             if( firstArray[i] != secondArray[i] )
172             {
173                 fail( "Array data does not match " + fileMsg );
174             }
175         }
176     }
177 }
Popular Tags