-
Notifications
You must be signed in to change notification settings - Fork 1.1k
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
gcloud-java-core/src/test/java/com/google/cloud/ByteArrayTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed 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 com.google.cloud; | ||
|
|
||
| import static org.junit.Assert.assertArrayEquals; | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import com.google.common.io.ByteStreams; | ||
| import com.google.protobuf.ByteString; | ||
|
|
||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| public class ByteArrayTest { | ||
|
|
||
| private static final String STRING_CONTENT = "Hello, ByteArray!"; | ||
| private static final byte[] BYTES_CONTENT = STRING_CONTENT.getBytes(StandardCharsets.UTF_8); | ||
| private static final ByteBuffer BYTE_BUFFER_CONTENT = ByteBuffer.wrap(BYTES_CONTENT); | ||
| private static final InputStream STREAM_CONTENT = new ByteArrayInputStream(BYTES_CONTENT); | ||
| private static final ByteArray STRING_ARRAY = ByteArray.copyFrom(STRING_CONTENT); | ||
| private static final ByteArray BYTES_ARRAY = ByteArray.copyFrom(BYTES_CONTENT); | ||
| private static final ByteArray BYTE_BUFFER_ARRAY = ByteArray.copyFrom(BYTE_BUFFER_CONTENT); | ||
| private static final ByteArray ARRAY = new ByteArray(ByteString.copyFrom(BYTES_CONTENT)); | ||
|
|
||
| private static ByteArray streamArray; | ||
|
|
||
| @BeforeClass | ||
| public static void beforeClass() throws IOException { | ||
| streamArray = ByteArray.copyFrom(STREAM_CONTENT); | ||
| BYTE_BUFFER_CONTENT.flip(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyFromString() throws IOException { | ||
| assertEquals(STRING_CONTENT, STRING_ARRAY.toStringUtf8()); | ||
| assertArrayEquals(BYTES_CONTENT, STRING_ARRAY.toByteArray()); | ||
| assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), STRING_ARRAY.asReadOnlyByteBuffer()); | ||
| assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(STRING_ARRAY.asInputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyFromByteArray() throws IOException { | ||
| assertEquals(STRING_CONTENT, BYTES_ARRAY.toStringUtf8()); | ||
| assertArrayEquals(BYTES_CONTENT, BYTES_ARRAY.toByteArray()); | ||
| assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), BYTES_ARRAY.asReadOnlyByteBuffer()); | ||
| assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(BYTES_ARRAY.asInputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyFromByteBuffer() throws IOException { | ||
| assertEquals(STRING_CONTENT, BYTE_BUFFER_ARRAY.toStringUtf8()); | ||
| assertArrayEquals(BYTES_CONTENT, BYTE_BUFFER_ARRAY.toByteArray()); | ||
| assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), BYTE_BUFFER_ARRAY.asReadOnlyByteBuffer()); | ||
| assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(BYTE_BUFFER_ARRAY.asInputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCopyFromStream() throws IOException { | ||
| assertEquals(STRING_CONTENT, streamArray.toStringUtf8()); | ||
| assertArrayEquals(BYTES_CONTENT, streamArray.toByteArray()); | ||
| assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), streamArray.asReadOnlyByteBuffer()); | ||
| assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(streamArray.asInputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLength() { | ||
| assertEquals(BYTES_CONTENT.length, ARRAY.length()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToStringUtf8() { | ||
| assertEquals(STRING_CONTENT, ARRAY.toStringUtf8()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testToByteArray() { | ||
| assertArrayEquals(BYTES_CONTENT, ARRAY.toByteArray()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAsReadOnlyByteBuffer() { | ||
| assertEquals(BYTE_BUFFER_CONTENT.asReadOnlyBuffer(), ARRAY.asReadOnlyByteBuffer()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAsInputStream() throws IOException { | ||
| assertArrayEquals(BYTES_CONTENT, ByteStreams.toByteArray(ARRAY.asInputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHashCode() { | ||
| assertEquals(STRING_ARRAY.hashCode(), BYTES_ARRAY.hashCode()); | ||
| assertEquals(BYTES_ARRAY.hashCode(), BYTE_BUFFER_ARRAY.hashCode()); | ||
| assertEquals(BYTE_BUFFER_ARRAY.hashCode(), streamArray.hashCode()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add tests and javadoc for Message and ByteArray #1000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Add tests and javadoc for Message and ByteArray #1000
Changes from all commits
84b292153f58aeFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.