Skip to content

Commit 7a6fcb6

Browse files
committed
fix(domain): make BinaryContent immutable class and strengthen defensive copy tests
1 parent fd2b6d5 commit 7a6fcb6

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

src/main/java/io/github/blueprintplatform/codegen/domain/port/out/artifact/BinaryContent.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@
44

55
import java.util.Arrays;
66

7-
public record BinaryContent(byte[] bytes) {
7+
@SuppressWarnings("java:S6206")
8+
public final class BinaryContent {
9+
10+
private final byte[] bytes;
811

912
public BinaryContent(byte[] bytes) {
1013
requireBinaryContent(bytes);
1114
this.bytes = Arrays.copyOf(bytes, bytes.length);
1215
}
1316

14-
@Override
1517
public byte[] bytes() {
1618
return Arrays.copyOf(bytes, bytes.length);
1719
}
1820

1921
@Override
2022
public boolean equals(Object o) {
21-
return (this == o) || (o instanceof BinaryContent(byte[] other) && Arrays.equals(bytes, other));
23+
return (this == o)
24+
|| (o instanceof BinaryContent other && Arrays.equals(this.bytes, other.bytes));
2225
}
2326

2427
@Override
2528
public int hashCode() {
2629
return Arrays.hashCode(bytes);
2730
}
2831

29-
@SuppressWarnings("NullableProblems")
3032
@Override
3133
public String toString() {
3234
return "BinaryContent[size=" + bytes.length + "]";

src/test/java/io/github/blueprintplatform/codegen/domain/port/out/artifact/GeneratedResourceTest.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ void text_nullContent_shouldFailContentNotBlank() {
4343
dve -> assertThat(dve.getMessageKey()).isEqualTo("file.content.not.blank"));
4444
}
4545

46+
@Test
47+
@DisplayName(
48+
"BinaryContent should defensively copy, and equals/hashCode/toString should be stable")
49+
void binaryContent_shouldBeImmutableAndValueBased() {
50+
byte[] original = new byte[] {1, 2, 3};
51+
52+
BinaryContent c1 = new BinaryContent(original);
53+
54+
original[0] = 9;
55+
56+
assertThat(c1.bytes()).containsExactly(1, 2, 3);
57+
58+
byte[] view = c1.bytes();
59+
view[1] = 8;
60+
61+
assertThat(c1.bytes()).containsExactly(1, 2, 3);
62+
63+
BinaryContent c2 = new BinaryContent(new byte[] {1, 2, 3});
64+
BinaryContent c3 = new BinaryContent(new byte[] {1, 2, 4});
65+
66+
assertThat(c1).isEqualTo(c2).hasSameHashCodeAs(c2).isNotEqualTo(c3);
67+
68+
assertThat(c1.toString()).contains("BinaryContent").contains("size=3");
69+
}
70+
4671
@Test
4772
@DisplayName("Binary should defensively copy bytes in ctor and accessor")
4873
void binary_shouldDefensivelyCopyBytes() {
@@ -51,13 +76,9 @@ void binary_shouldDefensivelyCopyBytes() {
5176
GeneratedBinaryResource binary =
5277
new GeneratedBinaryResource(Path.of("bin.dat"), new BinaryContent(original));
5378

54-
// ctor defensive copy (BinaryContent ctor kopyalıyor olmalı)
5579
original[0] = 9;
56-
5780
byte[] fromGetter = binary.bytes();
5881
assertThat(fromGetter).containsExactly(1, 2, 3);
59-
60-
// accessor defensive copy (BinaryContent.bytes() kopya dönmeli)
6182
fromGetter[1] = 8;
6283

6384
byte[] fromGetterAgain = binary.bytes();

0 commit comments

Comments
 (0)