diff options
author | ritchie <ritchie@gmx.at> | 2015-10-16 16:09:16 +0200 |
---|---|---|
committer | ritchie <ritchie@gmx.at> | 2015-10-16 16:09:16 +0200 |
commit | cd2779c430690b6ca793d1fafdff0edb0497a686 (patch) | |
tree | eb9c28e4d71fb175cfe9238f391461e54d5f5555 /core | |
parent | 39935521d2b5d8b09cf2261240b7e5f1bd51b026 (diff) | |
download | nanohttpd-cd2779c430690b6ca793d1fafdff0edb0497a686.tar.gz |
little imp correction and more test coverage #243
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/java/fi/iki/elonen/NanoHTTPD.java | 34 | ||||
-rw-r--r-- | core/src/test/java/fi/iki/elonen/JavaIOTempDirExistTest.java | 101 |
2 files changed, 90 insertions, 45 deletions
diff --git a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java index b15c1a9..20fbc79 100644 --- a/core/src/main/java/fi/iki/elonen/NanoHTTPD.java +++ b/core/src/main/java/fi/iki/elonen/NanoHTTPD.java @@ -33,15 +33,6 @@ package fi.iki.elonen; * #L% */ -import fi.iki.elonen.NanoHTTPD.Response.IStatus; -import fi.iki.elonen.NanoHTTPD.Response.Status; -import javax.net.ssl.KeyManager; -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLServerSocket; -import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.TrustManagerFactory; - import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -93,6 +84,16 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPOutputStream; +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.TrustManagerFactory; + +import fi.iki.elonen.NanoHTTPD.Response.IStatus; +import fi.iki.elonen.NanoHTTPD.Response.Status; + /** * A simple, tiny, nicely embeddable HTTP server in Java * <p/> @@ -387,8 +388,8 @@ public abstract class NanoHTTPD { private final OutputStream fstream; - public DefaultTempFile(String tempdir) throws IOException { - this.file = File.createTempFile("NanoHTTPD-", "", new File(tempdir)); + public DefaultTempFile(File tempdir) throws IOException { + this.file = File.createTempFile("NanoHTTPD-", "", tempdir); this.fstream = new FileOutputStream(this.file); } @@ -423,15 +424,14 @@ public abstract class NanoHTTPD { */ public static class DefaultTempFileManager implements TempFileManager { - private final String tmpdir; + private final File tmpdir; private final List<TempFile> tempFiles; public DefaultTempFileManager() { - this.tmpdir = System.getProperty("java.io.tmpdir"); - File dir = new File(tmpdir); - if (!dir.exists()) { - dir.mkdirs(); + this.tmpdir = new File(System.getProperty("java.io.tmpdir")); + if (!tmpdir.exists()) { + tmpdir.mkdirs(); } this.tempFiles = new ArrayList<TempFile>(); } @@ -1104,7 +1104,7 @@ public abstract class NanoHTTPD { /** * HTTP response. Return one of these from serve(). */ - public static class Response implements java.io.Closeable { + public static class Response implements Closeable { public interface IStatus { diff --git a/core/src/test/java/fi/iki/elonen/JavaIOTempDirExistTest.java b/core/src/test/java/fi/iki/elonen/JavaIOTempDirExistTest.java index e5164ad..b586bc2 100644 --- a/core/src/test/java/fi/iki/elonen/JavaIOTempDirExistTest.java +++ b/core/src/test/java/fi/iki/elonen/JavaIOTempDirExistTest.java @@ -1,42 +1,87 @@ package fi.iki.elonen; -import org.junit.Assert; -import org.junit.Test; +/* + * #%L + * NanoHttpd-Core + * %% + * Copyright (C) 2012 - 2015 nanohttpd + * %% + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. Neither the name of the nanohttpd nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * #L% + */ import java.io.File; import java.io.IOException; import java.util.UUID; +import org.junit.Assert; +import org.junit.Test; + +import fi.iki.elonen.NanoHTTPD.DefaultTempFile; + /** * Created by Victor Nikiforov on 10/16/15. */ public class JavaIOTempDirExistTest { - @Test - public void testJavaIoTempDefault() { - NanoHTTPD.DefaultTempFileManager manager = new NanoHTTPD.DefaultTempFileManager(); - String tmpdir = System.getProperty("java.io.tmpdir"); - File dir = new File(tmpdir); - Assert.assertEquals(true, dir.exists()); - } - - @Test - public void testJavaIoTempSpecific() throws IOException { - String tmpdir = System.getProperty("java.io.tmpdir"); - String tempFileName = UUID.randomUUID().toString(); - String orgDir = System.getProperty("java.io.tmpdir"); - String newDir = orgDir + File.separator + tempFileName; - File dir = new File(newDir); - System.setProperty("java.io.tmpdir", newDir); - Assert.assertEquals(false, dir.exists()); - - NanoHTTPD.DefaultTempFileManager manager = new NanoHTTPD.DefaultTempFileManager(); - Assert.assertEquals(true, dir.exists()); - System.setProperty("java.io.tmpdir", tmpdir); - - dir.delete(); - Assert.assertEquals(false, dir.exists()); - - } + @Test + public void testJavaIoTempDefault() throws Exception { + String tmpdir = System.getProperty("java.io.tmpdir"); + NanoHTTPD.DefaultTempFileManager manager = new NanoHTTPD.DefaultTempFileManager(); + DefaultTempFile tempFile = (DefaultTempFile) manager.createTempFile("xx"); + File tempFileBackRef = new File(tempFile.getName()); + Assert.assertEquals(tempFileBackRef.getParentFile(), new File(tmpdir)); + + // force an exception + tempFileBackRef.delete(); + Exception e = null; + try { + tempFile.delete(); + } catch (Exception ex) { + e = ex; + } + Assert.assertNotNull(e); + manager.clear(); + } + + @Test + public void testJavaIoTempSpecific() throws IOException { + final String tmpdir = System.getProperty("java.io.tmpdir"); + try { + String tempFileName = UUID.randomUUID().toString(); + File newDir = new File("target", tempFileName); + System.setProperty("java.io.tmpdir", newDir.getAbsolutePath()); + Assert.assertEquals(false, newDir.exists()); + new NanoHTTPD.DefaultTempFileManager(); + Assert.assertEquals(true, newDir.exists()); + newDir.delete(); + } finally { + System.setProperty("java.io.tmpdir", tmpdir); + } + + } } |