1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.filter.input

File AbstractInstanceInputFilterStreamTest.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
44
7
1
198
135
10
0.23
6.29
7
1.43

Classes

Class Line # Actions
AbstractInstanceInputFilterStreamTest 68 44 0% 10 4
0.929824693%
 

Contributing tests

This file is covered by 1 test. .

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 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    * Base class to validate an instance sub {@link InputInstanceFilterStream}.
64    *
65    * @version $Id: 54693eced29d71ca943759a566dbadf2a7ee2c38 $
66    */
67    @AllComponents
 
68    public class AbstractInstanceInputFilterStreamTest extends AbstractInstanceFilterStreamTest
69    {
70    protected BeanOutputFilterStreamFactory<FilterXMLOutputProperties> xmlOutputFilterStreamFactory;
71   
72    protected BeanInputFilterStreamFactory<InstanceInputProperties> inputFilterStreamFactory;
73   
74    protected InstanceModel instanceModelMock;
75   
 
76  1 toggle @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    // Query manager
90   
91    // Wikis
92  1 when(this.instanceModelMock.getWikiReferences()).thenAnswer(new Answer<List<WikiReference>>()
93    {
 
94  1 toggle @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    // Spaces
111  1 when(this.instanceModelMock.getSpaceReferences(any(WikiReference.class))).thenAnswer(
112    new Answer<EntityReferenceTreeNode>()
113    {
 
114  1 toggle @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    // Documents
132  1 when(this.instanceModelMock.getDocumentReferences(any(SpaceReference.class))).thenAnswer(
133    new Answer<List<DocumentReference>>()
134    {
 
135  3 toggle @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   
 
155  1 toggle @AfterComponent
156    public void afterComponent() throws Exception
157    {
158  1 this.instanceModelMock = this.oldcore.getMocker().registerMockComponent(InstanceModel.class);
159    }
160   
 
161  1 toggle 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   
 
192  1 toggle 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    }