From 68bba4ef78128cfb8709c0cb08fc0e18482cc991 Mon Sep 17 00:00:00 2001 From: Ramu Date: Mon, 5 Jun 2017 22:12:25 +0530 Subject: [PATCH] CAMEL-11188:Use Files.newFileInputStream instead of new FileInputStream --- .../apache/camel/component/bean/MethodInfo.java | 2 +- .../apache/camel/builder/xml/XsltBuilderTest.java | 11 ++++++----- .../file/FileProducerCharsetUTFOptimizedTest.java | 7 +++++-- ...FileProducerCharsetUTFtoISOConfiguredTest.java | 10 +++++----- ...eProducerCharsetUTFtoISOConvertBodyToTest.java | 7 +++++-- .../file/FileProducerCharsetUTFtoISOTest.java | 7 +++++-- .../file/FileProducerCharsetUTFtoUTFTest.java | 10 +++++----- .../validator/ValidatorBeanCallTest.java | 7 +++++-- .../camel/converter/IOConverterCharsetTest.java | 15 ++++++++------- .../apache/camel/converter/IOConverterTest.java | 4 +++- .../java/org/apache/camel/util/IOHelperTest.java | 7 ++++--- .../camel/util/XmlLineNumberParserTest.java | 10 ++++++---- 12 files changed, 58 insertions(+), 39 deletions(-) diff --git a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java index e3842473a0d51..28dfe770d8476 100644 --- a/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java +++ b/camel-core/src/main/java/org/apache/camel/component/bean/MethodInfo.java @@ -247,7 +247,7 @@ public String toString() { public MethodInvocation createMethodInvocation(final Object pojo, boolean hasParameters, final Exchange exchange) { final Object[] arguments; if (hasParameters) { - arguments = parametersExpression.evaluate(exchange, Object[].class); + arguments = parametersExpression.evaluate(exchange, Object[].class); } else { arguments = null; } diff --git a/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java b/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java index aa02a9050b9e9..054be1da39a18 100644 --- a/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java +++ b/camel-core/src/test/java/org/apache/camel/builder/xml/XsltBuilderTest.java @@ -17,8 +17,9 @@ package org.apache.camel.builder.xml; import java.io.File; -import java.io.FileInputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import javax.xml.transform.Source; import javax.xml.transform.Templates; @@ -104,7 +105,7 @@ public void testXsltTransformerFile() throws Exception { public void testXsltInputStream() throws Exception { File styleSheet = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl"); - XsltBuilder builder = XsltBuilder.xslt(new FileInputStream(styleSheet)); + XsltBuilder builder = XsltBuilder.xslt(Files.newInputStream(Paths.get(styleSheet.getAbsolutePath()))); Exchange exchange = new DefaultExchange(context); exchange.getIn().setBody("world!"); @@ -118,7 +119,7 @@ public void testXsltTransformerInputStream() throws Exception { File styleSheet = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl"); XsltBuilder builder = new XsltBuilder(); - builder.setTransformerInputStream(new FileInputStream(styleSheet)); + builder.setTransformerInputStream(Files.newInputStream(Paths.get(styleSheet.getAbsolutePath()))); Exchange exchange = new DefaultExchange(context); exchange.getIn().setBody("world!"); @@ -130,7 +131,7 @@ public void testXsltTransformerInputStream() throws Exception { public void testXsltSource() throws Exception { File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl"); - Source styleSheet = new SAXSource(new InputSource(new FileInputStream(file))); + Source styleSheet = new SAXSource(new InputSource(Files.newInputStream(Paths.get(file.getAbsolutePath())))); XsltBuilder builder = XsltBuilder.xslt(styleSheet); @@ -144,7 +145,7 @@ public void testXsltSource() throws Exception { public void testXsltTemplates() throws Exception { File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl"); - Source source = new SAXSource(new InputSource(new FileInputStream(file))); + Source source = new SAXSource(new InputSource(Files.newInputStream(Paths.get(file.getAbsolutePath())))); XmlConverter converter = new XmlConverter(); Templates styleSheet = converter.getTransformerFactory().newTemplates(source); diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java index b881d2ebcfc2d..7ec8904fc2407 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFOptimizedTest.java @@ -20,7 +20,10 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; @@ -48,7 +51,7 @@ protected void setUp() throws Exception { } // write the byte array to a file using plain API - FileOutputStream fos = new FileOutputStream("target/charset/input/input.txt"); + OutputStream fos = Files.newOutputStream(Paths.get("target/charset/input/input.txt")); fos.write(utf); fos.close(); @@ -61,7 +64,7 @@ public void testFileProducerCharsetUTFOptimized() throws Exception { File file = new File("target/charset/output.txt"); assertTrue("File should exist", file.exists()); - InputStream fis = IOHelper.buffered(new FileInputStream(file)); + InputStream fis = Files.newInputStream(Paths.get(file.getAbsolutePath())); byte[] buffer = new byte[100]; int len = fis.read(buffer); diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java index 5ad6c860fc812..f7cb32653de13 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConfiguredTest.java @@ -17,14 +17,14 @@ package org.apache.camel.component.file; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.util.IOHelper; /** * @@ -54,7 +54,7 @@ protected void setUp() throws Exception { } // write the byte array to a file using plain API - FileOutputStream fos = new FileOutputStream("target/charset/input/input.txt"); + OutputStream fos = Files.newOutputStream(Paths.get("target/charset/input/input.txt")); fos.write(utf); fos.close(); @@ -67,7 +67,7 @@ public void testFileProducerCharsetUTFtoISO() throws Exception { File file = new File("target/charset/output.txt"); assertTrue("File should exist", file.exists()); - InputStream fis = IOHelper.buffered(new FileInputStream(file)); + InputStream fis = Files.newInputStream(Paths.get(file.getAbsolutePath())); byte[] buffer = new byte[100]; int len = fis.read(buffer); diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java index d660d4305c309..d7d1dbde7134a 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOConvertBodyToTest.java @@ -20,7 +20,10 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.Exchange; @@ -55,7 +58,7 @@ protected void setUp() throws Exception { } // write the byte array to a file using plain API - FileOutputStream fos = new FileOutputStream("target/charset/input/input.txt"); + OutputStream fos = Files.newOutputStream(Paths.get("target/charset/input/input.txt")); fos.write(utf); fos.close(); @@ -68,7 +71,7 @@ public void testFileProducerCharsetUTFtoISOConvertBodyTo() throws Exception { File file = new File("target/charset/output.txt"); assertTrue("File should exist", file.exists()); - InputStream fis = IOHelper.buffered(new FileInputStream(file)); + InputStream fis = Files.newInputStream(Paths.get(file.getAbsolutePath())); byte[] buffer = new byte[100]; int len = fis.read(buffer); diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java index a91c7bb023d92..f15859f874c6f 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoISOTest.java @@ -20,7 +20,10 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; @@ -54,7 +57,7 @@ protected void setUp() throws Exception { } // write the byte array to a file using plain API - FileOutputStream fos = new FileOutputStream("target/charset/input/input.txt"); + OutputStream fos = Files.newOutputStream(Paths.get("target/charset/input/input.txt")); fos.write(utf); fos.close(); @@ -67,7 +70,7 @@ public void testFileProducerCharsetUTFtoISO() throws Exception { File file = new File("target/charset/output.txt"); assertTrue("File should exist", file.exists()); - InputStream fis = IOHelper.buffered(new FileInputStream(file)); + InputStream fis = Files.newInputStream(Paths.get(file.getAbsolutePath())); byte[] buffer = new byte[100]; int len = fis.read(buffer); diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java index 187b0b1fe29a2..bfcc19249ecd4 100644 --- a/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/file/FileProducerCharsetUTFtoUTFTest.java @@ -17,14 +17,14 @@ package org.apache.camel.component.file; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.util.IOHelper; /** * @@ -48,7 +48,7 @@ protected void setUp() throws Exception { } // write the byte array to a file using plain API - FileOutputStream fos = new FileOutputStream("target/charset/input/input.txt"); + OutputStream fos = Files.newOutputStream(Paths.get("target/charset/input/input.txt")); fos.write(utf); fos.close(); @@ -61,7 +61,7 @@ public void testFileProducerCharsetUTFtoUTF() throws Exception { File file = new File("target/charset/output.txt"); assertTrue("File should exist", file.exists()); - InputStream fis = IOHelper.buffered(new FileInputStream(file)); + InputStream fis = Files.newInputStream(Paths.get(file.getAbsolutePath())); byte[] buffer = new byte[100]; int len = fis.read(buffer); diff --git a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java index b1953d75cc2de..c19409054f6e8 100644 --- a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorBeanCallTest.java @@ -18,7 +18,10 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.camel.ContextTestSupport; import org.apache.camel.builder.RouteBuilder; @@ -71,8 +74,8 @@ public void configure() throws Exception { public static class MyValidatorBean { - public InputStream loadFile() throws FileNotFoundException { - return new FileInputStream("src/test/resources/report.xsd"); + public InputStream loadFile() throws Exception { + return Files.newInputStream(Paths.get("src/test/resources/report.xsd")); } } diff --git a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java index 909563e2487fe..2abf0e2fc779a 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java @@ -18,11 +18,12 @@ import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import org.apache.camel.ContextTestSupport; @@ -35,7 +36,7 @@ public void testToInputStreamFileWithCharsetUTF8() throws Exception { File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt"); try (InputStream in = IOConverter.toInputStream(file, "UTF-8"); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); - BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(file.getAbsolutePath())), StandardCharsets.UTF_8))) { String line = reader.readLine(); String naiveLine = naiveReader.readLine(); assertEquals(naiveLine, line); @@ -48,7 +49,7 @@ public void testToInputStreamFileWithCharsetUTF8withOtherDefaultEncoding() throw File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt"); try (InputStream in = IOConverter.toInputStream(file, "UTF-8"); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.ISO_8859_1)); - BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(file.getAbsolutePath())), StandardCharsets.UTF_8))) { String line = reader.readLine(); String naiveLine = naiveReader.readLine(); assertEquals(naiveLine, line); @@ -61,7 +62,7 @@ public void testToInputStreamFileWithCharsetLatin1() throws Exception { File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt"); try (InputStream in = IOConverter.toInputStream(file, "ISO-8859-1"); BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); - BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"))) { + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(file.getAbsolutePath())), "ISO-8859-1"))) { String line = reader.readLine(); String naiveLine = naiveReader.readLine(); assertEquals(naiveLine, line); @@ -73,7 +74,7 @@ public void testToInputStreamFileDirectByteDumpWithCharsetLatin1() throws Except switchToDefaultCharset(StandardCharsets.UTF_8); File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt"); try (InputStream in = IOConverter.toInputStream(file, "ISO-8859-1"); - InputStream naiveIn = new FileInputStream(file)) { + InputStream naiveIn = Files.newInputStream(Paths.get(file.getAbsolutePath()))) { byte[] bytes = new byte[8192]; in.read(bytes); byte[] naiveBytes = new byte[8192]; @@ -85,7 +86,7 @@ public void testToInputStreamFileDirectByteDumpWithCharsetLatin1() throws Except public void testToReaderFileWithCharsetUTF8() throws Exception { File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt"); try (BufferedReader reader = IOConverter.toReader(file, "UTF-8"); - BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) { + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(file.getAbsolutePath())), StandardCharsets.UTF_8))) { String line = reader.readLine(); String naiveLine = naiveReader.readLine(); assertEquals(naiveLine, line); @@ -96,7 +97,7 @@ public void testToReaderFileWithCharsetUTF8() throws Exception { public void testToReaderFileWithCharsetLatin1() throws Exception { File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt"); try (BufferedReader reader = IOConverter.toReader(file, "ISO-8859-1"); - BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1"))) { + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(file.getAbsolutePath())), "ISO-8859-1"))) { String line = reader.readLine(); String naiveLine = naiveReader.readLine(); assertEquals(naiveLine, line); diff --git a/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java b/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java index 1ed7bb27b7110..7132e8df36165 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/IOConverterTest.java @@ -30,6 +30,8 @@ import java.io.StringReader; import java.io.Writer; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; import org.apache.camel.ContextTestSupport; @@ -47,7 +49,7 @@ public class IOConverterTest extends ContextTestSupport { public void testToBytes() throws Exception { File file = new File("src/test/resources/org/apache/camel/converter/dummy.txt"); - byte[] data = IOConverter.toBytes(new FileInputStream(file)); + byte[] data = IOConverter.toBytes(Files.newInputStream(Paths.get(file.getAbsolutePath()))); assertEquals("get the wrong byte size", file.length(), data.length); assertEquals('#', (char) data[0]); diff --git a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java index 037c323e3648f..56282976e833d 100644 --- a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java +++ b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java @@ -19,14 +19,15 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.Paths; import junit.framework.TestCase; -import org.apache.camel.CamelContext; + import org.apache.camel.Exchange; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.DefaultExchange; @@ -90,7 +91,7 @@ public void testLine2LF() throws Exception { private void assertReadAsWritten(String testname, String text, String compareText) throws Exception { File file = tempFile(testname); write(file, text); - String loadText = IOHelper.loadText(new FileInputStream(file)); + String loadText = IOHelper.loadText(Files.newInputStream(Paths.get(file.getAbsolutePath()))); assertEquals(compareText, loadText); } diff --git a/camel-core/src/test/java/org/apache/camel/util/XmlLineNumberParserTest.java b/camel-core/src/test/java/org/apache/camel/util/XmlLineNumberParserTest.java index 13b26c0c44850..48922f48e6edd 100644 --- a/camel-core/src/test/java/org/apache/camel/util/XmlLineNumberParserTest.java +++ b/camel-core/src/test/java/org/apache/camel/util/XmlLineNumberParserTest.java @@ -16,7 +16,9 @@ */ package org.apache.camel.util; -import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.w3c.dom.Document; import org.w3c.dom.Node; @@ -27,7 +29,7 @@ public class XmlLineNumberParserTest extends TestCase { public void testParse() throws Exception { - FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml"); + InputStream fis = Files.newInputStream(Paths.get("src/test/resources/org/apache/camel/util/camel-context.xml")); Document dom = XmlLineNumberParser.parseXml(fis); assertNotNull(dom); @@ -43,7 +45,7 @@ public void testParse() throws Exception { } public void testParseCamelContext() throws Exception { - FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml"); + InputStream fis = Files.newInputStream(Paths.get("src/test/resources/org/apache/camel/util/camel-context.xml")); Document dom = XmlLineNumberParser.parseXml(fis, null, "camelContext", null); assertNotNull(dom); @@ -59,7 +61,7 @@ public void testParseCamelContext() throws Exception { } public void testParseCamelContextForceNamespace() throws Exception { - FileInputStream fis = new FileInputStream("src/test/resources/org/apache/camel/util/camel-context.xml"); + InputStream fis = Files.newInputStream(Paths.get("src/test/resources/org/apache/camel/util/camel-context.xml")); Document dom = XmlLineNumberParser.parseXml(fis, null, "camelContext", "http://camel.apache.org/schema/spring"); assertNotNull(dom);