You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2014/12/31 02:54:46 UTC

jclouds git commit: Implement read(byte[]) with read() for consistency

Repository: jclouds
Updated Branches:
  refs/heads/master 3ed8db787 -> d5fadb598


Implement read(byte[]) with read() for consistency

Previously multiple calls to read() returned different results than a
single call to read(byte[]).


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/d5fadb59
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/d5fadb59
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/d5fadb59

Branch: refs/heads/master
Commit: d5fadb598e40c070b2283dadbe773da62087eb75
Parents: 3ed8db7
Author: Andrew Gaul <ga...@apache.org>
Authored: Tue Dec 30 17:30:30 2014 -0800
Committer: Andrew Gaul <ga...@apache.org>
Committed: Tue Dec 30 17:37:26 2014 -0800

----------------------------------------------------------------------
 .../test/java/org/jclouds/utils/TestUtils.java  | 10 ++--
 .../java/org/jclouds/utils/TestUtilsTest.java   | 48 ++++++++++++++++++++
 2 files changed, 52 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/d5fadb59/core/src/test/java/org/jclouds/utils/TestUtils.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/utils/TestUtils.java b/core/src/test/java/org/jclouds/utils/TestUtils.java
index c2fde4f..ae3fb8e 100644
--- a/core/src/test/java/org/jclouds/utils/TestUtils.java
+++ b/core/src/test/java/org/jclouds/utils/TestUtils.java
@@ -71,16 +71,14 @@ public class TestUtils {
 
         @Override
         public synchronized int read(byte[] b) throws IOException {
-            random.nextBytes(b);
-            return b.length;
+            return read(b, 0, b.length);
         }
 
         @Override
         public synchronized int read(byte[] b, int off, int len) throws IOException {
-            // need extra array to cope with offset
-            byte[] srcBytes = new byte[len];
-            random.nextBytes(srcBytes);
-            System.arraycopy(srcBytes, 0, b, off, len);
+            for (int i = 0; i < len; ++i) {
+               b[off + i] = (byte) read();
+            }
             return len;
         }
     }

http://git-wip-us.apache.org/repos/asf/jclouds/blob/d5fadb59/core/src/test/java/org/jclouds/utils/TestUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/utils/TestUtilsTest.java b/core/src/test/java/org/jclouds/utils/TestUtilsTest.java
new file mode 100644
index 0000000..e5b96e8
--- /dev/null
+++ b/core/src/test/java/org/jclouds/utils/TestUtilsTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jclouds.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.InputStream;
+
+import com.google.common.io.ByteSource;
+import com.google.common.io.ByteStreams;
+import org.jclouds.util.Closeables2;
+import org.testng.annotations.Test;
+
+@Test(groups = "unit", testName = "TestUtilsTest")
+public class TestUtilsTest {
+   @Test
+   public void testRandomByteSource() throws Exception {
+      ByteSource byteSource = TestUtils.randomByteSource();
+      InputStream is1 = null;
+      InputStream is2 = null;
+      try {
+         is1 = byteSource.openStream();
+         is2 = byteSource.openStream();
+         byte[] bytes = new byte[16];
+         ByteStreams.readFully(is1, bytes);
+         for (byte b : bytes) {
+            assertThat(b).isEqualTo((byte) is2.read());
+         }
+      } finally {
+         Closeables2.closeQuietly(is1);
+         Closeables2.closeQuietly(is2);
+      }
+   }
+}