Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.operations.load

File AbstractLoaderTest.java

 

Code metrics

20
137
17
2
375
260
35
0.26
8.06
8.5
2.06
1.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
AbstractLoaderTest 68 136 0% 34 26
0.848837284.9%
AbstractLoaderTest.Loader 271 1 50% 1 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /**
2    * This file Copyright (c) 2012-2015 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.load;
35   
36    import static info.magnolia.imaging.AbstractImagingTest.newOutputFile;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.mock;
39   
40    import info.magnolia.imaging.AbstractRepositoryTestCase;
41    import info.magnolia.imaging.ImagingException;
42    import info.magnolia.imaging.ParameterProvider;
43   
44    import java.awt.Color;
45    import java.awt.image.BufferedImage;
46    import java.awt.image.WritableRaster;
47    import java.io.File;
48    import java.io.IOException;
49    import java.lang.reflect.Field;
50    import java.lang.reflect.Modifier;
51    import java.util.HashMap;
52    import java.util.Map;
53    import java.util.Set;
54   
55    import javax.imageio.ImageIO;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59    import org.reflections.ReflectionUtils;
60    import org.slf4j.Logger;
61    import org.slf4j.LoggerFactory;
62   
63    import com.google.common.base.Predicate;
64   
65    /**
66    * Tests for {@link info.magnolia.imaging.operations.load.AbstractLoader}.
67    */
 
68    public class AbstractLoaderTest extends AbstractRepositoryTestCase {
69    private static final Logger log = LoggerFactory.getLogger(AbstractLoaderTest.class);
70   
71    private Loader loader;
72    private final ParameterProvider provider = mock(ParameterProvider.class);
73   
74    private BufferedImage originalJpgImage;
75    private BufferedImage originalPngImage;
76    private BufferedImage originalGifImage;
77    private BufferedImage originalBmpImage;
78    private BufferedImage originalPngWithAlphaImage;
79    private BufferedImage originalPngWithAlphaImageBlackBackground;
80   
81    private BufferedImage png2gifOriginalImage;
82    private BufferedImage png2jpgOriginalImage;
83    private BufferedImage jpg2jpgOriginalImage;
84    private BufferedImage gif2gifOriginalImage;
85    private BufferedImage gif2pngOriginalImage;
86    private BufferedImage gif2jpgOriginalImage;
87   
88    private final String pathWithOriginals = "src/test/resources/AbstractLoaderTest/originals";
89   
 
90  7 toggle @Before
91    @Override
92    public void setUp() throws Exception {
93  7 super.setUp();
94  7 loader = new Loader();
95   
96  7 originalJpgImage = ImageIO.read(new File(pathWithOriginals, "originalJpg.jpg"));
97  7 originalPngImage = ImageIO.read(new File(pathWithOriginals, "originalPng.png"));
98  7 originalGifImage = ImageIO.read(new File(pathWithOriginals, "originalGif.gif"));
99  7 originalBmpImage = ImageIO.read(new File(pathWithOriginals, "originalBmp.bmp"));
100  7 originalPngWithAlphaImage = ImageIO.read(new File(pathWithOriginals, "originalPngWithAlpha.png"));
101  7 originalPngWithAlphaImageBlackBackground = ImageIO.read(new File(pathWithOriginals, "originalPngWithAlphaBlackBackground.png"));
102   
103  7 png2gifOriginalImage = ImageIO.read(new File(pathWithOriginals, "png2gifOriginal.gif"));
104  7 png2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "png2jpgOriginal.jpg"));
105  7 jpg2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "jpg2jpgOriginal.jpg"));
106  7 gif2gifOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2gifOriginal.gif"));
107  7 gif2pngOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2pngOriginal.png"));
108  7 gif2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2jpgOriginal.jpg"));
109    }
110   
 
111  1 toggle @Test
112    public void testBackgroundColor() throws Exception {
113    // GIVEN
114  1 loader.setSource(originalPngWithAlphaImage);
115  1 loader.setBackgroundColor(Color.black);
116    // WHEN
117  1 BufferedImage bufferedImage = loader.apply(null, provider);
118  1 log.info("\nTest BufferedImage from png with alpha image: source image type: {}, target image type: {}",
119    imageTypeToString(originalPngWithAlphaImage.getType()), imageTypeToString(bufferedImage.getType()));
120    // THEN
121  1 assertEquals(originalPngWithAlphaImage.getType(), bufferedImage.getType());
122  1 assertTrue(equalsPictures(originalPngWithAlphaImageBlackBackground, bufferedImage));
123  1 assertTrue(equalsAlpha(originalPngWithAlphaImageBlackBackground, bufferedImage));
124   
125    // for visual checkout
126  1 ImageIO.write(bufferedImage, "png", newOutputFile("loadertest", "pngWithAlpha2PngBlackBackground", "png"));
127    }
128   
129    // PNG
 
130  1 toggle @Test
131    public void testBufferedImageFromPng() throws Exception {
132    // GIVEN
133  1 loader.setSource(originalPngImage);
134    // WHEN
135  1 BufferedImage bufferedImage = loader.apply(null, provider);
136  1 log.info("\nTest BufferedImage from png image: source image type: {}, target image type: {}",
137    imageTypeToString(originalPngImage.getType()), imageTypeToString(bufferedImage.getType()));
138    // THEN
139  1 assertEquals(originalPngImage.getType(), bufferedImage.getType());
140  1 assertTrue(equalsPictures(originalPngImage, bufferedImage));
141  1 assertTrue(equalsAlpha(originalPngImage, bufferedImage));
142   
143  1 final File png2jpg = newOutputFile("loadertest", "png2jpg", "jpg");
144  1 final File png2png = newOutputFile("loadertest", "png2png", "png");
145  1 final File png2gif = newOutputFile("loadertest", "png2gif", "gif");
146   
147  1 ImageIO.write(bufferedImage, "gif", png2gif);
148  1 ImageIO.write(bufferedImage, "png", png2png);
149  1 ImageIO.write(bufferedImage, "jpg", png2jpg);
150   
151  1 assertImage("png to png", png2png, originalPngImage, originalPngImage);
152  1 assertImage("png to jpg", png2jpg, originalPngImage, png2jpgOriginalImage);
153  1 assertImage("png to gif", png2gif, originalPngImage, png2gifOriginalImage);
154    }
155   
156    // JPEG
 
157  1 toggle @Test
158    public void testBufferedImageFromJpg() throws Exception {
159    // GIVEN
160  1 loader.setSource(originalJpgImage);
161    // WHEN
162  1 BufferedImage bufferedImage = loader.apply(null, provider);
163  1 log.info("\nTest BufferedImage from jpg image: source image type: {}, target image type: {}",
164    imageTypeToString(originalJpgImage.getType()), imageTypeToString(bufferedImage.getType()));
165    // THEN
166  1 assertEquals(originalJpgImage.getType(), bufferedImage.getType());
167  1 assertTrue(equalsPictures(originalJpgImage, bufferedImage));
168  1 assertTrue(equalsAlpha(originalJpgImage, bufferedImage));
169   
170  1 final File jpg2jpg = newOutputFile("loadertest", "jpg2jpg", "jpg");
171  1 final File jpg2png = newOutputFile("loadertest", "jpg2png", "png");
172  1 final File jpg2gif = newOutputFile("loadertest", "jpg2gif", "gif");
173  1 ImageIO.write(bufferedImage, "gif", jpg2gif);
174  1 ImageIO.write(bufferedImage, "png", jpg2png);
175  1 ImageIO.write(bufferedImage, "jpg", jpg2jpg);
176   
177  1 assertImage("jpg to gif", jpg2gif, originalJpgImage, originalGifImage);
178  1 assertImage("jpg to png", jpg2png, originalJpgImage, originalJpgImage);
179  1 assertImage("jpg to jpg", jpg2jpg, originalJpgImage, jpg2jpgOriginalImage);
180    }
181   
 
182  1 toggle @Test
183    public void testBufferedImageFromGif() throws Exception {
184    // GIVEN
185  1 loader.setSource(originalGifImage);
186    // WHEN
187  1 BufferedImage bufferedImage = loader.apply(null, provider);
188  1 log.info("\nTest BufferedImage from gif image: source image type: {}, target image type: {}",
189    imageTypeToString(originalGifImage.getType()), imageTypeToString(bufferedImage.getType()));
190   
191    // THEN
192  1 assertEquals(originalGifImage.getType(), bufferedImage.getType());
193  1 assertTrue(equalsPictures(gif2gifOriginalImage, bufferedImage));
194  1 assertTrue(equalsAlpha(originalGifImage, bufferedImage));
195   
196  1 final File gif2gif = newOutputFile("loadertest", "gif2gif", "gif");
197  1 final File gif2png = newOutputFile("loadertest", "gif2png", "png");
198  1 final File gif2jpg = newOutputFile("loadertest", "gif2jpg", "jpg");
199  1 ImageIO.write(bufferedImage, "gif", gif2gif);
200  1 ImageIO.write(bufferedImage, "png", gif2png);
201  1 ImageIO.write(bufferedImage, "jpg", gif2jpg);
202   
203  1 assertImage("gif to gif", gif2gif, originalGifImage, gif2gifOriginalImage);
204  1 assertImage("gif to png", gif2png, originalGifImage, gif2pngOriginalImage);
205  1 assertImage("gif to jpg", gif2jpg, originalGifImage, gif2jpgOriginalImage);
206    }
207   
 
208  9 toggle private void assertImage(String desc, File src, BufferedImage original, BufferedImage expectedImg) throws IOException {
209    // WHEN
210  9 BufferedImage asRead = ImageIO.read(src);
211  9 log.info("\nTest {} image : {}, target image type: {}", desc, imageTypeToString(original.getType()), imageTypeToString(asRead.getType()));
212    // THEN
213  9 assertTrue(equalsPictures(expectedImg, asRead));
214  9 assertTrue(equalsAlpha(originalJpgImage, asRead));
215    }
216   
 
217  1 toggle @Test
218    public void testBufferedImageFromBmp() throws Exception {
219    // GIVEN
220  1 loader.setSource(originalBmpImage);
221    // WHEN
222  1 BufferedImage bufferedImage = loader.apply(null, provider);
223  1 log.info("\nTest BufferedImage from bmp image: source image type: {}, target image type: {}",
224    imageTypeToString(originalBmpImage.getType()), imageTypeToString(bufferedImage.getType()));
225    // THEN
226  1 assertEquals(originalBmpImage.getType(), bufferedImage.getType());
227  1 assertTrue(equalsPictures(originalBmpImage, bufferedImage));
228  1 assertTrue(equalsAlpha(originalBmpImage, bufferedImage));
229    }
230   
 
231  1 toggle @Test
232    public void testBufferedImageFromPngWithAlpha() throws Exception {
233    // GIVEN
234  1 loader.setSource(originalPngWithAlphaImage);
235    // WHEN
236  1 BufferedImage bufferedImage = loader.apply(null, provider);
237  1 log.info("\nTest BufferedImage from png with alpha image: source image type: {}, target image type: {}",
238    imageTypeToString(originalPngWithAlphaImage.getType()), imageTypeToString(bufferedImage.getType()));
239    // THEN
240  1 assertEquals(originalPngWithAlphaImage.getType(), bufferedImage.getType());
241  1 assertTrue(equalsPictures(originalPngWithAlphaImage, bufferedImage));
242  1 assertTrue(equalsAlpha(originalPngWithAlphaImage, bufferedImage));
243   
244    // for visual checkout
245  1 ImageIO.write(bufferedImage, "png", newOutputFile("loadertest", "pngWithAlpha2Png", "png"));
246    }
247   
 
248  1 toggle @Test
249    public void testTransparencyOfIndexedImageIsNotLost() throws Exception {
250    // GIVEN
251  1 final String fileName = "transparentIndexed";
252  1 File originalFile = new File(pathWithOriginals, fileName + ".png");
253  1 File outputFile = newOutputFile("loader-test", "Loaded", "png");
254  1 BufferedImage originalImage = ImageIO.read(originalFile);
255  1 loader.setSource(originalImage);
256    // WHEN
257  1 BufferedImage bufferedImage = loader.apply(null, provider);
258  1 log.info("\nTest BufferedImage from indexed image: source image type: {}, target image type: {}",
259    imageTypeToString(originalBmpImage.getType()), imageTypeToString(bufferedImage.getType()));
260    // THEN
261    // for visual checkout
262    // this.checkVisually(bufferedImage, originalFile, outputFile);
263  1 assertTrue(bufferedImage.getColorModel().hasAlpha());
264    }
265   
 
266  0 toggle private void checkVisually(BufferedImage bufferedImage, File originalFile, File outputFile) throws IOException {
267  0 ImageIO.write(bufferedImage, "png", outputFile);
268  0 Runtime.getRuntime().exec(String.format("open %s %s", originalFile.getAbsolutePath(), outputFile.getAbsolutePath()));
269    }
270   
 
271    final class Loader extends AbstractLoader {
272   
273    private BufferedImage source;
274   
 
275    toggle public void setSource(BufferedImage originalImage) {
276    source = originalImage;
277    }
278   
 
279  7 toggle @Override
280    protected BufferedImage loadSource(ParameterProvider filterParams) throws ImagingException {
281  7 return source;
282    }
283    }
284   
 
285  0 toggle private void showSupportedWriterFormats() {
286  0 String writerNames[] = ImageIO.getWriterFormatNames();
287  0 String msg = "\nSupported writer formats: ";
288  0 for (String name : writerNames) {
289  0 msg += name + ",";
290    }
291  0 log.info(msg);
292    }
293   
 
294  15 toggle private boolean equalsAlpha(BufferedImage bi1, BufferedImage bi2) {
295  15 if (bi1.getWidth() != bi2.getWidth() || bi1.getHeight() != bi2.getHeight()) {
296  0 log.error(String.format("Different size: '%dx%d' vs. '%dx%d'",
297    bi1.getWidth(), bi1.getHeight(), bi2.getWidth(), bi2.getHeight()));
298  0 return false;
299    }
300  15 WritableRaster wr1 = bi1.getAlphaRaster();
301  15 WritableRaster wr2 = bi2.getAlphaRaster();
302   
303  15 if (wr1 == null && wr2 == null) {
304  13 return true;
305  2 } else if (wr1 == null && wr2 != null || wr1 != null && wr2 == null) {
306  0 return false;
307    }
308   
309  2 int size = bi1.getWidth() * bi1.getHeight();
310  2 int dArray1[] = wr1.getPixel(0, 0, new int[size]);
311  2 int dArray2[] = wr2.getPixel(0, 0, new int[size]);
312   
313  131074 for (int x = 0; x < size; x++) {
314  131072 if (dArray1[x] != dArray2[x]) {
315  0 log.error(String.format("Different alpha value at position: %d': '%d' vs. '%d'.",
316    x, dArray1[x], dArray2[x]));
317  0 return false;
318    }
319    }
320  2 return true;
321    }
322   
 
323  15 toggle private boolean equalsPictures(BufferedImage bi1, BufferedImage bi2) {
324   
325  15 if (bi1.getWidth() != bi2.getWidth() || bi1.getHeight() != bi2.getHeight()) {
326  0 log.error(String.format("Different size: '%dx%d' vs. '%dx%d'",
327    bi1.getWidth(), bi1.getHeight(), bi2.getWidth(), bi2.getHeight()));
328  0 return false;
329    }
330   
331  4401 for (int x = 0; x < bi1.getWidth(); x++) {
332  790164 for (int y = 0; y < bi1.getHeight(); y++) {
333  785778 if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {
334  0 log.error(String.format("Different pixel at position: '%d,%d': '%d' vs. '%d'.",
335    x, y, bi1.getRGB(x, y), bi2.getRGB(x, y)));
336  0 return false;
337    }
338    }
339    }
340  15 return true;
341    }
342   
 
343  32 toggle private String imageTypeToString(int imageType) {
344  32 final String str = imageTypeToString.get(imageType);
345  32 if (imageType == 0 || str == null) {
346  0 throw new IllegalArgumentException("Unknown image type " + imageType + "(" + str + ")");
347    }
348  32 return str;
349    }
350   
351    static final Map<Integer, String> imageTypeToString = new HashMap<>();
352   
 
353  1 toggle static {
354  1 final Set<Field> typeFields = ReflectionUtils.getAllFields(BufferedImage.class, new Predicate<Field>() {
 
355  45 toggle @Override
356    public boolean apply(Field field) {
357  45 int mod = field.getModifiers();
358  45 return Modifier.isStatic(mod) &&
359    Modifier.isPublic(mod) &&
360    Modifier.isFinal(mod) &&
361    Integer.TYPE.equals(field.getType()) &&
362    field.getName().startsWith("TYPE_");
363    }
364    });
365  1 try {
366  1 for (Field field : typeFields) {
367  14 imageTypeToString.put((Integer) field.get(null), field.getName());
368    }
369    } catch (IllegalAccessException e) {
370  0 throw new IllegalStateException(e);
371    }
372   
373    }
374   
375    }