1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File ExportURLFactoryTest.java

 

Code metrics

0
22
2
1
119
67
2
0.09
11
2
1

Classes

Class Line # Actions
ExportURLFactoryTest 55 22 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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 com.xpn.xwiki.web;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.File;
24    import java.net.URL;
25    import java.util.Arrays;
26   
27    import javax.inject.Provider;
28   
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.junit.rules.TemporaryFolder;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.test.annotation.AllComponents;
36    import org.xwiki.url.filesystem.FilesystemExportContext;
37   
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.doc.XWikiAttachment;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.test.MockitoOldcoreRule;
42   
43    import static org.junit.Assert.assertEquals;
44    import static org.junit.Assert.assertTrue;
45    import static org.mockito.ArgumentMatchers.any;
46    import static org.mockito.Mockito.doReturn;
47    import static org.mockito.Mockito.mock;
48   
49    /**
50    * Unit tests for {@link ExportURLFactory}.
51    *
52    * @version $Id: 849172048981b4fc6bc76930170b8868c20d7a28 $
53    */
54    @AllComponents
 
55    public class ExportURLFactoryTest
56    {
57    @Rule
58    public MockitoOldcoreRule oldcoreRule = new MockitoOldcoreRule();
59   
60    @Rule
61    public TemporaryFolder tmpDirRule = new TemporaryFolder();
62   
63    private ExportURLFactory urlFactory;
64   
65    private File tmpDir;
66   
67    private FilesystemExportContext exportContext;
68   
 
69  1 toggle @Before
70    public void setUp() throws Exception
71    {
72  1 this.urlFactory = new ExportURLFactory();
73   
74  1 this.tmpDir = this.tmpDirRule.newFolder("xwikitests");
75  1 new File(this.tmpDir, "attachment").mkdir();
76   
77  1 Provider<FilesystemExportContext> exportContextProvider = this.oldcoreRule.getMocker().getInstance(
78    new DefaultParameterizedType(null, Provider.class, FilesystemExportContext.class));
79  1 this.exportContext = exportContextProvider.get();
80   
81  1 doReturn("/xwiki").when(this.oldcoreRule.getSpyXWiki()).getWebAppPath(any(XWikiContext.class));
82   
83  1 XWikiContext context = this.oldcoreRule.getXWikiContext();
84  1 XWikiRequest request = mock(XWikiRequest.class);
85  1 context.setRequest(request);
86  1 context.setURL(new URL("http://www.xwiki.org/"));
87   
88  1 this.urlFactory.init(null, this.tmpDir, this.exportContext, context);
89    }
90   
 
91  1 toggle @Test
92    public void createAttachmentURLWithWhitespacesInSpaceAndPageNames() throws Exception
93    {
94    // Prepare the exported document and attachment.
95  1 XWikiDocument doc = new XWikiDocument(
96    new DocumentReference("Wiki", Arrays.asList(" Space1 ", "Space2"), "Pa ge"));
97   
98    // Simulate the doc depth (there are 4 levels since the doc will be placed in
99    // pages/Wiki/Space1/Space2/Pa%20Page).
100  1 this.exportContext.setDocParentLevels(4);
101   
102  1 XWikiAttachment attachment = new XWikiAttachment(doc, "img .jpg");
103  1 attachment.setContent(new ByteArrayInputStream("test".getBytes()));
104  1 doc.getAttachmentList().add(attachment);
105  1 this.oldcoreRule.getSpyXWiki().saveDocument(doc, this.oldcoreRule.getXWikiContext());
106   
107  1 URL url = this.urlFactory.createAttachmentURL("img .jpg", " Space1 .Space2", "Pa ge", "view", "", "Wiki",
108    this.oldcoreRule.getXWikiContext());
109   
110    // Verify generated URL
111  1 assertEquals(new URL("file://../../../../attachment/Wiki/+Space1+/Space2/Pa+ge/img+.jpg"), url);
112   
113    // Verify that the file on the FS exists and that the name is ok
114  1 File generatedFile = new File(this.tmpDir, "attachment/Wiki/+Space1+/Space2/Pa+ge/img+.jpg");
115  1 assertTrue(generatedFile.exists());
116  1 assertEquals("img+.jpg", generatedFile.getName());
117    }
118    }
119