1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.filter.xar; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.IOException; |
24 |
|
import java.net.URL; |
25 |
|
import java.util.Date; |
26 |
|
|
27 |
|
import org.apache.commons.io.IOUtils; |
28 |
|
import org.apache.commons.lang3.StringUtils; |
29 |
|
import org.junit.Assert; |
30 |
|
import org.junit.BeforeClass; |
31 |
|
import org.junit.Rule; |
32 |
|
import org.junit.Test; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.extension.ExtensionId; |
35 |
|
import org.xwiki.extension.test.ExtensionPackager; |
36 |
|
import org.xwiki.filter.FilterException; |
37 |
|
import org.xwiki.filter.event.model.WikiDocumentFilter; |
38 |
|
import org.xwiki.filter.filterxml.output.FilterXMLOutputProperties; |
39 |
|
import org.xwiki.filter.input.BeanInputFilterStreamFactory; |
40 |
|
import org.xwiki.filter.input.DefaultFileInputSource; |
41 |
|
import org.xwiki.filter.input.InputFilterStream; |
42 |
|
import org.xwiki.filter.input.InputFilterStreamFactory; |
43 |
|
import org.xwiki.filter.output.BeanOutputFilterStreamFactory; |
44 |
|
import org.xwiki.filter.output.OutputFilterStream; |
45 |
|
import org.xwiki.filter.output.OutputFilterStreamFactory; |
46 |
|
import org.xwiki.filter.output.StringWriterOutputTarget; |
47 |
|
import org.xwiki.filter.type.FilterStreamType; |
48 |
|
import org.xwiki.filter.xar.input.XARInputProperties; |
49 |
|
import org.xwiki.model.EntityType; |
50 |
|
import org.xwiki.model.reference.EntityReference; |
51 |
|
import org.xwiki.model.reference.EntityReferenceSet; |
52 |
|
import org.xwiki.model.reference.LocalDocumentReference; |
53 |
|
import org.xwiki.test.AllLogRule; |
54 |
|
import org.xwiki.test.annotation.AllComponents; |
55 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
56 |
|
|
57 |
|
import static org.junit.Assert.assertTrue; |
58 |
|
|
59 |
|
@AllComponents |
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 4 |
Complexity Density: 0.13 |
|
60 |
|
public class XARInputFilterStreamTest |
61 |
|
{ |
62 |
|
@Rule |
63 |
|
public MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
64 |
|
|
65 |
|
@Rule |
66 |
|
public AllLogRule allLogRule = new AllLogRule(); |
67 |
|
|
68 |
|
private static final File FOLDER = new File("target/test-" + new Date().getTime()).getAbsoluteFile(); |
69 |
|
|
70 |
|
private static ExtensionPackager extensionPackager; |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
72 |
1 |
@BeforeClass... |
73 |
|
public static void beforeClass() throws Exception |
74 |
|
{ |
75 |
1 |
extensionPackager = new ExtensionPackager(null, FOLDER); |
76 |
1 |
extensionPackager.generateExtensions(); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
79 |
2 |
private void assertXML(String resource, XARInputProperties xarProperties) throws FilterException, IOException,... |
80 |
|
ComponentLookupException |
81 |
|
{ |
82 |
2 |
URL url = getClass().getResource("/xar/" + resource + ".output.xml"); |
83 |
|
|
84 |
2 |
String expected = IOUtils.toString(url, "UTF-8"); |
85 |
|
|
86 |
2 |
expected = StringUtils.removeStart(expected, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n"); |
87 |
|
|
88 |
2 |
BeanInputFilterStreamFactory<XARInputProperties> inputFilterStreamFactory = |
89 |
|
this.mocker.getInstance(InputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize()); |
90 |
2 |
InputFilterStream inputFilterStream = inputFilterStreamFactory.createInputFilterStream(xarProperties); |
91 |
|
|
92 |
2 |
StringWriterOutputTarget writer = new StringWriterOutputTarget(); |
93 |
|
|
94 |
2 |
FilterXMLOutputProperties properties = new FilterXMLOutputProperties(); |
95 |
2 |
properties.setTarget(writer); |
96 |
|
|
97 |
2 |
BeanOutputFilterStreamFactory<FilterXMLOutputProperties> xmlOutputFilterStreamFactory = |
98 |
|
this.mocker.getInstance(OutputFilterStreamFactory.class, FilterStreamType.FILTER_XML.serialize()); |
99 |
2 |
OutputFilterStream outputFilterStream = xmlOutputFilterStreamFactory.createOutputFilterStream(properties); |
100 |
|
|
101 |
2 |
inputFilterStream.read(outputFilterStream.getFilter()); |
102 |
|
|
103 |
2 |
inputFilterStream.close(); |
104 |
2 |
outputFilterStream.close(); |
105 |
|
|
106 |
2 |
Assert.assertEquals(expected, writer.getBuffer().toString()); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void testSkipFirstDocument() throws FilterException, IOException, ComponentLookupException |
111 |
|
{ |
112 |
1 |
XARInputProperties xarProperties = new XARInputProperties(); |
113 |
|
|
114 |
1 |
xarProperties.setSource(new DefaultFileInputSource(extensionPackager.getExtensionFile(new ExtensionId("xar1", |
115 |
|
"1.0")))); |
116 |
1 |
EntityReferenceSet entities = new EntityReferenceSet(); |
117 |
1 |
entities.includes(new LocalDocumentReference("space2", "page2")); |
118 |
1 |
xarProperties.setEntities(entities); |
119 |
|
|
120 |
1 |
assertXML("testSkipFirstDocument", xarProperties); |
121 |
|
|
122 |
1 |
assertTrue(this.allLogRule.getMarker(0).contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED)); |
123 |
|
} |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
125 |
1 |
@Test... |
126 |
|
public void testSkipLastSpace() throws FilterException, IOException, ComponentLookupException |
127 |
|
{ |
128 |
1 |
XARInputProperties xarProperties = new XARInputProperties(); |
129 |
|
|
130 |
1 |
xarProperties.setSource(new DefaultFileInputSource(extensionPackager.getExtensionFile(new ExtensionId("xar1", |
131 |
|
"1.0")))); |
132 |
1 |
EntityReferenceSet entities = new EntityReferenceSet(); |
133 |
1 |
entities.excludes(new EntityReference("space2", EntityType.SPACE)); |
134 |
1 |
xarProperties.setEntities(entities); |
135 |
|
|
136 |
1 |
assertXML("testSkipLastSpace", xarProperties); |
137 |
|
|
138 |
1 |
assertTrue(this.allLogRule.getMarker(0).contains(WikiDocumentFilter.LOG_DOCUMENT_SKIPPED)); |
139 |
|
} |
140 |
|
} |