@@ -22,49 +22,145 @@ This file is part of the iText (R) project.
22
22
*/
23
23
package com .itextpdf .io .image ;
24
24
25
+ import com .itextpdf .io .util .StreamUtil ;
25
26
import com .itextpdf .io .util .UrlUtil ;
26
27
import com .itextpdf .test .ExtendedITextTest ;
27
28
import com .itextpdf .test .annotations .type .UnitTest ;
28
29
30
+ import java .io .FileInputStream ;
31
+ import java .io .FileNotFoundException ;
32
+ import java .io .IOException ;
33
+ import java .io .InputStream ;
29
34
import java .net .MalformedURLException ;
30
35
import java .net .URL ;
31
36
import org .junit .Assert ;
37
+ import org .junit .Rule ;
32
38
import org .junit .Test ;
33
39
import org .junit .experimental .categories .Category ;
40
+ import org .junit .rules .ExpectedException ;
34
41
35
42
@ Category (UnitTest .class )
36
43
public class ImageTypeDetectorTest extends ExtendedITextTest {
37
44
38
45
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/io/image/ImageTypeDetectorTest/" ;
39
46
private static final String IMAGE_NAME = "image" ;
40
47
48
+ @ Rule
49
+ public ExpectedException junitExpectedException = ExpectedException .none ();
50
+
51
+ @ Test
52
+ public void testUrlUnknown () throws MalformedURLException {
53
+ testURL (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".txt" ), ImageType .NONE );
54
+ }
55
+
56
+ @ Test
57
+ public void testUrlGif () throws MalformedURLException {
58
+ testURL (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".gif" ), ImageType .GIF );
59
+ }
60
+
61
+ @ Test
62
+ public void testUrlJpeg () throws MalformedURLException {
63
+ testURL (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".jpg" ), ImageType .JPEG );
64
+ }
65
+
66
+ @ Test
67
+ public void testUrlTiff () throws MalformedURLException {
68
+ testURL (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".tiff" ), ImageType .TIFF );
69
+ }
70
+
71
+ @ Test
72
+ public void testUrlWmf () throws MalformedURLException {
73
+ testURL (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".wmf" ), ImageType .WMF );
74
+ }
75
+
76
+ @ Test
77
+ public void testNullUrl () throws MalformedURLException {
78
+ junitExpectedException .expect (com .itextpdf .io .IOException .class );
79
+
80
+ ImageTypeDetector .detectImageType (UrlUtil .toURL ("not existing path" ));
81
+
82
+ Assert .fail ("This line is not expected to be triggered: "
83
+ + "an exception should have been thrown" );
84
+ }
85
+
86
+ @ Test
87
+ public void testStreamUnknown () throws FileNotFoundException {
88
+ testStream (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".txt" ), ImageType .NONE );
89
+ }
90
+
91
+ @ Test
92
+ public void testStreamGif () throws FileNotFoundException {
93
+ testStream (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".gif" ), ImageType .GIF );
94
+ }
95
+
96
+ @ Test
97
+ public void testStreamJpeg () throws FileNotFoundException {
98
+ testStream (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".jpg" ), ImageType .JPEG );
99
+ }
100
+
101
+ @ Test
102
+ public void testStreamTiff () throws FileNotFoundException {
103
+ testStream (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".tiff" ), ImageType .TIFF );
104
+ }
105
+
41
106
@ Test
42
- public void testUnknown () throws MalformedURLException {
43
- test ( UrlUtil . toURL (SOURCE_FOLDER + IMAGE_NAME + ".txt " ), ImageType .NONE );
107
+ public void testStreamWmf () throws FileNotFoundException {
108
+ testStream ( new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".wmf " ), ImageType .WMF );
44
109
}
45
110
46
111
@ Test
47
- public void testGif () throws MalformedURLException {
48
- test (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".gif" ), ImageType .GIF );
112
+ public void testStreamClosed () throws IOException {
113
+ // A common exception is expected instead of com.itextpdf.io.IOException, because in .NET
114
+ // the thrown exception is different
115
+ junitExpectedException .expect (Exception .class );
116
+
117
+ InputStream stream = new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".wmf" );
118
+ stream .close ();
119
+ ImageTypeDetector .detectImageType (stream );
120
+
121
+ Assert .fail ("This line is not expected to be triggered: "
122
+ + "an exception should have been thrown" );
123
+ }
124
+
125
+ @ Test
126
+ public void testBytesUnknown () throws IOException {
127
+ testBytes (StreamUtil .inputStreamToArray (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".txt" )),
128
+ ImageType .NONE );
49
129
}
50
130
51
131
@ Test
52
- public void testJpeg () throws MalformedURLException {
53
- test (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".jpg" ), ImageType .JPEG );
132
+ public void testBytesGif () throws IOException {
133
+ testBytes (StreamUtil .inputStreamToArray (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".gif" )),
134
+ ImageType .GIF );
54
135
}
55
136
56
137
@ Test
57
- public void testTiff () throws MalformedURLException {
58
- test (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".tiff" ), ImageType .TIFF );
138
+ public void testBytesJpeg () throws IOException {
139
+ testBytes (StreamUtil .inputStreamToArray (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".jpg" )),
140
+ ImageType .JPEG );
59
141
}
60
142
61
143
@ Test
62
- public void testWmf () throws MalformedURLException {
63
- test (UrlUtil .toURL (SOURCE_FOLDER + IMAGE_NAME + ".wmf" ), ImageType .WMF );
144
+ public void testBytesTiff () throws IOException {
145
+ testBytes (StreamUtil .inputStreamToArray (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".tiff" )),
146
+ ImageType .TIFF );
64
147
}
65
148
66
- private void test (URL location , ImageType expectedType ) {
149
+ @ Test
150
+ public void testBytesWmf () throws IOException {
151
+ testBytes (StreamUtil .inputStreamToArray (new FileInputStream (SOURCE_FOLDER + IMAGE_NAME + ".wmf" )),
152
+ ImageType .WMF );
153
+ }
154
+
155
+ private static void testURL (URL location , ImageType expectedType ) {
67
156
Assert .assertEquals (expectedType , ImageTypeDetector .detectImageType (location ));
68
157
}
69
158
159
+ private static void testStream (InputStream stream , ImageType expectedType ) {
160
+ Assert .assertEquals (expectedType , ImageTypeDetector .detectImageType (stream ));
161
+ }
162
+
163
+ private static void testBytes (byte [] bytes , ImageType expectedType ) {
164
+ Assert .assertEquals (expectedType , ImageTypeDetector .detectImageType (bytes ));
165
+ }
70
166
}
0 commit comments