Skip to content

Commit 1ccbc40

Browse files
committed
Merge branch 'master' into JAVA-1782-main
2 parents a1742fc + 024874d commit 1ccbc40

7 files changed

Lines changed: 233 additions & 2 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.varargs;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class HeapPollutionUnitTest {
12+
13+
@Test(expected = ClassCastException.class)
14+
public void givenGenericVararg_whenUsedUnsafe_shouldThrowClassCastException() {
15+
String one = firstOfFirst(Arrays.asList("one", "two"), Collections.emptyList());
16+
17+
assertEquals("one", one);
18+
}
19+
20+
@Test(expected = ClassCastException.class)
21+
public void givenGenericVararg_whenRefEscapes_mayCauseSubtleBugs() {
22+
String[] args = returnAsIs("One", "Two");
23+
}
24+
25+
private static String firstOfFirst(List<String>... strings) {
26+
List<Integer> ints = Collections.singletonList(42);
27+
Object[] objects = strings;
28+
objects[0] = ints;
29+
30+
return strings[0].get(0);
31+
}
32+
33+
private static <T> T[] toArray(T... arguments) {
34+
return arguments;
35+
}
36+
37+
private static <T> T[] returnAsIs(T a, T b) {
38+
return toArray(a, b);
39+
}
40+
}

core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void givenClassField_whenGetsType_thenCorrect() throws Exception {
200200
@Test
201201
public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
202202
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
203-
final Bird bird = (Bird) birdClass.newInstance();
203+
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
204204
final Field field = birdClass.getDeclaredField("walks");
205205
field.setAccessible(true);
206206

@@ -266,7 +266,7 @@ public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception {
266266
@Test
267267
public void givenMethod_whenInvokes_thenCorrect() throws Exception {
268268
final Class<?> birdClass = Class.forName("com.baeldung.java.reflection.Bird");
269-
final Bird bird = (Bird) birdClass.newInstance();
269+
final Bird bird = (Bird) birdClass.getConstructor().newInstance();
270270
final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class);
271271
final Method walksMethod = birdClass.getDeclaredMethod("walks");
272272
final boolean walks = (boolean) walksMethod.invoke(bird);

core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public void givenString_whenCallingIntegerValueOf_shouldConvertToInt() {
2626
assertThat(result).isEqualTo(new Integer(42));
2727
}
2828

29+
@Test
30+
public void givenString_whenCallingValueOf_shouldCacheSomeValues() {
31+
for (int i = -128; i <= 127; i++) {
32+
String value = i + "";
33+
Integer first = Integer.valueOf(value);
34+
Integer second = Integer.valueOf(value);
35+
36+
assertThat(first).isSameAs(second);
37+
}
38+
}
39+
2940
@Test
3041
public void givenString_whenCallingIntegerConstructor_shouldConvertToInt() {
3142
String givenString = "42";

libraries-data-2/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@
157157
<groupId>net.bytebuddy</groupId>
158158
<artifactId>byte-buddy</artifactId>
159159
<version>${byte-buddy.version}</version>
160+
<scope>test</scope>
161+
</dependency>
162+
<dependency>
163+
<groupId>org.apache.kafka</groupId>
164+
<artifactId>kafka-clients</artifactId>
165+
<version>${kafka.version}</version>
166+
<classifier>test</classifier>
160167
<scope>test</scope>
161168
</dependency>
162169
</dependencies>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.kafka.producer;
2+
3+
import org.apache.kafka.clients.producer.internals.DefaultPartitioner;
4+
import org.apache.kafka.common.Cluster;
5+
6+
public class EvenOddPartitioner extends DefaultPartitioner {
7+
8+
@Override
9+
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
10+
11+
if (((String) key).length() % 2 == 0) {
12+
return 0;
13+
}
14+
15+
return 1;
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.kafka.producer;
2+
3+
import org.apache.kafka.clients.producer.Producer;
4+
import org.apache.kafka.clients.producer.ProducerRecord;
5+
import org.apache.kafka.clients.producer.RecordMetadata;
6+
7+
import java.util.concurrent.Future;
8+
9+
public class KafkaProducer {
10+
11+
private final Producer<String, String> producer;
12+
13+
public KafkaProducer(Producer<String, String> producer) {
14+
this.producer = producer;
15+
}
16+
17+
public Future<RecordMetadata> send(String key, String value) {
18+
ProducerRecord record = new ProducerRecord("topic_sports_news",
19+
key, value);
20+
return producer.send(record);
21+
}
22+
23+
public void flush() {
24+
producer.flush();
25+
}
26+
27+
public void beginTransaction() {
28+
producer.beginTransaction();
29+
}
30+
31+
public void initTransaction() {
32+
producer.initTransactions();
33+
}
34+
35+
public void commitTransaction() {
36+
producer.commitTransaction();
37+
}
38+
39+
40+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.baeldung.kafka.producer;
2+
3+
import com.baeldung.kafka.producer.EvenOddPartitioner;
4+
import com.baeldung.kafka.producer.KafkaProducer;
5+
import org.apache.kafka.clients.producer.MockProducer;
6+
import org.apache.kafka.clients.producer.RecordMetadata;
7+
import org.apache.kafka.common.Cluster;
8+
import org.apache.kafka.common.Node;
9+
import org.apache.kafka.common.PartitionInfo;
10+
import org.apache.kafka.common.serialization.StringSerializer;
11+
import org.junit.jupiter.api.Test;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.concurrent.ExecutionException;
16+
import java.util.concurrent.Future;
17+
18+
import static java.util.Collections.emptySet;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
23+
class KafkaProducerUnitTest {
24+
25+
private final String TOPIC_NAME = "topic_sports_news";
26+
27+
private KafkaProducer kafkaProducer;
28+
private MockProducer<String, String> mockProducer;
29+
30+
private void buildMockProducer(boolean autoComplete) {
31+
this.mockProducer = new MockProducer<>(autoComplete, new StringSerializer(), new StringSerializer());
32+
}
33+
34+
@Test
35+
void givenKeyValue_whenSend_thenVerifyHistory() throws ExecutionException, InterruptedException {
36+
37+
buildMockProducer(true);
38+
//when
39+
kafkaProducer = new KafkaProducer(mockProducer);
40+
Future<RecordMetadata> recordMetadataFuture = kafkaProducer.send("data", "{\"site\" : \"baeldung\"}");
41+
42+
//then
43+
assertTrue(mockProducer.history().size() == 1);
44+
assertTrue(mockProducer.history().get(0).key().equalsIgnoreCase("data"));
45+
assertTrue(recordMetadataFuture.get().partition() == 0);
46+
47+
}
48+
49+
@Test
50+
void givenKeyValue_whenSend_thenSendOnlyAfterFlush() {
51+
52+
buildMockProducer(false);
53+
//when
54+
kafkaProducer = new KafkaProducer(mockProducer);
55+
Future<RecordMetadata> record = kafkaProducer.send("data", "{\"site\" : \"baeldung\"}");
56+
assertFalse(record.isDone());
57+
58+
//then
59+
kafkaProducer.flush();
60+
assertTrue(record.isDone());
61+
}
62+
63+
@Test
64+
void givenKeyValue_whenSend_thenReturnException() {
65+
66+
buildMockProducer(false);
67+
//when
68+
kafkaProducer = new KafkaProducer(mockProducer);
69+
Future<RecordMetadata> record = kafkaProducer.send("site", "{\"site\" : \"baeldung\"}");
70+
RuntimeException e = new RuntimeException();
71+
mockProducer.errorNext(e);
72+
//then
73+
try {
74+
record.get();
75+
} catch (ExecutionException | InterruptedException ex) {
76+
assertEquals(e, ex.getCause());
77+
}
78+
assertTrue(record.isDone());
79+
}
80+
81+
@Test
82+
void givenKeyValue_whenSendWithTxn_thenSendOnlyOnTxnCommit() {
83+
84+
buildMockProducer(true);
85+
//when
86+
kafkaProducer = new KafkaProducer(mockProducer);
87+
kafkaProducer.initTransaction();
88+
kafkaProducer.beginTransaction();
89+
Future<RecordMetadata> record = kafkaProducer.send("data", "{\"site\" : \"baeldung\"}");
90+
91+
//then
92+
assertTrue(mockProducer.history().isEmpty());
93+
kafkaProducer.commitTransaction();
94+
assertTrue(mockProducer.history().size() == 1);
95+
}
96+
97+
@Test
98+
void givenKeyValue_whenSendWithPartitioning_thenVerifyPartitionNumber() throws ExecutionException, InterruptedException {
99+
100+
PartitionInfo partitionInfo0 = new PartitionInfo(TOPIC_NAME, 0, null, null, null);
101+
PartitionInfo partitionInfo1 = new PartitionInfo(TOPIC_NAME, 1, null, null, null);
102+
List<PartitionInfo> list = new ArrayList<>();
103+
list.add(partitionInfo0);
104+
list.add(partitionInfo1);
105+
Cluster cluster = new Cluster("kafkab", new ArrayList<Node>(), list, emptySet(), emptySet());
106+
this.mockProducer = new MockProducer<>(cluster, true, new EvenOddPartitioner(), new StringSerializer(), new StringSerializer());
107+
//when
108+
kafkaProducer = new KafkaProducer(mockProducer);
109+
Future<RecordMetadata> recordMetadataFuture = kafkaProducer.send("partition", "{\"site\" : \"baeldung\"}");
110+
111+
//then
112+
assertTrue(recordMetadataFuture.get().partition() == 1);
113+
114+
}
115+
116+
}

0 commit comments

Comments
 (0)