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

File ImageFilterTest.java

 

Code metrics

0
32
5
1
144
97
5
0.16
6.4
5
1

Classes

Class Line # Actions
ImageFilterTest 52 32 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 4 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.officeimporter.internal.filter;
21   
22    import java.nio.charset.Charset;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.HashSet;
27    import java.util.Map;
28   
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.w3c.dom.Document;
32    import org.xwiki.bridge.DocumentAccessBridge;
33    import org.xwiki.model.reference.AttachmentReference;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.DocumentReferenceResolver;
36    import org.xwiki.rendering.listener.reference.ResourceReference;
37    import org.xwiki.rendering.listener.reference.ResourceType;
38    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
39    import org.xwiki.test.mockito.MockitoComponentMockingRule;
40    import org.xwiki.xml.html.filter.HTMLFilter;
41   
42    import com.github.ooxi.jdatauri.DataUri;
43   
44    import static org.junit.Assert.*;
45    import static org.mockito.Mockito.*;
46   
47    /**
48    * Unit tests for {@link ImageFilter}.
49    *
50    * @version $Id: 50a8aa2070335ef4fad36a168a5e21e3e9073e82 $
51    */
 
52    public class ImageFilterTest extends AbstractHTMLFilterTest
53    {
54    @Rule
55    public MockitoComponentMockingRule<HTMLFilter> mocker =
56    new MockitoComponentMockingRule<HTMLFilter>(ImageFilter.class);
57   
58    private DocumentAccessBridge dab;
59   
60    private ResourceReferenceSerializer xhtmlMarkerSerializer;
61   
62    private DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path.To"), "Page");
63   
 
64  4 toggle @Override
65    public void configure() throws Exception
66    {
67  4 super.configure();
68   
69  4 this.filter = this.mocker.getComponentUnderTest();
70  4 this.dab = this.mocker.getInstance(DocumentAccessBridge.class);
71  4 this.xhtmlMarkerSerializer = this.mocker.getInstance(ResourceReferenceSerializer.class, "xhtmlmarker");
72   
73  4 DocumentReferenceResolver<String> currentMixedResolver =
74    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
75  4 when(currentMixedResolver.resolve("Path.To.Page")).thenReturn(this.documentReference);
76    }
77   
 
78  1 toggle @Test
79    public void filterRemovesAlignAttribute()
80    {
81  1 filterAndAssertOutput("<img align=\"center\"/>", "<img/>");
82    }
83   
 
84  1 toggle @Test
85    public void filterAddsImageMarkers()
86    {
87  1 AttachmentReference attachmentReference = new AttachmentReference("-foo--bar.png-", this.documentReference);
88  1 when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
89   
90  1 ResourceReference resourceReference = new ResourceReference("-foo--bar.png-", ResourceType.ATTACHMENT);
91  1 resourceReference.setTyped(false);
92  1 when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|-foo--bar.png-");
93   
94  1 filterAndAssertOutput("<img src=\"../../some/path/-foo--b%61r.png-\"/>",
95    Collections.singletonMap("targetDocument", "Path.To.Page"),
96    "<!--startimage:false|-|attach|-|-foo-\\-bar.png-\\--><img src=\"/path/to/foo.png\"/><!--stopimage-->");
97    }
98   
 
99  1 toggle @Test
100    public void filterIgnoresAbsoluteURLs()
101    {
102  1 filterAndAssertOutput("<img src=\"http://server/path/to/image.png\"/>",
103    Collections.singletonMap("targetDocument", "Path.To.Page"),
104    "<img src=\"http://server/path/to/image.png\"/>");
105  1 filterAndAssertOutput("<img src=\"file://path/to/image.png\"/>",
106    Collections.singletonMap("targetDocument", "Path.To.Page"), "<img src=\"file://path/to/image.png\"/>");
107    }
108   
 
109  1 toggle @Test
110    public void filterCollectsEmbeddedImages()
111    {
112  1 AttachmentReference attachmentReference = new AttachmentReference("foo.png", this.documentReference);
113  1 when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
114   
115  1 ResourceReference resourceReference = new ResourceReference("foo.png", ResourceType.ATTACHMENT);
116  1 resourceReference.setTyped(false);
117  1 when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|foo.png");
118   
119  1 String fileName =
120    DataUri.parse("data:image/jpeg;base64,GgoAAAAN==", Charset.forName("UTF-8")).hashCode() + ".jpg";
121  1 attachmentReference = new AttachmentReference(fileName, this.documentReference);
122  1 when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/" + fileName);
123   
124  1 resourceReference = new ResourceReference(fileName, ResourceType.ATTACHMENT);
125  1 resourceReference.setTyped(false);
126  1 when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|" + fileName);
127   
128  1 Map<String, String> parameters = new HashMap<String, String>();
129  1 parameters.put("targetDocument", "Path.To.Page");
130  1 parameters.put("attachEmbeddedImages", "true");
131   
132  1 Document document = filterAndAssertOutput(
133    "<img src=\"data:image/png;fileName=foo.png;base64,iVBORw0K==\"/>"
134    + "<img src=\"data:image/jpeg;base64,GgoAAAAN==\"/>",
135    parameters,
136    "<!--startimage:false|-|attach|-|foo.png--><img src=\"/path/to/foo.png\"/><!--stopimage-->"
137    + "<!--startimage:false|-|attach|-|" + fileName + "--><img src=\"/path/to/" + fileName
138    + "\"/><!--stopimage-->");
139   
140  1 @SuppressWarnings("unchecked")
141    Map<String, byte[]> embeddedImages = (Map<String, byte[]>) document.getUserData("embeddedImages");
142  1 assertEquals(new HashSet<String>(Arrays.asList("foo.png", fileName)), embeddedImages.keySet());
143    }
144    }