Refactored to use the constructor instead of the class/static method to load properties - makes unit test runtime cleaning

This commit is contained in:
2025-06-15 10:36:11 -05:00
parent d13fc4a863
commit f6859d040f
2 changed files with 38 additions and 16 deletions

View File

@ -51,8 +51,21 @@ public class SimpleFileSystemDirectoryRouter implements QJavalinRouteProviderInt
private static final QLogger LOG = QLogger.getLogger(SimpleFileSystemDirectoryRouter.class);
public static boolean loadStaticFilesFromJar = false;
static
private final String hostedPath;
private final String fileSystemPath;
private QCodeReference routeAuthenticator;
private QInstance qInstance;
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public SimpleFileSystemDirectoryRouter(String hostedPath, String fileSystemPath)
{
this.hostedPath = hostedPath;
this.fileSystemPath = fileSystemPath;
///////////////////////////////////////////////////////////////////////////////////////////////////////
// read the property to see if we should load static files from the jar file or from the file system //
// Javan only supports loading via one method per path, so its a choice of one or the other... //
@ -72,21 +85,6 @@ public class SimpleFileSystemDirectoryRouter implements QJavalinRouteProviderInt
}
}
private final String hostedPath;
private final String fileSystemPath;
private QCodeReference routeAuthenticator;
private QInstance qInstance;
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public SimpleFileSystemDirectoryRouter(String hostedPath, String fileSystemPath)
{
this.hostedPath = hostedPath;
this.fileSystemPath = fileSystemPath;
}
/***************************************************************************

View File

@ -29,6 +29,7 @@ import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.instances.AbstractQQQApplication;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.javalin.TestUtils;
import com.kingsrook.qqq.middleware.javalin.routeproviders.SimpleFileSystemDirectoryRouter;
import com.kingsrook.qqq.middleware.javalin.specs.v1.MiddlewareVersionV1;
import io.javalin.http.HttpStatus;
import kong.unirest.HttpResponse;
@ -70,6 +71,7 @@ class QApplicationJavalinServerTest
{
javalinServer.stop();
TestApplication.callCount = 0;
System.clearProperty("qqq.javalin.enableStaticFilesFromJar");
}
@ -226,6 +228,28 @@ class QApplicationJavalinServerTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testStaticRouterFilesFromClasspath() throws Exception
{
System.setProperty("qqq.javalin.enableStaticFilesFromJar", "true");
javalinServer = new QApplicationJavalinServer(new QApplicationJavalinServerTest.TestApplication())
.withServeFrontendMaterialDashboard(false)
.withPort(PORT)
.withAdditionalRouteProvider(new SimpleFileSystemDirectoryRouter("/statically-served-from-jar", "static-site-from-jar/"));
javalinServer.start();
Unirest.config().setDefaultResponseEncoding("UTF-8");
HttpResponse<String> response = Unirest.get("http://localhost:" + PORT + "/statically-served-from-jar/foo-in-jar.html").asString();
assertEquals("Foo in a Jar!\n", response.getBody());
}
/*******************************************************************************
**
*******************************************************************************/