1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.platform.svg.script

File SVGScriptServiceTest.java

 

Code metrics

0
27
13
1
165
115
13
0.48
2.08
13
1

Classes

Class Line # Actions
SVGScriptServiceTest 48 27 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 12 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.script;
21   
22    import java.io.IOException;
23    import java.lang.reflect.Type;
24   
25    import org.junit.Assert;
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.Mock;
30    import org.mockito.Mockito;
31    import org.mockito.MockitoAnnotations;
32    import org.xwiki.component.util.DefaultParameterizedType;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.platform.svg.SVGRasterizer;
35    import org.xwiki.resource.ResourceReferenceSerializer;
36    import org.xwiki.resource.temporary.TemporaryResourceReference;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38    import org.xwiki.url.ExtendedURL;
39   
40    import static org.mockito.Mockito.when;
41   
42    /**
43    * Tests for the {@link SVGScriptService} component.
44    *
45    * @version $Id: 567925c35e888118458622ae64824f561b0d66ac $
46    * @since 8.0M1
47    */
 
48    public class SVGScriptServiceTest
49    {
50    private static final String SVG = "<svg>";
51   
52    @Rule
53    public final MockitoComponentMockingRule<SVGScriptService> mocker =
54    new MockitoComponentMockingRule<>(SVGScriptService.class);
55   
56    @Mock
57    private TemporaryResourceReference tref;
58   
59    @Mock
60    private DocumentReference dref;
61   
62    @Mock
63    private ExtendedURL eurl;
64   
65    private SVGRasterizer internal;
66   
67    private ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> serializer;
68   
 
69  12 toggle @Before
70    public void setup() throws Exception
71    {
72  12 MockitoAnnotations.initMocks(this);
73   
74  12 this.internal = this.mocker.getInstance(SVGRasterizer.class);
75  12 when(this.internal.rasterizeToTemporaryResource(SVG, 0, 0)).thenReturn(this.tref);
76  12 when(this.internal.rasterizeToTemporaryResource(SVG, 100, 200)).thenReturn(this.tref);
77  12 when(this.internal.rasterizeToTemporaryResource(SVG, 0, 0, this.dref)).thenReturn(this.tref);
78  12 when(this.internal.rasterizeToTemporaryResource(SVG, 100, 200, this.dref)).thenReturn(this.tref);
79   
80  12 Type stype = new DefaultParameterizedType(null, ResourceReferenceSerializer.class,
81    TemporaryResourceReference.class, ExtendedURL.class);
82  12 this.serializer = this.mocker.getInstance(stype, "standard/tmp");
83  12 when(this.serializer.serialize(this.tref)).thenReturn(this.eurl);
84    }
85   
 
86  1 toggle @Test
87    public void rasterizeToTemporaryResource1PForwardsCallsAndNormalizesResult() throws Exception
88    {
89  1 Assert.assertSame(this.eurl, this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG));
90    }
91   
 
92  1 toggle @Test
93    public void rasterizeToTemporaryResource1PCatchesExceptionsAndReturnsNull() throws Exception
94    {
95  1 when(this.internal.rasterizeToTemporaryResource(SVG, 0, 0)).thenThrow(new NullPointerException());
96  1 Assert.assertNull(this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG));
97    }
98   
 
99  1 toggle @Test
100    public void rasterizeToTemporaryResource2PForwardsCallsAndNormalizesResult() throws Exception
101    {
102  1 Assert.assertSame(this.eurl, this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, this.dref));
103    }
104   
 
105  1 toggle @Test
106    public void rasterizeToTemporaryResource2PCatchesExceptionsAndReturnsNull() throws Exception
107    {
108  1 when(this.internal.rasterizeToTemporaryResource(SVG, 0, 0, this.dref)).thenThrow(new NullPointerException());
109  1 Assert.assertNull(this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, this.dref));
110    }
111   
 
112  1 toggle @Test
113    public void rasterizeToTemporaryResource3PForwardsCallsAndNormalizesResult() throws Exception
114    {
115  1 Assert.assertSame(this.eurl, this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, 100, 200));
116    }
117   
 
118  1 toggle @Test
119    public void rasterizeToTemporaryResource3PCatchesExceptionsAndReturnsNull() throws Exception
120    {
121  1 when(this.internal.rasterizeToTemporaryResource(SVG, 100, 200)).thenThrow(new NullPointerException());
122  1 Assert.assertNull(this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, 100, 200));
123    }
124   
 
125  1 toggle @Test
126    public void rasterizeToTemporaryResource4PForwardsCallsAndNormalizesResult() throws Exception
127    {
128  1 Assert.assertSame(this.eurl,
129    this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, 100, 200, this.dref));
130    }
131   
 
132  1 toggle @Test
133    public void rasterizeToTemporaryResource4PCatchesExceptionsAndReturnsNull() throws Exception
134    {
135  1 when(this.internal.rasterizeToTemporaryResource(SVG, 100, 200, this.dref))
136    .thenThrow(new NullPointerException());
137  1 Assert.assertNull(this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(SVG, 100, 200, this.dref));
138    }
139   
 
140  1 toggle @Test
141    public void rasterizeToResponseForwardsCalls() throws Exception
142    {
143  1 Assert.assertTrue(this.mocker.getComponentUnderTest().rasterizeToResponse(SVG));
144    }
145   
 
146  1 toggle @Test
147    public void rasterizeToResponseCatchesExceptions() throws Exception
148    {
149  1 Mockito.doThrow(new IOException()).when(this.internal).rasterizeToResponse(SVG, 0, 0);
150  1 Assert.assertFalse(this.mocker.getComponentUnderTest().rasterizeToResponse(SVG));
151    }
152   
 
153  1 toggle @Test
154    public void rasterizeToResponseWithSizeForwardsCalls() throws Exception
155    {
156  1 Assert.assertTrue(this.mocker.getComponentUnderTest().rasterizeToResponse(SVG, 100, 200));
157    }
158   
 
159  1 toggle @Test
160    public void rasterizeToResponseWithSizeCatchesExceptions() throws Exception
161    {
162  1 Mockito.doThrow(new IOException()).when(this.internal).rasterizeToResponse(SVG, 100, 200);
163  1 Assert.assertFalse(this.mocker.getComponentUnderTest().rasterizeToResponse(SVG, 100, 200));
164    }
165    }