| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.extension.script; |
| 21 |
|
|
| 22 |
|
import java.util.HashMap; |
| 23 |
|
import java.util.List; |
| 24 |
|
import java.util.Map; |
| 25 |
|
|
| 26 |
|
import org.junit.Assert; |
| 27 |
|
import org.junit.Before; |
| 28 |
|
import org.junit.Rule; |
| 29 |
|
import org.junit.Test; |
| 30 |
|
import org.xwiki.extension.InstallException; |
| 31 |
|
import org.xwiki.extension.UninstallException; |
| 32 |
|
import org.xwiki.extension.job.InstallRequest; |
| 33 |
|
import org.xwiki.extension.repository.InstalledExtensionRepository; |
| 34 |
|
import org.xwiki.extension.repository.internal.core.CoreExtensionScanner; |
| 35 |
|
import org.xwiki.extension.test.MockitoRepositoryUtilsRule; |
| 36 |
|
import org.xwiki.job.Job; |
| 37 |
|
import org.xwiki.logging.LogLevel; |
| 38 |
|
import org.xwiki.logging.event.LogEvent; |
| 39 |
|
import org.xwiki.model.reference.DocumentReference; |
| 40 |
|
import org.xwiki.script.service.ScriptService; |
| 41 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
| 42 |
|
import org.xwiki.security.authorization.Right; |
| 43 |
|
import org.xwiki.test.annotation.AfterComponent; |
| 44 |
|
import org.xwiki.test.annotation.AllComponents; |
| 45 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
| 46 |
|
|
| 47 |
|
import com.xpn.xwiki.XWiki; |
| 48 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
| 49 |
|
import com.xpn.xwiki.test.MockitoOldcoreRule; |
| 50 |
|
import com.xpn.xwiki.util.XWikiStubContextProvider; |
| 51 |
|
|
| 52 |
|
import static org.junit.Assert.assertEquals; |
| 53 |
|
import static org.junit.Assert.assertNotNull; |
| 54 |
|
import static org.mockito.Mockito.doThrow; |
| 55 |
|
import static org.mockito.Mockito.mock; |
| 56 |
|
|
| 57 |
|
@AllComponents |
| |
|
| 89.9% |
Uncovered Elements: 8 (79) |
Complexity: 20 |
Complexity Density: 0.38 |
|
| 58 |
|
public class ExtensionManagerScriptServiceTest |
| 59 |
|
{ |
| 60 |
|
private MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
| 61 |
|
|
| 62 |
|
private MockitoOldcoreRule xwikiBridge = new MockitoOldcoreRule(this.mocker); |
| 63 |
|
|
| 64 |
|
@Rule |
| 65 |
|
public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker, this.xwikiBridge); |
| 66 |
|
|
| 67 |
|
private XWiki mockXWiki; |
| 68 |
|
|
| 69 |
|
private Map<String, BaseClass> classes = new HashMap<String, BaseClass>(); |
| 70 |
|
|
| 71 |
|
private DocumentReference contextUser; |
| 72 |
|
|
| 73 |
|
private ExtensionManagerScriptService scriptService; |
| 74 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 75 |
10 |
@AfterComponent... |
| 76 |
|
public void afterComponent() throws Exception |
| 77 |
|
{ |
| 78 |
|
|
| 79 |
10 |
this.mocker.registerMockComponent(CoreExtensionScanner.class); |
| 80 |
|
} |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 82 |
10 |
@Before... |
| 83 |
|
public void before() throws Exception |
| 84 |
|
{ |
| 85 |
|
|
| 86 |
|
|
| 87 |
10 |
this.mockXWiki = mock(XWiki.class); |
| 88 |
|
|
| 89 |
10 |
this.xwikiBridge.getXWikiContext().setWiki(this.mockXWiki); |
| 90 |
10 |
this.xwikiBridge.getXWikiContext().setWikiId("xwiki"); |
| 91 |
10 |
this.contextUser = |
| 92 |
|
new DocumentReference(this.xwikiBridge.getXWikiContext().getWikiId(), "XWiki", "ExtensionUser"); |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
10 |
BaseClass styleSheetClass = new BaseClass(); |
| 97 |
10 |
this.classes.put("StyleSheetExtension", styleSheetClass); |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
10 |
this.xwikiBridge.getXWikiContext().setUserReference(this.contextUser); |
| 102 |
|
|
| 103 |
10 |
((XWikiStubContextProvider) this.mocker.getInstance(XWikiStubContextProvider.class)) |
| 104 |
|
.initialize(this.xwikiBridge.getXWikiContext()); |
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
10 |
this.scriptService = this.mocker.getInstance(ScriptService.class, "extension"); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 113 |
4 |
private Job install(String id, String version, String namespace) throws Throwable... |
| 114 |
|
{ |
| 115 |
4 |
Job job = this.scriptService.install(id, version, namespace); |
| 116 |
4 |
if (job == null) { |
| 117 |
0 |
throw this.scriptService.getLastError(); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
4 |
job.join(); |
| 121 |
|
|
| 122 |
4 |
List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN); |
| 123 |
4 |
if (!errors.isEmpty()) { |
| 124 |
2 |
throw errors.get(0).getThrowable(); |
| 125 |
|
} |
| 126 |
|
|
| 127 |
2 |
return job; |
| 128 |
|
} |
| 129 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
| 130 |
4 |
private Job uninstall(String id, String namespace) throws Throwable... |
| 131 |
|
{ |
| 132 |
4 |
Job job = this.scriptService.uninstall(id, namespace); |
| 133 |
4 |
if (job == null) { |
| 134 |
0 |
throw this.scriptService.getLastError(); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
4 |
job.join(); |
| 138 |
|
|
| 139 |
4 |
List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN); |
| 140 |
4 |
if (!errors.isEmpty()) { |
| 141 |
2 |
throw errors.get(0).getThrowable(); |
| 142 |
|
} |
| 143 |
|
|
| 144 |
2 |
return job; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
|
| 150 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 151 |
1 |
@Test... |
| 152 |
|
public void testInstallOnRoot() throws Throwable |
| 153 |
|
{ |
| 154 |
1 |
install("extension", "version", null); |
| 155 |
|
} |
| 156 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 157 |
1 |
@Test... |
| 158 |
|
public void testInstallOnNamespace() throws Throwable |
| 159 |
|
{ |
| 160 |
1 |
install("extension", "version", "namespace"); |
| 161 |
|
} |
| 162 |
|
|
| |
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 3 |
Complexity Density: 0.25 |
1PASS
|
|
| 163 |
1 |
@Test... |
| 164 |
|
public void testOverwriteAllowedNamespaces() throws Throwable |
| 165 |
|
{ |
| 166 |
1 |
InstallRequest installRequest = this.scriptService.createInstallRequest("extension", "version", "namespace"); |
| 167 |
|
|
| 168 |
|
|
| 169 |
1 |
((ScriptExtensionRewriter) installRequest.getRewriter()).installExtensionTypeOnRootNamespace("test"); |
| 170 |
|
|
| 171 |
|
|
| 172 |
1 |
installRequest.setRootModificationsAllowed(true); |
| 173 |
|
|
| 174 |
1 |
Job job = this.scriptService.install(installRequest); |
| 175 |
1 |
if (job == null) { |
| 176 |
0 |
throw this.scriptService.getLastError(); |
| 177 |
|
} |
| 178 |
|
|
| 179 |
1 |
job.join(); |
| 180 |
|
|
| 181 |
1 |
List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN); |
| 182 |
1 |
if (!errors.isEmpty()) { |
| 183 |
0 |
throw errors.get(0).getThrowable(); |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
1 |
InstalledExtensionRepository repository = mocker.getInstance(InstalledExtensionRepository.class); |
| 189 |
|
|
| 190 |
1 |
assertNotNull(repository.getInstalledExtension("extension", null)); |
| 191 |
|
} |
| 192 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 193 |
1 |
@Test(expected = InstallException.class)... |
| 194 |
|
public void testInstallOnRootWithoutProgrammingRigths() throws Throwable |
| 195 |
|
{ |
| 196 |
1 |
doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager()) |
| 197 |
|
.checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null); |
| 198 |
|
|
| 199 |
1 |
install("extension", "version", null); |
| 200 |
|
} |
| 201 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 202 |
1 |
@Test(expected = InstallException.class)... |
| 203 |
|
public void testInstallOnNamespaceWithoutProgrammingRigths() throws Throwable |
| 204 |
|
{ |
| 205 |
1 |
doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager()) |
| 206 |
|
.checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null); |
| 207 |
|
|
| 208 |
1 |
install("extension", "version", "namespace"); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
|
| 212 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 213 |
1 |
@Test... |
| 214 |
|
public void testUninstallFromRoot() throws Throwable |
| 215 |
|
{ |
| 216 |
1 |
uninstall("installedonroot", null); |
| 217 |
|
} |
| 218 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
| 219 |
1 |
@Test... |
| 220 |
|
public void testUninstallOnNamespace() throws Throwable |
| 221 |
|
{ |
| 222 |
1 |
uninstall("installedonnamespace", "namespace"); |
| 223 |
|
} |
| 224 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 225 |
1 |
@Test(expected = UninstallException.class)... |
| 226 |
|
public void testUninstallOnRootWithoutProgrammingRigths() throws Throwable |
| 227 |
|
{ |
| 228 |
1 |
doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager()) |
| 229 |
|
.checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null); |
| 230 |
|
|
| 231 |
1 |
uninstall("installedonroot", null); |
| 232 |
|
} |
| 233 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 234 |
1 |
@Test(expected = UninstallException.class)... |
| 235 |
|
public void testUninstallOnNamespaceWithoutProgrammingRigths() throws Throwable |
| 236 |
|
{ |
| 237 |
1 |
doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager()) |
| 238 |
|
.checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null); |
| 239 |
|
|
| 240 |
1 |
uninstall("installedonnamespace", "namespace"); |
| 241 |
|
} |
| 242 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 243 |
1 |
@Test... |
| 244 |
|
public void testGet() |
| 245 |
|
{ |
| 246 |
1 |
Assert.assertNotNull(this.scriptService.get(CoreExtensionScriptService.ID)); |
| 247 |
1 |
Assert.assertNotNull(this.scriptService.get(LocalExtensionScriptService.ID)); |
| 248 |
1 |
Assert.assertNotNull(this.scriptService.get(InstalledExtensionScriptService.ID)); |
| 249 |
|
} |
| 250 |
|
} |