1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File ExportURLFactoryTest.java

 

Code metrics

0
27
3
1
103
58
3
0.11
9
3
1

Classes

Class Line # Actions
ExportURLFactoryTest 36 27 0% 3 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 org.apache.commons.io.FileUtils;
28    import org.jmock.Mock;
29    import org.xwiki.model.reference.DocumentReference;
30   
31    import com.xpn.xwiki.XWiki;
32    import com.xpn.xwiki.doc.XWikiAttachment;
33    import com.xpn.xwiki.doc.XWikiDocument;
34    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
35   
 
36    public class ExportURLFactoryTest extends AbstractBridgedXWikiComponentTestCase
37    {
38    private Mock mockXWiki;
39   
40    /** Temporary directory where to put exported files. Will be deleted at the end of the test. */
41    private File tmpDir;
42   
43    /** The tested instance. */
44    private ExportURLFactory urlFactory;
45   
 
46  1 toggle @Override
47    protected void setUp() throws Exception
48    {
49  1 super.setUp();
50   
51  1 this.mockXWiki = mock(XWiki.class);
52  1 this.mockXWiki.stubs().method("getWebAppPath").will(returnValue("/xwiki"));
53  1 this.mockXWiki.stubs().method("Param").will(returnValue(null));
54  1 getContext().setWiki((XWiki) this.mockXWiki.proxy());
55  1 getContext().setURL(new URL("http://www.xwiki.org/"));
56   
57    // The URLFactory uses a request to determine the values for the context and servlet path.
58  1 Mock mockXWikiResquest = mock(XWikiRequest.class, new Class[] {}, new Object[] {});
59  1 mockXWikiResquest.stubs().method("getScheme").will(returnValue("http"));
60  1 mockXWikiResquest.stubs().method("isSecure").will(returnValue(false));
61  1 mockXWikiResquest.stubs().method("getServletPath").will(returnValue("/bin"));
62  1 mockXWikiResquest.stubs().method("getContextPath").will(returnValue("/xwiki"));
63  1 mockXWikiResquest.stubs().method("getHeader").will(returnValue(null));
64  1 getContext().setRequest((XWikiRequest) mockXWikiResquest.proxy());
65   
66    // Since the ExportURLFactory saves requested attachments to the disk, create a temporary folder to hold these
67    // files, which will be deleted after the test ends.
68  1 this.tmpDir = new File(System.getProperty("java.io.tmpdir"), "xwikitests");
69  1 this.tmpDir.mkdirs();
70  1 new File(this.tmpDir, "attachment").mkdir();
71   
72  1 this.urlFactory = new ExportURLFactory();
73  1 this.urlFactory.init(null, this.tmpDir, getContext());
74    }
75   
76    /**
77    * Test that
78    * {@link ExportURLFactory#createAttachmentURL(String, String, String, String, String, com.xpn.xwiki.XWikiContext)}
79    * correctly escapes spaces into %20 when the exported document contains spaces in its name.
80    */
 
81  1 toggle public void testCreateAttachmentURL() throws Exception
82    {
83    // Prepare the exported document and attachment.
84  1 XWikiDocument doc = new XWikiDocument(
85    new DocumentReference("xwiki", Arrays.asList(" Space1 ", "Space2"), "New Page"));
86  1 XWikiAttachment attachment = new XWikiAttachment(doc, "img .jpg");
87  1 attachment.setContent(new ByteArrayInputStream("test".getBytes()));
88  1 doc.getAttachmentList().add(attachment);
89  1 this.mockXWiki.stubs().method("getDocument").will(returnValue(doc));
90   
91  1 URL url = this.urlFactory.createAttachmentURL("img .jpg", " Space1 .Space2", "Pa ge", "view", "", "x",
92    getContext());
93  1 assertEquals(new URL("file://attachment/x/+Space1+/Space2/Pa+ge/img+%252Ejpg"), url);
94    }
95   
96    /** When the test is over, delete the folder where the exported attachments were placed. */
 
97  1 toggle @Override
98    protected void tearDown() throws Exception
99    {
100  1 FileUtils.deleteDirectory(this.tmpDir);
101  1 super.tearDown();
102    }
103    }