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

File ServletEnvironmentTest.java

 

Code metrics

0
54
13
1
206
138
14
0.26
4.15
13
1.08

Classes

Class Line # Actions
ServletEnvironmentTest 44 54 0% 14 1
0.9850746498.5%
 

Contributing tests

This file is covered by 11 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.environment.internal;
21   
22    import java.io.File;
23    import java.net.MalformedURLException;
24   
25    import javax.servlet.ServletContext;
26   
27    import org.apache.commons.io.FileUtils;
28    import org.junit.*;
29    import org.slf4j.Logger;
30    import org.xwiki.component.embed.EmbeddableComponentManager;
31    import org.xwiki.component.util.ReflectionUtils;
32    import org.xwiki.environment.Environment;
33    import org.xwiki.test.AllLogRule;
34   
35    import static org.junit.Assert.*;
36    import static org.mockito.Mockito.*;
37   
38    /**
39    * Unit tests for {@link ServletEnvironment}.
40    *
41    * @version $Id: 4187b1e6873b320eb7d497ac6a3010ec98258019 $
42    * @since 3.5M1
43    */
 
44    public class ServletEnvironmentTest
45    {
46    private File servletTmpDir;
47   
48    private File systemTmpDir;
49   
50    private ServletEnvironment environment;
51   
52    /**
53    * Capture logs.
54    */
55    @Rule
56    public AllLogRule logRule = new AllLogRule();
57   
 
58  11 toggle @Before
59    public void setUp() throws Exception
60    {
61  11 this.servletTmpDir = new File(System.getProperty("java.io.tmpdir"), "ServletEnvironmentTest-tmpDir");
62  11 this.systemTmpDir = new File(System.getProperty("java.io.tmpdir"), "xwiki-temp");
63   
64  11 EmbeddableComponentManager ecm = new EmbeddableComponentManager();
65  11 ecm.initialize(getClass().getClassLoader());
66  11 this.environment = ecm.getInstance(Environment.class);
67    }
68   
 
69  11 toggle @After
70    public void tearDown() throws Exception
71    {
72  11 FileUtils.deleteQuietly(this.servletTmpDir);
73    }
74   
 
75  1 toggle @Test
76    public void getResourceWhenServletContextNotSet()
77    {
78  1 try {
79  1 this.environment.getResource("/whatever");
80  0 fail();
81    } catch (RuntimeException expected) {
82  1 assertEquals("The Servlet Environment has not been properly initialized "
83    + "(The Servlet Context is not set)", expected.getMessage());
84    }
85    }
86   
 
87  1 toggle @Test
88    public void getResourceOk() throws Exception
89    {
90  1 ServletContext servletContext = mock(ServletContext.class);
91  1 this.environment.setServletContext(servletContext);
92  1 this.environment.getResource("/test");
93   
94  1 verify(servletContext).getResource("/test");
95    }
96   
 
97  1 toggle @Test
98    public void getResourceAsStreamOk() throws Exception
99    {
100  1 ServletContext servletContext = mock(ServletContext.class);
101  1 this.environment.setServletContext(servletContext);
102  1 this.environment.getResourceAsStream("/test");
103   
104  1 verify(servletContext).getResourceAsStream("/test");
105    }
106   
 
107  1 toggle @Test
108    public void getResourceNotExisting() throws Exception
109    {
110  1 ServletContext servletContext = mock(ServletContext.class);
111  1 this.environment.setServletContext(servletContext);
112  1 assertNull(this.environment.getResource("unknown resource"));
113    }
114   
 
115  1 toggle @Test
116    public void getResourceWhenMalformedURLException() throws Exception
117    {
118  1 ServletContext servletContext = mock(ServletContext.class);
119  1 when(servletContext.getResource("bad resource")).thenThrow(new MalformedURLException("invalid url"));
120  1 this.environment.setServletContext(servletContext);
121  1 assertNull(this.environment.getResource("bad resource"));
122  1 assertEquals("Error getting resource [bad resource] because of invalid path format. Reason: [invalid url]",
123    this.logRule.getMessage(0));
124    }
125   
 
126  1 toggle @Test
127    public void getPermanentDirectoryWhenSetWithAPI() throws Exception
128    {
129  1 File permanentDirectory = new File("/permanent");
130  1 this.environment.setPermanentDirectory(permanentDirectory);
131  1 assertEquals(permanentDirectory.getCanonicalFile(),
132    this.environment.getPermanentDirectory().getCanonicalFile());
133    }
134   
 
135  1 toggle @Test
136    public void getPermanentDirectoryWhenSetWithSystemProperty() throws Exception
137    {
138  1 File expectedPermanentDirectory = new File(System.getProperty("java.io.tmpdir"), "permanent");
139  1 System.setProperty("xwiki.data.dir", expectedPermanentDirectory.toString());
140   
141  1 try {
142  1 this.environment.setServletContext(mock(ServletContext.class));
143   
144  1 assertEquals(expectedPermanentDirectory.getCanonicalFile(),
145    this.environment.getPermanentDirectory().getCanonicalFile());
146    } finally {
147  1 System.clearProperty("xwiki.data.dir");
148    }
149    }
150   
 
151  1 toggle @Test
152    public void getPermanentDirectoryWhenNotSet() throws Exception
153    {
154  1 ServletContext servletContext = mock(ServletContext.class);
155  1 when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(this.servletTmpDir);
156  1 this.environment.setServletContext(servletContext);
157   
158  1 Logger logger = mock(Logger.class);
159  1 ReflectionUtils.setFieldValue(this.environment, "logger", logger);
160   
161  1 assertEquals(this.servletTmpDir.getCanonicalFile(),
162    this.environment.getPermanentDirectory().getCanonicalFile());
163   
164    // Also verify that we log a warning!
165  1 verify(logger).warn("No permanent directory configured. Using temporary directory [{}].",
166    this.servletTmpDir.getCanonicalFile());
167    }
168   
 
169  1 toggle @Test
170    public void getTemporaryDirectory() throws Exception
171    {
172  1 File tmpDir = new File("tmpdir");
173  1 this.environment.setTemporaryDirectory(tmpDir);
174  1 assertEquals(tmpDir.getCanonicalFile(), this.environment.getTemporaryDirectory().getCanonicalFile());
175    }
176   
 
177  1 toggle @Test
178    public void getTemporaryDirectoryWhenNotSet() throws Exception
179    {
180  1 ServletContext servletContext = mock(ServletContext.class);
181  1 when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(this.servletTmpDir);
182  1 this.environment.setServletContext(servletContext);
183   
184  1 File tmpDir = this.environment.getTemporaryDirectory();
185   
186    // Make sure it is the "xwiki-temp" dir which is under the main temp dir.
187  1 assertEquals(this.servletTmpDir.listFiles()[0].getCanonicalFile(), tmpDir.getCanonicalFile());
188    }
189   
190    /**
191    * Verify we default to the system tmp dir if the Servlet tmp dir is not set.
192    */
 
193  1 toggle @Test
194    public void getTemporaryDirectoryWhenServletTempDirNotSet() throws Exception
195    {
196  1 ServletContext servletContext = mock(ServletContext.class);
197  1 this.environment.setServletContext(servletContext);
198   
199  1 assertEquals(this.systemTmpDir.getCanonicalFile(),
200    this.environment.getTemporaryDirectory().getCanonicalFile());
201   
202    // Verify that servletContext.getAttribute was called (and that we returned null - this happens because we
203    // didn't set any stubbing on servletContext and null is the default returned by Mockito).
204  1 verify(servletContext).getAttribute("javax.servlet.context.tempdir");
205    }
206    }