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

File AbstractImagingTest.java

 

Coverage histogram

../../../img/srcFileCovDistChart9.png
50% of files have more coverage

Code metrics

12
79
18
2
268
168
27
0.34
4.39
9
1.5

Classes

Class Line # Actions
AbstractImagingTest 76 78 0% 26 19
0.822429982.2%
AbstractImagingTest.SampleImageStreamer 243 1 0% 1 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2009-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;
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    * Provides utility/convenience methods to load and write images.
67    * Also provides well known source images for tests.
68    *
69    * Some of these methods are using the imaging framework itself,
70    * so they might not be appropriate for all testing situations !
71    *
72    * The write methods also keep track of generated images, so that one
73    * can open them easily after all tests have run, if human verification
74    * is necessary.
75    */
 
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   
 
83  44 toggle @Before
84    public void setUp() throws Exception {
85    // this is set via the module descriptor (imaging.xml)
86  44 ComponentsTestUtil.setImplementation(ImageDecoder.class, DefaultImageIOImageDecoder.class);
87    }
88   
 
89  44 toggle @After
90    public void tearDown() throws Exception {
91  44 ComponentsTestUtil.clear();
92    }
93   
 
94  6 toggle protected BufferedImage loadFromResource(String source) throws ImagingException {
95  6 final ClasspathImageLoader loader = new ClasspathImageLoader(source);
96  6 return loader.apply(null, null);
97    }
98   
 
99  11 toggle protected void write(BufferedImage res) throws IOException {
100  11 write(res, BASIC_JPEG);
101    }
102   
 
103  19 toggle protected void write(BufferedImage res, OutputFormat outputFormat) throws IOException {
104  19 write(null, res, outputFormat);
105    }
106   
 
107  34 toggle 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   
 
115  48 toggle protected File newOutputFile(String suffix, String extension) throws IOException {
116  48 return newOutputFile(getClass().getSimpleName() + "-" + TestUtil.getCurrentTestMethodName(), suffix, extension);
117    }
118   
 
119  60 toggle 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   
 
142  60 toggle 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    // having these images as static constants is a mediocre attempt at making the tests run faster (AutoCropAndResizeTest was using around 7 seconds...)
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    // preload images for this test
163   
 
164  1 toggle 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    * Gets a well-known image, ensuring its dimensions haven't changed since the test was written and started.
179    */
 
180  16 toggle 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    * Gets a well-known image, ensuring its dimensions haven't changed since the test was written and started.
188    */
 
189  8 toggle 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    * Gets a well-known image, ensuring its dimensions haven't changed since the test was written and started.
197    */
 
198  2 toggle 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    * Gets a well-known image, ensuring its dimensions haven't changed since the test was written and started.
206    */
 
207  2 toggle 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    * Generates an image of arbitrary width and height. Re-generated for every call.
215    */
 
216  4 toggle 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    // background
220  4 g.setColor(Color.green);
221  4 g.fill(new Rectangle(0, 0, width, height));
222    // dashed diagonals
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    // small square in each corner
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    * This inner class exists for the sole purpose of being able to call
241    * info.magnolia.imaging.DefaultImageStreamer#write
242    */
 
243    private static class SampleImageStreamer extends DefaultImageStreamer {
 
244  34 toggle @Override
245    protected void write(BufferedImage img, OutputStream out, OutputFormat outputFormat) throws IOException {
246  34 super.write(img, out, outputFormat);
247    }
248    }
249   
 
250  1 toggle static {
251  1 if (KEEP_GENERATED_FILES_FOR_INSPECTION && OPEN_GENERATED_FILES_FOR_INSPECTION) {
252  0 Runtime.getRuntime().addShutdownHook(new Thread() {
 
253  0 toggle @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    }