1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.environment.internal

File StandardEnvironmentTest.java

 

Code metrics

4
47
13
1
193
136
15
0.32
3.62
13
1.15

Classes

Class Line # Actions
StandardEnvironmentTest 51 47 0% 15 0
1.0100%
 

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 org.apache.commons.io.FileUtils;
26    import org.junit.jupiter.api.AfterEach;
27    import org.junit.jupiter.api.BeforeEach;
28    import org.junit.jupiter.api.Test;
29    import org.junit.jupiter.api.extension.RegisterExtension;
30    import org.xwiki.test.LogLevel;
31    import org.xwiki.test.junit5.LogCaptureExtension;
32    import org.xwiki.test.junit5.mockito.ComponentTest;
33    import org.xwiki.test.junit5.mockito.InjectMockComponents;
34    import org.xwiki.test.mockito.MockitoComponentManager;
35   
36    import ch.qos.logback.classic.Level;
37   
38    import static org.junit.jupiter.api.Assertions.assertEquals;
39    import static org.junit.jupiter.api.Assertions.assertNotNull;
40    import static org.junit.jupiter.api.Assertions.assertNull;
41    import static org.junit.jupiter.api.Assertions.assertThrows;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Unit tests for {@link StandardEnvironment}.
46    *
47    * @version $Id: 432593a42a80e4058f33ae10bcb036faf0c13e79 $
48    * @since 3.5M1
49    */
50    @ComponentTest
 
51    public class StandardEnvironmentTest
52    {
53    private static final File TMPDIR = new File(System.getProperty("java.io.tmpdir"), "xwiki-temp");
54   
55    @InjectMockComponents
56    private StandardEnvironment environment;
57   
58    @RegisterExtension
59    LogCaptureExtension logCapture = new LogCaptureExtension(LogLevel.INFO);
60   
 
61  11 toggle @BeforeEach
62    public void setUp() throws Exception
63    {
64  11 if (TMPDIR.exists()) {
65  1 FileUtils.forceDelete(TMPDIR);
66    }
67    }
68   
 
69  11 toggle @AfterEach
70    public void tearDown() throws Exception
71    {
72  11 if (TMPDIR.exists()) {
73  3 FileUtils.forceDelete(TMPDIR);
74    }
75    }
76   
 
77  1 toggle @Test
78    public void getResourceWhenResourceDirNotSet()
79    {
80  1 assertNull(this.environment.getResource("doesntexist"));
81    }
82   
 
83  1 toggle @Test
84    public void getResourceOk()
85    {
86    // Make sure our resource really exists on the file system...
87    // TODO: find a better way...
88  1 File resourceFile = new File("target/getResourceOk");
89  1 resourceFile.mkdirs();
90   
91  1 this.environment.setResourceDirectory(new File("target"));
92  1 URL resource = this.environment.getResource("getResourceOk");
93  1 assertNotNull(resource);
94    }
95   
 
96  1 toggle @Test
97    public void getResourceWhenResourceDirNotSetButResourceAvailableInDefaultClassLoader()
98    {
99  1 URL resource = this.environment.getResource("test");
100  1 assertNotNull(resource);
101    }
102   
 
103  1 toggle @Test
104    public void getResourceWhenResourceDirSetButResourceAvailableInDefaultClassLoader()
105    {
106  1 this.environment.setResourceDirectory(new File("/resource"));
107  1 URL resource = this.environment.getResource("test");
108  1 assertNotNull(resource);
109    }
110   
 
111  1 toggle @Test
112    public void getPermanentDirectory()
113    {
114  1 File permanentDirectory = new File("/permanent");
115  1 this.environment.setPermanentDirectory(permanentDirectory);
116  1 assertEquals(permanentDirectory, this.environment.getPermanentDirectory());
117    }
118   
 
119  1 toggle @Test
120    public void getConfiguredPermanentDirectory(MockitoComponentManager componentManager) throws Exception
121    {
122  1 File persistentDir =
123    new File(System.getProperty("java.io.tmpdir"), "xwiki-test-persistentDir").getAbsoluteFile();
124  1 EnvironmentConfiguration configuration = componentManager.getInstance(EnvironmentConfiguration.class);
125  1 when(configuration.getPermanentDirectoryPath()).thenReturn(persistentDir.getAbsolutePath());
126   
127  1 assertEquals(persistentDir, this.environment.getPermanentDirectory());
128   
129  1 assertEquals(1, this.logCapture.size());
130  1 assertEquals(String.format("Using permanent directory [%s]", persistentDir), this.logCapture.getMessage(0));
131    }
132   
 
133  1 toggle @Test
134    public void getPermanentDirectoryWhenNotSet()
135    {
136  1 assertEquals(new File(System.getProperty("java.io.tmpdir")), this.environment.getPermanentDirectory());
137   
138  1 assertEquals(2, this.logCapture.size());
139  1 assertEquals(Level.WARN, this.logCapture.getLogEvent(0).getLevel());
140  1 assertEquals("No permanent directory configured, fallbacking to temporary directory. You should set the "
141    + "\"environment.permanentDirectory\" configuration property in the xwiki.properties file.",
142    this.logCapture.getMessage(0));
143  1 assertEquals(Level.INFO, this.logCapture.getLogEvent(1).getLevel());
144  1 assertEquals(String.format("Using permanent directory [%s]",
145    new File(System.getProperty("java.io.tmpdir"))), this.logCapture.getMessage(1));
146    }
147   
 
148  1 toggle @Test
149    public void getTemporaryDirectory()
150    {
151  1 File tmpDir = new File("tmpdir");
152  1 this.environment.setTemporaryDirectory(tmpDir);
153  1 assertEquals(tmpDir, this.environment.getTemporaryDirectory());
154    }
155   
 
156  1 toggle @Test
157    public void getTemporaryDirectoryWhenNotSet()
158    {
159  1 assertEquals(TMPDIR, this.environment.getTemporaryDirectory());
160    }
161   
 
162  1 toggle @Test
163    public void getTemporaryDirectoryWhenNotADirectory() throws Exception
164    {
165  1 FileUtils.write(TMPDIR, "test", "UTF-8");
166   
167  1 Throwable exception = assertThrows(RuntimeException.class, () -> {
168  1 this.environment.getTemporaryDirectory();
169    });
170  1 assertEquals("Could not find a writable temporary directory. Check the server logs for more information.",
171    exception.getMessage());
172   
173  1 assertEquals(1, this.logCapture.size());
174  1 assertEquals(String.format("Configured temporary directory [%s] is not a directory.",
175    TMPDIR.getAbsolutePath()), this.logCapture.getMessage(0));
176    }
177   
 
178  1 toggle @Test
179    public void getTemporaryDirectoryFailOver(MockitoComponentManager componentManager) throws Exception
180    {
181  1 FileUtils.forceMkdir(TMPDIR);
182  1 File txtFile = new File(TMPDIR, "test.txt");
183  1 FileUtils.write(txtFile, "test", "UTF-8");
184   
185  1 EnvironmentConfiguration configuration = componentManager.getInstance(EnvironmentConfiguration.class);
186  1 when(configuration.getPermanentDirectoryPath()).thenReturn(txtFile.getAbsolutePath());
187   
188  1 assertEquals(TMPDIR, this.environment.getTemporaryDirectory());
189   
190    // Check that the directory was cleared.
191  1 assertEquals(0, TMPDIR.listFiles().length);
192    }
193    }