ServletEnvironment extends AbstractEnvironment { public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; }  void setServletContext(ServletContext servletContext); ServletContext getServletContext(); @Override InputStream getResourceAsStream(String resourceName); @Override URL getResource(String resourceName);  }
@Test void getPermanentDirectoryWhenSetWithSystemProperty() throws Exception { Logger logger = mock(Logger.class); ReflectionUtils.setFieldValue(this.environment, "logger", logger); File expectedPermanentDirectory = new File(System.getProperty("java.io.tmpdir"), "permanent"); System.setProperty("xwiki.data.dir", expectedPermanentDirectory.toString()); try { this.environment.setServletContext(mock(ServletContext.class)); assertEquals(expectedPermanentDirectory.getCanonicalFile(), this.environment.getPermanentDirectory().getCanonicalFile()); } finally { System.clearProperty("xwiki.data.dir"); } verify(logger).info("Using permanent directory [{}]", expectedPermanentDirectory); }
@Test void getPermanentDirectoryWhenNotSet() throws Exception { ServletContext servletContext = mock(ServletContext.class); when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(this.servletTmpDir); this.environment.setServletContext(servletContext); Logger logger = mock(Logger.class); ReflectionUtils.setFieldValue(this.environment, "logger", logger); assertEquals(this.servletTmpDir.getCanonicalFile(), this.environment.getPermanentDirectory().getCanonicalFile()); verify(logger).warn("No permanent directory configured, fallbacking to temporary directory. You should set " + "the \"environment.permanentDirectory\" configuration property in the xwiki.properties file."); verify(logger).info("Using permanent directory [{}]", this.servletTmpDir.getCanonicalFile()); }
@Test void getTemporaryDirectoryWhenNotSet() throws Exception { ServletContext servletContext = mock(ServletContext.class); when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(this.servletTmpDir); this.environment.setServletContext(servletContext); File tmpDir = this.environment.getTemporaryDirectory(); assertEquals(this.servletTmpDir.listFiles()[0].getCanonicalFile(), tmpDir.getCanonicalFile()); }
@Test void getTemporaryDirectoryWhenServletTempDirNotSet() throws Exception { ServletContext servletContext = mock(ServletContext.class); this.environment.setServletContext(servletContext); assertEquals(this.systemTmpDir.getCanonicalFile(), this.environment.getTemporaryDirectory().getCanonicalFile()); verify(servletContext).getAttribute("javax.servlet.context.tempdir"); }