Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.cropresize

File BoundedResizeTest.java

 

Code metrics

0
24
11
1
121
66
11
0.46
2.18
11
1

Classes

Class Line # Actions
BoundedResizeTest 45 24 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 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.cropresize;
35   
36    import static org.junit.Assert.assertEquals;
37   
38    import info.magnolia.imaging.AbstractImagingTest;
39   
40    import java.awt.image.BufferedImage;
41   
42    import org.junit.Before;
43    import org.junit.Test;
44   
 
45    public class BoundedResizeTest extends AbstractImagingTest {
46   
47    BoundedResize op;
48   
 
49  9 toggle @Override
50    @Before
51    public void setUp() throws Exception {
52  9 super.setUp();
53  9 op = new BoundedResize();
54    }
55   
 
56  1 toggle @Test
57    public void justResizeIfTargetRatioIsEquivalentToSourceRatio() throws Exception {
58  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getHorizontalTestImage(), 400, 300, 400, 300);
59    }
60   
 
61  1 toggle @Test
62    public void keepsOriginalRatioAndCompliesToSmallestMaximumDimension() throws Exception {
63    // source is w:1600*h:1200
64  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getHorizontalTestImage(), 2500, 300, 400, 300);
65  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getHorizontalTestImage(), 200, 3000, 200, 150);
66    }
67   
 
68  1 toggle @Test
69    public void keepsOriginalRatioAndCompliesToSmallestMaximumDimensionForVerticalSourceToo() throws Exception {
70    // source is w:1200*h:1600
71  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getVerticalTestImage(), 2500, 400, 300, 400);
72  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getVerticalTestImage(), 300, 4000, 300, 400);
73    }
74   
 
75  1 toggle @Test
76    public void keepsOriginalRatioAndCompliesToSmallestMaximumDimensionForSquareSourceToo() throws Exception {
77    // source is w:500*h:500
78  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getSquareTestImage(), 2500, 400, 400, 400);
79  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getSquareTestImage(), 300, 4000, 300, 300);
80    }
81   
 
82  1 toggle @Test
83    public void alsoWorksWithOblongImages() throws Exception {
84  1 final BufferedImage img = generateArbitraryTestImage(228, 700);
85  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(img, 160, 260, 84, 260);
86    }
87   
 
88  1 toggle @Test
89    public void anotherOblongCase() throws Exception {
90  1 final BufferedImage img = generateArbitraryTestImage(800, 200);
91  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(img, 160, 160, 160, 40);
92    }
93   
 
94  1 toggle @Test(expected = IllegalArgumentException.class)
95    public void whatHappensIfWeOnlySpecifyMaxWidth() throws Exception {
96  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getHorizontalTestImage(), 400, 0, 400, 300);
97    }
98   
 
99  1 toggle @Test
100    public void smallImageNotEnlargedWhileExpandFalse() throws Exception {
101  1 op.setExpand(false);
102  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getSmallTestImage(), 400, 400, 202, 51);
103    }
104   
 
105  1 toggle @Test
106    public void smallImageIsEnlargedWhileExpandTrue() throws Exception {
107  1 op.setExpand(true);
108  1 doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(getSmallTestImage(), 400, 400, 400, 100);
109    }
110   
 
111  12 toggle private void doTestKeepsOriginalRatioAndCompliesToSmallestMaximumDimension(final BufferedImage src, final int maxWidth, final int maxHeight, final int expectedWidth, final int expectedHeight) throws Exception {
112  12 op.setMaxWidth(maxWidth);
113  12 op.setMaxHeight(maxHeight);
114   
115  12 final BufferedImage res = op.apply(src, null);
116  11 write(String.format("%dx%x-to-%dx%d", maxWidth, maxHeight, expectedWidth, expectedHeight), res, BASIC_JPEG);
117   
118  11 assertEquals(expectedWidth, res.getWidth());
119  11 assertEquals(expectedHeight, res.getHeight());
120    }
121    }