Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/WritingInterruptionTest.java
41152 views
1
/*
2
* Copyright (c) 2007, 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 6557086 6547241
27
* @summary Test verifies that invocation of reset/abort/dispose methods from
28
* another thread does not cause crash in jpeg library.
29
* @run main WritingInterruptionTest
30
*/
31
32
import java.awt.Color;
33
import java.awt.Graphics2D;
34
import java.awt.RadialGradientPaint;
35
import java.awt.geom.Point2D;
36
import java.awt.image.BufferedImage;
37
import java.io.File;
38
import java.io.IOException;
39
import javax.imageio.ImageIO;
40
import javax.imageio.ImageWriter;
41
import javax.imageio.ImageWriteParam;
42
import javax.imageio.event.IIOWriteProgressListener;
43
import javax.imageio.stream.ImageOutputStream;
44
45
46
public class WritingInterruptionTest implements IIOWriteProgressListener {
47
48
static File pwd = new File(".");
49
static BufferedImage img;
50
51
public static void main(String[] args) {
52
img = createTestImage();
53
54
System.out.println("Test abort()....");
55
WritingInterruptionTest t = new WritingInterruptionTest(new AbortAction());
56
t.doTest();
57
58
System.out.println("Test reset()....");
59
t = new WritingInterruptionTest(new ResetAction());
60
t.doTest();
61
62
System.out.println("Test dispose()....");
63
t = new WritingInterruptionTest(new DisposeAction());
64
t.doTest();
65
}
66
67
protected abstract static class Action implements Runnable {
68
protected ImageWriter target;
69
70
public void setTarget(ImageWriter target) {
71
this.target = target;
72
}
73
74
public abstract void run();
75
}
76
77
protected static class DisposeAction extends Action {
78
public void run() {
79
try {
80
target.dispose();
81
} catch (IllegalStateException e) {
82
System.out.println("Test PASSED: IllegalStateException was thrown.");
83
} catch (Throwable e) {
84
throw new RuntimeException("Test FAILED.", e);
85
}
86
}
87
}
88
89
protected static class AbortAction extends Action {
90
public void run() {
91
try {
92
target.abort();
93
} catch (IllegalStateException e) {
94
System.out.println("Test PASSED: IllegalStateException was thrown.");
95
} catch (Throwable e) {
96
throw new RuntimeException("Test FAILED.", e);
97
}
98
}
99
}
100
101
protected static class ResetAction extends Action {
102
public void run() {
103
try {
104
target.reset();
105
} catch (IllegalStateException e) {
106
System.out.println("Test PASSED: IllegalStateException was thrown.");
107
} catch (Throwable e) {
108
throw new RuntimeException("Test FAILED.", e);
109
}
110
}
111
}
112
113
114
Action action;
115
ImageOutputStream ios;
116
ImageWriter w;
117
118
protected WritingInterruptionTest(Action action) {
119
this.action = action;
120
121
w = ImageIO.getImageWritersByFormatName("JPEG").next();
122
123
this.action.setTarget(w);
124
}
125
126
public void doTest() {
127
try {
128
w.addIIOWriteProgressListener(this);
129
File f = File.createTempFile("writer_", ".jpg", pwd);
130
ImageOutputStream ios = ImageIO.createImageOutputStream(f);
131
w.setOutput(ios);
132
Thread.sleep(70);
133
w.write(img);
134
Thread.sleep(70);
135
} catch (Exception e) {
136
/*
137
* we do expect that concurrent attempt to dispose this
138
* instance of image writer will be blocked. So, this image
139
* should be writen sucessfuly. Otherwise, something went wrong
140
* and we need to report test failure.
141
*/
142
throw new RuntimeException("Test FAILED", e);
143
} finally {
144
/*
145
* it would happen that concurrent invocation of dispose() method
146
* will be successful. Due to race condition it seems to be possible
147
* that dispose operation will be performed after than write() operation
148
* leaveled thread lock. In this case all subsequent calls for writer
149
* methods should results in IllegalStateException. So, we treat
150
* IllegalStateException as success. Everything else means test failure.
151
*/
152
try {
153
w.reset();
154
} catch (IllegalStateException e) {
155
System.out.println("Expected exception was caught: " + e);
156
} catch(Exception e) {
157
throw new RuntimeException("Test FAILED.", e);
158
}
159
}
160
System.out.println("Test PASSED.");
161
}
162
163
// listener medthods
164
public void imageStarted(ImageWriter source,
165
int imageIndex) {} ;
166
167
public void imageProgress(ImageWriter source,
168
float percentageDone)
169
{
170
if (20f < percentageDone && percentageDone < 80f ) {
171
Thread t = new Thread(action);
172
t.start();
173
}
174
};
175
176
public void imageComplete(ImageWriter source) {};
177
178
public void thumbnailStarted(ImageWriter source,
179
int imageIndex,
180
int thumbnailIndex) {};
181
182
public void thumbnailProgress(ImageWriter source,
183
float percentageDone) {};
184
185
public void thumbnailComplete(ImageWriter source) {};
186
187
public void writeAborted(ImageWriter source) {};
188
189
190
private static BufferedImage createTestImage() {
191
int w = 1024;
192
int h = 768;
193
194
BufferedImage img = new
195
BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
196
Graphics2D g = img.createGraphics();
197
Color[] colors = { Color.red, Color.green, Color.blue };
198
float[] dist = {0.0f, 0.5f, 1.0f };
199
Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
200
201
RadialGradientPaint p =
202
new RadialGradientPaint(center, 0.5f * w, dist, colors);
203
g.setPaint(p);
204
g.fillRect(0, 0, w, h);
205
g.dispose();
206
207
return img;
208
}
209
}
210
211