1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.vfs.internal.script

File PathConverterTest.java

 

Code metrics

0
23
3
1
111
74
5
0.22
7.67
3
1.67

Classes

Class Line # Actions
PathConverterTest 49 23 0% 5 2
0.923076992.3%
 

Contributing tests

This file is covered by 3 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.vfs.internal.script;
21   
22    import java.net.URI;
23    import java.nio.file.Path;
24   
25    import org.apache.commons.lang3.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    * Unit tests for {@link PathConverter}.
45    *
46    * @version $Id: 615eded22e97926e47bb6efa7ecabdc17bc8aaaf $
47    * @since 7.4M2
48    */
 
49    public class PathConverterTest
50    {
51    @Rule
52    public MockitoComponentMockingRule<PathConverter> mocker =
53    new MockitoComponentMockingRule<>(PathConverter.class);
54   
 
55  1 toggle @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   
 
70  1 toggle @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   
 
89  1 toggle @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    }