1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
40 |
|
|
41 |
|
@version |
42 |
|
@since |
43 |
|
|
|
|
| 98.5% |
Uncovered Elements: 1 (67) |
Complexity: 14 |
Complexity Density: 0.26 |
|
44 |
|
public class ServletEnvironmentTest |
45 |
|
{ |
46 |
|
private File servletTmpDir; |
47 |
|
|
48 |
|
private File systemTmpDir; |
49 |
|
|
50 |
|
private ServletEnvironment environment; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@Rule |
56 |
|
public AllLogRule logRule = new AllLogRule(); |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
58 |
11 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
69 |
11 |
@After... |
70 |
|
public void tearDown() throws Exception |
71 |
|
{ |
72 |
11 |
FileUtils.deleteQuietly(this.servletTmpDir); |
73 |
|
} |
74 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
75 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
87 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
97 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
107 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
115 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
126 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
135 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
151 |
1 |
@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 |
|
|
165 |
1 |
verify(logger).warn("No permanent directory configured. Using temporary directory [{}].", |
166 |
|
this.servletTmpDir.getCanonicalFile()); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
169 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
177 |
1 |
@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 |
|
|
187 |
1 |
assertEquals(this.servletTmpDir.listFiles()[0].getCanonicalFile(), tmpDir.getCanonicalFile()); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
193 |
1 |
@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 |
|
|
203 |
|
|
204 |
1 |
verify(servletContext).getAttribute("javax.servlet.context.tempdir"); |
205 |
|
} |
206 |
|
} |