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