14
14
15
15
import static org .junit .Assert .assertEquals ;
16
16
import static org .junit .Assert .assertNull ;
17
+ import static org .junit .Assert .assertTrue ;
17
18
import static org .junit .Assert .fail ;
18
19
19
20
import java .io .ByteArrayInputStream ;
25
26
import java .io .OutputStream ;
26
27
import java .util .jar .JarInputStream ;
27
28
import java .util .jar .JarOutputStream ;
28
- import java .util .jar .Pack200 ;
29
29
import java .util .zip .ZipEntry ;
30
30
import java .util .zip .ZipInputStream ;
31
31
import java .util .zip .ZipOutputStream ;
32
32
33
33
import org .jacoco .core .test .TargetLoader ;
34
34
import org .junit .Test ;
35
+ import org .junit .internal .AssumptionViolatedException ;
35
36
36
37
/**
37
38
* Unit tests for {@link Pack200Streams}.
38
39
*/
39
40
public class Pack200StreamsTest {
40
41
41
42
@ Test
42
- public void testPack () throws IOException {
43
+ public void pack_should_pack () throws Exception {
44
+ try {
45
+ Class .forName ("java.util.jar.Pack200" );
46
+ } catch (ClassNotFoundException e ) {
47
+ throw new AssumptionViolatedException (
48
+ "this test requires JDK with Pack200" );
49
+ }
50
+
43
51
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream ();
44
52
ZipOutputStream zipout = new ZipOutputStream (jarbuffer );
45
53
zipout .putNextEntry (new ZipEntry ("Test.class" ));
@@ -51,9 +59,13 @@ public void testPack() throws IOException {
51
59
new NoCloseOutputStream (pack200buffer ));
52
60
53
61
jarbuffer .reset ();
54
- Pack200 .newUnpacker ().unpack (
55
- new ByteArrayInputStream (pack200buffer .toByteArray ()),
56
- new JarOutputStream (jarbuffer ));
62
+ final Object unpacker = Class .forName ("java.util.jar.Pack200" )
63
+ .getMethod ("newUnpacker" ).invoke (null );
64
+ Class .forName ("java.util.jar.Pack200$Unpacker" )
65
+ .getMethod ("unpack" , InputStream .class , JarOutputStream .class )
66
+ .invoke (unpacker ,
67
+ new ByteArrayInputStream (pack200buffer .toByteArray ()),
68
+ new JarOutputStream (jarbuffer ));
57
69
58
70
ZipInputStream zipin = new ZipInputStream (
59
71
new ByteArrayInputStream (jarbuffer .toByteArray ()));
@@ -62,16 +74,64 @@ public void testPack() throws IOException {
62
74
}
63
75
64
76
@ Test
65
- public void testUnpack () throws IOException {
77
+ public void pack_should_throw_IOException_when_can_not_write_to_OutputStream () {
78
+ try {
79
+ Class .forName ("java.util.jar.Pack200" );
80
+ } catch (ClassNotFoundException e ) {
81
+ throw new AssumptionViolatedException (
82
+ "this test requires JDK with Pack200" );
83
+ }
84
+
85
+ final OutputStream outputStream = new BrokenOutputStream ();
86
+ try {
87
+ Pack200Streams .pack (new byte [0 ], outputStream );
88
+ fail ("expected exception" );
89
+ } catch (IOException e ) {
90
+ assertTrue (e .getCause () instanceof IOException );
91
+ assertEquals ("fake broken output stream" ,
92
+ e .getCause ().getMessage ());
93
+ }
94
+ }
95
+
96
+ @ Test
97
+ public void pack_should_throw_IOException_when_Pack200_not_available_in_JDK () {
98
+ try {
99
+ Class .forName ("java.util.jar.Pack200" );
100
+ throw new AssumptionViolatedException (
101
+ "this test requires JDK without Pack200" );
102
+ } catch (ClassNotFoundException ignore ) {
103
+ }
104
+
105
+ try {
106
+ Pack200Streams .pack (new byte [0 ], new ByteArrayOutputStream ());
107
+ fail ("expected exception" );
108
+ } catch (IOException e ) {
109
+ assertNull (e .getMessage ());
110
+ assertTrue (e .getCause () instanceof ClassNotFoundException );
111
+ }
112
+ }
113
+
114
+ @ Test
115
+ public void unpack_should_unpack () throws Exception {
116
+ try {
117
+ Class .forName ("java.util.jar.Pack200" );
118
+ } catch (ClassNotFoundException e ) {
119
+ throw new AssumptionViolatedException (
120
+ "this test requires JDK with Pack200" );
121
+ }
122
+
66
123
ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream ();
67
124
ZipOutputStream zipout = new ZipOutputStream (jarbuffer );
68
125
zipout .putNextEntry (new ZipEntry ("Test.class" ));
69
126
zipout .write (TargetLoader .getClassDataAsBytes (getClass ()));
70
127
zipout .finish ();
71
128
72
129
ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream ();
73
- Pack200 .newPacker ()
74
- .pack (new JarInputStream (
130
+ final Object packer = Class .forName ("java.util.jar.Pack200" )
131
+ .getMethod ("newPacker" ).invoke (null );
132
+ Class .forName ("java.util.jar.Pack200$Packer" )
133
+ .getMethod ("pack" , JarInputStream .class , OutputStream .class )
134
+ .invoke (packer , new JarInputStream (
75
135
new ByteArrayInputStream (jarbuffer .toByteArray ())),
76
136
pack200buffer );
77
137
@@ -83,6 +143,43 @@ public void testUnpack() throws IOException {
83
143
assertNull (zipin .getNextEntry ());
84
144
}
85
145
146
+ @ Test
147
+ public void unpack_should_throw_IOException_when_can_not_read_from_InputStream () {
148
+ try {
149
+ Class .forName ("java.util.jar.Pack200" );
150
+ } catch (ClassNotFoundException e ) {
151
+ throw new AssumptionViolatedException (
152
+ "this test requires JDK with Pack200" );
153
+ }
154
+
155
+ final InputStream inputStream = new BrokenInputStream ();
156
+ try {
157
+ Pack200Streams .unpack (inputStream );
158
+ fail ("expected exception" );
159
+ } catch (IOException e ) {
160
+ assertTrue (e .getCause () instanceof IOException );
161
+ assertEquals ("fake broken input stream" , e .getCause ().getMessage ());
162
+ }
163
+ }
164
+
165
+ @ Test
166
+ public void unpack_should_throw_IOException_when_Pack200_not_available_in_JDK () {
167
+ try {
168
+ Class .forName ("java.util.jar.Pack200" );
169
+ throw new AssumptionViolatedException (
170
+ "this test requires JDK without Pack200" );
171
+ } catch (ClassNotFoundException ignore ) {
172
+ }
173
+
174
+ try {
175
+ Pack200Streams .unpack (new ByteArrayInputStream (new byte [0 ]));
176
+ fail ("expected exception" );
177
+ } catch (IOException e ) {
178
+ assertNull (e .getMessage ());
179
+ assertTrue (e .getCause () instanceof ClassNotFoundException );
180
+ }
181
+ }
182
+
86
183
static class NoCloseInputStream extends FilterInputStream {
87
184
public NoCloseInputStream (InputStream in ) {
88
185
super (in );
@@ -105,4 +202,18 @@ public void close() throws IOException {
105
202
}
106
203
}
107
204
205
+ private static class BrokenInputStream extends InputStream {
206
+ @ Override
207
+ public int read () throws IOException {
208
+ throw new IOException ("fake broken input stream" );
209
+ }
210
+ }
211
+
212
+ private static class BrokenOutputStream extends OutputStream {
213
+ @ Override
214
+ public void write (int b ) throws IOException {
215
+ throw new IOException ("fake broken output stream" );
216
+ }
217
+ }
218
+
108
219
}
0 commit comments