|
| 1 | +package com.baeldung.bytearraybytestring; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 5 | + |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.IOException; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import com.google.protobuf.ByteString; |
| 12 | + |
| 13 | +public class ByteArrayByteStringUnitTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + public void givenByteArray_whenModified_thenChangesPersist() { |
| 17 | + // Here, we'll initialize a mutable buffer |
| 18 | + byte[] data = new byte[4]; |
| 19 | + |
| 20 | + // We'll read data into the buffer |
| 21 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] { 0x01, 0x02, 0x03, 0x04 }); |
| 22 | + try { |
| 23 | + inputStream.read(data); |
| 24 | + } catch (IOException e) { |
| 25 | + e.printStackTrace(); |
| 26 | + } |
| 27 | + |
| 28 | + // Note, the first byte is 1 |
| 29 | + assertEquals(1, data[0]); |
| 30 | + |
| 31 | + // We can directly modify the first byte |
| 32 | + data[0] = 0x05; |
| 33 | + |
| 34 | + // The modification is persisted |
| 35 | + assertEquals(5, data[0]); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenByteString_whenCreated_thenIsImmutable() { |
| 40 | + // We'll create an immutable ByteString from a mutable byte array |
| 41 | + byte[] originalArray = new byte[] { 0x01, 0x02, 0x03, 0x04 }; |
| 42 | + ByteString byteString = ByteString.copyFrom(originalArray); |
| 43 | + |
| 44 | + // The value of the first byte is 1 |
| 45 | + assertEquals(1, byteString.byteAt(0)); |
| 46 | + |
| 47 | + // We'll try to modify the original array |
| 48 | + originalArray[0] = 0x05; |
| 49 | + |
| 50 | + // The ByteString's contents remain unchanged |
| 51 | + assertEquals(1, byteString.byteAt(0)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenByteArray_whenCopiedToByteString_thenDataIsCopied() { |
| 56 | + // We'll start with a mutable byte array |
| 57 | + byte[] byteArray = new byte[] { 0x01, 0x02, 0x03 }; |
| 58 | + |
| 59 | + // Create a new ByteString from it |
| 60 | + ByteString byteString = ByteString.copyFrom(byteArray); |
| 61 | + |
| 62 | + // We'll assert that the data is the same |
| 63 | + assertEquals(byteArray[0], byteString.byteAt(0)); |
| 64 | + |
| 65 | + // Here, we change the original array |
| 66 | + byteArray[0] = 0x05; |
| 67 | + |
| 68 | + // Note, the ByteString remains unchanged, confirming the copy |
| 69 | + assertEquals(1, byteString.byteAt(0)); |
| 70 | + assertNotSame(byteArray, byteString.toByteArray()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void givenByteString_whenConvertedToByteArray_thenDataIsCopied() { |
| 75 | + // We'll start with an immutable ByteString |
| 76 | + ByteString byteString = ByteString.copyFromUtf8("Baeldung"); |
| 77 | + |
| 78 | + // We create a mutable byte array from it |
| 79 | + byte[] byteArray = byteString.toByteArray(); |
| 80 | + |
| 81 | + // The byte array now has a copy of the data |
| 82 | + assertEquals('B', (char) byteArray[0]); |
| 83 | + |
| 84 | + // We change the new array |
| 85 | + byteArray[0] = 'X'; |
| 86 | + |
| 87 | + // The original ByteString remains unchanged |
| 88 | + assertEquals('B', (char) byteString.byteAt(0)); |
| 89 | + assertNotSame(byteArray, byteString.toByteArray()); |
| 90 | + } |
| 91 | +} |
0 commit comments