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.URL; |
24 |
|
|
25 |
|
import javax.inject.Provider; |
26 |
|
|
27 |
|
import org.apache.commons.io.FileUtils; |
28 |
|
import org.jmock.Expectations; |
29 |
|
import org.junit.After; |
30 |
|
import org.junit.Assert; |
31 |
|
import org.junit.Before; |
32 |
|
import org.junit.Rule; |
33 |
|
import org.junit.Test; |
34 |
|
import org.slf4j.Logger; |
35 |
|
import org.xwiki.component.embed.EmbeddableComponentManager; |
36 |
|
import org.xwiki.component.util.ReflectionUtils; |
37 |
|
import org.xwiki.environment.Environment; |
38 |
|
import org.xwiki.test.jmock.JMockRule; |
39 |
|
|
40 |
|
|
41 |
|
@link |
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 95.3% |
Uncovered Elements: 4 (85) |
Complexity: 22 |
Complexity Density: 0.36 |
|
46 |
|
public class StandardEnvironmentTest |
47 |
|
{ |
48 |
|
private static final File TMPDIR = new File(System.getProperty("java.io.tmpdir"), "xwiki-temp"); |
49 |
|
|
50 |
|
@Rule |
51 |
|
public final JMockRule mockery = new JMockRule(); |
52 |
|
|
53 |
|
private StandardEnvironment environment; |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
55 |
11 |
@Before... |
56 |
|
public void setUp() throws Exception |
57 |
|
{ |
58 |
11 |
EmbeddableComponentManager ecm = new EmbeddableComponentManager(); |
59 |
11 |
ecm.initialize(getClass().getClassLoader()); |
60 |
11 |
this.environment = (StandardEnvironment) ecm.getInstance(Environment.class); |
61 |
11 |
ReflectionUtils.setFieldValue(this.environment, "isTesting", true); |
62 |
11 |
if (TMPDIR.exists()) { |
63 |
1 |
FileUtils.forceDelete(TMPDIR); |
64 |
|
} |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
67 |
11 |
@After... |
68 |
|
public void tearDown() throws Exception |
69 |
|
{ |
70 |
11 |
if (TMPDIR.exists()) { |
71 |
3 |
FileUtils.forceDelete(TMPDIR); |
72 |
|
} |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
75 |
1 |
@Test... |
76 |
|
public void testGetResourceWhenResourceDirNotSet() |
77 |
|
{ |
78 |
1 |
Assert.assertNull(this.environment.getResource("doesntexist")); |
79 |
|
} |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
81 |
1 |
@Test... |
82 |
|
public void testGetResourceOk() |
83 |
|
{ |
84 |
|
|
85 |
|
|
86 |
1 |
File resourceFile = new File("target/testGetResourceOk"); |
87 |
1 |
resourceFile.mkdirs(); |
88 |
|
|
89 |
1 |
this.environment.setResourceDirectory(new File("target")); |
90 |
1 |
URL resource = this.environment.getResource("testGetResourceOk"); |
91 |
1 |
Assert.assertNotNull(resource); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void testGetResourceWhenResourceDirNotSetButResourceAvailableInDefaultClassLoader() |
96 |
|
{ |
97 |
1 |
URL resource = this.environment.getResource("test"); |
98 |
1 |
Assert.assertNotNull(resource); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
101 |
1 |
@Test... |
102 |
|
public void testGetResourceWhenResourceDirSetButResourceAvailableInDefaultClassLoader() |
103 |
|
{ |
104 |
1 |
this.environment.setResourceDirectory(new File("/resource")); |
105 |
1 |
URL resource = this.environment.getResource("test"); |
106 |
1 |
Assert.assertNotNull(resource); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void testGetPermanentDirectory() |
111 |
|
{ |
112 |
1 |
File permanentDirectory = new File("/permanent"); |
113 |
1 |
this.environment.setPermanentDirectory(permanentDirectory); |
114 |
1 |
Assert.assertEquals(permanentDirectory, this.environment.getPermanentDirectory()); |
115 |
|
} |
116 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
117 |
1 |
private void setPersistentDir(final String dirPath)... |
118 |
|
{ |
119 |
1 |
@SuppressWarnings("unchecked") |
120 |
|
final Provider<EnvironmentConfiguration> configurationProvider = this.mockery.mock(Provider.class); |
121 |
1 |
final EnvironmentConfiguration config = this.mockery.mock(EnvironmentConfiguration.class); |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
122 |
1 |
this.mockery.checking(new Expectations() {{... |
123 |
1 |
allowing(configurationProvider).get(); |
124 |
1 |
will(returnValue(config)); |
125 |
1 |
allowing(config).getPermanentDirectoryPath(); |
126 |
1 |
will(returnValue(dirPath)); |
127 |
|
}}); |
128 |
1 |
ReflectionUtils.setFieldValue(this.environment, "configurationProvider", configurationProvider); |
129 |
|
} |
130 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
131 |
1 |
@Test... |
132 |
|
public void testGetConfiguredPermanentDirectory() |
133 |
|
{ |
134 |
1 |
final File persistentDir = |
135 |
|
new File(System.getProperty("java.io.tmpdir"), "xwiki-test-persistentDir"); |
136 |
1 |
this.setPersistentDir(persistentDir.getAbsolutePath()); |
137 |
1 |
Assert.assertEquals(persistentDir, this.environment.getPermanentDirectory()); |
138 |
|
} |
139 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
140 |
1 |
@Test... |
141 |
|
public void testGetPermanentDirectoryWhenNotSet() |
142 |
|
{ |
143 |
|
|
144 |
1 |
final Logger logger = this.mockery.mock(Logger.class); |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
145 |
1 |
this.mockery.checking(new Expectations() {{... |
146 |
1 |
oneOf(logger).warn("No permanent directory configured. Using temporary directory [{}].", |
147 |
|
new File(System.getProperty("java.io.tmpdir"))); |
148 |
|
}}); |
149 |
|
|
150 |
1 |
ReflectionUtils.setFieldValue(this.environment, "logger", logger); |
151 |
|
|
152 |
1 |
Assert.assertEquals(new File(System.getProperty("java.io.tmpdir")), this.environment.getPermanentDirectory()); |
153 |
|
} |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
155 |
1 |
@Test... |
156 |
|
public void testGetTemporaryDirectory() |
157 |
|
{ |
158 |
1 |
File tmpDir = new File("tmpdir"); |
159 |
1 |
this.environment.setTemporaryDirectory(tmpDir); |
160 |
1 |
Assert.assertEquals(tmpDir, this.environment.getTemporaryDirectory()); |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
163 |
1 |
@Test... |
164 |
|
public void testGetTemporaryDirectoryWhenNotSet() |
165 |
|
{ |
166 |
1 |
Assert.assertEquals(TMPDIR, this.environment.getTemporaryDirectory()); |
167 |
|
} |
168 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
169 |
1 |
@Test(expected = RuntimeException.class)... |
170 |
|
public void testGetTemporaryDirectoryWhenNotADirectory() throws Exception |
171 |
|
{ |
172 |
1 |
FileUtils.write(TMPDIR, "test"); |
173 |
|
|
174 |
1 |
final Logger logger = this.mockery.mock(Logger.class); |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
175 |
1 |
this.mockery.checking(new Expectations() {{... |
176 |
1 |
oneOf(logger).error("Configured {} directory [{}] is {}.", "temporary", |
177 |
|
TMPDIR.getAbsolutePath(), |
178 |
|
"not a directory"); |
179 |
|
}}); |
180 |
1 |
ReflectionUtils.setFieldValue(this.environment, "logger", logger); |
181 |
|
|
182 |
1 |
this.environment.getTemporaryDirectory(); |
183 |
|
} |
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
185 |
1 |
@Test... |
186 |
|
public void testGetTemporaryDirectoryFailOver() throws Exception |
187 |
|
{ |
188 |
1 |
FileUtils.forceMkdir(TMPDIR); |
189 |
1 |
final File txtFile = new File(TMPDIR, "test.txt"); |
190 |
1 |
FileUtils.write(txtFile, "test"); |
191 |
|
|
192 |
1 |
final Provider<EnvironmentConfiguration> prov = new Provider<EnvironmentConfiguration>() |
193 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
194 |
0 |
@Override... |
195 |
|
public EnvironmentConfiguration get() |
196 |
|
{ |
197 |
0 |
return new EnvironmentConfiguration() |
198 |
|
{ |
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
199 |
0 |
@Override... |
200 |
|
public String getPermanentDirectoryPath() |
201 |
|
{ |
202 |
0 |
return txtFile.getAbsolutePath(); |
203 |
|
} |
204 |
|
}; |
205 |
|
} |
206 |
|
}; |
207 |
1 |
ReflectionUtils.setFieldValue(this.environment, "configurationProvider", prov); |
208 |
|
|
209 |
1 |
final Logger logger = this.mockery.mock(Logger.class); |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
1 |
this.mockery.checking(new Expectations() {{... |
211 |
1 |
allowing(logger).error(with(any(String.class)), with(any(Object[].class))); |
212 |
|
}}); |
213 |
1 |
ReflectionUtils.setFieldValue(this.environment, "logger", logger); |
214 |
|
|
215 |
1 |
Assert.assertEquals(TMPDIR, this.environment.getTemporaryDirectory()); |
216 |
|
|
217 |
|
|
218 |
1 |
Assert.assertEquals(0, TMPDIR.listFiles().length); |
219 |
|
} |
220 |
|
} |