1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.filter.input; |
21 |
|
|
22 |
|
import java.io.IOException; |
23 |
|
import java.net.URL; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.Collections; |
26 |
|
import java.util.HashSet; |
27 |
|
import java.util.List; |
28 |
|
import java.util.Set; |
29 |
|
|
30 |
|
import org.apache.commons.io.IOUtils; |
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.junit.Assert; |
33 |
|
import org.junit.Before; |
34 |
|
import org.mockito.invocation.InvocationOnMock; |
35 |
|
import org.mockito.stubbing.Answer; |
36 |
|
import org.xwiki.filter.FilterException; |
37 |
|
import org.xwiki.filter.filterxml.output.FilterXMLOutputProperties; |
38 |
|
import org.xwiki.filter.input.BeanInputFilterStreamFactory; |
39 |
|
import org.xwiki.filter.input.InputFilterStream; |
40 |
|
import org.xwiki.filter.input.InputFilterStreamFactory; |
41 |
|
import org.xwiki.filter.instance.input.InstanceInputProperties; |
42 |
|
import org.xwiki.filter.instance.internal.InstanceModel; |
43 |
|
import org.xwiki.filter.instance.output.InstanceOutputProperties; |
44 |
|
import org.xwiki.filter.output.BeanOutputFilterStreamFactory; |
45 |
|
import org.xwiki.filter.output.OutputFilterStream; |
46 |
|
import org.xwiki.filter.output.OutputFilterStreamFactory; |
47 |
|
import org.xwiki.filter.output.StringWriterOutputTarget; |
48 |
|
import org.xwiki.filter.type.FilterStreamType; |
49 |
|
import org.xwiki.model.reference.DocumentReference; |
50 |
|
import org.xwiki.model.reference.EntityReferenceTree; |
51 |
|
import org.xwiki.model.reference.EntityReferenceTreeNode; |
52 |
|
import org.xwiki.model.reference.SpaceReference; |
53 |
|
import org.xwiki.model.reference.WikiReference; |
54 |
|
import org.xwiki.test.annotation.AfterComponent; |
55 |
|
import org.xwiki.test.annotation.AllComponents; |
56 |
|
|
57 |
|
import com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest; |
58 |
|
|
59 |
|
import static org.mockito.ArgumentMatchers.any; |
60 |
|
import static org.mockito.Mockito.when; |
61 |
|
|
62 |
|
|
63 |
|
@link |
64 |
|
|
65 |
|
@version |
66 |
|
|
67 |
|
@AllComponents |
|
|
| 93% |
Uncovered Elements: 4 (57) |
Complexity: 10 |
Complexity Density: 0.23 |
|
68 |
|
public class AbstractInstanceInputFilterStreamTest extends AbstractInstanceFilterStreamTest |
69 |
|
{ |
70 |
|
protected BeanOutputFilterStreamFactory<FilterXMLOutputProperties> xmlOutputFilterStreamFactory; |
71 |
|
|
72 |
|
protected BeanInputFilterStreamFactory<InstanceInputProperties> inputFilterStreamFactory; |
73 |
|
|
74 |
|
protected InstanceModel instanceModelMock; |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
76 |
1 |
@Before... |
77 |
|
@Override |
78 |
|
public void before() throws Exception |
79 |
|
{ |
80 |
1 |
super.before(); |
81 |
|
|
82 |
1 |
this.xmlOutputFilterStreamFactory = |
83 |
|
this.oldcore.getMocker().getInstance(OutputFilterStreamFactory.class, |
84 |
|
FilterStreamType.FILTER_XML.serialize()); |
85 |
1 |
this.inputFilterStreamFactory = |
86 |
|
this.oldcore.getMocker().getInstance(InputFilterStreamFactory.class, |
87 |
|
FilterStreamType.XWIKI_INSTANCE.serialize()); |
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
1 |
when(this.instanceModelMock.getWikiReferences()).thenAnswer(new Answer<List<WikiReference>>() |
93 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
94 |
1 |
@Override... |
95 |
|
public List<WikiReference> answer(InvocationOnMock invocation) throws Throwable |
96 |
|
{ |
97 |
1 |
Set<WikiReference> wikis = new HashSet<WikiReference>(); |
98 |
|
|
99 |
1 |
for (DocumentReference reference : oldcore.getDocuments().keySet()) { |
100 |
3 |
wikis.add(reference.getWikiReference()); |
101 |
|
} |
102 |
|
|
103 |
1 |
List<WikiReference> list = new ArrayList<WikiReference>(wikis); |
104 |
1 |
Collections.sort(list); |
105 |
|
|
106 |
1 |
return list; |
107 |
|
} |
108 |
|
}); |
109 |
|
|
110 |
|
|
111 |
1 |
when(this.instanceModelMock.getSpaceReferences(any(WikiReference.class))).thenAnswer( |
112 |
|
new Answer<EntityReferenceTreeNode>() |
113 |
|
{ |
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
114 |
1 |
@Override... |
115 |
|
public EntityReferenceTreeNode answer(InvocationOnMock invocation) throws Throwable |
116 |
|
{ |
117 |
1 |
WikiReference wiki = (WikiReference) invocation.getArguments()[0]; |
118 |
|
|
119 |
1 |
Set<SpaceReference> spaces = new HashSet<SpaceReference>(); |
120 |
|
|
121 |
1 |
for (DocumentReference reference : oldcore.getDocuments().keySet()) { |
122 |
3 |
if (reference.getWikiReference().equals(wiki)) { |
123 |
3 |
spaces.add(reference.getLastSpaceReference()); |
124 |
|
} |
125 |
|
} |
126 |
|
|
127 |
1 |
return new EntityReferenceTree(spaces).getChildren().iterator().next(); |
128 |
|
} |
129 |
|
}); |
130 |
|
|
131 |
|
|
132 |
1 |
when(this.instanceModelMock.getDocumentReferences(any(SpaceReference.class))).thenAnswer( |
133 |
|
new Answer<List<DocumentReference>>() |
134 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
135 |
3 |
@Override... |
136 |
|
public List<DocumentReference> answer(InvocationOnMock invocation) throws Throwable |
137 |
|
{ |
138 |
3 |
SpaceReference space = (SpaceReference) invocation.getArguments()[0]; |
139 |
|
|
140 |
3 |
Set<DocumentReference> docs = new HashSet<DocumentReference>(); |
141 |
|
|
142 |
3 |
for (DocumentReference reference : oldcore.getDocuments().keySet()) { |
143 |
9 |
if (reference.getLastSpaceReference().equals(space)) { |
144 |
3 |
docs.add(reference); |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
3 |
List<DocumentReference> list = new ArrayList<DocumentReference>(docs); |
149 |
3 |
Collections.sort(list); |
150 |
3 |
return list; |
151 |
|
} |
152 |
|
}); |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
1 |
@AfterComponent... |
156 |
|
public void afterComponent() throws Exception |
157 |
|
{ |
158 |
1 |
this.instanceModelMock = this.oldcore.getMocker().registerMockComponent(InstanceModel.class); |
159 |
|
} |
160 |
|
|
|
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 2 |
Complexity Density: 0.13 |
|
161 |
1 |
protected void assertXML(String resource, InstanceInputProperties instanceProperties) throws FilterException,... |
162 |
|
IOException |
163 |
|
{ |
164 |
1 |
if (instanceProperties == null) { |
165 |
0 |
instanceProperties = new InstanceInputProperties(); |
166 |
0 |
instanceProperties.setVerbose(false); |
167 |
|
} |
168 |
|
|
169 |
1 |
URL url = getClass().getResource("/filter/" + resource + ".xml"); |
170 |
|
|
171 |
1 |
String expected = IOUtils.toString(url, "UTF-8"); |
172 |
|
|
173 |
1 |
expected = StringUtils.removeStart(expected, "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n\n"); |
174 |
|
|
175 |
1 |
InputFilterStream inputFilterStream = this.inputFilterStreamFactory.createInputFilterStream(instanceProperties); |
176 |
|
|
177 |
1 |
StringWriterOutputTarget writer = new StringWriterOutputTarget(); |
178 |
|
|
179 |
1 |
FilterXMLOutputProperties properties = new FilterXMLOutputProperties(); |
180 |
1 |
properties.setTarget(writer); |
181 |
|
|
182 |
1 |
OutputFilterStream outputFilterStream = this.xmlOutputFilterStreamFactory.createOutputFilterStream(properties); |
183 |
|
|
184 |
1 |
inputFilterStream.read(outputFilterStream.getFilter()); |
185 |
|
|
186 |
1 |
inputFilterStream.close(); |
187 |
1 |
outputFilterStream.close(); |
188 |
|
|
189 |
1 |
Assert.assertEquals(expected, writer.getBuffer().toString()); |
190 |
|
} |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
192 |
1 |
protected void assertXML(String resource, InstanceOutputProperties outputProperties,... |
193 |
|
InstanceInputProperties inputProperties) throws FilterException, IOException |
194 |
|
{ |
195 |
1 |
importFromXML(resource, outputProperties); |
196 |
1 |
assertXML(resource, inputProperties); |
197 |
|
} |
198 |
|
} |