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

File ExtensionInstanceOutputFilterStreamTest.java

 

Code metrics

2
35
6
1
175
121
9
0.26
5.83
6
1.5

Classes

Class Line # Actions
ExtensionInstanceOutputFilterStreamTest 62 35 0% 9 3
0.930232693%
 

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.instance.internal.output;
21   
22    import static org.mockito.ArgumentMatchers.any;
23    import static org.mockito.Mockito.doReturn;
24    import static org.mockito.Mockito.doThrow;
25   
26    import java.io.IOException;
27    import java.io.UnsupportedEncodingException;
28    import java.net.URL;
29   
30    import org.junit.Assert;
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.extension.ExtensionId;
35    import org.xwiki.extension.ResolveException;
36    import org.xwiki.extension.repository.ExtensionRepositoryManager;
37    import org.xwiki.extension.repository.InstalledExtensionRepository;
38    import org.xwiki.extension.repository.internal.core.CoreExtensionScanner;
39    import org.xwiki.extension.test.EmptyExtension;
40    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
41    import org.xwiki.filter.FilterException;
42    import org.xwiki.filter.filterxml.input.FilterXMLInputProperties;
43    import org.xwiki.filter.input.BeanInputFilterStreamFactory;
44    import org.xwiki.filter.input.DefaultURLInputSource;
45    import org.xwiki.filter.input.InputFilterStream;
46    import org.xwiki.filter.input.InputFilterStreamFactory;
47    import org.xwiki.filter.instance.output.ExtensionInstanceOutputProperties;
48    import org.xwiki.filter.instance.output.InstanceOutputProperties;
49    import org.xwiki.filter.output.BeanOutputFilterStreamFactory;
50    import org.xwiki.filter.output.OutputFilterStream;
51    import org.xwiki.filter.output.OutputFilterStreamFactory;
52    import org.xwiki.filter.type.FilterStreamType;
53    import org.xwiki.test.annotation.AfterComponent;
54    import org.xwiki.test.annotation.AllComponents;
55   
56    /**
57    * Validate {@link ExtensionInstanceOutputProperties}.
58    *
59    * @version $Id: 0425d59e6b8332155cd6bd36f85fa72b4499dac6 $
60    */
61    @AllComponents
 
62    public class ExtensionInstanceOutputFilterStreamTest
63    {
64    @Rule
65    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule();
66   
67    private BeanInputFilterStreamFactory<FilterXMLInputProperties> xmlInputFilterStreamFactory;
68   
69    private BeanOutputFilterStreamFactory<InstanceOutputProperties> outputFilterStreamFactory;
70   
71    private InstalledExtensionRepository installedExtensionRepository;
72   
73    private ExtensionRepositoryManager extensionRepositoryMock;
74   
 
75  2 toggle @AfterComponent
76    public void afterComponent() throws Exception
77    {
78  2 this.extensionRepositoryMock =
79    this.repositoryUtil.getComponentManager().registerMockComponent(ExtensionRepositoryManager.class);
80  2 doThrow(ResolveException.class).when(this.extensionRepositoryMock).resolve((ExtensionId) any());
81    }
82   
83    // Tests
84   
 
85  2 toggle @Before
86    public void before() throws Exception
87    {
88  2 this.xmlInputFilterStreamFactory =
89    this.repositoryUtil.getComponentManager().getInstance(InputFilterStreamFactory.class,
90    FilterStreamType.FILTER_XML.serialize());
91  2 this.outputFilterStreamFactory =
92    this.repositoryUtil.getComponentManager().getInstance(OutputFilterStreamFactory.class,
93    FilterStreamType.XWIKI_INSTANCE.serialize());
94   
95  2 this.installedExtensionRepository =
96    this.repositoryUtil.getComponentManager().getInstance(InstalledExtensionRepository.class);
97    }
98   
 
99  2 toggle protected void importFromXML(String resource) throws FilterException
100    {
101  2 importFromXML(resource, null);
102    }
103   
 
104  2 toggle protected void importFromXML(String resource, InstanceOutputProperties instanceProperties) throws FilterException
105    {
106  2 if (instanceProperties == null) {
107  2 instanceProperties = new InstanceOutputProperties();
108  2 instanceProperties.setVerbose(false);
109    }
110   
111  2 OutputFilterStream outputFilterStream =
112    this.outputFilterStreamFactory.createOutputFilterStream(instanceProperties);
113   
114  2 URL url = getClass().getResource("/" + resource + ".xml");
115   
116  2 FilterXMLInputProperties properties = new FilterXMLInputProperties();
117  2 properties.setSource(new DefaultURLInputSource(url));
118   
119  2 InputFilterStream inputFilterStream = this.xmlInputFilterStreamFactory.createInputFilterStream(properties);
120   
121  2 inputFilterStream.read(outputFilterStream.getFilter());
122   
123  2 try {
124  2 inputFilterStream.close();
125    } catch (IOException e) {
126  0 throw new FilterException("Failed to close input wiki stream", e);
127    }
128  2 try {
129  2 outputFilterStream.close();
130    } catch (IOException e) {
131  0 throw new FilterException("Failed to close output wiki stream", e);
132    }
133    }
134   
135    // tests
136   
 
137  1 toggle @Test
138    public void testImportExtensionId() throws FilterException, ResolveException, UnsupportedEncodingException
139    {
140  1 doReturn(new EmptyExtension(new ExtensionId("extensionid1", "version1"), "test")).when(
141    this.extensionRepositoryMock).resolve(new ExtensionId("extensionid1", "version1"));
142  1 doReturn(new EmptyExtension(new ExtensionId("extensionid2", "version2"), "test")).when(
143    this.extensionRepositoryMock).resolve(new ExtensionId("extensionid2", "version2"));
144   
145  1 importFromXML("extensionid");
146   
147  1 Assert.assertNull(this.installedExtensionRepository.getInstalledExtension("extensionid1", null));
148  1 Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid1", "namespace1"));
149  1 Assert.assertEquals("version1",
150    this.installedExtensionRepository.getInstalledExtension("extensionid1", "namespace1").getId().getVersion()
151    .getValue());
152   
153  1 Assert.assertNull(this.installedExtensionRepository.getInstalledExtension("extensionid2", null));
154  1 Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid2", "wiki:wiki2"));
155  1 Assert.assertEquals("version2",
156    this.installedExtensionRepository.getInstalledExtension("extensionid2", "wiki:wiki2").getId().getVersion()
157    .getValue());
158    }
159   
 
160  1 toggle @Test
161    public void testImportExtensionIdWithoutNamespace() throws FilterException, ResolveException,
162    UnsupportedEncodingException
163    {
164  1 doReturn(new EmptyExtension(new ExtensionId("extensionid", "version"), "test")).when(
165    this.extensionRepositoryMock).resolve(new ExtensionId("extensionid", "version"));
166   
167  1 importFromXML("extensionidwithoutnamespace");
168   
169  1 Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid", null));
170  1 Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid", "namespace"));
171  1 Assert.assertEquals("version",
172    this.installedExtensionRepository.getInstalledExtension("extensionid", "namespace").getId().getVersion()
173    .getValue());
174    }
175    }