Skip to content

Commit

Permalink
junit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
perwendel committed Sep 28, 2015
1 parent 05278c4 commit 70b54d7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/main/java/spark/staticfiles/StaticFiles.java
Expand Up @@ -95,7 +95,6 @@ public static void configureStaticResources(String folder) {
staticResourceHandlers = new ArrayList<>(); staticResourceHandlers = new ArrayList<>();
} }
staticResourceHandlers.add(new ClassPathResourceHandler(folder, "index.html")); staticResourceHandlers.add(new ClassPathResourceHandler(folder, "index.html"));
System.out.println("StaticResourceHandler configured with folder = " + folder);
LOG.info("StaticResourceHandler configured with folder = " + folder); LOG.info("StaticResourceHandler configured with folder = " + folder);
} catch (IOException e) { } catch (IOException e) {
LOG.error("Error when creating StaticResourceHandler", e); LOG.error("Error when creating StaticResourceHandler", e);
Expand Down
20 changes: 13 additions & 7 deletions src/test/java/spark/StaticFilesTest.java
Expand Up @@ -43,18 +43,26 @@ public class StaticFilesTest {


private static final String EXTERNAL_FILE_NAME_HTML = "externalFile.html"; private static final String EXTERNAL_FILE_NAME_HTML = "externalFile.html";
private static final String EXTERNAL_FILE_NAME_CSS = "stylish.css"; private static final String EXTERNAL_FILE_NAME_CSS = "stylish.css";
public static final String CONTENT_OF_EXTERNAL_FILE = "Content of external file";
public static final String SUB_DIR = "subdir"; private static final String CONTENT_OF_EXTERNAL_FILE = "Content of external file";
private static final String SUB_DIR = "subdir";


private static SparkTestUtil testUtil; private static SparkTestUtil testUtil;

private static File tmpExternalFile; private static File tmpExternalFile;
private static File tmpExternalFolder;


@AfterClass @AfterClass
public static void tearDown() { public static void tearDown() {
Spark.stop(); Spark.stop();
if (tmpExternalFile != null) { if (tmpExternalFile != null) {
LOGGER.debug("tearDown().deleting: " + tmpExternalFile);
tmpExternalFile.delete(); tmpExternalFile.delete();
} }
if (tmpExternalFolder != null) {
LOGGER.debug("tearDown().deleting: " + tmpExternalFolder);
tmpExternalFolder.delete();
}
} }


@BeforeClass @BeforeClass
Expand All @@ -64,8 +72,6 @@ public static void setup() throws IOException {
tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML); tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE_NAME_HTML);
createExternalSubDirectoryAndFile(System.getProperty("java.io.tmpdir") + SUB_DIR); createExternalSubDirectoryAndFile(System.getProperty("java.io.tmpdir") + SUB_DIR);


System.out.println("System.getProperty(\"java.io.tmpdir\" = " + System.getProperty("java.io.tmpdir"));

FileWriter writer = new FileWriter(tmpExternalFile); FileWriter writer = new FileWriter(tmpExternalFile);
writer.write(CONTENT_OF_EXTERNAL_FILE); writer.write(CONTENT_OF_EXTERNAL_FILE);
writer.flush(); writer.flush();
Expand All @@ -80,15 +86,15 @@ public static void setup() throws IOException {
} }


private static void createExternalSubDirectoryAndFile(String directoryName) throws IOException { private static void createExternalSubDirectoryAndFile(String directoryName) throws IOException {
File directory = new File(directoryName); tmpExternalFolder = new File(directoryName);


// if the directory does not exist, create it // if the directory does not exist, create it
if (!directory.exists()) { if (!tmpExternalFolder.exists()) {
System.out.println("creating directory: " + directoryName); System.out.println("creating directory: " + directoryName);
boolean result = false; boolean result = false;


try { try {
directory.mkdir(); tmpExternalFolder.mkdir();
result = true; result = true;
} catch (SecurityException se) { } catch (SecurityException se) {
//handle it //handle it
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/spark/servlet/MyApp.java
Expand Up @@ -16,13 +16,15 @@ public class MyApp implements SparkApplication {


public static final String EXTERNAL_FILE = "externalFileServlet.html"; public static final String EXTERNAL_FILE = "externalFileServlet.html";


static File tmpExternalFile;

@Override @Override
public void init() { public void init() {
try { try {
externalStaticFileLocation(System.getProperty("java.io.tmpdir")); externalStaticFileLocation(System.getProperty("java.io.tmpdir"));
staticFileLocation("/public"); staticFileLocation("/public");


File tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE); tmpExternalFile = new File(System.getProperty("java.io.tmpdir"), EXTERNAL_FILE);
FileWriter writer = new FileWriter(tmpExternalFile); FileWriter writer = new FileWriter(tmpExternalFile);
writer.write("Content of external file"); writer.write("Content of external file");
writer.flush(); writer.flush();
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/spark/servlet/ServletTest.java
Expand Up @@ -28,6 +28,10 @@ public class ServletTest {
@AfterClass @AfterClass
public static void tearDown() { public static void tearDown() {
Spark.stop(); Spark.stop();
if (MyApp.tmpExternalFile != null) {
LOGGER.debug("tearDown().deleting: " + MyApp.tmpExternalFile);
MyApp.tmpExternalFile.delete();
}
} }


@BeforeClass @BeforeClass
Expand Down

0 comments on commit 70b54d7

Please sign in to comment.