Skip to content

Commit

Permalink
Move change to PropertyReader.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Jun 21, 2023
1 parent 90b32bf commit 84299ef
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 53 deletions.
Expand Up @@ -43,7 +43,7 @@ public class WikiTest {
public void testWikiInit() {
Mockito.doReturn( sc ).when( conf ).getServletContext();
final Properties properties = Wiki.init( sc );
Assertions.assertEquals( 5, properties.size() );
Assertions.assertEquals( 6, properties.size() );

// verify SPIs are initialized and can be invoked
Assertions.assertNull( Wiki.acls().acl() );
Expand Down
Expand Up @@ -47,7 +47,6 @@ public class WikiBootstrapServletContextListener implements ServletContextListen
public void contextInitialized( final ServletContextEvent sce ) {
final Properties properties = initWikiSPIs( sce );
initWikiLoggingFramework( properties );
setWorkDir(properties, sce.getServletContext());
}

/**
Expand Down Expand Up @@ -100,34 +99,4 @@ ConfigurationSource createConfigurationSource( final Properties properties ) {
@Override
public void contextDestroyed( final ServletContextEvent sce ) {
}

/**
* This method sets the JSPWiki working directory (jspwiki.workDir). It first checks if this property
* is already set. If it isn't, it attempts to use the servlet container's temporary directory
* (javax.servlet.context.tempdir). If that is also unavailable, it defaults to the system's temporary
* directory (java.io.tmpdir).
* <p>
* This method is package-private to allow for unit testing.
*
* @param properties the JSPWiki properties
* @param servletContext the Servlet context from which to fetch the tempdir if needed
* @since JSPWiki 2.11.1
*/
void setWorkDir(final Properties properties, final ServletContext servletContext) {
final String workDir = TextUtil.getStringProperty(properties, "jspwiki.workDir", null);
if (workDir == null) {
final File tempDir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
if (tempDir != null) {
properties.setProperty("jspwiki.workDir", tempDir.getAbsolutePath());
LOG.info("Setting jspwiki.workDir to ServletContext's temporary directory: {}", tempDir.getAbsolutePath());
} else {
final String defaultTmpDir = System.getProperty("java.io.tmpdir");
properties.setProperty("jspwiki.workDir", defaultTmpDir);
LOG.info("ServletContext's temporary directory not found. Setting jspwiki.workDir to system's temporary directory: {}", defaultTmpDir);
}
} else {
LOG.info("jspwiki.workDir is already set to: {}", workDir);
}
}

}
Expand Up @@ -26,12 +26,9 @@ Licensed to the Apache Software Foundation (ASF) under one

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import java.io.File;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@ExtendWith( MockitoExtension.class )
Expand All @@ -46,7 +43,7 @@ public void testWikiInit() {
final WikiBootstrapServletContextListener listener = new WikiBootstrapServletContextListener();
final Properties properties = listener.initWikiSPIs( sce );

assertEquals( 35, properties.size() );
Assertions.assertEquals( 36, properties.size() );
}

@Test
Expand All @@ -67,20 +64,4 @@ public void testServletContextListenerLifeCycle() {
Assertions.assertDoesNotThrow( () -> listener.contextInitialized( sce ) );
Assertions.assertDoesNotThrow( () -> listener.contextDestroyed( sce ) );
}

@Test
public void testSetWorkDir() {
// Given
final WikiBootstrapServletContextListener listener = new WikiBootstrapServletContextListener();
final Properties properties = new Properties();
final ServletContext servletContext = mock(ServletContext.class);
final File tempDir = new File(System.getProperty("java.io.tmpdir"));
when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(tempDir);

// When
listener.setWorkDir(properties, servletContext);

// Then
assertEquals(properties.getProperty("jspwiki.workDir"), tempDir.getAbsolutePath());
}
}
Expand Up @@ -86,7 +86,7 @@ public Installer( final HttpServletRequest request, final ServletConfig config )
// Stash the request
m_request = request;
m_validated = false;
TMP_DIR = m_engine.getWikiProperties().getProperty( "java.io.tmpdir" );
TMP_DIR = m_engine.getWikiProperties().getProperty( "jspwiki.workDir" );
}

/**
Expand Down
7 changes: 7 additions & 0 deletions jspwiki-util/pom.xml
Expand Up @@ -99,5 +99,12 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Expand Up @@ -139,6 +139,9 @@ public static Properties loadWebAppProps( final ServletContext context ) {
// now load the cascade (new in 2.5)
loadWebAppPropsCascade( context, props );

// sets the JSPWiki working directory (jspwiki.workDir)
setWorkDir( context, props );

// add system properties beginning with jspwiki...
final Map< String, String > sysprops = collectPropertiesFrom( System.getProperties().entrySet().stream()
.collect( Collectors.toMap( Object::toString, Object::toString ) ) );
Expand Down Expand Up @@ -393,4 +396,33 @@ static String createResourceLocation( final String path, final String name ) {
return result.toString();
}

/**
* This method sets the JSPWiki working directory (jspwiki.workDir). It first checks if this property
* is already set. If it isn't, it attempts to use the servlet container's temporary directory
* (javax.servlet.context.tempdir). If that is also unavailable, it defaults to the system's temporary
* directory (java.io.tmpdir).
* <p>
* This method is package-private to allow for unit testing.
*
* @param properties the JSPWiki properties
* @param servletContext the Servlet context from which to fetch the tempdir if needed
* @since JSPWiki 2.11.1
*/
static void setWorkDir( final ServletContext servletContext, final Properties properties ) {
final String workDir = TextUtil.getStringProperty(properties, "jspwiki.workDir", null);
if (workDir == null) {
final File tempDir = (File) servletContext.getAttribute("javax.servlet.context.tempdir");
if (tempDir != null) {
properties.setProperty("jspwiki.workDir", tempDir.getAbsolutePath());
LOG.info("Setting jspwiki.workDir to ServletContext's temporary directory: {}", tempDir.getAbsolutePath());
} else {
final String defaultTmpDir = System.getProperty("java.io.tmpdir");
properties.setProperty("jspwiki.workDir", defaultTmpDir);
LOG.info("ServletContext's temporary directory not found. Setting jspwiki.workDir to system's temporary directory: {}", defaultTmpDir);
}
} else {
LOG.info("jspwiki.workDir is already set to: {}", workDir);
}
}

}
Expand Up @@ -22,11 +22,16 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static org.mockito.Mockito.mock;


/**
* Unit test for PropertyReader.
Expand Down Expand Up @@ -122,4 +127,30 @@ public void testCollectPropertiesFrom() {
Assertions.assertNull( test.get( "secretEnv" ) );
}

@Test
public void testSetWorkDir() {
final Properties properties = new Properties();
final ServletContext servletContext = mock(ServletContext.class);
Mockito.when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(new File("/tmp"));

PropertyReader.setWorkDir(servletContext, properties);

// Test when the "jspwiki.workDir" is not set, it should get set to servlet's temporary directory
String workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals("/tmp", workDir);

// Test when the "jspwiki.workDir" is set, it should remain as it is
properties.setProperty("jspwiki.workDir", "/custom/dir");
PropertyReader.setWorkDir(servletContext, properties);
workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals("/custom/dir", workDir);

// Test when the servlet's temporary directory is null, it should get set to system's temporary directory
Mockito.when(servletContext.getAttribute("javax.servlet.context.tempdir")).thenReturn(null);
properties.remove("jspwiki.workDir");
PropertyReader.setWorkDir(servletContext, properties);
workDir = properties.getProperty("jspwiki.workDir");
Assertions.assertEquals(System.getProperty("java.io.tmpdir"), workDir);
}

}

0 comments on commit 84299ef

Please sign in to comment.