1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
49 |
|
|
50 |
|
@version |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (37) |
Complexity: 5 |
Complexity Density: 0.16 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
64 |
4 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
78 |
1 |
@Test... |
79 |
|
public void filterRemovesAlignAttribute() |
80 |
|
{ |
81 |
1 |
filterAndAssertOutput("<img align=\"center\"/>", "<img/>"); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
84 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
99 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
109 |
1 |
@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 |
|
} |