1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.imaging; |
35 |
|
|
36 |
|
import static org.junit.Assert.assertEquals; |
37 |
|
|
38 |
|
import info.magnolia.imaging.operations.load.ClasspathImageLoader; |
39 |
|
import info.magnolia.imaging.operations.load.DefaultImageIOImageDecoder; |
40 |
|
import info.magnolia.imaging.operations.load.ImageDecoder; |
41 |
|
import info.magnolia.imaging.util.ImageUtil; |
42 |
|
import info.magnolia.test.ComponentsTestUtil; |
43 |
|
import info.magnolia.test.TestUtil; |
44 |
|
|
45 |
|
import java.awt.BasicStroke; |
46 |
|
import java.awt.Color; |
47 |
|
import java.awt.Graphics2D; |
48 |
|
import java.awt.Rectangle; |
49 |
|
import java.awt.image.BufferedImage; |
50 |
|
import java.io.File; |
51 |
|
import java.io.FileOutputStream; |
52 |
|
import java.io.IOException; |
53 |
|
import java.io.OutputStream; |
54 |
|
import java.nio.file.Files; |
55 |
|
import java.util.HashSet; |
56 |
|
import java.util.Set; |
57 |
|
|
58 |
|
import javax.imageio.ImageIO; |
59 |
|
|
60 |
|
import org.junit.After; |
61 |
|
import org.junit.Before; |
62 |
|
|
63 |
|
import com.jhlabs.image.FlipFilter; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
|
|
| 82.2% |
Uncovered Elements: 19 (107) |
Complexity: 26 |
Complexity Density: 0.33 |
|
76 |
|
public abstract class AbstractImagingTest { |
77 |
|
private static final boolean KEEP_GENERATED_FILES_FOR_INSPECTION = false; |
78 |
|
private static final boolean OPEN_GENERATED_FILES_FOR_INSPECTION = false; |
79 |
|
|
80 |
|
protected static final OutputFormat BASIC_JPEG = new OutputFormat("jpeg", false, 80, null, false); |
81 |
|
private static final Set<String> generatedFiles = new HashSet<String>(); |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
44 |
@Before... |
84 |
|
public void setUp() throws Exception { |
85 |
|
|
86 |
44 |
ComponentsTestUtil.setImplementation(ImageDecoder.class, DefaultImageIOImageDecoder.class); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
44 |
@After... |
90 |
|
public void tearDown() throws Exception { |
91 |
44 |
ComponentsTestUtil.clear(); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
94 |
6 |
protected BufferedImage loadFromResource(String source) throws ImagingException {... |
95 |
6 |
final ClasspathImageLoader loader = new ClasspathImageLoader(source); |
96 |
6 |
return loader.apply(null, null); |
97 |
|
} |
98 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
99 |
11 |
protected void write(BufferedImage res) throws IOException {... |
100 |
11 |
write(res, BASIC_JPEG); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
19 |
protected void write(BufferedImage res, OutputFormat outputFormat) throws IOException {... |
104 |
19 |
write(null, res, outputFormat); |
105 |
|
} |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
107 |
34 |
protected void write(String filenameSuffix, BufferedImage res, OutputFormat outputFormat) throws IOException {... |
108 |
34 |
final SampleImageStreamer streamer = new SampleImageStreamer(); |
109 |
34 |
final String extension = outputFormat.getFormatName(); |
110 |
34 |
final File f = newOutputFile(filenameSuffix, extension); |
111 |
34 |
final FileOutputStream outputStream = new FileOutputStream(f); |
112 |
34 |
streamer.write(res, outputStream, outputFormat); |
113 |
|
} |
114 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
115 |
48 |
protected File newOutputFile(String suffix, String extension) throws IOException {... |
116 |
48 |
return newOutputFile(getClass().getSimpleName() + "-" + TestUtil.getCurrentTestMethodName(), suffix, extension); |
117 |
|
} |
118 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.29 |
|
119 |
60 |
public static File newOutputFile(String prefix, String suffix, String extension) throws IOException {... |
120 |
60 |
final StringBuilder filename = new StringBuilder(); |
121 |
60 |
filename.append(prefix); |
122 |
60 |
if (suffix != null) { |
123 |
41 |
filename.append("-"); |
124 |
41 |
filename.append(suffix); |
125 |
|
} |
126 |
60 |
filename.append(".").append(extension); |
127 |
|
|
128 |
60 |
final File dir = getTestOutputDirectory(); |
129 |
|
|
130 |
60 |
final File file = new File(dir, filename.toString()); |
131 |
60 |
if (file.exists()) { |
132 |
0 |
throw new IllegalStateException(file + " already exists"); |
133 |
|
} |
134 |
60 |
generatedFiles.add(file.getAbsolutePath()); |
135 |
|
|
136 |
60 |
if (!KEEP_GENERATED_FILES_FOR_INSPECTION) { |
137 |
60 |
file.deleteOnExit(); |
138 |
|
} |
139 |
60 |
return file; |
140 |
|
} |
141 |
|
|
|
|
| 50% |
Uncovered Elements: 6 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
142 |
60 |
public static File getTestOutputDirectory() throws IOException {... |
143 |
60 |
String outputDir = System.getProperty("magnolia.test.output.folder"); |
144 |
60 |
if (outputDir == null) { |
145 |
0 |
if (new File("target").exists()) { |
146 |
0 |
outputDir = "target/test-output"; |
147 |
|
} else { |
148 |
0 |
outputDir = "test-output"; |
149 |
|
} |
150 |
|
} |
151 |
60 |
final File dir = new File(outputDir); |
152 |
60 |
Files.createDirectories(dir.toPath()); |
153 |
60 |
return dir; |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
|
private static final BufferedImage horizontalImage; |
158 |
|
private static final BufferedImage verticalImage; |
159 |
|
private static final BufferedImage squareImage; |
160 |
|
private static final BufferedImage smallImage; |
161 |
|
|
162 |
|
|
163 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
164 |
1 |
static {... |
165 |
1 |
try { |
166 |
1 |
final FlipFilter f = new FlipFilter(FlipFilter.FLIP_90CW); |
167 |
1 |
squareImage = ImageIO.read(AbstractImagingTest.class.getResource("/IMG_4995.JPG")); |
168 |
1 |
horizontalImage = ImageIO.read(AbstractImagingTest.class.getResource("/IMG_2463.JPG")); |
169 |
1 |
verticalImage = f.filter(horizontalImage, null); |
170 |
1 |
smallImage = ImageIO.read(AbstractImagingTest.class.getResource("/magnolia-logo.png")); |
171 |
|
|
172 |
|
} catch (IOException e) { |
173 |
0 |
throw new RuntimeException(e); |
174 |
|
} |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
180 |
16 |
protected BufferedImage getHorizontalTestImage() throws Exception {... |
181 |
16 |
assertEquals(1600, horizontalImage.getWidth()); |
182 |
16 |
assertEquals(1200, horizontalImage.getHeight()); |
183 |
16 |
return horizontalImage; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
189 |
8 |
protected BufferedImage getVerticalTestImage() throws Exception {... |
190 |
8 |
assertEquals(1200, verticalImage.getWidth()); |
191 |
8 |
assertEquals(1600, verticalImage.getHeight()); |
192 |
8 |
return verticalImage; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
198 |
2 |
protected BufferedImage getSquareTestImage() throws Exception {... |
199 |
2 |
assertEquals(500, squareImage.getWidth()); |
200 |
2 |
assertEquals(500, squareImage.getHeight()); |
201 |
2 |
return squareImage; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
207 |
2 |
protected BufferedImage getSmallTestImage() throws Exception {... |
208 |
2 |
assertEquals(202, smallImage.getWidth()); |
209 |
2 |
assertEquals(51, smallImage.getHeight()); |
210 |
2 |
return smallImage; |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
|
216 |
4 |
protected BufferedImage generateArbitraryTestImage(final int width, final int height) {... |
217 |
4 |
final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
218 |
4 |
final Graphics2D g = img.createGraphics(); |
219 |
|
|
220 |
4 |
g.setColor(Color.green); |
221 |
4 |
g.fill(new Rectangle(0, 0, width, height)); |
222 |
|
|
223 |
4 |
g.setColor(Color.darkGray); |
224 |
4 |
g.setStroke(new BasicStroke(3, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1f, new float[]{10, 10}, 0)); |
225 |
4 |
g.drawLine(0, 0, width, height); |
226 |
4 |
g.drawLine(0, height, width, 0); |
227 |
|
|
228 |
4 |
g.setColor(Color.red); |
229 |
4 |
g.fill(new Rectangle(10, 10, 30, 30)); |
230 |
4 |
g.setColor(Color.blue); |
231 |
4 |
g.fill(new Rectangle(width - 40, height - 40, 30, 30)); |
232 |
4 |
g.setColor(Color.yellow); |
233 |
4 |
g.fill(new Rectangle(width - 40, 10, 30, 30)); |
234 |
4 |
g.setColor(Color.white); |
235 |
4 |
g.fill(new Rectangle(10, height - 40, 30, 30)); |
236 |
4 |
return ImageUtil.flattenTransparentImageForOpaqueFormat(img, BASIC_JPEG); |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
243 |
|
private static class SampleImageStreamer extends DefaultImageStreamer { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
34 |
@Override... |
245 |
|
protected void write(BufferedImage img, OutputStream out, OutputFormat outputFormat) throws IOException { |
246 |
34 |
super.write(img, out, outputFormat); |
247 |
|
} |
248 |
|
} |
249 |
|
|
|
|
| 50% |
Uncovered Elements: 2 (4) |
Complexity: 3 |
Complexity Density: 1.5 |
|
250 |
1 |
static {... |
251 |
1 |
if (KEEP_GENERATED_FILES_FOR_INSPECTION && OPEN_GENERATED_FILES_FOR_INSPECTION) { |
252 |
0 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
253 |
0 |
@Override... |
254 |
|
public void run() { |
255 |
0 |
final StringBuilder command = new StringBuilder("open"); |
256 |
0 |
for (String file : generatedFiles) { |
257 |
0 |
command.append(" ").append(file); |
258 |
|
} |
259 |
0 |
try { |
260 |
0 |
Runtime.getRuntime().exec(command.toString()); |
261 |
|
} catch (IOException e) { |
262 |
0 |
throw new IllegalStateException("Could run open command with generated files... ", e); |
263 |
|
} |
264 |
|
} |
265 |
|
}); |
266 |
|
} |
267 |
|
} |
268 |
|
} |