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

File StandardEnvironmentTest.java

 

Code metrics

4
61
20
1
220
162
22
0.36
3.05
20
1.1

Classes

Class Line # Actions
StandardEnvironmentTest 46 61 0% 22 4
0.952941295.3%
 

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.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    * Unit tests for {@link StandardEnvironment}.
42    *
43    * @version $Id: 33da601b557640f87053be5d5c0f376a65b5500b $
44    * @since 3.5M1
45    */
 
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   
 
55  11 toggle @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   
 
67  11 toggle @After
68    public void tearDown() throws Exception
69    {
70  11 if (TMPDIR.exists()) {
71  3 FileUtils.forceDelete(TMPDIR);
72    }
73    }
74   
 
75  1 toggle @Test
76    public void testGetResourceWhenResourceDirNotSet()
77    {
78  1 Assert.assertNull(this.environment.getResource("doesntexist"));
79    }
80   
 
81  1 toggle @Test
82    public void testGetResourceOk()
83    {
84    // Make sure our resource really exists on the file system...
85    // TODO: find a better way...
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   
 
94  1 toggle @Test
95    public void testGetResourceWhenResourceDirNotSetButResourceAvailableInDefaultClassLoader()
96    {
97  1 URL resource = this.environment.getResource("test");
98  1 Assert.assertNotNull(resource);
99    }
100   
 
101  1 toggle @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   
 
109  1 toggle @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   
 
117  1 toggle 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);
 
122  1 toggle 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   
 
131  1 toggle @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   
 
140  1 toggle @Test
141    public void testGetPermanentDirectoryWhenNotSet()
142    {
143    // Also verify that we log a warning!
144  1 final Logger logger = this.mockery.mock(Logger.class);
 
145  1 toggle 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   
 
155  1 toggle @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   
 
163  1 toggle @Test
164    public void testGetTemporaryDirectoryWhenNotSet()
165    {
166  1 Assert.assertEquals(TMPDIR, this.environment.getTemporaryDirectory());
167    }
168   
 
169  1 toggle @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);
 
175  1 toggle 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   
 
185  1 toggle @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    {
 
194  0 toggle @Override
195    public EnvironmentConfiguration get()
196    {
197  0 return new EnvironmentConfiguration()
198    {
 
199  0 toggle @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);
 
210  1 toggle 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    // Check that the directory was cleared.
218  1 Assert.assertEquals(0, TMPDIR.listFiles().length);
219    }
220    }