1 /*
2  * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4429365
27  * @summary Checks that ImageReadParam.setSourceProgressivePasses handles
28  *          overflow correctly
29  */
30 
31 import javax.imageio.ImageReadParam;
32 
33 public class ImageReadParamPasses {
34 
35     private static final int maxint = Integer.MAX_VALUE;
36 
expect(int i, int j)37     private static void expect(int i, int j) {
38         if (i != j) {
39             throw new RuntimeException("Expected " + i + ", got " + j);
40         }
41     }
42 
checkForIAE(int minPass, int numPasses)43     private static void checkForIAE(int minPass, int numPasses) {
44         ImageReadParam param = new ImageReadParam();
45 
46         boolean gotIAE = false;
47         try {
48             param.setSourceProgressivePasses(minPass, numPasses);
49         } catch (IllegalArgumentException iae) {
50             gotIAE = true;
51         }
52         if (!gotIAE) {
53             throw new RuntimeException("Failed to get IAE for wraparound!");
54         }
55     }
56 
test(int minPass, int numPasses)57     private static void test(int minPass, int numPasses) {
58         ImageReadParam param = new ImageReadParam();
59 
60         param.setSourceProgressivePasses(minPass, numPasses);
61         expect(param.getSourceMinProgressivePass(), minPass);
62         expect(param.getSourceNumProgressivePasses(), numPasses);
63 
64         int maxPass = numPasses == maxint ? maxint : minPass + numPasses - 1;
65         expect(param.getSourceMaxProgressivePass(), maxPass);
66     }
67 
main(String[] args)68     public static void main(String[] args) {
69         // Typical case
70         test(17, 30);
71 
72         // Read all passes
73         test(0, maxint);
74 
75         // Start at pass 17, continue indefinitely
76         test(17, maxint);
77 
78         // Start at pass maxint - 10, continue indefinitely
79         test(maxint - 10, maxint);
80 
81         // Start at pass maxint - 10, go up to maxint - 1
82         test(maxint - 10, 10);
83 
84         // Start at pass maxint - 10, go up to maxint
85         test(maxint - 10, 11);
86 
87         // Start at maxint, continue indefinitely :-)
88         test(maxint, maxint);
89 
90         // Start at maxint, go up to maxint
91         test(maxint, 1);
92 
93         // Check that an IllegalArgumentException is thrown if
94         // wraparound occurs
95         checkForIAE(maxint, 2);
96         checkForIAE(maxint - 5, 10);
97         checkForIAE(10, maxint - 5);
98         checkForIAE(maxint - 1000, maxint - 1000);
99         checkForIAE(maxint - 1, maxint - 1);
100     }
101 }
102