1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.filter.xar

File XARInputFilterStreamTest.java

 

Code metrics

0
30
4
1
140
95
4
0.13
7.5
4
1

Classes

Class Line # Actions
XARInputFilterStreamTest 60 30 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 2 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.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
 
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   
 
72  1 toggle @BeforeClass
73    public static void beforeClass() throws Exception
74    {
75  1 extensionPackager = new ExtensionPackager(null, FOLDER);
76  1 extensionPackager.generateExtensions();
77    }
78   
 
79  2 toggle 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   
 
109  1 toggle @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   
 
125  1 toggle @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    }