1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.vfs.internal.script; |
21 |
|
|
22 |
|
import java.net.URI; |
23 |
|
import java.nio.file.Path; |
24 |
|
|
25 |
|
import org.apache.commons.lang.exception.ExceptionUtils; |
26 |
|
import org.junit.Rule; |
27 |
|
import org.junit.Test; |
28 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
29 |
|
import org.xwiki.properties.converter.ConversionException; |
30 |
|
import org.xwiki.resource.ResourceReferenceSerializer; |
31 |
|
import org.xwiki.resource.SerializeResourceReferenceException; |
32 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
33 |
|
import org.xwiki.vfs.VfsException; |
34 |
|
import org.xwiki.vfs.VfsPermissionChecker; |
35 |
|
import org.xwiki.vfs.VfsResourceReference; |
36 |
|
|
37 |
|
import net.java.truevfs.access.TPath; |
38 |
|
|
39 |
|
import static org.junit.Assert.*; |
40 |
|
import static org.mockito.Mockito.doThrow; |
41 |
|
import static org.mockito.Mockito.when; |
42 |
|
|
43 |
|
|
44 |
|
@link |
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
|
|
| 92.3% |
Uncovered Elements: 2 (26) |
Complexity: 5 |
Complexity Density: 0.22 |
|
49 |
|
public class PathConverterTest |
50 |
|
{ |
51 |
|
@Rule |
52 |
|
public MockitoComponentMockingRule<PathConverter> mocker = |
53 |
|
new MockitoComponentMockingRule<>(PathConverter.class); |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
55 |
1 |
@Test... |
56 |
|
public void convertWhenOk() throws Exception |
57 |
|
{ |
58 |
1 |
ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance( |
59 |
|
new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, |
60 |
|
URI.class), "truevfs"); |
61 |
1 |
VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c"); |
62 |
1 |
when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c")); |
63 |
|
|
64 |
1 |
Path path = this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), |
65 |
|
"attach:Sandbox.WebHome@my.zip/a/b/c"); |
66 |
1 |
assertEquals("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c", path.toString()); |
67 |
1 |
assertEquals(TPath.class.getName(), path.getClass().getName()); |
68 |
|
} |
69 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
public void convertWhenError() throws Exception |
72 |
|
{ |
73 |
1 |
ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance( |
74 |
|
new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, |
75 |
|
URI.class), "truevfs"); |
76 |
1 |
VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c"); |
77 |
1 |
when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error")); |
78 |
|
|
79 |
1 |
try { |
80 |
1 |
this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), |
81 |
|
"attach:Sandbox.WebHome@my.zip/a/b/c"); |
82 |
0 |
fail("Should have thrown an exception here"); |
83 |
|
} catch (ConversionException expected) { |
84 |
1 |
assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", |
85 |
|
expected.getMessage()); |
86 |
|
} |
87 |
|
} |
88 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
public void convertWhenNoPermission() throws Exception |
91 |
|
{ |
92 |
1 |
ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance( |
93 |
|
new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, |
94 |
|
URI.class), "truevfs"); |
95 |
1 |
VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c"); |
96 |
1 |
when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c")); |
97 |
|
|
98 |
1 |
VfsPermissionChecker permissionChecker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading"); |
99 |
1 |
doThrow(new VfsException("unauthorized")).when(permissionChecker).checkPermission(reference); |
100 |
|
|
101 |
1 |
try { |
102 |
1 |
this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), |
103 |
|
"attach:Sandbox.WebHome@my.zip/a/b/c"); |
104 |
0 |
fail("Should have thrown an exception here"); |
105 |
|
} catch (ConversionException expected) { |
106 |
1 |
assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", |
107 |
|
expected.getMessage()); |
108 |
1 |
assertEquals("VfsException: unauthorized", ExceptionUtils.getRootCauseMessage(expected)); |
109 |
|
} |
110 |
|
} |
111 |
|
} |