1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.webjars.internal

File FilesystemResourceReferenceSerializerTest.java

 

Code metrics

0
19
4
1
105
63
4
0.21
4.75
4
1

Classes

Class Line # Actions
FilesystemResourceReferenceSerializerTest 39 19 0% 4 0
1.0100%
 

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.webjars.internal;
21   
22    import java.io.File;
23    import java.util.Arrays;
24   
25    import javax.inject.Provider;
26   
27    import org.apache.commons.io.FileUtils;
28    import org.junit.After;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.mockito.Mockito;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.test.mockito.MockitoComponentMockingRule;
35    import org.xwiki.url.filesystem.FilesystemExportContext;
36   
37    import static org.junit.Assert.*;
38   
 
39    public class FilesystemResourceReferenceSerializerTest
40    {
41    private static final File BASEDIR = new File(System.getProperty("java.io.tmpdir"), "xwikitest");
42   
43    @Rule
44    public MockitoComponentMockingRule<FilesystemResourceReferenceSerializer> mocker =
45    new MockitoComponentMockingRule<>(FilesystemResourceReferenceSerializer.class);
46   
47    private ClassLoader originalThreadContextClassLoader;
48   
 
49  2 toggle @Before
50    public void setUp() throws Exception
51    {
52  2 this.originalThreadContextClassLoader = Thread.currentThread().getContextClassLoader();
53  2 Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
54  2 FileUtils.deleteDirectory(BASEDIR);
55    }
56   
 
57  2 toggle @After
58    public void tearDown()
59    {
60  2 Thread.currentThread().setContextClassLoader(this.originalThreadContextClassLoader);
61    }
62   
 
63  1 toggle @Test
64    public void serialize() throws Exception
65    {
66  1 FilesystemExportContext exportContext = new FilesystemExportContext();
67  1 exportContext.setExportDir(BASEDIR);
68   
69  1 Provider<FilesystemExportContext> exportContextProvider = this.mocker.getInstance(new DefaultParameterizedType(
70    null, Provider.class, FilesystemExportContext.class));
71  1 Mockito.when(exportContextProvider.get()).thenReturn(exportContext);
72   
73  1 WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList(
74    "angular-paginate-anything", "2.5.3", "paginate-anything.js"));
75   
76    // Verify that the returned URL is ok
77  1 assertEquals("webjars/angular-paginate-anything/2.5.3/paginate-anything.js",
78    this.mocker.getComponentUnderTest().serialize(reference).serialize());
79   
80    // Also verify that the resource has been copied!
81  1 assertTrue(new File(BASEDIR, "webjars/angular-paginate-anything/2.5.3/paginate-anything.js").exists());
82    }
83   
 
84  1 toggle @Test
85    public void serializeWithCSSPathAdjustments() throws Exception
86    {
87  1 FilesystemExportContext exportContext = new FilesystemExportContext();
88  1 exportContext.setExportDir(BASEDIR);
89  1 exportContext.pushCSSParentLevels(3);
90   
91  1 Provider<FilesystemExportContext> exportContextProvider = this.mocker.getInstance(new DefaultParameterizedType(
92    null, Provider.class, FilesystemExportContext.class));
93  1 Mockito.when(exportContextProvider.get()).thenReturn(exportContext);
94   
95  1 WebJarsResourceReference reference = new WebJarsResourceReference("wiki:wiki", Arrays.asList(
96    "angular-paginate-anything", "2.5.3", "paginate-anything.js"));
97   
98    // Verify that the returned URL is ok
99  1 assertEquals("../../../webjars/angular-paginate-anything/2.5.3/paginate-anything.js",
100    this.mocker.getComponentUnderTest().serialize(reference).serialize());
101   
102    // Also verify that the resource has been copied!
103  1 assertTrue(new File(BASEDIR, "webjars/angular-paginate-anything/2.5.3/paginate-anything.js").exists());
104    }
105    }