1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.svg.internal

File BatikSVGRasterizerTest.java

 

Code metrics

0
99
18
2
313
242
28
0.28
5.5
9
1.56

Classes

Class Line # Actions
BatikSVGRasterizerTest 62 98 0% 27 4
0.965217496.5%
BatikSVGRasterizerTest.CapturingOutputStream 303 1 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 13 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 org.xwiki.platform.svg.internal;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.io.File;
24    import java.io.FileInputStream;
25    import java.io.FileOutputStream;
26    import java.io.IOException;
27    import java.io.InputStream;
28    import java.io.OutputStream;
29    import java.util.Arrays;
30   
31    import javax.servlet.ServletOutputStream;
32    import javax.servlet.http.HttpServletResponse;
33   
34    import org.apache.commons.io.FileUtils;
35    import org.apache.commons.io.IOUtils;
36    import org.junit.Assert;
37    import org.junit.Before;
38    import org.junit.Rule;
39    import org.junit.Test;
40    import org.junit.rules.TemporaryFolder;
41    import org.mockito.Mock;
42    import org.mockito.Mockito;
43    import org.mockito.MockitoAnnotations;
44    import org.xwiki.container.Container;
45    import org.xwiki.container.Response;
46    import org.xwiki.container.servlet.ServletResponse;
47    import org.xwiki.model.reference.DocumentReference;
48    import org.xwiki.model.reference.DocumentReferenceResolver;
49    import org.xwiki.platform.svg.SVGRasterizer;
50    import org.xwiki.resource.temporary.TemporaryResourceReference;
51    import org.xwiki.resource.temporary.TemporaryResourceStore;
52    import org.xwiki.test.mockito.MockitoComponentMockingRule;
53   
54    import static org.mockito.Mockito.*;
55   
56    /**
57    * Tests for the {@link BatikSVGRasterizer} component.
58    *
59    * @version $Id: 042c123e060f62f10f74fec1eac436d4107a4d66 $
60    * @since 8.0M1
61    */
 
62    public class BatikSVGRasterizerTest
63    {
64    private static final String VALID_SVG =
65    "<svg xmlns='http://www.w3.org/2000/svg'><ellipse cx='50' cy='100' rx='25' ry='50'/></svg>";
66   
67    private static final String INVALID_SVG = "<bad>svg!";
68   
69    private static final String RASTER_FILE_NAME = Math.abs(VALID_SVG.hashCode()) + ".png";
70   
71    @Rule
72    public final MockitoComponentMockingRule<SVGRasterizer> mocker =
73    new MockitoComponentMockingRule<SVGRasterizer>(BatikSVGRasterizer.class);
74   
75    @Rule
76    public final TemporaryFolder baseDirectory = new TemporaryFolder();
77   
78    private DocumentReference dref = new DocumentReference("wiki", "Space", "Document");
79   
80    private TemporaryResourceStore temporaryResourceStore;
81   
82    private DocumentReferenceResolver<String> resolver;
83   
84    private Container container;
85   
86    @Mock
87    private ServletResponse sresponse;
88   
89    @Mock
90    private HttpServletResponse hsresponse;
91   
92    private File rasterFile;
93   
94    private File temporaryFile;
95   
96    private String temporaryFilePath;
97   
 
98  13 toggle @Before
99    public void setup() throws Exception
100    {
101  13 MockitoAnnotations.initMocks(this);
102  13 this.rasterFile = new File(this.baseDirectory.getRoot() + "/temp/svg/wiki/Space/Document/" + RASTER_FILE_NAME);
103  13 this.temporaryFile = new File(this.baseDirectory.getRoot() + "/temp/svg/" + RASTER_FILE_NAME);
104  13 this.temporaryFilePath = this.temporaryFile.getAbsolutePath();
105   
106  13 this.temporaryResourceStore = this.mocker.getInstance(TemporaryResourceStore.class);
107   
108  13 TemporaryResourceReference rasterFileReferece = new TemporaryResourceReference("svg", RASTER_FILE_NAME, dref);
109  13 when(this.temporaryResourceStore.getTemporaryFile(rasterFileReferece)).thenReturn(rasterFile);
110   
111  13 TemporaryResourceReference temporaryFileReferece =
112    new TemporaryResourceReference("svg", RASTER_FILE_NAME, null);
113  13 when(this.temporaryResourceStore.getTemporaryFile(temporaryFileReferece)).thenReturn(temporaryFile);
114   
115  13 String invalidRasterFileName = Math.abs(INVALID_SVG.hashCode()) + ".png";
116  13 File invalidRasterFile =
117    new File(this.baseDirectory.getRoot() + "/temp/svg/wiki/Space/Document/" + invalidRasterFileName);
118  13 File invalidTemporaryFile = new File(this.baseDirectory.getRoot() + "/temp/svg/" + invalidRasterFileName);
119   
120  13 TemporaryResourceReference invalidRasterFileReferece =
121    new TemporaryResourceReference("svg", invalidRasterFileName, dref);
122  13 when(this.temporaryResourceStore.getTemporaryFile(invalidRasterFileReferece)).thenReturn(invalidRasterFile);
123   
124  13 TemporaryResourceReference invalidTemporaryFileReferece =
125    new TemporaryResourceReference("svg", invalidRasterFileName, null);
126  13 when(this.temporaryResourceStore.getTemporaryFile(invalidTemporaryFileReferece))
127    .thenReturn(invalidTemporaryFile);
128   
129  13 this.resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
130  13 when(this.resolver.resolve("")).thenReturn(this.dref);
131   
132  13 this.container = this.mocker.getInstance(Container.class);
133  13 when(this.container.getResponse()).thenReturn(this.sresponse);
134  13 when(this.sresponse.getHttpServletResponse()).thenReturn(this.hsresponse);
135    }
136   
 
137  1 toggle @Test
138    public void rasterizeToTemporaryFileCreatesTemporaryFile() throws Exception
139    {
140  1 File tfile = this.mocker.getComponentUnderTest().rasterizeToTemporaryFile(VALID_SVG, 100, 200);
141  1 Assert.assertEquals(this.temporaryFilePath, tfile.getAbsolutePath());
142  1 Assert.assertTrue(tfile.exists());
143  1 Assert.assertTrue(isPNG(tfile));
144    }
145   
 
146  1 toggle @Test
147    public void rasterizeToTemporaryFileReusesFile() throws Exception
148    {
149  1 writeTestFile(this.temporaryFile);
150  1 File tfile = this.mocker.getComponentUnderTest().rasterizeToTemporaryFile(VALID_SVG, 100, 200);
151  1 Assert.assertEquals(this.temporaryFilePath, tfile.getAbsolutePath());
152  1 Assert.assertTrue(tfile.exists());
153  1 Assert.assertTrue(isTestFile(tfile));
154    }
155   
 
156  1 toggle @Test
157    public void rasterizeToTemporaryFileReturnsNullOnExceptions() throws Exception
158    {
159  1 File tfile = this.mocker.getComponentUnderTest().rasterizeToTemporaryFile(INVALID_SVG, 0, 0);
160  1 Assert.assertNull(tfile);
161    }
162   
 
163  1 toggle @Test
164    public void rasterizeToTemporaryFileReturnsNullWhenParentFolderCannotBeCreated() throws Exception
165    {
166  1 this.baseDirectory.getRoot().mkdirs();
167  1 writeTestFile(new File(this.baseDirectory.getRoot(), "temp"));
168  1 File tfile = this.mocker.getComponentUnderTest().rasterizeToTemporaryFile(VALID_SVG, 100, 200);
169  1 Assert.assertNull(tfile);
170    }
171   
 
172  1 toggle @Test
173    public void rasterizeToTemporaryFileThrowsExceptionWhenFileCannotBeCreated() throws Exception
174    {
175  1 this.temporaryFile.mkdirs();
176  1 try {
177  1 this.mocker.getComponentUnderTest().rasterizeToTemporaryFile(VALID_SVG, 100, 200);
178  0 Assert.fail();
179    } catch (IOException e) {
180    // Cannot write temporary file because it's a directory.
181    }
182    }
183   
 
184  1 toggle @Test
185    public void rasterizeToTemporaryResourceUsesContextDocument() throws Exception
186    {
187  1 TemporaryResourceReference tref =
188    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
189  1 Assert.assertEquals("svg", tref.getModuleId());
190  1 Assert.assertTrue(tref.getParameters().isEmpty());
191  1 Assert.assertEquals(this.dref, tref.getOwningEntityReference());
192  1 Assert.assertEquals(RASTER_FILE_NAME, tref.getResourceName());
193    }
194   
 
195  1 toggle @Test
196    public void rasterizeToTemporaryResourceReusesFile() throws Exception
197    {
198  1 writeTestFile(this.rasterFile);
199  1 TemporaryResourceReference tref =
200    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
201  1 Assert.assertEquals("svg", tref.getModuleId());
202  1 Assert.assertTrue(tref.getParameters().isEmpty());
203  1 Assert.assertEquals(this.dref, tref.getOwningEntityReference());
204  1 Assert.assertEquals(Math.abs(VALID_SVG.hashCode()) + ".png", tref.getResourceName());
205  1 Assert.assertTrue(isTestFile(this.rasterFile));
206    }
207   
 
208  1 toggle @Test
209    public void rasterizeToTemporaryResourceReturnsNullOnExceptions() throws Exception
210    {
211  1 TemporaryResourceReference tref =
212    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(INVALID_SVG, 0, 0);
213  1 Assert.assertNull(tref);
214    }
215   
 
216  1 toggle @Test
217    public void rasterizeToTemporaryResourceReturnsNullWhenBaseTempDirCannotBeCreated() throws Exception
218    {
219  1 this.baseDirectory.getRoot().mkdirs();
220  1 writeTestFile(new File(this.baseDirectory.getRoot(), "temp"));
221  1 TemporaryResourceReference tref =
222    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
223  1 Assert.assertNull(tref);
224    }
225   
 
226  1 toggle @Test
227    public void rasterizeToTemporaryResourceReturnsNullWhenContextTempDirCannotBeCreated() throws Exception
228    {
229  1 this.baseDirectory.getRoot().mkdirs();
230  1 writeTestFile(
231    new File(new File(new File(new File(this.baseDirectory.getRoot(), "temp"), "svg"), "wiki"), "Space"));
232  1 TemporaryResourceReference tref =
233    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
234  1 Assert.assertNull(tref);
235    }
236   
 
237  1 toggle @Test
238    public void rasterizeToResponseWritesImageToServletOutputStream() throws Exception
239    {
240  1 CapturingOutputStream out = new CapturingOutputStream();
241  1 when(this.hsresponse.getOutputStream()).thenReturn(out);
242  1 this.mocker.getComponentUnderTest().rasterizeToResponse(VALID_SVG, 100, 200);
243  1 Assert.assertTrue(out.out.size() > 0);
244  1 Assert.assertArrayEquals("PNG".getBytes("UTF-8"), Arrays.copyOfRange(out.out.toByteArray(), 1, 4));
245    }
246   
 
247  1 toggle @Test
248    public void rasterizeToResponseDoesNothingIfNotHttpResponse() throws Exception
249    {
250  1 Response r = Mockito.mock(Response.class);
251  1 when(this.container.getResponse()).thenReturn(r);
252  1 this.mocker.getComponentUnderTest().rasterizeToResponse(VALID_SVG, 100, 200);
253  1 Mockito.verifyZeroInteractions(r);
254    }
255   
 
256  1 toggle @Test
257    public void rasterizeToResponseDoesNothingOnExceptions() throws Exception
258    {
259  1 CapturingOutputStream out = new CapturingOutputStream();
260  1 when(this.hsresponse.getOutputStream()).thenReturn(out);
261  1 this.mocker.getComponentUnderTest().rasterizeToResponse(INVALID_SVG, 0, 0);
262  1 Assert.assertEquals(0, out.out.size());
263    }
264   
 
265  1 toggle private boolean isPNG(File file)
266    {
267  1 try (InputStream in = new FileInputStream(file)) {
268  1 byte[] expected = "PNG".getBytes("UTF-8");
269  1 byte[] actual = new byte[3];
270  1 in.read();
271  1 Assert.assertEquals(3, in.read(actual, 0, 3));
272  1 Assert.assertArrayEquals(expected, actual);
273  1 return true;
274    } catch (Exception ex) {
275  0 return false;
276    }
277    }
278   
 
279  2 toggle private boolean isTestFile(File file)
280    {
281  2 try (InputStream in = new FileInputStream(file)) {
282  2 byte[] expected = "test".getBytes("UTF-8");
283  2 byte[] actual = new byte[4];
284  2 Assert.assertEquals(4, in.read(actual, 0, 4));
285  2 Assert.assertArrayEquals(expected, actual);
286  2 return true;
287    } catch (Exception ex) {
288  0 return false;
289    }
290    }
291   
 
292  5 toggle private boolean writeTestFile(File file) throws IOException
293    {
294  5 FileUtils.forceMkdir(file.getParentFile());
295  5 try (OutputStream out = new FileOutputStream(file)) {
296  5 IOUtils.write("test".getBytes(), out);
297  5 return true;
298    } catch (Exception ex) {
299  0 return false;
300    }
301    }
302   
 
303    private class CapturingOutputStream extends ServletOutputStream
304    {
305    private ByteArrayOutputStream out = new ByteArrayOutputStream();
306   
 
307  1319 toggle @Override
308    public void write(int b) throws IOException
309    {
310  1319 this.out.write(b);
311    }
312    }
313    }