From 2b8b72758fc92144b07a1bb3ecf6f9e4dc5501e2 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 10 Oct 2012 21:24:37 -0700 Subject: [PATCH] Prefer Files.toString over FileInputStream --- .../integration/internal/BaseBlobIntegrationTest.java | 9 ++++----- .../test/java/org/jclouds/blobstore/GetPathLiveTest.java | 7 +++---- .../java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java | 5 +++-- .../java/org/jclouds/sshj/SshjSshClientLiveTest.java | 5 +++-- .../java/org/jclouds/nodepool/NodeMetadataStoreTest.java | 8 ++++---- .../virtualbox/functions/YamlImagesFromFileConfig.java | 5 +++-- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java index 68e7cc41a2..d194001c7f 100644 --- a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java +++ b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseBlobIntegrationTest.java @@ -31,7 +31,6 @@ import static org.testng.Assert.assertNull; import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -68,12 +67,12 @@ import org.jclouds.io.Payloads; import org.jclouds.io.WriteTo; import org.jclouds.io.payloads.StreamingPayload; import org.jclouds.logging.Logger; -import org.jclouds.util.Strings2; import org.testng.ITestContext; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import com.google.common.base.Charsets; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.base.Throwables; @@ -482,10 +481,10 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest { @DataProvider(name = "putTests") public Object[][] createData1() throws IOException { + File file = new File("pom.xml"); + String realObject = Files.toString(file, Charsets.UTF_8); - String realObject = Strings2.toStringAndClose(new FileInputStream("pom.xml")); - - return new Object[][] { { "file", "text/xml", new File("pom.xml"), realObject }, + return new Object[][] { { "file", "text/xml", file, realObject }, { "string", "text/xml", realObject, realObject }, { "bytes", "application/octet-stream", realObject.getBytes(), realObject } }; } diff --git a/demos/getpath/src/test/java/org/jclouds/blobstore/GetPathLiveTest.java b/demos/getpath/src/test/java/org/jclouds/blobstore/GetPathLiveTest.java index 0a8704ebc2..cad7a210f6 100644 --- a/demos/getpath/src/test/java/org/jclouds/blobstore/GetPathLiveTest.java +++ b/demos/getpath/src/test/java/org/jclouds/blobstore/GetPathLiveTest.java @@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import static org.testng.Assert.assertEquals; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; @@ -33,7 +32,6 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; import org.jclouds.logging.log4j.config.Log4JLoggingModule; -import org.jclouds.util.Strings2; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeClass; import org.testng.annotations.Optional; @@ -44,6 +42,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.common.io.Files; /** * @@ -153,8 +152,8 @@ public class GetPathLiveTest { new File(tmpDirectory).mkdirs(); GetPath.main(uriKey, tmpDirectory); for (Entry entry : fiveStrings.entrySet()) { - assertEquals(Strings2.toStringAndClose(new FileInputStream(new File(tmpDirectory, entry - .getKey()))), entry.getValue()); + assertEquals(Files.toString(new File(tmpDirectory, + entry.getKey())), entry.getValue()); } } diff --git a/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java b/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java index 033c01bfed..0466d0e253 100644 --- a/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java +++ b/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java @@ -26,7 +26,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; @@ -49,9 +48,11 @@ import org.jclouds.util.Strings2; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import com.google.common.base.Charsets; import com.google.common.base.Strings; import com.google.common.base.Suppliers; import com.google.common.io.Closeables; +import com.google.common.io.Files; import com.google.common.net.HostAndPort; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; @@ -148,7 +149,7 @@ public class JschSshClientLiveTest { SshClient connection; if (Strings.emptyToNull(sshKeyFile) != null) { connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser) - .privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile))).build()); + .privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build()); } else { connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser).password(sshPass).build()); diff --git a/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java b/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java index 7f64209a2c..fa55a3fbe2 100644 --- a/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java +++ b/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java @@ -24,7 +24,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; @@ -42,9 +41,11 @@ import org.jclouds.util.Strings2; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; +import com.google.common.base.Charsets; import com.google.common.base.Strings; import com.google.common.base.Suppliers; import com.google.common.io.Closeables; +import com.google.common.io.Files; import com.google.common.net.HostAndPort; import com.google.inject.Guice; import com.google.inject.Injector; @@ -136,7 +137,7 @@ public class SshjSshClientLiveTest { SshClient connection; if (Strings.emptyToNull(sshKeyFile) != null) { connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser) - .privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile))).build()); + .privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build()); } else { connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser).password(sshPass).build()); diff --git a/labs/nodepool/src/test/java/org/jclouds/nodepool/NodeMetadataStoreTest.java b/labs/nodepool/src/test/java/org/jclouds/nodepool/NodeMetadataStoreTest.java index 3500bdf5df..25beec120e 100644 --- a/labs/nodepool/src/test/java/org/jclouds/nodepool/NodeMetadataStoreTest.java +++ b/labs/nodepool/src/test/java/org/jclouds/nodepool/NodeMetadataStoreTest.java @@ -25,7 +25,6 @@ import static org.testng.Assert.assertSame; import static org.testng.Assert.assertTrue; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; @@ -42,12 +41,13 @@ import org.jclouds.compute.options.TemplateOptions; import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.nodepool.config.NodePoolProperties; import org.jclouds.nodepool.internal.NodeMetadataStore; -import org.jclouds.util.Strings2; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import com.google.common.base.Charsets; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.google.common.io.Files; import com.google.inject.Injector; /** @@ -94,8 +94,8 @@ public class NodeMetadataStoreTest { public void testStore() throws FileNotFoundException, IOException { store.store(nodeMeta1, templateOptions, "testgroup"); store.store(nodeMeta2, templateOptions, "testgroup"); - String readJSon = Strings2.toStringAndClose(new FileInputStream(baseDir + File.separator + "nodes" - + File.separator + nodeMeta1.getId())); + String readJSon = Files.toString(new File(baseDir + File.separator + "nodes" + + File.separator + nodeMeta1.getId()), Charsets.UTF_8); assertEquals(readJSon, "{\"group\":\"testgroup\",\"tags\":[\"tag1\",\"tag2\"]," + "\"userMetadata\":{\"testmetakey\":\"testmetavalue\",\"testmetakey2\":\"testmetavalue2\"}," + "\"user\":\"testuser\",\"password\":\"testpass\",\"privateKey\":\"pk\",\"authenticateSudo\":true}"); diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/YamlImagesFromFileConfig.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/YamlImagesFromFileConfig.java index 63a3a2a3d3..d22d99c810 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/YamlImagesFromFileConfig.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/functions/YamlImagesFromFileConfig.java @@ -22,7 +22,6 @@ package org.jclouds.virtualbox.functions; import static com.google.common.base.Preconditions.checkNotNull; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; @@ -32,7 +31,9 @@ import javax.inject.Named; import org.apache.commons.io.IOUtils; import org.jclouds.virtualbox.config.VirtualBoxConstants; +import com.google.common.base.Charsets; import com.google.common.base.Supplier; +import com.google.common.io.Files; /** * A supplier for vbox yaml config that reads a yaml whose path is stored under @@ -59,7 +60,7 @@ public class YamlImagesFromFileConfig implements Supplier { if (!yamlFile.exists()) { yamlDesc = IOUtils.toString(new InputStreamReader(getClass().getResourceAsStream("/default-images.yaml"))); } else { - yamlDesc = IOUtils.toString(new FileInputStream(yamlFile)); + yamlDesc = Files.toString(yamlFile, Charsets.UTF_8); } checkNotNull(yamlDesc, "yaml descriptor"); return yamlDesc;