| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
@link |
| 58 |
|
|
| 59 |
|
@version |
| 60 |
|
|
| 61 |
|
@AllComponents |
| |
|
| 93.2% |
Uncovered Elements: 3 (44) |
Complexity: 9 |
Complexity Density: 0.25 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 75 |
2 |
@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 |
2 |
this.repositoryUtil.getComponentManager().registerMockComponent(CoreExtensionScanner.class); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 88 |
2 |
@Before... |
| 89 |
|
public void before() throws Exception |
| 90 |
|
{ |
| 91 |
2 |
this.xmlInputFilterStreamFactory = |
| 92 |
|
this.repositoryUtil.getComponentManager().getInstance(InputFilterStreamFactory.class, |
| 93 |
|
FilterStreamType.FILTER_XML.serialize()); |
| 94 |
2 |
this.outputFilterStreamFactory = |
| 95 |
|
this.repositoryUtil.getComponentManager().getInstance(OutputFilterStreamFactory.class, |
| 96 |
|
FilterStreamType.XWIKI_INSTANCE.serialize()); |
| 97 |
|
|
| 98 |
2 |
this.installedExtensionRepository = |
| 99 |
|
this.repositoryUtil.getComponentManager().getInstance(InstalledExtensionRepository.class); |
| 100 |
|
} |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 102 |
2 |
protected void importFromXML(String resource) throws FilterException... |
| 103 |
|
{ |
| 104 |
2 |
importFromXML(resource, null); |
| 105 |
|
} |
| 106 |
|
|
| |
|
| 82.4% |
Uncovered Elements: 3 (17) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 107 |
2 |
protected void importFromXML(String resource, InstanceOutputProperties instanceProperties) throws FilterException... |
| 108 |
|
{ |
| 109 |
2 |
if (instanceProperties == null) { |
| 110 |
2 |
instanceProperties = new InstanceOutputProperties(); |
| 111 |
2 |
instanceProperties.setVerbose(false); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
2 |
OutputFilterStream outputFilterStream = |
| 115 |
|
this.outputFilterStreamFactory.createOutputFilterStream(instanceProperties); |
| 116 |
|
|
| 117 |
2 |
URL url = getClass().getResource("/" + resource + ".xml"); |
| 118 |
|
|
| 119 |
2 |
FilterXMLInputProperties properties = new FilterXMLInputProperties(); |
| 120 |
2 |
properties.setSource(new DefaultURLInputSource(url)); |
| 121 |
|
|
| 122 |
2 |
InputFilterStream inputFilterStream = this.xmlInputFilterStreamFactory.createInputFilterStream(properties); |
| 123 |
|
|
| 124 |
2 |
inputFilterStream.read(outputFilterStream.getFilter()); |
| 125 |
|
|
| 126 |
2 |
try { |
| 127 |
2 |
inputFilterStream.close(); |
| 128 |
|
} catch (IOException e) { |
| 129 |
0 |
throw new FilterException("Failed to close input wiki stream", e); |
| 130 |
|
} |
| 131 |
2 |
try { |
| 132 |
2 |
outputFilterStream.close(); |
| 133 |
|
} catch (IOException e) { |
| 134 |
0 |
throw new FilterException("Failed to close output wiki stream", e); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 140 |
1 |
@Test... |
| 141 |
|
public void testImportExtensionId() throws FilterException, ResolveException, UnsupportedEncodingException |
| 142 |
|
{ |
| 143 |
1 |
doReturn(new EmptyExtension(new ExtensionId("extensionid1", "version1"), "test")).when( |
| 144 |
|
this.extensionRepositoryMock).resolve(new ExtensionId("extensionid1", "version1")); |
| 145 |
1 |
doReturn(new EmptyExtension(new ExtensionId("extensionid2", "version2"), "test")).when( |
| 146 |
|
this.extensionRepositoryMock).resolve(new ExtensionId("extensionid2", "version2")); |
| 147 |
|
|
| 148 |
1 |
importFromXML("extensionid"); |
| 149 |
|
|
| 150 |
1 |
Assert.assertNull(this.installedExtensionRepository.getInstalledExtension("extensionid1", null)); |
| 151 |
1 |
Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid1", "namespace1")); |
| 152 |
1 |
Assert.assertEquals("version1", |
| 153 |
|
this.installedExtensionRepository.getInstalledExtension("extensionid1", "namespace1").getId().getVersion() |
| 154 |
|
.getValue()); |
| 155 |
|
|
| 156 |
1 |
Assert.assertNull(this.installedExtensionRepository.getInstalledExtension("extensionid2", null)); |
| 157 |
1 |
Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid2", "wiki:wiki2")); |
| 158 |
1 |
Assert.assertEquals("version2", |
| 159 |
|
this.installedExtensionRepository.getInstalledExtension("extensionid2", "wiki:wiki2").getId().getVersion() |
| 160 |
|
.getValue()); |
| 161 |
|
} |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 163 |
1 |
@Test... |
| 164 |
|
public void testImportExtensionIdWithoutNamespace() throws FilterException, ResolveException, |
| 165 |
|
UnsupportedEncodingException |
| 166 |
|
{ |
| 167 |
1 |
doReturn(new EmptyExtension(new ExtensionId("extensionid", "version"), "test")).when( |
| 168 |
|
this.extensionRepositoryMock).resolve(new ExtensionId("extensionid", "version")); |
| 169 |
|
|
| 170 |
1 |
importFromXML("extensionidwithoutnamespace"); |
| 171 |
|
|
| 172 |
1 |
Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid", null)); |
| 173 |
1 |
Assert.assertNotNull(this.installedExtensionRepository.getInstalledExtension("extensionid", "namespace")); |
| 174 |
1 |
Assert.assertEquals("version", |
| 175 |
|
this.installedExtensionRepository.getInstalledExtension("extensionid", "namespace").getId().getVersion() |
| 176 |
|
.getValue()); |
| 177 |
|
} |
| 178 |
|
} |