Skip to content

Commit

Permalink
cronet: Update to StandardCharsets and assertNotNull API's
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashok-Varma authored and temawi committed Apr 22, 2024
1 parent 163efa3 commit 77e59b2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
10 changes: 5 additions & 5 deletions cronet/src/main/java/io/grpc/cronet/CronetClientStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import io.grpc.internal.WritableBuffer;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -372,10 +372,10 @@ private void setGrpcHeaders(BidirectionalStream.Builder builder) {
// String and byte array.
byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers);
for (int i = 0; i < serializedHeaders.length; i += 2) {
String key = new String(serializedHeaders[i], Charset.forName("UTF-8"));
String key = new String(serializedHeaders[i], StandardCharsets.UTF_8);
// TODO(ericgribkoff): log an error or throw an exception
if (isApplicationHeader(key)) {
String value = new String(serializedHeaders[i + 1], Charset.forName("UTF-8"));
String value = new String(serializedHeaders[i + 1], StandardCharsets.UTF_8);
builder.addHeader(key, value);
}
}
Expand Down Expand Up @@ -552,8 +552,8 @@ private void reportHeaders(List<Map.Entry<String, String>> headers, boolean endO

byte[][] headerValues = new byte[headerList.size()][];
for (int i = 0; i < headerList.size(); i += 2) {
headerValues[i] = headerList.get(i).getBytes(Charset.forName("UTF-8"));
headerValues[i + 1] = headerList.get(i + 1).getBytes(Charset.forName("UTF-8"));
headerValues[i] = headerList.get(i).getBytes(StandardCharsets.UTF_8);
headerValues[i + 1] = headerList.get(i + 1).getBytes(StandardCharsets.UTF_8);
}
Metadata metadata =
InternalMetadata.newMetadata(TransportFrameUtil.toRawSerializedHeaders(headerValues));
Expand Down
17 changes: 9 additions & 8 deletions cronet/src/test/java/io/grpc/cronet/CronetClientStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -46,7 +47,7 @@
import java.io.ByteArrayInputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -102,7 +103,7 @@ void setStream(CronetClientStream stream) {
@Override
@SuppressWarnings("GuardedBy")
public void run() {
assertTrue(stream != null);
assertNotNull(stream);
stream.transportState().start(factory);
}
}
Expand Down Expand Up @@ -168,7 +169,7 @@ public void write() {
for (int i = 0; i < 5; ++i) {
requests[i] = "request" + i;
buffers[i] = allocator.allocate(requests[i].length());
buffers[i].write(requests[i].getBytes(Charset.forName("UTF-8")), 0, requests[i].length());
buffers[i].write(requests[i].getBytes(StandardCharsets.UTF_8), 0, requests[i].length());
// The 3rd and 5th writeFrame calls have flush=true.
clientStream.abstractClientStreamSink().writeFrame(buffers[i], false, i == 2 || i == 4, 1);
}
Expand Down Expand Up @@ -253,7 +254,7 @@ public void read() {
callback.onReadCompleted(
cronetStream,
info,
createMessageFrame(new String("response1").getBytes(Charset.forName("UTF-8"))),
createMessageFrame("response1".getBytes(StandardCharsets.UTF_8)),
false);
// Haven't request any message, so no callback is called here.
verify(clientListener, times(0)).messagesAvailable(isA(MessageProducer.class));
Expand Down Expand Up @@ -283,9 +284,9 @@ public void streamSucceeded() {
verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
// Send the first data frame.
CronetWritableBufferAllocator allocator = new CronetWritableBufferAllocator();
String request = new String("request");
String request = "request";
WritableBuffer writableBuffer = allocator.allocate(request.length());
writableBuffer.write(request.getBytes(Charset.forName("UTF-8")), 0, request.length());
writableBuffer.write(request.getBytes(StandardCharsets.UTF_8), 0, request.length());
clientStream.abstractClientStreamSink().writeFrame(writableBuffer, false, true, 1);
ArgumentCaptor<ByteBuffer> bufferCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
verify(cronetStream, times(1)).write(bufferCaptor.capture(), isA(Boolean.class));
Expand All @@ -304,7 +305,7 @@ public void streamSucceeded() {
callback.onReadCompleted(
cronetStream,
info,
createMessageFrame(new String("response").getBytes(Charset.forName("UTF-8"))),
createMessageFrame("response".getBytes(StandardCharsets.UTF_8)),
false);
verify(clientListener, times(1)).messagesAvailable(isA(MessageProducer.class));
verify(cronetStream, times(2)).read(isA(ByteBuffer.class));
Expand Down Expand Up @@ -680,7 +681,7 @@ public void getUnaryRequest() {
.newBidirectionalStreamBuilder(
isA(String.class), isA(BidirectionalStream.Callback.class), isA(Executor.class));

byte[] msg = "request".getBytes(Charset.forName("UTF-8"));
byte[] msg = "request".getBytes(StandardCharsets.UTF_8);
stream.writeMessage(new ByteArrayInputStream(msg));
// We still haven't built the stream or sent anything.
verify(cronetStream, times(0)).write(isA(ByteBuffer.class), isA(Boolean.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package io.grpc.cronet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -91,7 +91,7 @@ public void setUp() {
false,
false);
Runnable callback = transport.start(clientTransportListener);
assertTrue(callback != null);
assertNotNull(callback);
callback.run();
verify(clientTransportListener).transportReady();
}
Expand Down

0 comments on commit 77e59b2

Please sign in to comment.