1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.image

File ImagePluginTest.java

 

Code metrics

0
25
6
1
132
96
6
0.24
4.17
6
1

Classes

Class Line # Actions
ImagePluginTest 40 25 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.plugin.image;
21   
22    import java.awt.Image;
23    import java.io.ByteArrayInputStream;
24    import com.xpn.xwiki.XWiki;
25    import com.xpn.xwiki.XWikiContext;
26    import com.xpn.xwiki.doc.XWikiAttachment;
27    import com.xpn.xwiki.doc.XWikiAttachmentContent;
28    import com.xpn.xwiki.plugin.image.ImageProcessor;
29    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
30    import com.xpn.xwiki.web.XWikiServletRequest;
31    import com.xpn.xwiki.web.Utils;
32    import org.apache.commons.codec.binary.Base64;
33    import org.jmock.Mock;
34   
35    /**
36    * Unit tests for the {@link com.xpn.xwiki.plugin.image.ImagePlugin} class.
37    *
38    * @version $Id: 788eef33d1cbfbc20c2d6aee5b97eaceb685f59c $
39    */
 
40    public class ImagePluginTest extends AbstractBridgedXWikiComponentTestCase
41    {
42    private static final byte[] testPngImageContent = Base64.decodeBase64(
43    "iVBORw0KGgoAAAANSUhEUgAAAJYAAAA8CAMAAACzWLNYAAACZFBMVEXUVQD////+"
44    + "/fz//v7UVgLVWQbUVgH+/PvWXg3XXw/XYBD+/PrcdC7WXQz89fD78Oj44tT77+fi"
45    + "jlXWXg799vHpqn/XYBHdejfWWwn77uXefj3ZaB3ccy3abSTVVwP89O/++/nqrIPW"
46    + "XAvWXArvwqPZah/eeznYZBb9+PT33s7bcCj34NHvv5/noHD99vLpqHzpp3v338/d"
47    + "ejjijFLZaR7fgEHyzbTxya7klmHjkFjijVTuvp322sfZaiDz0Ln00rznnm7cdTDu"
48    + "vJr66+H34NDjkFnfgkTXYhTopXjVWATpqX7bciv12MT23czdeTb++vfVWgfYZBfa"
49    + "bCPaayH89O7deDX9+PXtuJTyzLLVWAXjkVrxxqrcdC/12cbXYRL++vj88+3VWgjv"
50    + "vp7cdjL34dLfgULvwKHqrIL559v01L/00rv67OPlmmfghEfll2P67eTYZhn56N3o"
51    + "pHbww6XppnrtuZb66t/YYxX77ubcdjHyyq/qroXstI7gg0Xz0LjwxKfuvJv44tPZ"
52    + "aBzuvZzqqoDghEbkk13ghUjjjlbllmLhiE3opnn22sjklWDnn2/ZZxvrsYrssozj"
53    + "j1fzz7f12MXuupjXYhPstI/z0brmnWz88uvefTzdeDTlmWbuu5nii1Hrrobefj7m"
54    + "nGvfgEDhh0v56NzhilDrsIjwxqnwwqT228nbciz55trqq4HbcSr99/P9+fboo3Xy"
55    + "y7H23Mr45NbbbyfklF/45djhiU7YZRj//v3007378OnabiXlmGTnoXLtt5PyzLPe"
56    + "fDr88uztuJXxx6v01cDstpHhiEzbcCnfgkPttpJaT6BrAAAD6klEQVR4Xu2YY5Ms"
57    + "SxCG6xmvbdt7bNu2bV3btm3btn3/1O2s7p7G7n6ZihNx4kZXREa+uTk5+0RXdmFU"
58    + "NCKLLLLIIovsf2Gp1AWJdVNx/wXDsvfF9a4s4wFXvjrxgLhkd7WvoPrZHb6ofa24"
59    + "VHeR/TVXN/sBmhq1q61J5kZVuJWyQkeXMOSoF+Cg+IOsTmQLEhO40SvMlFFluU72"
60    + "SFR0BZf5AGYWMFP8GhbmhjUeOHdLCKsKiHVY4jPw5vUIsDEb3Qnb9RMmz3K7YbYH"
61    + "UJ5GP/g8KMsN6x6AgYoA1v0Aj4naBUu81gPGZ6N74SFdQ77lFsB9Aao+gc2HEpXb"
62    + "WAhwcoevt5YWAMMLvP9tj+RmoFRLl3h0rLuEaq8yw1rxKMCgyIdLpMsaK4H0GXem"
63    + "5gWeK41u+Dr0B7GCVIZYqqgP4DY3fPAUwFk72AD73cSlsA6udaKKOL2J0bAeiWkq"
64    + "Yyx10SKA6XaQWQxwzM3VwWRbdfTCEFzsJF6W7gtiBajMsdSqYiB9vZ7SuQBHs6mp"
65    + "8IbzFGDCimegxg6HYNsoWDM8KmMsdUQ3uTTWLIB53qrZBu/b6hq4RF0OT9rhUTg7"
66    + "EmuJj8ocS92BvXytBLg5z0tMgu1a5O0j3qXq4aqEjg/DoRBWmMocS40DOHwaoK7B"
67    + "l9gE57SYrnspdQqmSHggTUEyjLUzRGWOpW4FiAPDuwOJFrhS/FPwtKwh2NvJFlis"
68    + "QlgulTnWnH5rPKF7/QYASN+uE12SWGaJEyB/6YkR22D552C/dN7zMBjCqnSozLGu"
69    + "A6C4S3SmFIC7deIlACqb9EeWW9lX4KTOHIfXLPc4zA9hEaQyx2oYgfUmAMMb9cvw"
70    + "lpVd7WaOwduWewfqzxtWoknmqig4iV5CNvB34T2lJsdpzdclH8CHtar5I+IZg0k0"
71    + "bvnmdQLwMXziFAxAuVoFx5VBy5stEGKfynT1wQwnXg7TZGp3KWWwQJgtp05zfw69"
72    + "7e7LC/sq7BfBYDk12nzETsNgFb7j8mz44gScEW2w+Rhs1WJrYeBL+CpbMB/mFkOP"
73    + "aIOt2uBgI1YbIw4t3g3m62+IQ51Ig4ON0TFQrBRglq/gW4CponI4BuZwaN723feh"
74    + "Q7NYJ8BSX8UPAG2iDA7NRlcMsR+BEv/dtFryk0SdvytG/RgXMu+OMwX4KVDzM7DJ"
75    + "lmNfyPKNsFaOdX1t7fD6ll8CNTuhxZFjX1/HGV1f52zl15GX/d/i/J4tmMYfqUBN"
76    + "eyl/ej2wRnfoItr8l/3WeLn4PXSq3EbDXz2j/DTy9z++V3ViJlSzrGa9K5M1tdpX"
77    + "bwl+otAu7U4p4/Hv5kPqQhwJx0cWWWSRRRZZZNH4DzmZwO7NW2cKAAAAAElFTkSu"
78    + "QmCC");
79   
80    private ImagePlugin plugin;
81   
 
82  2 toggle @Override
83    protected void setUp() throws Exception
84    {
85  2 super.setUp();
86   
87  2 Mock mockXWiki = mock(XWiki.class);
88  2 mockXWiki.stubs().method("Param").will(returnValue("10"));
89  2 getContext().setWiki((XWiki) mockXWiki.proxy());
90  2 this.plugin = new ImagePlugin("image", ImagePlugin.class.getName(), getContext());
91    }
92   
 
93  1 toggle public void testDownloadAttachmentWithUnsupportedFileType()
94    {
95  1 Mock attachmentMock = mock(XWikiAttachment.class);
96  1 attachmentMock.stubs().method("getMimeType").will(returnValue("image/notsupported"));
97  1 XWikiAttachment attachment = (XWikiAttachment) attachmentMock.proxy();
98  1 assertSame(attachment, plugin.downloadAttachment(attachment, new XWikiContext()));
99    }
100   
 
101  1 toggle public void testCacheOfScaledAttachment() throws Exception
102    {
103  1 XWikiAttachment attach = (new XWikiAttachment() {
 
104  1 toggle @Override
105    public XWikiAttachment clone() {
106  1 XWikiAttachment out = new XWikiAttachment();
107  1 out.setAttachment_content(new XWikiAttachmentContent(this.getAttachment_content()));
108  1 return out;
109    }
 
110  3 toggle @Override
111  3 public String getMimeType(XWikiContext context) { return "image/png"; }
112    });
113  1 attach.setContent(new ByteArrayInputStream(testPngImageContent));
114  1 XWikiServletRequest req = (new XWikiServletRequest(null) {
 
115  8 toggle @Override
116  8 public String getParameter(String prop) { return "30"; }
117    });
118  1 this.getContext().setRequest(req);
119   
120  1 final XWikiAttachment scaled = plugin.downloadAttachment(attach, this.getContext());
121   
122    // Check that the width is indeed 30
123  1 ImageProcessor imageProcessor = Utils.getComponent(ImageProcessor.class);
124  1 Image scaledImage = imageProcessor.readImage(scaled.getContentInputStream(this.getContext()));
125  1 assertEquals(30, scaledImage.getWidth(null));
126   
127    // Load the scaled attachment again and make sure it's the same object in memory.
128  1 final XWikiAttachment cached = plugin.downloadAttachment(attach, this.getContext());
129  1 assertTrue(scaled == cached);
130  1 assertTrue(scaled.getAttachment_content() == cached.getAttachment_content());
131    }
132    }