From 178baa451e13e56b1a1af0fb4b896ebe8112e982 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 26 Jan 2017 20:03:05 +0100 Subject: [PATCH] Fix deprecation warnings from commons-io and other compiler warnings --- .../commons/http/HttpClientWrapper.java | 2 +- .../org/dstadler/commons/zip/ZipUtils.java | 15 +++++----- .../commons/exec/ExecutionHelperTest.java | 14 +++++----- .../commons/graphviz/DotUtilsTest.java | 16 +++++------ .../commons/http/HttpClientWrapperTest.java | 4 +-- .../dstadler/commons/http/NanoHTTPDTest.java | 4 +-- .../io/DeleteOnCloseInputStreamTest.java | 10 +++---- .../commons/io/TailInputStreamTest.java | 10 +++---- .../commons/util/DocumentStarterTest.java | 2 +- .../xml/AbstractSimpleContentHandlerTest.java | 6 ++-- .../zip/ZipFileCloseInputStreamTest.java | 22 +++++++++------ .../commons/zip/ZipFileWalkerTest.java | 6 ++-- .../dstadler/commons/zip/ZipUtilsTest.java | 28 +++++++++---------- 13 files changed, 72 insertions(+), 67 deletions(-) diff --git a/src/main/java/org/dstadler/commons/http/HttpClientWrapper.java b/src/main/java/org/dstadler/commons/http/HttpClientWrapper.java index ba1472e3..3cfacd78 100644 --- a/src/main/java/org/dstadler/commons/http/HttpClientWrapper.java +++ b/src/main/java/org/dstadler/commons/http/HttpClientWrapper.java @@ -117,7 +117,7 @@ public String simpleGet(String url) throws IOException { @Override public void accept(InputStream inputStream) { try { - str.set(IOUtils.toString(inputStream)); + str.set(IOUtils.toString(inputStream, "UTF-8")); } catch (IOException e) { throw new IllegalStateException(e); } diff --git a/src/main/java/org/dstadler/commons/zip/ZipUtils.java b/src/main/java/org/dstadler/commons/zip/ZipUtils.java index 0413802e..5bf19b4f 100644 --- a/src/main/java/org/dstadler/commons/zip/ZipUtils.java +++ b/src/main/java/org/dstadler/commons/zip/ZipUtils.java @@ -113,9 +113,7 @@ public static void findZip(String zipName, InputStream zipInput, FileFilter sear final ZipEntry en; try { en = zin.getNextEntry(); - } catch (IOException e) { - throw new IOException("While handling file " + zipName, e); - } catch (IllegalArgumentException e) { + } catch (IOException | IllegalArgumentException e) { throw new IOException("While handling file " + zipName, e); } if(en == null) { @@ -266,7 +264,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE try { try (InputStream str = new FileInputStream(file)) { if (str.available() > 0) { - return IOUtils.toString(str); + return IOUtils.toString(str, "UTF-8"); } return ""; @@ -318,7 +316,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE try (InputStream str = zipfile.getInputStream(entry)) { if (str.available() > 0) { - return IOUtils.toString(str); + return IOUtils.toString(str, "UTF-8"); } return ""; @@ -337,7 +335,7 @@ public static String getZipStringContentsRecursive(final String file) throws IOE * * @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files */ - public static final void extractZip(File zip, File toDir) throws IOException{ + public static void extractZip(File zip, File toDir) throws IOException{ if(!toDir.exists()) { throw new IOException("Directory '" + toDir + "' does not exist."); } @@ -389,7 +387,7 @@ public static final void extractZip(File zip, File toDir) throws IOException{ * * @throws IOException Thrown if files can not be read or any other error occurs while handling the Zip-files */ - public static final void extractZip(InputStream zip, final File toDir) throws IOException{ + public static void extractZip(InputStream zip, final File toDir) throws IOException{ if(!toDir.exists()) { throw new IOException("Directory '" + toDir + "' does not exist."); } @@ -492,7 +490,7 @@ public static void replaceInZip(File zip, String file, String data, String encod if(!found) { zos.putNextEntry(new ZipEntry(file)); try { - IOUtils.write(data, zos); + IOUtils.write(data, zos, "UTF-8"); } finally { zos.closeEntry(); } @@ -504,6 +502,7 @@ public static void replaceInZip(File zip, String file, String data, String encod FileUtils.copyFile(zipOutFile, zip); } finally { if(!zipOutFile.delete()) { + //noinspection ThrowFromFinallyBlock throw new IOException("Error deleting file: " + zipOutFile); } } diff --git a/src/test/java/org/dstadler/commons/exec/ExecutionHelperTest.java b/src/test/java/org/dstadler/commons/exec/ExecutionHelperTest.java index 6fb74ffd..840c46cc 100644 --- a/src/test/java/org/dstadler/commons/exec/ExecutionHelperTest.java +++ b/src/test/java/org/dstadler/commons/exec/ExecutionHelperTest.java @@ -35,7 +35,7 @@ public void testGetCommandResult() throws IOException { log.info("Working dir: " + new File(".").getAbsolutePath()); try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), 0, 60000)) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -61,7 +61,7 @@ public void testGetCommandResultIgnoreExitValueStatus() throws IOException { try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -72,7 +72,7 @@ public void testGetCommandResultIgnoreExitValueHelp() throws IOException { try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -83,7 +83,7 @@ public void testGetCommandResultIgnoreExitValueWrongCmd() throws IOException { try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000)) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -95,7 +95,7 @@ public void testGetCommandResultInputStream() throws IOException { log.info("Working dir: " + new File(".").getAbsolutePath()); try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), 0, 60000, new ByteArrayInputStream(new byte[] {}))) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -125,7 +125,7 @@ public void testGetCommandResultIgnoreExitValueInputStream() throws IOException try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000, new ByteArrayInputStream(new byte[] {}))) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } @@ -136,7 +136,7 @@ public void testGetCommandResultIgnoreExitValueWrongCmdInputStream() throws IOEx try (InputStream result = ExecutionHelper.getCommandResult(cmdLine, new File("."), -1, 60000, new ByteArrayInputStream(new byte[] {}))) { assertNotNull(result); - log.info("Svn-Update reported:\n" + IOUtils.toString(result)); + log.info("Svn-Update reported:\n" + IOUtils.toString(result, "UTF-8")); } } diff --git a/src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java b/src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java index d9754a31..121849c0 100755 --- a/src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java +++ b/src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java @@ -54,7 +54,7 @@ public void setUp() { public void testRenderGraph() throws Exception { File file = File.createTempFile("DotUtilsTest", ".dot"); try { - FileUtils.write(file, DOT_FILE); + FileUtils.write(file, DOT_FILE, "UTF-8"); File out = File.createTempFile("DotUtilsTest", ".png"); try { @@ -77,7 +77,7 @@ public void testRenderGraph() throws Exception { public void testRenderGraphInvalidFile() throws Exception { File file = File.createTempFile("DotUtilsTest", ".dot"); try { - FileUtils.write(file, "{ digraph"); + FileUtils.write(file, "{ digraph", "UTF-8"); try { File out = File.createTempFile("DotUtilsTest", ".png"); @@ -135,7 +135,7 @@ public void testWriteHeaderContent() throws Exception { try (FileWriter fileWriter = new FileWriter(temp, false)) { DotUtils.writeHeader(fileWriter, 0, null, "G", null); fileWriter.flush(); - assertEquals(FileUtils.readFileToString(temp), + assertEquals(FileUtils.readFileToString(temp, "UTF-8"), "digraph G {\n" + "rankdir=LR;\n" + "node [shape=box];\n\n"); @@ -145,7 +145,7 @@ public void testWriteHeaderContent() throws Exception { try (FileWriter fileWriter = new FileWriter(temp, false)) { DotUtils.writeHeader(fileWriter, 200, null, "G", null); fileWriter.flush(); - assertEquals(FileUtils.readFileToString(temp), + assertEquals(FileUtils.readFileToString(temp, "UTF-8"), "digraph G {\n" + "dpi=200;\n" + "rankdir=LR;\n" + @@ -156,7 +156,7 @@ public void testWriteHeaderContent() throws Exception { try (FileWriter fileWriter = new FileWriter(temp, false)) { DotUtils.writeHeader(fileWriter, 0, "AB", "G", null); fileWriter.flush(); - assertEquals(FileUtils.readFileToString(temp), + assertEquals(FileUtils.readFileToString(temp, "UTF-8"), "digraph G {\n" + "rankdir=AB;\n" + "node [shape=box];\n\n"); @@ -170,7 +170,7 @@ public void testWriteHeaderContent() throws Exception { DotUtils.writeHeader(fileWriter, 0, null, "G", lines); fileWriter.flush(); - assertEquals(FileUtils.readFileToString(temp), + assertEquals(FileUtils.readFileToString(temp, "UTF-8"), "digraph G {\n" + "rankdir=LR;\n" + "someline;\n" + @@ -178,11 +178,11 @@ public void testWriteHeaderContent() throws Exception { "node [shape=box];\n\n"); } - // different titlke + // different title try (FileWriter fileWriter = new FileWriter(temp, false)) { DotUtils.writeHeader(fileWriter, 0, null, "mygraph", null); fileWriter.flush(); - assertEquals(FileUtils.readFileToString(temp), + assertEquals(FileUtils.readFileToString(temp, "UTF-8"), "digraph mygraph {\n" + "rankdir=LR;\n" + "node [shape=box];\n\n"); diff --git a/src/test/java/org/dstadler/commons/http/HttpClientWrapperTest.java b/src/test/java/org/dstadler/commons/http/HttpClientWrapperTest.java index 1b0e38ce..912940d4 100644 --- a/src/test/java/org/dstadler/commons/http/HttpClientWrapperTest.java +++ b/src/test/java/org/dstadler/commons/http/HttpClientWrapperTest.java @@ -43,7 +43,7 @@ public void testHttpClientWrapperSimpleGetStream() throws Exception { @Override public void accept(InputStream inputStream) { try { - str.set(IOUtils.toString(inputStream)); + str.set(IOUtils.toString(inputStream, "UTF-8")); } catch (IOException e) { throw new IllegalStateException(e); } @@ -70,7 +70,7 @@ public void testHttpClientWrapperNormalGet() throws Exception { HttpEntity entity = response.getEntity(); try { - String string = IOUtils.toString(entity.getContent()); + String string = IOUtils.toString(entity.getContent(), "UTF-8"); assertNotNull(string); } finally { // ensure all content is taken out to free resources diff --git a/src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java b/src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java index cc1951c3..bf37f19a 100644 --- a/src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java +++ b/src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java @@ -209,7 +209,7 @@ public void testServeTimeoutInitial() throws Exception { // wait some time to trigger the timeout Thread.sleep(2000); - assertTrue(IOUtils.toString(socket.getInputStream()).startsWith("HTTP/1.0 500 Internal Server Error")); + assertTrue(IOUtils.toString(socket.getInputStream(), "UTF-8").startsWith("HTTP/1.0 500 Internal Server Error")); } httpd.stop(); @@ -227,7 +227,7 @@ public void testServeTimeoutStarted() throws Exception { // wait some time to trigger the timeout Thread.sleep(2000); - assertTrue(IOUtils.toString(socket.getInputStream()).startsWith("HTTP/1.0 500 Internal Server Error")); + assertTrue(IOUtils.toString(socket.getInputStream(), "UTF-8").startsWith("HTTP/1.0 500 Internal Server Error")); } httpd.stop(); diff --git a/src/test/java/org/dstadler/commons/io/DeleteOnCloseInputStreamTest.java b/src/test/java/org/dstadler/commons/io/DeleteOnCloseInputStreamTest.java index c0d67e98..a527cb6f 100644 --- a/src/test/java/org/dstadler/commons/io/DeleteOnCloseInputStreamTest.java +++ b/src/test/java/org/dstadler/commons/io/DeleteOnCloseInputStreamTest.java @@ -19,7 +19,7 @@ public class DeleteOnCloseInputStreamTest { public void testDeleteOnCloseInputStream() throws Exception { File file = File.createTempFile("deleteOnClose1_", ".test"); FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file)) { assertTrue(file.exists()); assertTrue(file.length() > 0); @@ -33,7 +33,7 @@ public void testDeleteOnCloseInputStream() throws Exception { assertTrue(file.exists()); assertTrue(file.length() > 0); - List lines = IOUtils.readLines(stream); + List lines = IOUtils.readLines(stream, "UTF-8"); assertEquals(1, lines.size()); assertTrue(file.exists()); @@ -49,7 +49,7 @@ public void testDeleteOnCloseInputStream() throws Exception { public void testCoverMethods() throws Exception { File file = File.createTempFile("deleteOnClose2_", ".test"); FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file)) { try (DeleteOnCloseInputStream stream = new DeleteOnCloseInputStream(input, file)) { @@ -71,7 +71,7 @@ public void testCoverMethods() throws Exception { stream.mark(2); - stream.skip(1); + assertEquals(1, stream.skip(1)); try { stream.reset(); fail("Mark/Reset not supported"); @@ -90,7 +90,7 @@ public void testCoverMethods() throws Exception { public void testReset() throws Exception { File file = File.createTempFile("deleteOnClose3_", ".test"); FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file) { @Override public synchronized void reset() throws IOException { diff --git a/src/test/java/org/dstadler/commons/io/TailInputStreamTest.java b/src/test/java/org/dstadler/commons/io/TailInputStreamTest.java index ec1246ab..9163d4e1 100644 --- a/src/test/java/org/dstadler/commons/io/TailInputStreamTest.java +++ b/src/test/java/org/dstadler/commons/io/TailInputStreamTest.java @@ -20,7 +20,7 @@ public class TailInputStreamTest { public void testTailInputStream() throws Exception { File file = File.createTempFile("TailInputStream", ".test"); try { - FileUtils.write(file, StringUtils.repeat("1234567890\n", 100)); + FileUtils.write(file, StringUtils.repeat("1234567890\n", 100), "UTF-8"); assertEquals(1100, file.length()); @@ -42,7 +42,7 @@ public void testTailInputStream() throws Exception { public void testTailInputStreamRN() throws Exception { File file = File.createTempFile("TailInputStream", ".test"); try { - FileUtils.write(file, StringUtils.repeat("1234567890\r\n", 100)); + FileUtils.write(file, StringUtils.repeat("1234567890\r\n", 100), "UTF-8"); assertEquals(1200, file.length()); @@ -64,7 +64,7 @@ public void testTailInputStreamRN() throws Exception { public void testTailInputStreamR() throws Exception { File file = File.createTempFile("TailInputStream", ".test"); try { - FileUtils.write(file, StringUtils.repeat("1234567890\r", 100)); + FileUtils.write(file, StringUtils.repeat("1234567890\r", 100), "UTF-8"); assertEquals(1100, file.length()); @@ -86,7 +86,7 @@ public void testTailInputStreamR() throws Exception { public void testTailInputStreamRNOnly() throws Exception { File file = File.createTempFile("TailInputStream", ".test"); try { - FileUtils.write(file, StringUtils.repeat("\r\n", 100)); + FileUtils.write(file, StringUtils.repeat("\r\n", 100), "UTF-8"); assertEquals(200, file.length()); @@ -107,7 +107,7 @@ public void testTailInputStreamRNOnly() throws Exception { public void testTailInputStreamRead() throws Exception { File file = File.createTempFile("TailInputStream", ".test"); try { - FileUtils.write(file, StringUtils.repeat("1234567890\r", 100)); + FileUtils.write(file, StringUtils.repeat("1234567890\r", 100), "UTF-8"); assertEquals(1100, file.length()); diff --git a/src/test/java/org/dstadler/commons/util/DocumentStarterTest.java b/src/test/java/org/dstadler/commons/util/DocumentStarterTest.java index 05805c2d..b6536b0f 100644 --- a/src/test/java/org/dstadler/commons/util/DocumentStarterTest.java +++ b/src/test/java/org/dstadler/commons/util/DocumentStarterTest.java @@ -57,7 +57,7 @@ private void runWithSpecialName(DocumentStarter starter, String str) throws IOEx public void testStartURLWithBlanks() throws IOException { File file = File.createTempFile("DocumentStarterTest-some file with blanks", ".txt"); try { - FileUtils.writeStringToFile(file, "test"); + FileUtils.writeStringToFile(file, "test", "UTF-8"); new DocumentStarter().openURL(file.getAbsolutePath()); } finally { assertTrue(file.delete()); diff --git a/src/test/java/org/dstadler/commons/xml/AbstractSimpleContentHandlerTest.java b/src/test/java/org/dstadler/commons/xml/AbstractSimpleContentHandlerTest.java index 7262a72d..034b8094 100644 --- a/src/test/java/org/dstadler/commons/xml/AbstractSimpleContentHandlerTest.java +++ b/src/test/java/org/dstadler/commons/xml/AbstractSimpleContentHandlerTest.java @@ -34,7 +34,7 @@ public void testParseContentURL() throws Exception { AbstractSimpleContentHandler< String, String> handler = new AbstractSimpleContentHandler() { }; - try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) { + try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) { handler.parseContent(new URL("http://localhost:" + server.getPort()), "", null, 10_000); } } @@ -49,7 +49,7 @@ public SortedMap parseContent(InputStream strm) throws SAXExcept }; try { - try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) { + try (MockRESTServer server = new MockRESTServer("200", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) { handler.parseContent(new URL("http://localhost:" + server.getPort()), "", null, 10_000); } fail("Should catch exception here"); @@ -94,7 +94,7 @@ public void testParseContentNotFound() throws Exception { AbstractSimpleContentHandler< String, String> handler = new AbstractSimpleContentHandler() { }; - try (MockRESTServer server = new MockRESTServer("404", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml")))) { + try (MockRESTServer server = new MockRESTServer("404", "text/xml", FileUtils.readFileToString(new File("src/test/data/svnlog.xml"), "UTF-8"))) { try { handler.parseContent(new URL("http://localhost:" + server.getPort() + "/notfound"), "", null, 10_000); fail("Should catch exception"); diff --git a/src/test/java/org/dstadler/commons/zip/ZipFileCloseInputStreamTest.java b/src/test/java/org/dstadler/commons/zip/ZipFileCloseInputStreamTest.java index 5205834d..2c5be85e 100644 --- a/src/test/java/org/dstadler/commons/zip/ZipFileCloseInputStreamTest.java +++ b/src/test/java/org/dstadler/commons/zip/ZipFileCloseInputStreamTest.java @@ -35,14 +35,14 @@ public void testZipFileCloseInputStream() throws Exception { File file = File.createTempFile("ZipFileClose", ".test"); try { FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file)) { assertTrue(input.available() > 0); try (ZipFileCloseInputStream stream = new ZipFileCloseInputStream(input, zipfile)) { assertTrue(stream.available() > 0); - List lines = IOUtils.readLines(stream); + List lines = IOUtils.readLines(stream, "UTF-8"); assertEquals(1, lines.size()); } @@ -57,7 +57,9 @@ public void testZipFileCloseInputStream() throws Exception { assertTrue(file.delete()); } } finally { - assertTrue(new File(name).delete()); + if(name != null) { + assertTrue(new File(name).delete()); + } } } @@ -70,7 +72,7 @@ public void testCoverMethods() throws Exception { File file = File.createTempFile("ZipFileClose", ".test"); try { FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file)) { try (ZipFileCloseInputStream stream = new ZipFileCloseInputStream(input, zipfile)) { assertTrue(stream.available() > 0); @@ -91,7 +93,7 @@ public void testCoverMethods() throws Exception { stream.mark(2); - stream.skip(1); + assertEquals(1, stream.skip(1)); try { stream.reset(); fail("Mark/Reset not supported"); @@ -112,7 +114,9 @@ public void testCoverMethods() throws Exception { assertTrue(file.delete()); } } finally { - assertTrue(new File(name).delete()); + if(name != null) { + assertTrue(new File(name).delete()); + } } } @@ -124,7 +128,7 @@ public void testReset() throws Exception { File file = File.createTempFile("ZipFileClose", ".test"); try { FileUtils.writeStringToFile(file, - "somedata to have something to read..."); + "somedata to have something to read...", "UTF-8"); try (InputStream input = new FileInputStream(file) { @Override @@ -147,7 +151,9 @@ public synchronized void reset() throws IOException { assertTrue(file.delete()); } } finally { - assertTrue(new File(name).delete()); + if(name != null) { + assertTrue(new File(name).delete()); + } } } diff --git a/src/test/java/org/dstadler/commons/zip/ZipFileWalkerTest.java b/src/test/java/org/dstadler/commons/zip/ZipFileWalkerTest.java index 604788e0..a45f2c09 100755 --- a/src/test/java/org/dstadler/commons/zip/ZipFileWalkerTest.java +++ b/src/test/java/org/dstadler/commons/zip/ZipFileWalkerTest.java @@ -43,7 +43,7 @@ public boolean found(File file, InputStream content) throws IOException { log.info("File: " + file); if(file.getAbsolutePath().endsWith(".log")) { - assertEquals(LOGLINE, IOUtils.toString(content)); + assertEquals(LOGLINE, IOUtils.toString(content, "UTF-8")); found.set(true); } @@ -75,7 +75,7 @@ public boolean found(File file, InputStream content) throws IOException { log.info("File: " + file); if(file.getAbsolutePath().endsWith(".log")) { - assertEquals(LOGLINE, IOUtils.toString(content)); + assertEquals(LOGLINE, IOUtils.toString(content, "UTF-8")); found.set(true); } @@ -123,7 +123,7 @@ public void testWalkNotexisting() throws Exception { } } - public static File createNestedZip() throws IOException, FileNotFoundException { + public static File createNestedZip() throws IOException { File zipfile = File.createTempFile("zipfile", ".zip"); File zipfile2 = File.createTempFile("zipfile2", ".zip"); diff --git a/src/test/java/org/dstadler/commons/zip/ZipUtilsTest.java b/src/test/java/org/dstadler/commons/zip/ZipUtilsTest.java index e2b4b8f7..d65be137 100644 --- a/src/test/java/org/dstadler/commons/zip/ZipUtilsTest.java +++ b/src/test/java/org/dstadler/commons/zip/ZipUtilsTest.java @@ -40,17 +40,15 @@ public class ZipUtilsTest { /** * Test method for {@link org.dstadler.commons.zip.ZipUtils#getZipContentsRecursive(java.lang.String)}. - * - * @throws Exception */ @Test public void testGetZipContentsRecursive() throws Exception { File file = File.createTempFile("somefile", ".txt"); try { - FileUtils.writeStringToFile(file, "somedata"); + FileUtils.writeStringToFile(file, "somedata", "UTF-8"); try (InputStream zipContents = ZipUtils.getZipContentsRecursive(file.getAbsolutePath())) { - assertEquals("somedata", IOUtils.toString(zipContents)); + assertEquals("somedata", IOUtils.toString(zipContents, "UTF-8")); } } finally { assertTrue(file.exists()); @@ -77,7 +75,7 @@ public void testGetZipContentsRecursive1() throws Exception { } try (InputStream zipContents = ZipUtils.getZipContentsRecursive(zipfile.getAbsolutePath() + "!filename")) { - assertEquals("somedata", IOUtils.toString(zipContents)); + assertEquals("somedata", IOUtils.toString(zipContents, "UTF-8")); } } finally { assertTrue(zipfile.exists()); @@ -97,7 +95,7 @@ public void testGetZipContentsRecursive2() throws Exception { try { try (InputStream zipContents = ZipUtils.getZipContentsRecursive(zipfile2.getAbsolutePath() + "!nested.zip!filename")) { assertEquals("somedata", - IOUtils.toString(zipContents)); + IOUtils.toString(zipContents, "UTF-8")); } } finally { assertTrue(zipfile2.exists()); @@ -129,7 +127,7 @@ public void testGetZipContentsRecursiveError() throws Exception { } File file = File.createTempFile("ZipUtils", ".test"); - FileUtils.writeStringToFile(file, ""); + FileUtils.writeStringToFile(file, "", "UTF-8"); assertEquals(0, file.length()); try { try { @@ -249,7 +247,7 @@ public void testFindZip() throws Exception { } } - private File createNestedZip() throws IOException, FileNotFoundException { + private File createNestedZip() throws IOException { File zipfile = File.createTempFile("zipfile", ".zip"); try @@ -323,6 +321,7 @@ public int read() throws IOException { throw ioException; } + assertNotNull(runtimeException); throw runtimeException; } } @@ -347,7 +346,7 @@ public void run() { public void testGetZipStringContentsRecursive() throws Exception { File file = File.createTempFile("somefile", ".txt"); try { - FileUtils.writeStringToFile(file, "somedata"); + FileUtils.writeStringToFile(file, "somedata", "UTF-8"); assertEquals("somedata", ZipUtils.getZipStringContentsRecursive(file.getAbsolutePath())); } finally { @@ -430,7 +429,7 @@ public void testGetZipStringContentsRecursiveError() throws Exception { } File file = File.createTempFile("ZipUtils", ".test"); - FileUtils.writeStringToFile(file, ""); + FileUtils.writeStringToFile(file, "", "UTF-8"); assertEquals(0, file.length()); try { try { @@ -515,7 +514,7 @@ public void testIsFileInZip() { } @Test - public void testExtractZip() throws FileNotFoundException, IOException { + public void testExtractZip() throws IOException { File zipfile2 = createNestedZip(); try { @@ -596,7 +595,7 @@ public void testExtractInvalidZip() throws IOException { } @Test - public void testExtractZipFromStream() throws FileNotFoundException, IOException { + public void testExtractZipFromStream() throws IOException { File zipfile2 = createNestedZip(); try (InputStream stream = new FileInputStream(zipfile2)){ @@ -632,6 +631,7 @@ public void testExtractZipFromStream() throws FileNotFoundException, IOException @Test public void testReplaceInZipFailed() { try { + //noinspection ConstantConditions ZipUtils.replaceInZip(null, "somedata", null); fail("Should catch exception here"); } catch (IOException e) { @@ -673,7 +673,7 @@ public void testReplaceInZipReplace() throws IOException { try (ZipFile checkZip = new ZipFile(zipfile)) { assertNotNull(checkZip.getEntry("nested.zip")); try (InputStream stream = checkZip.getInputStream(checkZip.getEntry("nested.zip"))) { - assertEquals("somenewdata", IOUtils.toString(stream)); + assertEquals("somenewdata", IOUtils.toString(stream, "UTF-8")); } } } finally { @@ -718,7 +718,7 @@ public void testReplaceInZipAdd() throws IOException { try (ZipFile checkZip = new ZipFile(zipfile)) { assertNotNull(checkZip.getEntry("newfile.zip")); try (InputStream stream = checkZip.getInputStream(checkZip.getEntry("newfile.zip"))) { - assertEquals("somemorenewdata", IOUtils.toString(stream)); + assertEquals("somemorenewdata", IOUtils.toString(stream, "UTF-8")); } } } finally {